/** * Builds a Java TimeZone from the VTIMEZONE info in an * iCal file. * @return a Java TimeZone that matches the iCal one, or NULL if no TZ info present */ public static SimpleTimeZone buildTimeZone(String ical) { return buildTimeZone( getICalParams(ical) ); }
/** * Splits an iCal line into key and value by the first * unquoted colon. * @param icalLine String */ protected static String[] icalLineKeyValue(String icalLine){ int delim = indexOfFirstUnquotedColon(icalLine); if(delim == -1){ return new String[]{"",""}; } String key = icalLine.substring(0,delim); String value = icalLine.substring(delim+1); return new String[]{key,value}; }
/** * Checks we correctly build the Timezone for somewhere * that doesn't have DST (eg Brisbane) */ @Test public void simpleTimeZoneNoDST() { SimpleTimeZone tz = CalendarTimezoneHelper.buildTimeZone(ICAL_TZ_BRISBANE); assertNotNull(tz); assertEquals("Brisbane", tz.getID()); // Doesn't do DST assertEquals(false, tz.useDaylightTime()); // Always 10 hours ahead assertEquals(10*60*60*1000, tz.getOffset(date(2011,3,1).getTime())); assertEquals(10*60*60*1000, tz.getOffset(date(2011,9,1).getTime())); assertEquals(10*60*60*1000, tz.getOffset(date(2011,11,1).getTime())); }
for (String line : segregatedLines) String[] keyValue = icalLineKeyValue(line); if (keyValue.length >= 2)
int offset = getOffset(tzStandard.get("TZOFFSETTO")); return new SimpleTimeZone(offset, tzID); int stdOffset = getOffset(tzDaylight.get("TZOFFSETFROM")); int dstOffset = getOffset(tzDaylight.get("TZOFFSETTO")); int[] stdRules = getRuleForSimpleTimeZone(tzStandard.get("RRULE")); int[] dstRules = getRuleForSimpleTimeZone(tzDaylight.get("RRULE"));
protected static Map<String,String> getICalParams(String icalText) { return CalendarTimezoneHelper.getICalParams(icalText); } }
SimpleTimeZone tz = CalendarTimezoneHelper.buildTimeZone(ICAL_TZ_LONDON);
for (String line : segregatedLines) String[] keyValue = icalLineKeyValue(line); if (keyValue.length >= 2)
int offset = getOffset(tzStandard.get("TZOFFSETTO")); return new SimpleTimeZone(offset, tzID); int stdOffset = getOffset(tzDaylight.get("TZOFFSETFROM")); int dstOffset = getOffset(tzDaylight.get("TZOFFSETTO")); int[] stdRules = getRuleForSimpleTimeZone(tzStandard.get("RRULE")); int[] dstRules = getRuleForSimpleTimeZone(tzDaylight.get("RRULE"));
/** * Builds a Java TimeZone from the VTIMEZONE info in an * iCal file. * @return a Java TimeZone that matches the iCal one, or NULL if no TZ info present */ public static SimpleTimeZone buildTimeZone(String ical) { return buildTimeZone( getICalParams(ical) ); }
SimpleTimeZone tz = CalendarTimezoneHelper.buildTimeZone(ICAL_TZ_SYDNEY);
String[] keyVal = CalendarTimezoneHelper.icalLineKeyValue(icalLine); assertEquals("", keyVal[0]); assertEquals("", keyVal[1]); keyVal = CalendarTimezoneHelper.icalLineKeyValue(icalLine); assertEquals("a", keyVal[0]); assertEquals("b", keyVal[1]); keyVal = CalendarTimezoneHelper.icalLineKeyValue(icalLine); assertEquals("a", keyVal[0]); assertEquals("bcd:ds", keyVal[1]); keyVal = CalendarTimezoneHelper.icalLineKeyValue(icalLine); assertEquals("a\":bcdA\"", keyVal[0]); assertEquals("ds", keyVal[1]); keyVal = CalendarTimezoneHelper.icalLineKeyValue(icalLine); assertEquals("a\":bcdA\"", keyVal[0]); assertEquals("ds\"hello\"", keyVal[1]); keyVal = CalendarTimezoneHelper.icalLineKeyValue(icalLine); assertEquals("", keyVal[0]); assertEquals("", keyVal[1]);
/** * Splits an iCal line into key and value by the first * unquoted colon. * @param icalLine String */ protected static String[] icalLineKeyValue(String icalLine){ int delim = indexOfFirstUnquotedColon(icalLine); if(delim == -1){ return new String[]{"",""}; } String key = icalLine.substring(0,delim); String value = icalLine.substring(delim+1); return new String[]{key,value}; }
/** * @param icalLine String * @return location of first non quote enclosed colon */ private static int indexOfFirstUnquotedColon(String icalLine){ int colon = icalLine.indexOf(":"); int quote = icalLine.indexOf("\""); if(quote == -1){ return colon; }else{ if(colon<quote){ return colon; }else{ //next quote, skipping past a colon if exists int nextQuote = icalLine.indexOf("\"", quote+1); if(nextQuote==-1){ //will only happen if the quotes are unbalanced return -1; }else{ return nextQuote + indexOfFirstUnquotedColon(icalLine.substring(nextQuote+1, icalLine.length())) + 1; } } } }
/** * @param icalLine String * @return location of first non quote enclosed colon */ private static int indexOfFirstUnquotedColon(String icalLine){ int colon = icalLine.indexOf(":"); int quote = icalLine.indexOf("\""); if(quote == -1){ return colon; }else{ if(colon<quote){ return colon; }else{ //next quote, skipping past a colon if exists int nextQuote = icalLine.indexOf("\"", quote+1); if(nextQuote==-1){ //will only happen if the quotes are unbalanced return -1; }else{ return nextQuote + indexOfFirstUnquotedColon(icalLine.substring(nextQuote+1, icalLine.length())) + 1; } } } }