/** @tests java.util.SimpleTimeZone#getOffset(int, int, int, int, int, int) */ public void test_getOffsetIIIIII() { // Test for method int java.util.SimpleTimeZone.getOffset(int, int, int, // int, int, int) TimeZone st1 = TimeZone.getTimeZone("EST"); assertTrue( "Incorrect offset returned", st1.getOffset(GregorianCalendar.AD, 1998, Calendar.NOVEMBER, 11, Calendar.WEDNESDAY, 0) == -(5 * 60 * 60 * 1000)); st1 = TimeZone.getTimeZone("EST"); assertEquals( "Incorrect offset returned", -(5 * 60 * 60 * 1000), st1.getOffset(GregorianCalendar.AD, 1998, Calendar.JUNE, 11, Calendar.THURSDAY, 0)); // Regression for HARMONY-5459 st1 = (TimeZone) TimeZone.getDefault(); int fourHours = 4 * 60 * 60 * 1000; st1.setRawOffset(fourHours); assertEquals(fourHours, st1.getOffset(1, 2099, 01, 1, 5, 0)); }
@Override public void parse(ToDateParser params, FormatTokenEnum formatTokenEnum, String formatTokenStr) { Calendar result = params.getResultCalendar(); String inputFragmentStr = null; int dateNr = 0; switch (formatTokenEnum) { case HH24: inputFragmentStr = matchStringOrThrow(PATTERN_TWO_DIGITS_OR_LESS, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); result.set(Calendar.HOUR_OF_DAY, dateNr); break; case HH12: case HH: inputFragmentStr = matchStringOrThrow(PATTERN_TWO_DIGITS_OR_LESS, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); result.set(Calendar.HOUR, dateNr); break; case MI: inputFragmentStr = matchStringOrThrow(PATTERN_TWO_DIGITS_OR_LESS, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); result.set(Calendar.MINUTE, dateNr); break; case SS: inputFragmentStr = matchStringOrThrow(PATTERN_TWO_DIGITS_OR_LESS, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); result.set(Calendar.SECOND, dateNr); break; case SSSSS: inputFragmentStr = matchStringOrThrow(PATTERN_NUMBER, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); result.set(Calendar.HOUR_OF_DAY, 0); result.set(Calendar.MINUTE, 0); result.set(Calendar.SECOND, dateNr); break; case FF: inputFragmentStr = matchStringOrThrow(PATTERN_NUMBER, params, formatTokenEnum); String paddedRightNrStr = format("%-9s", inputFragmentStr).replace(' ', '0'); paddedRightNrStr = paddedRightNrStr.substring(0, 9); Double nineDigits = Double.parseDouble(paddedRightNrStr); params.setNanos(nineDigits.intValue()); dateNr = (int) Math.round(nineDigits / 1000000.0); result.set(Calendar.MILLISECOND, dateNr); break; case AM_PM: inputFragmentStr = matchStringOrThrow(PATTERN_AM_PM, params, formatTokenEnum); if (inputFragmentStr.toUpperCase().startsWith("A")) { result.set(Calendar.AM_PM, Calendar.AM); } else { result.set(Calendar.AM_PM, Calendar.PM); } break; case TZH: inputFragmentStr = matchStringOrThrow(PATTERN_TWO_DIGITS_OR_LESS, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); TimeZone tz = result.getTimeZone(); int offsetMillis = tz.getRawOffset(); // purge min and sec offsetMillis = (offsetMillis / MILLIS_IN_HOUR) * MILLIS_IN_HOUR; tz.setRawOffset(offsetMillis + dateNr); result.setTimeZone(tz); break; case TZM: inputFragmentStr = matchStringOrThrow(PATTERN_TWO_DIGITS_OR_LESS, params, formatTokenEnum); dateNr = parseInt(inputFragmentStr); tz = result.getTimeZone(); offsetMillis = tz.getRawOffset(); // purge hour offsetMillis = offsetMillis % MILLIS_IN_HOUR; tz.setRawOffset(dateNr * MILLIS_IN_HOUR + offsetMillis); result.setTimeZone(tz); break; case TZR: // Example: US/Pacific String s = params.getInputStr(); tz = result.getTimeZone(); for (String tzName : TimeZone.getAvailableIDs()) { int length = tzName.length(); if (s.length() >= length && tzName.equalsIgnoreCase(s.substring(0, length))) { tz.setID(tzName); result.setTimeZone(tz); inputFragmentStr = tzName; break; } } break; case TZD: // Must correspond with TZR region. Example: PST (for US/Pacific // standard time) throwException(params, format("token '%s' not supported yet.", formatTokenEnum.name())); break; default: throw new IllegalArgumentException( format( "%s: Internal Error. Unhandled case: %s", this.getClass().getSimpleName(), formatTokenEnum)); } params.remove(inputFragmentStr, formatTokenStr); }
private void addTransitions(VComponent timeRule, long from, long to) throws Exception { int offset; int previousOffset; String start; long startTime; long time; String transitionName; Property tzName = timeRule.getProperty("TZNAME"); Property tzOffsetFrom = timeRule.getProperty("TZOFFSETFROM"); Property tzOffsetTo = timeRule.getProperty("TZOFFSETTO"); Property tzDtStart = timeRule.getProperty("DTSTART"); Property tzRRule = timeRule.getProperty("RRULE"); Property tzRDate = timeRule.getProperty("RDATE"); if (tzDtStart != null) { start = tzDtStart.getValue(); startTime = parseDateTime(start); } else { throw new Exception("Required property DTSTART (of a time zone) is missing"); } if (tzOffsetTo != null) { offset = parseOffset(tzOffsetTo.getValue()); } else { throw new Exception("Required property TZOFFSETTO is missing"); } if (tzOffsetFrom != null) { previousOffset = parseOffset(tzOffsetFrom.getValue()); } else { throw new Exception("Required property TZOFFSETFROM is missing"); } if (tzName != null) { transitionName = tzName.getValue(); } else { transitionName = ""; } if (tzRDate != null) { String[] rDates = tzRDate.getValue().split(","); for (String rDate : rDates) { time = parseDateTime(rDate); transitions.add(new TimeZoneTransition(offset, time, transitionName)); } } if (tzRRule != null) { RecurrencePattern rrule = VCalendarContentConverter.getRecurrencePattern( start, null, tzRRule.getValue(), null, // as of specs false); // iCalendar if (((rrule.getTypeId() == RecurrencePattern.TYPE_MONTH_NTH) && (rrule.getInterval() == 12)) || ((rrule.getTypeId() == RecurrencePattern.TYPE_YEAR_NTH) && (rrule.getInterval() == 1))) { // yearly int dayOfWeek = getDayOfWeekFromMask(rrule.getDayOfWeekMask()); if (dayOfWeek > 0) { // one day TimeZone fixed = TimeZone.getTimeZone("UTC"); fixed.setRawOffset(previousOffset); Calendar finder = new GregorianCalendar(fixed); finder.setTimeInMillis(startTime); // Sets hour and minutes int hh = finder.get(Calendar.HOUR_OF_DAY); int mm = finder.get(Calendar.MINUTE); int m = rrule.getMonthOfYear() - 1; // Yes, it works int yearStart = year(startTime); int yearFrom = (startTime > from) ? yearStart : year(from); int yearTo = year(to); if (rrule.isNoEndDate()) { int count = rrule.getOccurrences(); int yearRecurrenceEnd; if (count != -1) { yearRecurrenceEnd = yearStart + count - 1; if (yearRecurrenceEnd < yearTo) { yearTo = yearRecurrenceEnd; } } } else { try { int yearRecurrenceEnd = year(rrule.getEndDatePattern()); if (yearRecurrenceEnd < yearTo) { yearTo = yearRecurrenceEnd; } } catch (ParseException e) { // Ignores the UNTIL part } } for (int y = yearFrom; y <= yearTo; y++) { finder.clear(); finder.set(Calendar.YEAR, y); finder.set(Calendar.MONTH, m); finder.set(Calendar.DAY_OF_WEEK, dayOfWeek); finder.set(Calendar.DAY_OF_WEEK_IN_MONTH, rrule.getInstance()); finder.set(Calendar.HOUR_OF_DAY, hh); finder.set(Calendar.MINUTE, mm); long transitionTime = finder.getTimeInMillis() - (previousOffset - getBasicOffset()); transitions.add(new TimeZoneTransition(offset, transitionTime, transitionName)); } } } } }