@Override public ZonedDateTime createdAt() { return ZonedDateTime.of(2016, 12, 16, 12, 35, 0, 0, ZoneOffset.UTC); }
private static ZonedDateTime newZonedDateTime( long epochSecondLocal, long nano, ZoneId zoneId ) { Instant instant = Instant.ofEpochSecond( epochSecondLocal, nano ); LocalDateTime localDateTime = LocalDateTime.ofInstant( instant, UTC ); return ZonedDateTime.of( localDateTime, zoneId ); } }
/** * Converts local date time to Calendar. */ public static Calendar toCalendar(final LocalDateTime localDateTime) { return GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault())); }
public static DateTimeValue datetime( int year, int month, int day, int hour, int minute, int second, int nanoOfSecond, ZoneId zone ) { return new DateTimeValue( assertValidArgument( () -> ZonedDateTime.of( year, month, day, hour, minute, second, nanoOfSecond, zone ) ) ); }
@Test public void shouldRangeSeekInOrderAscendingDateTimeArray() throws Exception { Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )}; Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )}; Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )}; Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )}; Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )}; Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )}; shouldRangeSeekInOrder( IndexOrder.ASCENDING, o0, o1, o2, o3, o4, o5 ); }
@Test public void shouldRangeSeekInOrderDescendingDateTimeArray() throws Exception { Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )}; Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )}; Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )}; Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )}; Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )}; Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )}; shouldSeekInOrderExactWithRange( IndexOrder.DESCENDING, o0, o1, o2, o3, o4, o5 ); }
@Test public void shouldRangeSeekInOrderDescendingDateTimeArray() throws Exception { Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )}; Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )}; Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )}; Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )}; Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )}; Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )}; shouldRangeSeekInOrder( IndexOrder.DESCENDING, o0, o1, o2, o3, o4, o5 ); }
@Test public void shouldRangeSeekInOrderAscendingDateTimeArray() throws Exception { Object o0 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 0, ZoneId.of( "UTC" ) )}; Object o1 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 1, ZoneId.of( "UTC" ) )}; Object o2 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 2, ZoneId.of( "UTC" ) )}; Object o3 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 3, ZoneId.of( "UTC" ) )}; Object o4 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 4, ZoneId.of( "UTC" ) )}; Object o5 = new ZonedDateTime[]{ZonedDateTime.of( 10, 10, 10, 10, 10, 10, 5, ZoneId.of( "UTC" ) )}; shouldSeekInOrderExactWithRange( IndexOrder.ASCENDING, o0, o1, o2, o3, o4, o5 ); }
public static DateTimeValue datetime( DateValue date, LocalTimeValue time, ZoneId zone ) { return new DateTimeValue( ZonedDateTime.of( date.temporal(), time.temporal(), zone ) ); }
@Test public void lastModified() { HttpHeaders headers = new HttpHeaders(); ZonedDateTime lastModified = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); headers.setLastModified(lastModified.toInstant().toEpochMilli()); HeaderAssertions assertions = headerAssertions(headers); assertions.lastModified(lastModified.toInstant().toEpochMilli()); try { assertions.lastModified(lastModified.toInstant().toEpochMilli() + 1); fail("Wrong value expected"); } catch (AssertionError error) { // Expected } }
private TemporalValue attachDate( TemporalValue temporal, LocalDate dateToAttach ) { LocalTime timePart = temporal.getLocalTimePart(); if ( temporal.supportsTimeZone() ) { // turn time into date time return datetime( ZonedDateTime.of( dateToAttach, timePart, temporal.getZoneOffset() ) ); } else { // turn local time into local date time return localDateTime( LocalDateTime.of( dateToAttach, timePart ) ); } }
@Test public void expires() { HttpHeaders headers = new HttpHeaders(); ZonedDateTime expires = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); headers.setExpires(expires); HeaderAssertions assertions = headerAssertions(headers); assertions.expires(expires.toInstant().toEpochMilli()); try { assertions.expires(expires.toInstant().toEpochMilli() + 1); fail("Wrong value expected"); } catch (AssertionError error) { // Expected } }
@Test // SPR-17330 public void matchDateFormattedWithHttpHeaders() throws Exception { long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneId.of("GMT")).toInstant().toEpochMilli(); HttpHeaders headers = new HttpHeaders(); headers.setDate("myDate", epochMilli); this.response.setHeader("d", headers.getFirst("myDate")); this.matchers.dateValue("d", epochMilli).match(this.mvcResult); }
@Test public void createDateTimeFormatterWithTimeZone() { factory.setPattern("yyyyMMddHHmmss Z"); factory.setTimeZone(TEST_TIMEZONE); ZoneId dateTimeZone = TEST_TIMEZONE.toZoneId(); ZonedDateTime dateTime = ZonedDateTime.of(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200"); assertThat(factory.createDateTimeFormatter().format(dateTime), is("20091021121000 " + offset)); }
@Test public void expiresZonedDateTime() { ZonedDateTime zonedDateTime = ZonedDateTime.of(2008, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT")); headers.setExpires(zonedDateTime); assertEquals("Invalid Expires header", zonedDateTime.toInstant().toEpochMilli(), headers.getExpires()); assertEquals("Invalid Expires header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("expires")); }
public static DateTimeValue datetime( DateValue date, TimeValue time ) { OffsetTime t = time.temporal(); return new DateTimeValue( ZonedDateTime.of( date.temporal(), t.toLocalTime(), t.getOffset() ) ); }
@Override protected DateTimeValue selectDateTime( AnyValue datetime ) { if ( datetime instanceof DateTimeValue ) { DateTimeValue value = (DateTimeValue) datetime; ZoneId zone = optionalTimezone(); return zone == null ? value : new DateTimeValue( ZonedDateTime.of( value.temporal().toLocalDateTime(), zone ) ); } if ( datetime instanceof LocalDateTimeValue ) { return new DateTimeValue( ZonedDateTime.of( ((LocalDateTimeValue) datetime).temporal(), timezone() ) ); } throw new UnsupportedTemporalUnitException( "Cannot select datetime from: " + datetime ); } };
@Test public void firstZonedDateTime() { ZonedDateTime date = ZonedDateTime.of(2017, 6, 22, 22, 22, 0, 0, ZoneId.of("GMT")); headers.setZonedDateTime(HttpHeaders.DATE, date); assertThat(headers.getFirst(HttpHeaders.DATE), is("Thu, 22 Jun 2017 22:22:00 GMT")); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); headers.clear(); ZonedDateTime otherDate = ZonedDateTime.of(2010, 12, 18, 10, 20, 0, 0, ZoneId.of("GMT")); headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(date)); headers.add(HttpHeaders.DATE, RFC_1123_DATE_TIME.format(otherDate)); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); // obsolete RFC 850 format headers.clear(); headers.set(HttpHeaders.DATE, "Thursday, 22-Jun-17 22:22:00 GMT"); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); // ANSI C's asctime() format headers.clear(); headers.set(HttpHeaders.DATE, "Thu Jun 22 22:22:00 2017"); assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date)); }
@Test public void shouldPackLocalDateTimeWithTimeZoneId() { LocalDateTime localDateTime = LocalDateTime.of( 1999, 12, 30, 9, 49, 20, 999999999 ); ZoneId zoneId = ZoneId.of( "Europe/Stockholm" ); ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, zoneId ); PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) ); ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() ); buffer.getShort(); // skip struct header assertEquals( INT_32, buffer.get() ); assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() ); assertEquals( INT_32, buffer.get() ); assertEquals( localDateTime.getNano(), buffer.getInt() ); buffer.getShort(); // skip zoneId string header byte[] zoneIdBytes = new byte[zoneId.getId().getBytes( UTF_8 ).length]; buffer.get( zoneIdBytes ); assertEquals( zoneId.getId(), new String( zoneIdBytes, UTF_8 ) ); }
@Test public void shouldPackLocalDateTimeWithTimeZoneOffset() { LocalDateTime localDateTime = LocalDateTime.of( 2015, 3, 23, 19, 15, 59, 10 ); ZoneOffset offset = ZoneOffset.ofHoursMinutes( -5, -15 ); ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, offset ); PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) ); ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() ); buffer.getShort(); // skip struct header assertEquals( INT_32, buffer.get() ); assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() ); assertEquals( localDateTime.getNano(), buffer.get() ); assertEquals( INT_16, buffer.get() ); assertEquals( offset.getTotalSeconds(), buffer.getShort() ); }