Java Code Examples for org.joda.time.DateTimeZone#nextTransition()

The following examples show how to use org.joda.time.DateTimeZone#nextTransition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerTimeZoneBackend.java    From unitime with Apache License 2.0 6 votes vote down vote up
@Override
public ServerTimeZoneResponse execute(ServerTimeZoneRequest request, SessionContext context) {
	Date first = null, last = null;
	for (Session session: SessionDAO.getInstance().findAll()) {
		if (first == null || first.after(session.getEventBeginDate()))
			first = session.getEventBeginDate();
		if (last == null || last.before(session.getEventEndDate()))
			last = session.getEventEndDate();
	}
	DateTimeZone zone = DateTimeZone.getDefault();
	int offsetInMinutes = zone.getOffset(first.getTime()) / 60000;
	ServerTimeZoneResponse ret = new ServerTimeZoneResponse();
	ret.setId(zone.getID());
	ret.addName(zone.getName(new Date().getTime()));
	ret.setTimeZoneOffsetInMinutes(offsetInMinutes);
	long time = first.getTime();
	long transition;
	while (time != (transition = zone.nextTransition(time)) && time < last.getTime()) {
		int adjustment = (zone.getOffset(transition) / 60000) - offsetInMinutes;
		ret.addTransition((int)(transition / 3600000), adjustment);
		time = transition;
	}
	return ret;
}
 
Example 2
Source File: TimeZoneInfo.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public String getDisplayName(TimeZone tz, long date) {
    DateTimeZone timeZone = toDateTimeZone(tz);
    if (IGNORE_LOCAL_BEFORE_EPOCH) {
        if (date < EPOCH) {
            date = toFirstDateWithNormalizedTime(timeZone, date);
        }
    } else if (IGNORE_LOCAL) {
        if (date <= LAST_LOCAL_TRANSITION) {
            date = toFirstDateWithNormalizedTime(timeZone, date);
        }
    } else if (IGNORE_LMT) {
        if (date <= LAST_LMT_TRANSITION) {
            date = toFirstDateAfterLocalMeanTime(timeZone, date);
        }
    }
    String displayName = timeZone.getNameKey(date);
    if (displayName != null) {
        return displayName;
    }
    boolean daylightSavings = !timeZone.isStandardOffset(date);
    if (!daylightSavings
            && timeZone.getOffset(date) != timeZone.getStandardOffset(System.currentTimeMillis())) {
        daylightSavings = timeZone.nextTransition(date) != date;
    }
    return tz.getDisplayName(daylightSavings, TimeZone.SHORT, Locale.US);
}
 
Example 3
Source File: TestDateTimeZone.java    From joda-time-android with Apache License 2.0 5 votes vote down vote up
public void testJerusalem() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Jerusalem");
    DateTime winter = new DateTime(2006, 1, 1, 0, 0, 0, 0, zone);
    assertEquals(true, zone.isStandardOffset(winter.getMillis()));

    DateTime cutover = new DateTime(zone.nextTransition(winter.getMillis()), zone);
    assertEquals(false, zone.isStandardOffset(cutover.getMillis()));
    assertEquals(5, cutover.getDayOfWeek());
    assertEquals(31, cutover.getDayOfMonth());
    assertEquals(3, cutover.getMonthOfYear());
}
 
Example 4
Source File: TestBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static void testForwardTransitions(DateTimeZone tz, String[][] data) {
    long instant = Long.MIN_VALUE;
    for (int i=0; i<data.length; i++) {
        String[] row = data[i];
        long expectedInstant = instant;
        if (row[0] != null) {
            instant = tz.nextTransition(instant);
            expectedInstant = new DateTime(row[0]).getMillis();
        }

        String expectedKey = row[1];
        int expectedStandardOffset = -(int) OFFSET_FORMATTER.parseMillis(row[2]);
        int expectedOffset = -(int) OFFSET_FORMATTER.parseMillis(row[3]);

        assertEquals(expectedInstant, instant);
        assertEquals(expectedKey, tz.getNameKey(instant));
        assertEquals(expectedStandardOffset, tz.getStandardOffset(instant));
        assertEquals(expectedOffset, tz.getOffset(instant));

        // Sample a few instants between transitions.
        if (i < data.length - 1) {
            long nextInstant = new DateTime(data[i + 1][0]).getMillis();
            long span = (nextInstant - instant) / 10;
            for (int j=1; j<10; j++) {
                long between = instant + j * span;
                assertEquals(expectedKey, tz.getNameKey(between));
                assertEquals(expectedStandardOffset, tz.getStandardOffset(between));
                assertEquals(expectedOffset, tz.getOffset(between));
            }
        }
    }
}
 
