Search in sources :

Example 6 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class TimeZoneDefinition method addVtimezone.

public void addVtimezone(ContentHandler icalOutput) throws ParserException, URISyntaxException, IOException, ParseException {
    if (getTimezoneName() == null) {
        return;
    }
    if (effectiveRule == null) {
        return;
    }
    getTimeZone();
    if (theZone == null) {
        return;
    }
    VTimeZone vtz = theZone.getVTimeZone();
    icalOutput.startComponent(Component.VTIMEZONE);
    for (Object obj : vtz.getProperties()) {
        if (obj instanceof Property) {
            Property currProp = (Property) obj;
            IcalUtil.addProperty(icalOutput, currProp);
        }
    }
    for (Object obj : vtz.getObservances()) {
        if (obj instanceof Component) {
            Component currComp = (Component) obj;
            icalOutput.startComponent(currComp.getName());
            for (Object propObj : currComp.getProperties()) {
                if (propObj instanceof Property) {
                    Property obsProp = (Property) propObj;
                    IcalUtil.addProperty(icalOutput, obsProp);
                }
            }
            icalOutput.endComponent(currComp.getName());
        }
    }
    icalOutput.endComponent(Component.VTIMEZONE);
    if (true) {
        return;
    }
}
Also used : VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) Component(net.fortuna.ical4j.model.Component) Property(net.fortuna.ical4j.model.Property)

Example 7 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class TimeZoneDefinition method getTimeZone.

/**
     *
     * @return the appropriate iCal4j TimeZone for this TimeZoneDefinition
     */
public TimeZone getTimeZone() {
    if (theZone != null) {
        return theZone;
    }
    if (effectiveRule == null) {
        theZone = new TimeZone(0, TimeZones.UTC_ID);
        return theZone;
    }
    if (!effectiveRule.hasDaylightSaving()) {
        theZone = new TimeZone(effectiveRule.getStandardUtcOffsetMillis(), getTimezoneName());
        return theZone;
    }
    UtcOffset stdOffset = effectiveRule.getStandardUtcOffset();
    UtcOffset dlOffset = effectiveRule.getDaylightUtcOffset();
    PropertyList vtzProps = new PropertyList();
    TzId myTzid = new TzId(getTimezoneName());
    vtzProps.add(myTzid);
    VTimeZone vtz = new VTimeZone(vtzProps);
    Standard stdComp = new Standard();
    stdComp.getProperties().add(effectiveRule.getStandardDtStart());
    stdComp.getProperties().add(effectiveRule.icalStandardRRule());
    TzOffsetFrom offsetFrom = new TzOffsetFrom(dlOffset);
    TzOffsetTo offsetTo = new TzOffsetTo(stdOffset);
    stdComp.getProperties().add(offsetFrom);
    stdComp.getProperties().add(offsetTo);
    Daylight dayComp = new Daylight();
    dayComp.getProperties().add(effectiveRule.getDaylightDtStart());
    dayComp.getProperties().add(effectiveRule.icalDaylightRRule());
    offsetFrom = new TzOffsetFrom(stdOffset);
    offsetTo = new TzOffsetTo(dlOffset);
    dayComp.getProperties().add(offsetFrom);
    dayComp.getProperties().add(offsetTo);
    vtz.getObservances().add(stdComp);
    vtz.getObservances().add(dayComp);
    try {
        vtz.validate(true);
        theZone = new TimeZone(vtz);
    } catch (ValidationException e) {
        if (sLog.isDebugEnabled()) {
            sLog.debug("Problem with property %s - will default to UTC" + this.mpi.toString(), e);
        }
        theZone = new TimeZone(0, TimeZones.UTC_ID);
    }
    theZone = new TimeZone(vtz);
    return theZone;
}
Also used : UtcOffset(net.fortuna.ical4j.model.UtcOffset) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) TimeZone(net.fortuna.ical4j.model.TimeZone) PropertyList(net.fortuna.ical4j.model.PropertyList) Daylight(net.fortuna.ical4j.model.component.Daylight) ValidationException(net.fortuna.ical4j.model.ValidationException) TzOffsetFrom(net.fortuna.ical4j.model.property.TzOffsetFrom) TzOffsetTo(net.fortuna.ical4j.model.property.TzOffsetTo) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) TzId(net.fortuna.ical4j.model.property.TzId) Standard(net.fortuna.ical4j.model.component.Standard)

Example 8 with VTimeZone

use of net.fortuna.ical4j.model.component.VTimeZone in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method toVTimeZoneComp.

/**
     * @param zoneLines - Only the zoneLines related to a time zone that might be relevant from the reference date.
     */
