public Temporal setTimeZone(int offsetHours) { return setTimeZone(DateTimeZone.forOffsetHours(offsetHours)); }
@Test public void testTimeZoneAtExtremeLocations() { Function<String, DateTime> parser = TimestampParser.createTimestampParser("EEE MMM dd yy HH:mm:ss zZ z"); Assert.assertEquals(new DateTime(2005, 1, 22, 13, 0, DateTimeZone.forOffsetHours(-6)).getMillis(), parser.apply("Sat Jan 22 05 13:00:00 GMT-0600 CST").getMillis()); parser = TimestampParser.createTimestampParser("zZ z EEE MMM dd yy HH:mm:ss"); Assert.assertEquals(new DateTime(2005, 1, 22, 13, 0, DateTimeZone.forOffsetHours(-6)).getMillis(), parser.apply("GMT-0600 CST Sat Jan 22 05 13:00:00").getMillis()); }
@Test public void testTimeStampParserWithQuotes() { DateTime d = new DateTime(1994, 11, 9, 4, 0, DateTimeZone.forOffsetHours(-8)); Function<String, DateTime> parser = TimestampParser.createTimestampParser("EEE MMM dd HH:mm:ss z yyyy"); Assert.assertEquals(d.getMillis(), parser.apply(" \" Wed Nov 9 04:00:00 PST 1994 \" ").getMillis()); }
@Test public void testTimeStampParserWithShortTimeZone() { DateTime d = new DateTime(1994, 11, 9, 4, 0, DateTimeZone.forOffsetHours(-8)); Function<String, DateTime> parser = TimestampParser.createTimestampParser("EEE MMM dd HH:mm:ss z yyyy"); Assert.assertEquals(d.getMillis(), parser.apply("Wed Nov 9 04:00:00 PST 1994").getMillis()); }
@Test public void testForOffsetHours_int() { assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHours(0)); assertEquals(DateTimeZone.forID("+03:00"), DateTimeZone.forOffsetHours(3)); assertEquals(DateTimeZone.forID("-02:00"), DateTimeZone.forOffsetHours(-2)); try { DateTimeZone.forOffsetHours(999999); fail(); } catch (IllegalArgumentException ex) {} }
@Test public void testTimeStampParserWithLongTimeZone() { long millis1 = new DateTime(1994, 11, 9, 4, 0, DateTimeZone.forOffsetHours(-8)).getMillis(); long millis2 = new DateTime(1994, 11, 9, 4, 0, DateTimeZone.forOffsetHours(-6)).getMillis(); Function<String, DateTime> parser = TimestampParser.createTimestampParser("EEE MMM dd HH:mm:ss zZ z yyyy"); Assert.assertEquals(millis1, parser.apply("Wed Nov 9 04:00:00 GMT-0800 PST 1994").getMillis()); Assert.assertEquals(millis2, parser.apply("Wed Nov 9 04:00:00 GMT-0600 CST 1994").getMillis()); Assert.assertEquals(millis1, parser.apply("Wed Nov 9 04:00:00 UTC-0800 PST 1994").getMillis()); Assert.assertEquals(millis2, parser.apply("Wed Nov 9 04:00:00 UTC-0600 CST 1994").getMillis()); parser = TimestampParser.createTimestampParser("EEE MMM dd HH:mm:ss zZ yyyy"); Assert.assertEquals(millis1, parser.apply("Wed Nov 9 04:00:00 GMT-0800 1994").getMillis()); Assert.assertEquals(millis2, parser.apply("Wed Nov 9 04:00:00 GMT-0600 1994").getMillis()); Assert.assertEquals(millis1, parser.apply("Wed Nov 9 04:00:00 UTC-0800 1994").getMillis()); Assert.assertEquals(millis2, parser.apply("Wed Nov 9 04:00:00 UTC-0600 1994").getMillis()); }
@Test public void testJodaSymbolInsideLiteral() { DateTime d = new DateTime(1994, 11, 9, 4, 0, DateTimeZone.forOffsetHours(-8)); Assert.assertEquals(d.getMillis(), TimestampParser.createTimestampParser("EEE MMM dd HH:mm:ss z yyyy 'helloz'") .apply("Wed Nov 9 04:00:00 PST 1994 helloz") .getMillis() ); Assert.assertEquals(d.getMillis(), TimestampParser.createTimestampParser("EEE MMM dd HH:mm:ss 'helloz' z yyyy 'hello'") .apply("Wed Nov 9 04:00:00 helloz PST 1994 hello") .getMillis() ); } }
@Test public void testParseDatetime() { assertFunction("parse_datetime('1960/01/22 03:04', 'YYYY/MM/DD HH:mm')", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(new DateTime(1960, 1, 22, 3, 4, 0, 0, DATE_TIME_ZONE))); assertFunction("parse_datetime('1960/01/22 03:04 Asia/Oral', 'YYYY/MM/DD HH:mm ZZZZZ')", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(new DateTime(1960, 1, 22, 3, 4, 0, 0, DateTimeZone.forID("Asia/Oral")))); assertFunction("parse_datetime('1960/01/22 03:04 +0500', 'YYYY/MM/DD HH:mm Z')", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(new DateTime(1960, 1, 22, 3, 4, 0, 0, DateTimeZone.forOffsetHours(5)))); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDateC() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeC); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 20); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDate1() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime1); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 19); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDate3() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime3); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 20); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDateA() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeA); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 21); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDateB() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeB); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 21); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDate2() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime2); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 20); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }
.stringToTimeTransform("timestamp","dd/MMM/YYYY:HH:mm:ss Z", DateTimeZone.forOffsetHours(-4))
@Override public DateTimeZone deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonToken t = p.getCurrentToken(); if (t == JsonToken.VALUE_NUMBER_INT) { // for fun let's allow use of offsets... return DateTimeZone.forOffsetHours(p.getIntValue()); } if (t == JsonToken.VALUE_STRING) { return DateTimeZone.forID(p.getText().trim()); } return _handleNotNumberOrString(p, ctxt); } }
return DateTimeZone.forID(p.text()); } else { return DateTimeZone.forOffsetHours(p.intValue());
@Test(groups = "fast") public void testComputeOffset1() { final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8); final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime1); int offset = DateAndTimeZoneContext.computeOffsetFromUtc(effectiveDateTime, timeZone); assertEquals(offset, -1); }
@Test(groups = "fast") public void testComputeOffset2() { final DateTimeZone timeZone = DateTimeZone.forOffsetHours(-8); final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTime2); int offset = DateAndTimeZoneContext.computeOffsetFromUtc(effectiveDateTime, timeZone); assertEquals(offset, 0); }
@Test(groups = "fast") public void testComputeUTCDateTimeFromLocalDateA() { final DateTime effectiveDateTime = DATE_TIME_FORMATTER.parseDateTime(effectiveDateTimeA); final DateTimeZone timeZone = DateTimeZone.forOffsetHours(8); refreshCallContext(effectiveDateTime, timeZone); final LocalDate endDate = new LocalDate(2013, 01, 21); final DateTime endDateTimeInUTC = internalCallContext.toUTCDateTime(endDate); assertTrue(endDateTimeInUTC.compareTo(effectiveDateTime.plusYears(1)) == 0); }