Example 5
Source File: TestBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
static void testForwardTransitions(DateTimeZone tz, String[][] data) {
    long instant = Long.MIN_VALUE;
    for (int i=0; i<data.length; i++) {
        String[] row = data[i];
        long expectedInstant = instant;
        if (row[0] != null) {
            instant = tz.nextTransition(instant);
            expectedInstant = new DateTime(row[0]).getMillis();
        }

        String expectedKey = row[1];
        int expectedStandardOffset = -(int) OFFSET_FORMATTER.parseMillis(row[2]);
        int expectedOffset = -(int) OFFSET_FORMATTER.parseMillis(row[3]);

        assertEquals(expectedInstant, instant);
        assertEquals(expectedKey, tz.getNameKey(instant));
        assertEquals(expectedStandardOffset, tz.getStandardOffset(instant));
        assertEquals(expectedOffset, tz.getOffset(instant));

        // Sample a few instants between transitions.
        if (i < data.length - 1) {
            long nextInstant = new DateTime(data[i + 1][0]).getMillis();
            long span = (nextInstant - instant) / 10;
            for (int j=1; j<10; j++) {
                long between = instant + j * span;
                assertEquals(expectedKey, tz.getNameKey(between));
                assertEquals(expectedStandardOffset, tz.getStandardOffset(between));
                assertEquals(expectedOffset, tz.getOffset(between));
            }
        }
    }
}
 
Example 6
Source File: ZoneInfoCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return false if error.
 */