private static VTimeZone toVTimeZoneComp(Calendar referenceDate, List<ZoneLine> zoneLines, LastModified lastModified, Set<String> tzAliases, boolean isPrimary, Integer matchScore) {
    int hintYear = referenceDate.get(Calendar.YEAR);
    ZoneLine zline1 = zoneLines.get(0);
    PropertyList vtzProps = toVTimeZonePropertyList(zline1, lastModified, tzAliases, isPrimary, matchScore);
    if (zoneLines.size() == 1) {
        return toVTimeZoneComp(hintYear, toObservances(hintYear, zline1), vtzProps);
    }
    boolean suppressWarning = false;
    //  Rare to get here - generally happens for some new timezone changes in the near future.
    ZoneLine zline2 = zoneLines.get(1);
    Observances obs1 = toObservances(hintYear, zline1);
    if (zline1.hasRule()) {
        if ((null != obs1.std) && (null != obs1.daylight)) {
            VTimeZone vtz = null;
            vtz = toVTimeZoneComp(referenceDate, zline1, zline2, obs1, vtzProps, obs1.inDaylightTimeOnDate(referenceDate));
            if (vtz != null) {
                return vtz;
            }
        }
    } else {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String fmtRefDate = format1.format(referenceDate.getTime());
        if ((null != obs1.std) && (null == obs1.daylight)) {
            // At reference date, only using STANDARD time
            Observances obs2 = toObservances(hintYear, zline2);
            if ((null != obs2.std) && (null != obs2.daylight)) {
                if (obs2.inDaylightTimeOnDate(referenceDate)) {
                    System.err.println(String.format("1st zoneLine '%s' for '%s' only has STANDARD time.", zline1.toString(), zline1.getName()));
                    System.err.println(String.format("Reference date %s would be in DAYLIGHT time by rules of 2nd zoneLine '%s'", fmtRefDate, zline2.toString()));
                    System.err.println("Therefore, Ignoring 2nd zoneLine.");
                    suppressWarning = true;
                } else {
                    TzOffsetTo oldOffsetTo = (TzOffsetTo) obs1.std.getProperties().getProperty(Property.TZOFFSETTO);
                    TzOffsetTo newOffsetTo = (TzOffsetTo) obs2.std.getProperties().getProperty(Property.TZOFFSETTO);
                    if (oldOffsetTo.equals(newOffsetTo)) {
                        // Standard time same by current rules and new rules - can ignore 1st zoneLine going forward
                        return toVTimeZoneComp(hintYear, toObservances(hintYear, zline2), vtzProps);
                    }
                    System.err.println(String.format("1st zoneLine '%s' for '%s' only has STANDARD time.", zline1.toString(), zline1.getName()));
                    System.err.println(String.format("Reference date %s would also be in STANDARD time by rules of 2nd zoneLine '%s'", fmtRefDate, zline2.toString()));
                    System.err.println(String.format("BUT OLD STANDARD has TZOFFSETTO=%s which differs from new TZOFFSETTO=%s.", oldOffsetTo.toString(), newOffsetTo.toString()));
                    System.err.println("Therefore, Ignoring 2nd zoneLine.");
                    suppressWarning = true;
                }
            }
        }
    }
    if (!suppressWarning) {
        System.err.println(String.format("More than 1 zoneLine for zone '%s' but unknown scenario.  Using only zoneLine:\n    %s", zline1.getName(), zline1.toString()));
    }
    return toVTimeZoneComp(hintYear, toObservances(hintYear, zline1), vtzProps);
}
Also used : PropertyList(net.fortuna.ical4j.model.PropertyList) TzOffsetTo(net.fortuna.ical4j.model.property.TzOffsetTo) ZoneLine(com.zimbra.common.calendar.ZoneInfoParser.ZoneLine) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

VTimeZone (net.fortuna.ical4j.model.component.VTimeZone)8 Property (net.fortuna.ical4j.model.Property)3 PropertyList (net.fortuna.ical4j.model.PropertyList)3 TZDataParseException (com.zimbra.common.calendar.ZoneInfoParser.TZDataParseException)2 ZoneLine (com.zimbra.common.calendar.ZoneInfoParser.ZoneLine)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 ParserException (net.fortuna.ical4j.data.ParserException)2 Component (net.fortuna.ical4j.model.Component)2 TimeZone (net.fortuna.ical4j.model.TimeZone)2 XProperty (net.fortuna.ical4j.model.property.XProperty)2 Zone (com.zimbra.common.calendar.ZoneInfoParser.Zone)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1