Search in sources :

Example 1 with Observance

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

the class ZoneInfo2iCalendar method toVTimeZoneComp.

/**
     * @param referenceDate
     * @param zline1 ZoneLine for 1st rule applicable after referenceDate
     * @param zline2 ZoneLine for 2nd rule applicable after referenceDate
     * @param obs1 Observances corresponding to zline1
     * @param vtzProps Properties to associate with the VTIMEZONE component
     * @param inDaylightTime  true if referenceDate falls within daylight time by the rules in zline1
     * @return best timezone or null if unable to determine one.
     */
private static VTimeZone toVTimeZoneComp(Calendar referenceDate, ZoneLine zline1, ZoneLine zline2, Observances obs1, PropertyList vtzProps, boolean inDaylightTime) {
    int hintYear = referenceDate.get(Calendar.YEAR);
    Observance obs4zl2;
    Time daylightOffset;
    Time standardOffset = zline2.getGmtOff();
    String tznameFormat = zline2.getAbbrevFormat();
    if (zline2.hasRule()) {
        RuleInfo rl2 = getRuleInfo(hintYear, zline2);
        daylightOffset = (null == rl2.daylight) ? standardOffset : addTimes(standardOffset, rl2.daylight.getSave());
        if (inDaylightTime) {
            obs4zl2 = toObservanceComp(hintYear, rl2.standard, true, /* isStandard */
            standardOffset, daylightOffset, tznameFormat);
            return toVTimeZoneComp(hintYear, new Observances(obs4zl2, obs1.daylight), vtzProps);
        } else {
            if (null == rl2.daylight) {
                return null;
            }
            obs4zl2 = toObservanceComp(hintYear, rl2.daylight, false, /* isStandard */
            standardOffset, daylightOffset, tznameFormat);
            return toVTimeZoneComp(hintYear, new Observances(obs1.std, obs4zl2), vtzProps);
        }
    } else if (zline2.hasSave()) {
        List<String> tokens = Lists.newArrayList();
        Until prevRuleEnd = zline1.getUntil();
        if (zline2.hasUntil()) {
            Until currRuleEnd = zline2.getUntil();
            if (null == currRuleEnd) {
                // Don't think this can happen
                return null;
            }
            String fromYear;
            if (prevRuleEnd != null) {
                fromYear = String.format("%d", prevRuleEnd.getYear());
            } else {
                fromYear = String.format("%d", hintYear);
            }
            String toYear = String.format("%d", currRuleEnd.getYear());
            // NAME
            tokens.add(getObservanceName(tznameFormat, null));
            // FROM
            tokens.add(fromYear);
            // TO
            tokens.add(toYear);
            // TYPE
            tokens.add("-");
            // IN
            tokens.add(ZoneInfoParser.Month.toString(currRuleEnd.getMonth()));
            // ON
            tokens.add(String.format("%s", currRuleEnd.getDay().toString()));
            // AT
            tokens.add(String.format("%s", currRuleEnd.getTime().toString()));
            // SAVE
            tokens.add(zline2.getSave().toString());
            // LETTER/S
            tokens.add("-");
            RuleLine newRule = pseudoZoneLineTokensToRuleLine(tokens, zline2);
            if (null == newRule) {
                return null;
            }
            daylightOffset = addTimes(standardOffset, zline2.getSave());
            obs4zl2 = toObservanceComp(hintYear, newRule, inDaylightTime, /* need the opposite */
            standardOffset, daylightOffset, tznameFormat);
            if (inDaylightTime) {
                return toVTimeZoneComp(hintYear, new Observances(obs4zl2, obs1.daylight), vtzProps);
            } else {
                return toVTimeZoneComp(hintYear, new Observances(obs1.std, obs4zl2), vtzProps);
            }
        } else {
            if (!inDaylightTime) {
                // Only reason for having a save but no until is if changing to standard time only?
                return null;
            }
            if (prevRuleEnd == null) {
                return null;
            }
            String fromYear = String.format("%d", prevRuleEnd.getYear());
            String toYear = "max";
            // NAME
            tokens.add(getObservanceName(tznameFormat, null));
            // FROM
            tokens.add(fromYear);
            // TO
            tokens.add(toYear);
            // TYPE
            tokens.add("-");
            // IN
            tokens.add(ZoneInfoParser.Month.toString(prevRuleEnd.getMonth()));
            // ON
            tokens.add(String.format("%s", prevRuleEnd.getDay().toString()));
            // AT
            tokens.add(String.format("%s", prevRuleEnd.getTime().toString()));
            // SAVE
            tokens.add(zline2.getSave().toString());
            // LETTER/S
            tokens.add("-");
            RuleLine newRule = pseudoZoneLineTokensToRuleLine(tokens, zline2);
            if (null == newRule) {
                return null;
            }
            daylightOffset = standardOffset;
            /* random value - fix later */
            ;
            obs4zl2 = toObservanceComp(hintYear, newRule, true, standardOffset, daylightOffset, tznameFormat);
            TzOffsetFrom stdOffsetFrom = (TzOffsetFrom) obs4zl2.getProperties().getProperty(Property.TZOFFSETFROM);
            TzOffsetTo stdOffsetTo = (TzOffsetTo) obs4zl2.getProperties().getProperty(Property.TZOFFSETTO);
            TzOffsetTo dlOffsetTo = (TzOffsetTo) obs1.daylight.getProperties().getProperty(Property.TZOFFSETTO);
            if (stdOffsetTo.equals(dlOffsetTo)) {
                // New standard time is same as current daylight time - just use observance.
                obs4zl2 = toStandardComp(zline2.getGmtOff(), tznameFormat);
                return toVTimeZoneComp(hintYear, new Observances(obs4zl2, null), vtzProps);
            }
            // Make sure that the zones are consistent with each other
            stdOffsetFrom.setOffset(dlOffsetTo.getOffset());
            TzOffsetFrom dlOffsetFrom = (TzOffsetFrom) obs1.daylight.getProperties().getProperty(Property.TZOFFSETFROM);
            dlOffsetFrom.setOffset(stdOffsetTo.getOffset());
            return toVTimeZoneComp(hintYear, new Observances(obs4zl2, obs1.daylight), vtzProps);
        }
    }
    return null;
}
Also used : TzOffsetFrom(net.fortuna.ical4j.model.property.TzOffsetFrom) TzOffsetTo(net.fortuna.ical4j.model.property.TzOffsetTo) Observance(net.fortuna.ical4j.model.component.Observance) RuleLine(com.zimbra.common.calendar.ZoneInfoParser.RuleLine) DateTime(net.fortuna.ical4j.model.DateTime) Time(com.zimbra.common.calendar.ZoneInfoParser.Time) PropertyList(net.fortuna.ical4j.model.PropertyList) List(java.util.List) ArrayList(java.util.ArrayList) ParameterList(net.fortuna.ical4j.model.ParameterList) Until(com.zimbra.common.calendar.ZoneInfoParser.Until)

Aggregations

RuleLine (com.zimbra.common.calendar.ZoneInfoParser.RuleLine)1 Time (com.zimbra.common.calendar.ZoneInfoParser.Time)1 Until (com.zimbra.common.calendar.ZoneInfoParser.Until)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DateTime (net.fortuna.ical4j.model.DateTime)1 ParameterList (net.fortuna.ical4j.model.ParameterList)1 PropertyList (net.fortuna.ical4j.model.PropertyList)1 Observance (net.fortuna.ical4j.model.component.Observance)1 TzOffsetFrom (net.fortuna.ical4j.model.property.TzOffsetFrom)1 TzOffsetTo (net.fortuna.ical4j.model.property.TzOffsetTo)1