static boolean test(String id, DateTimeZone tz) {
    if (!id.equals(tz.getID())) {
        return true;
    }

    // Test to ensure that reported transitions are not duplicated.

    long millis = ISOChronology.getInstanceUTC().year().set(0, 1850);
    long end = ISOChronology.getInstanceUTC().year().set(0, 2050);

    int offset = tz.getOffset(millis);
    String key = tz.getNameKey(millis);

    List<Long> transitions = new ArrayList<Long>();

    while (true) {
        long next = tz.nextTransition(millis);
        if (next == millis || next > end) {
            break;
        }

        millis = next;

        int nextOffset = tz.getOffset(millis);
        String nextKey = tz.getNameKey(millis);

        if (offset == nextOffset
            && key.equals(nextKey)) {
            System.out.println("*d* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()));
            return false;
        }

        if (nextKey == null || (nextKey.length() < 3 && !"??".equals(nextKey))) {
            System.out.println("*s* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC())
                               + ", nameKey=" + nextKey);
            return false;
        }

        transitions.add(Long.valueOf(millis));

        offset = nextOffset;
        key = nextKey;
    }

    // Now verify that reverse transitions match up.

    millis = ISOChronology.getInstanceUTC().year().set(0, 2050);
    end = ISOChronology.getInstanceUTC().year().set(0, 1850);

    for (int i=transitions.size(); --i>= 0; ) {
        long prev = tz.previousTransition(millis);
        if (prev == millis || prev < end) {
            break;
        }

        millis = prev;

        long trans = transitions.get(i).longValue();
        
        if (trans - 1 != millis) {
            System.out.println("*r* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()) + " != "
                               + new DateTime(trans - 1,
                                              ISOChronology.getInstanceUTC()));
                               
            return false;
        }
    }

    return true;
}
 
Example 7
Source File: TestDateTimeZone.java    From joda-time-android with Apache License 2.0 4 votes vote down vote up
public void testToronto() {
    DateTimeZone zone = DateTimeZone.forID("America/Toronto");
    DateTime start = new DateTime(1927, 1, 1, 0, 0, 0, 0, zone);
    assertEquals(-5 * 3600000, zone.getStandardOffset(start.getMillis()));
    assertEquals(-5 * 3600000, zone.getOffset(start.getMillis()));

    DateTime summer1927 = new DateTime(zone.nextTransition(start.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1927.getMillis()));
    assertEquals(new LocalDate(1927, 5, 1), summer1927.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1927.getDayOfWeek());

    DateTime winter1927 = new DateTime(zone.nextTransition(summer1927.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1927.getMillis()));
    assertEquals(new LocalDate(1927, 9, 25), winter1927.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1927.getDayOfWeek());

    DateTime summer1928 = new DateTime(zone.nextTransition(winter1927.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1928.getMillis()));
    assertEquals(new LocalDate(1928, 4, 29), summer1928.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1928.getDayOfWeek());

    DateTime winter1928 = new DateTime(zone.nextTransition(summer1928.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1928.getMillis()));
    assertEquals(new LocalDate(1928, 9, 30), winter1928.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1928.getDayOfWeek());

    DateTime summer1929 = new DateTime(zone.nextTransition(winter1928.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1929.getMillis()));
    assertEquals(new LocalDate(1929, 4, 28), summer1929.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1929.getDayOfWeek());

    DateTime winter1929 = new DateTime(zone.nextTransition(summer1929.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1929.getMillis()));
    assertEquals(new LocalDate(1929, 9, 29), winter1929.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1929.getDayOfWeek());

    DateTime summer1930 = new DateTime(zone.nextTransition(winter1929.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1930.getMillis()));
    assertEquals(new LocalDate(1930, 4, 27), summer1930.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1930.getDayOfWeek());

    DateTime winter1930 = new DateTime(zone.nextTransition(summer1930.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1930.getMillis()));
    assertEquals(new LocalDate(1930, 9, 28), winter1930.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1930.getDayOfWeek());

    DateTime summer1931 = new DateTime(zone.nextTransition(winter1930.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1931.getMillis()));
    assertEquals(new LocalDate(1931, 4, 26), summer1931.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1931.getDayOfWeek());

    DateTime winter1931 = new DateTime(zone.nextTransition(summer1931.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1931.getMillis()));
    assertEquals(new LocalDate(1931, 9, 27), winter1931.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1931.getDayOfWeek());

    DateTime summer1932 = new DateTime(zone.nextTransition(winter1931.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1932.getMillis()));
    assertEquals(new LocalDate(1932, 5, 1), summer1932.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1932.getDayOfWeek());

    DateTime winter1932 = new DateTime(zone.nextTransition(summer1932.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1932.getMillis()));
    assertEquals(new LocalDate(1932, 9, 25), winter1932.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1932.getDayOfWeek());

    DateTime summer1933 = new DateTime(zone.nextTransition(winter1932.getMillis()), zone);
    assertEquals(-4 * 3600000, zone.getOffset(summer1933.getMillis()));
    assertEquals(new LocalDate(1933, 4, 30), summer1933.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, summer1933.getDayOfWeek());

    DateTime winter1933 = new DateTime(zone.nextTransition(summer1933.getMillis()), zone);
    assertEquals(-5 * 3600000, zone.getOffset(winter1933.getMillis()));
    assertEquals(new LocalDate(1933, 10, 1), winter1933.toLocalDate());
    assertEquals(DateTimeConstants.SUNDAY, winter1933.getDayOfWeek());
}
 
Example 8
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2006() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2006, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2006, 3, 31, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 9
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2005() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2005, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2005, 4, 1, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 10
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2004() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2004, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2004, 3, 26, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 11
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2003() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2003, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2003, 3, 28, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 12
Source File: Time_11_ZoneInfoCompiler_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @return false if error.
 */
static boolean test(String id, DateTimeZone tz) {
    if (!id.equals(tz.getID())) {
        return true;
    }

    // Test to ensure that reported transitions are not duplicated.

    long millis = ISOChronology.getInstanceUTC().year().set(0, 1850);
    long end = ISOChronology.getInstanceUTC().year().set(0, 2050);

    int offset = tz.getOffset(millis);
    String key = tz.getNameKey(millis);

    List<Long> transitions = new ArrayList<Long>();

    while (true) {
        long next = tz.nextTransition(millis);
        if (next == millis || next > end) {
            break;
        }

        millis = next;

        int nextOffset = tz.getOffset(millis);
        String nextKey = tz.getNameKey(millis);

        if (offset == nextOffset
            && key.equals(nextKey)) {
            System.out.println("*d* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()));
            return false;
        }

        if (nextKey == null || (nextKey.length() < 3 && !"??".equals(nextKey))) {
            System.out.println("*s* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC())
                               + ", nameKey=" + nextKey);
            return false;
        }

        transitions.add(Long.valueOf(millis));

        offset = nextOffset;
        key = nextKey;
    }

    // Now verify that reverse transitions match up.

    millis = ISOChronology.getInstanceUTC().year().set(0, 2050);
    end = ISOChronology.getInstanceUTC().year().set(0, 1850);

    for (int i=transitions.size(); --i>= 0; ) {
        long prev = tz.previousTransition(millis);
        if (prev == millis || prev < end) {
            break;
        }

        millis = prev;

        long trans = transitions.get(i).longValue();
        
        if (trans - 1 != millis) {
            System.out.println("*r* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()) + " != "
                               + new DateTime(trans - 1,
                                              ISOChronology.getInstanceUTC()));
                               
            return false;
        }
    }

    return true;
}
 
Example 13
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2006() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2006, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2006, 3, 31, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 14
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2005() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2005, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2005, 4, 1, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 15
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2004() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2004, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2004, 3, 26, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 16
Source File: TestCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void test_Amman_2003() {
    DateTimeZone zone = DateTimeZone.forID("Asia/Amman");
    DateTime dt = new DateTime(2003, 3, 1, 0, 0, zone);
    long next = zone.nextTransition(dt.getMillis());
    assertEquals(next, new DateTime(2003, 3, 28, 0, 0, DateTimeZone.forOffsetHours(2)).getMillis());
}
 
Example 17
Source File: ZoneInfoCompiler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return false if error.
 */
static boolean test(String id, DateTimeZone tz) {
    if (!id.equals(tz.getID())) {
        return true;
    }

    // Test to ensure that reported transitions are not duplicated.

    long millis = ISOChronology.getInstanceUTC().year().set(0, 1850);
    long end = ISOChronology.getInstanceUTC().year().set(0, 2050);

    int offset = tz.getOffset(millis);
    String key = tz.getNameKey(millis);

    List<Long> transitions = new ArrayList<Long>();

    while (true) {
        long next = tz.nextTransition(millis);
        if (next == millis || next > end) {
            break;
        }

        millis = next;

        int nextOffset = tz.getOffset(millis);
        String nextKey = tz.getNameKey(millis);

        if (offset == nextOffset
            && key.equals(nextKey)) {
            System.out.println("*d* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()));
            return false;
        }

        if (nextKey == null || (nextKey.length() < 3 && !"??".equals(nextKey))) {
            System.out.println("*s* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC())
                               + ", nameKey=" + nextKey);
            return false;
        }

        transitions.add(Long.valueOf(millis));

        offset = nextOffset;
        key = nextKey;
    }

    // Now verify that reverse transitions match up.

    millis = ISOChronology.getInstanceUTC().year().set(0, 2050);
    end = ISOChronology.getInstanceUTC().year().set(0, 1850);

    for (int i=transitions.size(); --i>= 0; ) {
        long prev = tz.previousTransition(millis);
        if (prev == millis || prev < end) {
            break;
        }

        millis = prev;

        long trans = transitions.get(i).longValue();
        
        if (trans - 1 != millis) {
            System.out.println("*r* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()) + " != "
                               + new DateTime(trans - 1,
                                              ISOChronology.getInstanceUTC()));
                               
            return false;
        }
    }

    return true;
}
 
Example 18
Source File: Time_11_ZoneInfoCompiler_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @return false if error.
 */
static boolean test(String id, DateTimeZone tz) {
    if (!id.equals(tz.getID())) {
        return true;
    }

    // Test to ensure that reported transitions are not duplicated.

    long millis = ISOChronology.getInstanceUTC().year().set(0, 1850);
    long end = ISOChronology.getInstanceUTC().year().set(0, 2050);

    int offset = tz.getOffset(millis);
    String key = tz.getNameKey(millis);

    List<Long> transitions = new ArrayList<Long>();

    while (true) {
        long next = tz.nextTransition(millis);
        if (next == millis || next > end) {
            break;
        }

        millis = next;

        int nextOffset = tz.getOffset(millis);
        String nextKey = tz.getNameKey(millis);

        if (offset == nextOffset
            && key.equals(nextKey)) {
            System.out.println("*d* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()));
            return false;
        }

        if (nextKey == null || (nextKey.length() < 3 && !"??".equals(nextKey))) {
            System.out.println("*s* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC())
                               + ", nameKey=" + nextKey);
            return false;
        }

        transitions.add(Long.valueOf(millis));

        offset = nextOffset;
        key = nextKey;
    }

    // Now verify that reverse transitions match up.

    millis = ISOChronology.getInstanceUTC().year().set(0, 2050);
    end = ISOChronology.getInstanceUTC().year().set(0, 1850);

    for (int i=transitions.size(); --i>= 0; ) {
        long prev = tz.previousTransition(millis);
        if (prev == millis || prev < end) {
            break;
        }

        millis = prev;

        long trans = transitions.get(i).longValue();
        
        if (trans - 1 != millis) {
            System.out.println("*r* Error in " + tz.getID() + " "
                               + new DateTime(millis,
                                              ISOChronology.getInstanceUTC()) + " != "
                               + new DateTime(trans - 1,
                                              ISOChronology.getInstanceUTC()));
                               
            return false;
        }
    }

    return true;
}