/** * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes. * * @param pattern The GMT offset * @return A TimeZone with offset from GMT or null, if pattern does not match. */ public static TimeZone getGmtTimeZone(final String pattern) { if ("Z".equals(pattern) || "UTC".equals(pattern)) { return GREENWICH; } final Matcher m = GMT_PATTERN.matcher(pattern); if (m.matches()) { final int hours = parseInt(m.group(2)); final int minutes = parseInt(m.group(4)); if (hours == 0 && minutes == 0) { return GREENWICH; } return new GmtTimeZone(parseSign(m.group(1)), hours, minutes); } return null; }
GmtTimeZone(final boolean negate, final int hours, final int minutes) { if (hours >= HOURS_PER_DAY) { throw new IllegalArgumentException(hours + " hours out of range"); } if (minutes >= MINUTES_PER_HOUR) { throw new IllegalArgumentException(minutes + " minutes out of range"); } final int milliseconds = (minutes + (hours * MINUTES_PER_HOUR)) * MILLISECONDS_PER_MINUTE; offset = negate ? -milliseconds : milliseconds; zoneId = twoDigits( twoDigits(new StringBuilder(9).append("GMT").append(negate ? '-' : '+'), hours) .append(':'), minutes).toString(); }
@Test public void getID() { assertEquals("GMT+00:00", new GmtTimeZone(false, 0, 0).getID()); assertEquals("GMT+01:02", new GmtTimeZone(false, 1, 2).getID()); assertEquals("GMT+11:22", new GmtTimeZone(false, 11, 22).getID()); assertEquals("GMT-01:02", new GmtTimeZone(true, 1, 2).getID()); assertEquals("GMT-11:22", new GmtTimeZone(true, 11, 22).getID()); }
@Test(expected = UnsupportedOperationException.class) public void setRawOffset() { new GmtTimeZone(false, 0, 0).setRawOffset(0); }
@Test public void inDaylightTime() { assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime()); }
@Test public void hoursInRange() { assertEquals(23 * 60 * 60 * 1000, new GmtTimeZone(false, 23, 0).getRawOffset()); }
@Test public void testGetOffset() { assertEquals(-(6 * 60 + 30) * 60 * 1000, new GmtTimeZone(true, 6, 30).getOffset(1, 1, 1, 1, 1, 1)); } }
@Test public void testToString() { assertEquals("[GmtTimeZone id=\"GMT-12:00\",offset=-43200000]", new GmtTimeZone(true, 12, 0).toString()); }
@Test public void getRawOffset() { assertEquals(0, new GmtTimeZone(false, 0, 0).getRawOffset()); }
@Test public void useDaylightTime() { assertFalse(new GmtTimeZone(false, 0, 0).useDaylightTime()); }
@Test public void getOffset() { assertEquals(0, new GmtTimeZone(false, 0, 0).getOffset(234304)); }
@Test(expected = IllegalArgumentException.class) public void minutesOutOfRange() { new GmtTimeZone(false, 0, 60); }
@Test public void minutesInRange() { assertEquals(59 * 60 * 1000, new GmtTimeZone(false, 0, 59).getRawOffset()); }
GmtTimeZone(final boolean negate, final int hours, final int minutes) { if (hours >= HOURS_PER_DAY) { throw new IllegalArgumentException(hours + " hours out of range"); } if (minutes >= MINUTES_PER_HOUR) { throw new IllegalArgumentException(minutes + " minutes out of range"); } final int milliseconds = (minutes + (hours * MINUTES_PER_HOUR)) * MILLISECONDS_PER_MINUTE; offset = negate ? -milliseconds : milliseconds; zoneId = twoDigits( twoDigits(new StringBuilder(9).append("GMT").append(negate ? '-' : '+'), hours) .append(':'), minutes).toString(); }
@Test(expected = IllegalArgumentException.class) public void hoursOutOfRange() { new GmtTimeZone(false, 24, 0); }
GmtTimeZone(final boolean negate, final int hours, final int minutes) { if (hours >= HOURS_PER_DAY) { throw new IllegalArgumentException(hours + " hours out of range"); } if (minutes >= MINUTES_PER_HOUR) { throw new IllegalArgumentException(minutes + " minutes out of range"); } final int milliseconds = (minutes + (hours * MINUTES_PER_HOUR)) * MILLISECONDS_PER_MINUTE; offset = negate ? -milliseconds : milliseconds; zoneId = twoDigits( twoDigits(new StringBuilder(9).append("GMT").append(negate ? '-' : '+'), hours) .append(':'), minutes).toString(); }
/** * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes. * * @param pattern The GMT offset * @return A TimeZone with offset from GMT or null, if pattern does not match. */ public static TimeZone getGmtTimeZone(final String pattern) { if ("Z".equals(pattern) || "UTC".equals(pattern)) { return GREENWICH; } final Matcher m = GMT_PATTERN.matcher(pattern); if (m.matches()) { final int hours = parseInt(m.group(2)); final int minutes = parseInt(m.group(4)); if (hours == 0 && minutes == 0) { return GREENWICH; } return new GmtTimeZone(parseSign(m.group(1)), hours, minutes); } return null; }
GmtTimeZone(final boolean negate, final int hours, final int minutes) { if (hours >= HOURS_PER_DAY) { throw new IllegalArgumentException(hours + " hours out of range"); } if (minutes >= MINUTES_PER_HOUR) { throw new IllegalArgumentException(minutes + " minutes out of range"); } final int milliseconds = (minutes + (hours * MINUTES_PER_HOUR)) * MILLISECONDS_PER_MINUTE; offset = negate ? -milliseconds : milliseconds; zoneId = twoDigits( twoDigits(new StringBuilder(9).append("GMT").append(negate ? '-' : '+'), hours) .append(':'), minutes).toString(); }
/** * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes. * * @param pattern The GMT offset * @return A TimeZone with offset from GMT or null, if pattern does not match. */ public static TimeZone getGmtTimeZone(final String pattern) { if ("Z".equals(pattern) || "UTC".equals(pattern)) { return GREENWICH; } final Matcher m = GMT_PATTERN.matcher(pattern); if (m.matches()) { final int hours = parseInt(m.group(2)); final int minutes = parseInt(m.group(4)); if (hours == 0 && minutes == 0) { return GREENWICH; } return new GmtTimeZone(parseSign(m.group(1)), hours, minutes); } return null; }
/** * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes. * * @param pattern The GMT offset * @return A TimeZone with offset from GMT or null, if pattern does not match. */ public static TimeZone getGmtTimeZone(final String pattern) { if ("Z".equals(pattern) || "UTC".equals(pattern)) { return GREENWICH; } final Matcher m = GMT_PATTERN.matcher(pattern); if (m.matches()) { final int hours = parseInt(m.group(2)); final int minutes = parseInt(m.group(4)); if (hours == 0 && minutes == 0) { return GREENWICH; } return new GmtTimeZone(parseSign(m.group(1)), hours, minutes); } return null; }