private static ZoneOffset parseOffset( Matcher matcher, Supplier<ZoneId> defaultZone ) { ZoneOffset offset = parseOffset( matcher ); if ( offset == null ) { ZoneId zoneId = defaultZone.get(); offset = zoneId instanceof ZoneOffset ? (ZoneOffset) zoneId : zoneId.getRules().getOffset( Instant.now() ); } return offset; }
@Override public Atop create(AtopTable table, ZonedDateTime date) { checkArgument(date.getZone().getRules().equals(timeZone.getRules()), "Split date (%s) is not in the local timezone (%s)", date.getZone(), timeZone); ProcessBuilder processBuilder = new ProcessBuilder(executablePath); processBuilder.command().add("-P"); processBuilder.command().add(table.getAtopLabel()); processBuilder.command().add("-r"); processBuilder.command().add(DATE_FORMATTER.format(date)); Process process; try { process = processBuilder.start(); } catch (IOException e) { throw new PrestoException(ATOP_CANNOT_START_PROCESS_ERROR, format("Cannot start %s", processBuilder.command()), e); } return new AtopProcess(process, readTimeout, executor); }
final LocalDateTime localDateTime = (LocalDateTime) value; final ZoneId defaultZoneId = ZoneId.systemDefault(); final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime); date = Date.from(localDateTime.toInstant(offset)); } else if (value instanceof LocalDate) { final LocalDateTime localDateTime = localDate.atStartOfDay(); final ZoneId defaultZoneId = ZoneId.systemDefault(); final ZoneOffset offset = defaultZoneId.getRules().getOffset(localDateTime); date = Date.from(localDateTime.toInstant(offset)); } else if (value instanceof Instant) {
@Test public void testDateToTimestampCoercion() { // allow running tests with a connector that supports TIMESTAMP but not DATE // ordinary date MaterializedResult rows = h2QueryRunner.execute(TEST_SESSION, "SELECT DATE '2018-01-13'", ImmutableList.of(TIMESTAMP)); assertEquals(rows.getOnlyValue(), LocalDate.of(2018, 1, 13).atStartOfDay()); // date, which midnight was skipped in JVM zone LocalDate forwardOffsetChangeAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); checkState(ZoneId.systemDefault().getRules().getValidOffsets(forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay()).size() == 0, "This test assumes certain JVM time zone"); rows = h2QueryRunner.execute(TEST_SESSION, DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(forwardOffsetChangeAtMidnightInJvmZone), ImmutableList.of(TIMESTAMP)); assertEquals(rows.getOnlyValue(), forwardOffsetChangeAtMidnightInJvmZone.atStartOfDay()); } }
/** * 将当前时区时间转成UTC时间. * * @param dateTime 时间 * @return LocalDateTime */ public static LocalDateTime toUTCDateTime(final LocalDateTime dateTime) { if (dateTime == null) { return null; } else { Instant instant = dateTime.toInstant(DEFAULT_ZONE.getRules().getOffset(dateTime)); return LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), ZoneOffset.UTC); } }
@Test public void testLocallyUnrepresentableTimeLiterals() { LocalDateTime localTimeThatDidNotExist = LocalDateTime.of(2017, 4, 2, 2, 10); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotExist).isEmpty(), "This test assumes certain JVM time zone"); // This tests that both Presto runner and H2 can return TIMESTAMP value that never happened in JVM's zone (e.g. is not representable using java.sql.Timestamp) @Language("SQL") String sql = DateTimeFormatter.ofPattern("'SELECT TIMESTAMP '''uuuu-MM-dd HH:mm:ss''").format(localTimeThatDidNotExist); assertEquals(computeScalar(sql), localTimeThatDidNotExist); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner LocalDate localDateThatDidNotHaveMidnight = LocalDate.of(1970, 1, 1); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localDateThatDidNotHaveMidnight.atStartOfDay()).isEmpty(), "This test assumes certain JVM time zone"); // This tests that both Presto runner and H2 can return DATE value for a day which midnight never happened in JVM's zone (e.g. is not exactly representable using java.sql.Date) sql = DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(localDateThatDidNotHaveMidnight); assertEquals(computeScalar(sql), localDateThatDidNotHaveMidnight); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner LocalTime localTimeThatDidNotOccurOn19700101 = LocalTime.of(0, 10); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotOccurOn19700101.atDate(LocalDate.ofEpochDay(0))).isEmpty(), "This test assumes certain JVM time zone"); checkState(!Objects.equals(java.sql.Time.valueOf(localTimeThatDidNotOccurOn19700101).toLocalTime(), localTimeThatDidNotOccurOn19700101), "This test assumes certain JVM time zone"); sql = DateTimeFormatter.ofPattern("'SELECT TIME '''HH:mm:ss''").format(localTimeThatDidNotOccurOn19700101); assertEquals(computeScalar(sql), localTimeThatDidNotOccurOn19700101); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner } }
try expected = zone.getRules().getOffset( local );
@Override public Date unmarshal(String date) throws Exception { final LocalDateTime now = LocalDateTime.now(); final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_TIME_FORMAT) .parseDefaulting(ChronoField.YEAR, now.getYear()) .parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue()) .parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth()) .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0) .toFormatter(Locale.US); final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser); return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now))); }
datetime.get(ChronoField.DAY_OF_MONTH) ).atStartOfDay().toInstant(ZoneOffset.UTC); ZoneOffset currentOffsetForMyZone = timezone.get().getRules().getOffset(reference); try { return Optional.of(java.time.LocalDateTime.of(
@Override public Date unmarshal(String date) throws Exception { final LocalDateTime now = LocalDateTime.now(); final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_DATE_TIME_FORMAT) .parseDefaulting(ChronoField.YEAR, now.getYear()) .parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue()) .parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth()) .parseDefaulting(ChronoField.HOUR_OF_DAY, now.getHour()) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, now.getMinute()) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, now.getSecond()) .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0) .toFormatter(Locale.US); final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser); return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now))); }
@Test public void testDate() { // Note: there is identical test for MySQL ZoneId jvmZone = ZoneId.systemDefault(); checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty()); ZoneId someZone = ZoneId.of("Europe/Vilnius"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty()); LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2); DataTypeTest testCases = DataTypeTest.create() .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1)) .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3)) .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST) .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone); for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) { Session session = Session.builder(getQueryRunner().getDefaultSession()) .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId)) .build(); testCases.execute(getQueryRunner(), session, postgresCreateAndInsert("tpch.test_date")); testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date")); } }
@Test public void testDate() { // Note: there is identical test for PostgreSQL ZoneId jvmZone = ZoneId.systemDefault(); checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty()); ZoneId someZone = ZoneId.of("Europe/Vilnius"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty()); LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2); DataTypeTest testCases = DataTypeTest.create() .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1)) .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3)) .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST) .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone); for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) { Session session = Session.builder(getQueryRunner().getDefaultSession()) .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId)) .build(); testCases.execute(getQueryRunner(), session, mysqlCreateAndInsert("tpch.test_date")); testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date")); } }
private void assertTimestamp(String c4) { // '2014-09-08 17:51:04.777' // MySQL container is in UTC and the test time is during summer time period ZonedDateTime expectedTimestamp = ZonedDateTime.ofInstant( LocalDateTime.parse("2014-09-08T17:51:04.780").atZone(ZoneId.of("US/Samoa")).toInstant(), ZoneId.systemDefault()); ZoneId defaultZoneId = ZoneId.systemDefault(); ZonedDateTime c4DateTime = ZonedDateTime.parse(c4, ZonedTimestamp.FORMATTER).withZoneSameInstant(defaultZoneId); assertThat(c4DateTime.getYear()).isEqualTo(expectedTimestamp.getYear()); assertThat(c4DateTime.getMonth()).isEqualTo(expectedTimestamp.getMonth()); assertThat(c4DateTime.getDayOfMonth()).isEqualTo(expectedTimestamp.getDayOfMonth()); assertThat(c4DateTime.getHour()).isEqualTo(expectedTimestamp.getHour()); assertThat(c4DateTime.getMinute()).isEqualTo(expectedTimestamp.getMinute()); assertThat(c4DateTime.getSecond()).isEqualTo(expectedTimestamp.getSecond()); assertThat(c4DateTime.getNano()).isEqualTo(expectedTimestamp.getNano()); // We're running the connector in the same timezone as the server, so the timezone in the timestamp // should match our current offset ... LocalDateTime expectedLocalDateTime = LocalDateTime.parse("2014-09-08T17:51:04.780"); ZoneOffset expectedOffset = defaultZoneId.getRules().getOffset(expectedLocalDateTime); assertThat(c4DateTime.getOffset()).isEqualTo(expectedOffset); }
ZoneId zoneId = ZoneId.of("Australia/Sydney"); ZoneRules rules = zoneId.getRules(); ZoneOffsetTransition nextTransition = rules.nextTransition(Instant.now()); System.out.println("Next transition at: " + nextTransition.getInstant().atZone(zoneId)); ZoneOffsetTransition nextNextTransition = rules.nextTransition(nextTransition.getInstant()); System.out.println("Next transition after that at: " + nextNextTransition.getInstant().atZone(zoneId));
return null; ZoneRules rules = zoneId.getRules(); TimeZone timeZone = TimeZone.getTimeZone(zoneId); List<Long> transitionsList = new ArrayList<>();
JdkBasedDateTimeZone(String zoneId) { super(zoneId); this.zoneRules = ZoneId.of(zoneId).getRules(); }
/** * Returns a {@link java.time.ZoneOffset} for this zone as of the provided {@link java.time.Instant}. * * @param self a ZoneId * @param instant an Instant * @return a ZoneOffset * @since 2.5.0 */ public static ZoneOffset getOffset(final ZoneId self, Instant instant) { return self.getRules().getOffset(instant); }
ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) ); … ZoneId z = now.getZone(); ZoneRules zoneRules = z.getRules(); Boolean isDst = zoneRules.isDaylightSavings( now.toInstant() );
/** * Converts this TimeZone to a corresponding {@link java.time.ZoneOffset}. The offset is determined * using the date/time of specified Instant. * * @param self a TimeZone * @return a ZoneOffset * @since 2.5.0 */ public static ZoneOffset toZoneOffset(final TimeZone self, Instant instant) { return self.toZoneId().getRules().getOffset(instant); } }