use of com.zimbra.common.calendar.ZoneInfoParser.RuleLine 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;
}
use of com.zimbra.common.calendar.ZoneInfoParser.RuleLine in project zm-mailbox by Zimbra.
the class ZoneInfo2iCalendar method getRuleInfo.
private static RuleInfo getRuleInfo(int hintYear, ZoneLine zline) {
if (!zline.hasRule()) {
return new RuleInfo(false, zline.getSave(), null, null);
}
Rule rule = zline.getRule();
List<RuleLine> rlines = getRuleLinesForYear(rule.getRuleLines(), hintYear);
RuleLine standard = null;
RuleLine daylight = null;
for (RuleLine rline : rlines) {
if (rline.getSave().getDuration() == 0) {
standard = rline;
} else {
daylight = rline;
}
}
return new RuleInfo(true, zline.getSave(), standard, daylight);
}
Aggregations