Search in sources :

Example 81 with ZoneId

use of java.time.ZoneId in project org.csstudio.display.builder by kasemir.

the class TemporalRoundingTest method testTimeRounding.

@Test
public void testTimeRounding() {
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    final ZoneId zone = ZoneId.of("America/New_York");
    ZonedDateTime time = ZonedDateTime.of(2014, 10, 25, 13, 40, 1, Duration.ofMillis(42).getNano(), zone);
    assertThat(formatter.format(time), equalTo("2014-10-25 13:40:01.042"));
    assertThat(formatter.format(time.with(nextOrSameMidnight)), equalTo("2014-10-26 00:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MILLIS, 1))), equalTo("2014-10-25 13:40:01.042"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MILLIS, 50))), equalTo("2014-10-25 13:40:01.050"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MILLIS, 500))), equalTo("2014-10-25 13:40:01.500"));
    // Truncating side-effect of rounding by 0
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.SECONDS, 0))), equalTo("2014-10-25 13:40:01.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.SECONDS, 1))), equalTo("2014-10-25 13:40:02.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.SECONDS, 5))), equalTo("2014-10-25 13:40:05.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.SECONDS, 8))), equalTo("2014-10-25 13:40:08.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.SECONDS, 30))), equalTo("2014-10-25 13:40:30.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.SECONDS, 60))), equalTo("2014-10-25 13:41:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MINUTES, 1))), equalTo("2014-10-25 13:41:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MINUTES, 5))), equalTo("2014-10-25 13:45:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MINUTES, 30))), equalTo("2014-10-25 14:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MINUTES, 60))), equalTo("2014-10-25 14:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.HOURS, 1))), equalTo("2014-10-25 14:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.HOURS, 2))), equalTo("2014-10-25 14:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.HOURS, 3))), equalTo("2014-10-25 15:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.HOURS, 5))), equalTo("2014-10-25 15:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.HOURS, 6))), equalTo("2014-10-25 18:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.HOURS, 12))), equalTo("2014-10-26 00:00:00.000"));
    // Truncating side-effect of rounding by 0
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.DAYS, 0))), equalTo("2014-10-25 00:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.DAYS, 1))), equalTo("2014-10-26 00:00:00.000"));
    // Every 'other' day counts from 1, i.e. 1, 3, .., 27
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.DAYS, 2))), equalTo("2014-10-27 00:00:00.000"));
    // Treat "7 days" as "next Monday"
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.DAYS, 7))), equalTo("2014-10-27 00:00:00.000"));
    assertThat(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.DAYS, 7)).getDayOfWeek(), equalTo(DayOfWeek.MONDAY));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MONTHS, 1))), equalTo("2014-11-01 00:00:00.000"));
    // Months counted from 1, so rounding by 2 gives 1, 3, ..9, 11
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MONTHS, 2))), equalTo("2014-11-01 00:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.MONTHS, 6))), equalTo("2015-01-01 00:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.YEARS, 1))), equalTo("2015-01-01 00:00:00.000"));
    assertThat(formatter.format(time.with(zonedDateTimerRoundedToNextOrSame(ChronoUnit.YEARS, 2))), equalTo("2016-01-01 00:00:00.000"));
}
Also used : ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.junit.Test)

Example 82 with ZoneId

use of java.time.ZoneId in project Bytecoder by mirkosertic.

the class DateTimePrintContext method adjust.

private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
    // normal case first (early return is an optimization)
    Chronology overrideChrono = formatter.getChronology();
    ZoneId overrideZone = formatter.getZone();
    if (overrideChrono == null && overrideZone == null) {
        return temporal;
    }
    // ensure minimal change (early return is an optimization)
    Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
    ZoneId temporalZone = temporal.query(TemporalQueries.zoneId());
    if (Objects.equals(overrideChrono, temporalChrono)) {
        overrideChrono = null;
    }
    if (Objects.equals(overrideZone, temporalZone)) {
        overrideZone = null;
    }
    if (overrideChrono == null && overrideZone == null) {
        return temporal;
    }
    // make adjustment
    final Chronology effectiveChrono = (overrideChrono != null ? overrideChrono : temporalChrono);
    if (overrideZone != null) {
        // if have zone and instant, calculation is simple, defaulting chrono if necessary
        if (temporal.isSupported(INSTANT_SECONDS)) {
            Chronology chrono = Objects.requireNonNullElse(effectiveChrono, IsoChronology.INSTANCE);
            return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
        }
        // block changing zone on OffsetTime, and similar problem cases
        if (overrideZone.normalized() instanceof ZoneOffset && temporal.isSupported(OFFSET_SECONDS) && temporal.get(OFFSET_SECONDS) != overrideZone.getRules().getOffset(Instant.EPOCH).getTotalSeconds()) {
            throw new DateTimeException("Unable to apply override zone '" + overrideZone + "' because the temporal object being formatted has a different offset but" + " does not represent an instant: " + temporal);
        }
    }
    final ZoneId effectiveZone = (overrideZone != null ? overrideZone : temporalZone);
    final ChronoLocalDate effectiveDate;
    if (overrideChrono != null) {
        if (temporal.isSupported(EPOCH_DAY)) {
            effectiveDate = effectiveChrono.date(temporal);
        } else {
            // check for date fields other than epoch-day, ignoring case of converting null to ISO
            if (!(overrideChrono == IsoChronology.INSTANCE && temporalChrono == null)) {
                for (ChronoField f : ChronoField.values()) {
                    if (f.isDateBased() && temporal.isSupported(f)) {
                        throw new DateTimeException("Unable to apply override chronology '" + overrideChrono + "' because the temporal object being formatted contains date fields but" + " does not represent a whole date: " + temporal);
                    }
                }
            }
            effectiveDate = null;
        }
    } else {
        effectiveDate = null;
    }
    // this better handles map-like underlying temporal instances
    return new TemporalAccessor() {

        @Override
        public boolean isSupported(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.isSupported(field);
            }
            return temporal.isSupported(field);
        }

        @Override
        public ValueRange range(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.range(field);
            }
            return temporal.range(field);
        }

        @Override
        public long getLong(TemporalField field) {
            if (effectiveDate != null && field.isDateBased()) {
                return effectiveDate.getLong(field);
            }
            return temporal.getLong(field);
        }

        @SuppressWarnings("unchecked")
        @Override
        public <R> R query(TemporalQuery<R> query) {
            if (query == TemporalQueries.chronology()) {
                return (R) effectiveChrono;
            }
            if (query == TemporalQueries.zoneId()) {
                return (R) effectiveZone;
            }
            if (query == TemporalQueries.precision()) {
                return temporal.query(query);
            }
            return query.queryFrom(this);
        }

        @Override
        public String toString() {
            return temporal + (effectiveChrono != null ? " with chronology " + effectiveChrono : "") + (effectiveZone != null ? " with zone " + effectiveZone : "");
        }
    };
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeException(java.time.DateTimeException) TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) ZoneId(java.time.ZoneId) ChronoField(java.time.temporal.ChronoField) TemporalQuery(java.time.temporal.TemporalQuery) Chronology(java.time.chrono.Chronology) IsoChronology(java.time.chrono.IsoChronology) ZoneOffset(java.time.ZoneOffset)

Example 83 with ZoneId

use of java.time.ZoneId in project light-4j by networknt.

the class ConfigDefaultTest method testObjectMapperZonedDateTime.

public void testObjectMapperZonedDateTime() throws Exception {
    ObjectMapper mapper = Config.getInstance().getMapper();
    ZonedDateTimeModel dm = mapper.readValue("{\"time\" : \"2014-07-02T04:00:00.000000Z\"}", ZonedDateTimeModel.class);
    System.out.println(dm.getTime());
    ZoneId zoneId = ZoneId.of("UTC");
    ZonedDateTime zonedDateTime2 = ZonedDateTime.of(2014, 7, 2, 4, 0, 0, 0, zoneId);
    System.out.println(zonedDateTime2);
    Assert.assertTrue(zonedDateTime2.equals(dm.getTime()));
}
Also used : ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 84 with ZoneId

use of java.time.ZoneId in project jdbi by jdbi.

the class TestJavaTime method zoneId.

@Test
public void zoneId() {
    final ZoneId zone = ZoneId.systemDefault();
    h.execute("insert into stuff(z) values (?)", zone);
    assertThat(h.createQuery("select z from stuff").mapTo(ZoneId.class).findOnly()).isEqualTo(zone);
}
Also used : ZoneId(java.time.ZoneId) Test(org.junit.Test)

Example 85 with ZoneId

use of java.time.ZoneId in project presto-audit by yahoojapan.

the class TestHelper method setUp.

public void setUp() {
    try {
        uri = new URI("http://example.com:8080/v1/query/20170521_140224_00002_gd5k3");
        statistics = new QueryStatistics(Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(300), Optional.of(Duration.ofMillis(400)), Optional.of(Duration.ofMillis(500)), 10001, 10002, 10003, 14, 0, 0, 0, 2048.0, 4096, true, new ArrayList<StageCpuDistribution>() {
        }, new ArrayList<String>() {

            {
                add("operatorSummaries 01");
            }
        });
        context = new QueryContext("test-user", Optional.of("principal"), Optional.of("example.com"), Optional.of("StatementClient 0.167"), Optional.of("clientInfo"), new HashSet<>(), Optional.of("presto-cli"), Optional.of("catalog"), Optional.of("schema"), Optional.of(""), new HashMap<>(), "127.0.0.1", "0.175", "environment");
        ioMetadata = new QueryIOMetadata(new ArrayList<QueryInputMetadata>(), Optional.empty());
        ZoneId jst_zone = ZoneId.of("Asia/Tokyo");
        createTime = ZonedDateTime.of(2017, 6 + 1, 15, 10, 0, 0, 0, jst_zone).toInstant();
        executionStartTime = ZonedDateTime.of(2017, 6 + 1, 15, 10, 0, 1, 0, jst_zone).toInstant();
        endTime = ZonedDateTime.of(2017, 6 + 1, 15, 10, 0, 3, 0, jst_zone).toInstant();
    } catch (Exception ignoreException) {
    }
}
Also used : ZoneId(java.time.ZoneId) QueryStatistics(com.facebook.presto.spi.eventlistener.QueryStatistics) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryContext(com.facebook.presto.spi.eventlistener.QueryContext) QueryIOMetadata(com.facebook.presto.spi.eventlistener.QueryIOMetadata) URI(java.net.URI) HashSet(java.util.HashSet)

Aggregations

ZoneId (java.time.ZoneId)420 Test (org.junit.Test)137 ZonedDateTime (java.time.ZonedDateTime)108 LocalDateTime (java.time.LocalDateTime)69 LocalDate (java.time.LocalDate)55 Instant (java.time.Instant)49 Test (org.testng.annotations.Test)41 ZoneOffset (java.time.ZoneOffset)37 DateTimeFormatter (java.time.format.DateTimeFormatter)32 Date (java.util.Date)29 ArrayList (java.util.ArrayList)23 LocalTime (java.time.LocalTime)21 TemporalAccessor (java.time.temporal.TemporalAccessor)15 List (java.util.List)15 HashMap (java.util.HashMap)14 Clock (java.time.Clock)13 Collectors (java.util.stream.Collectors)13 DateTimeException (java.time.DateTimeException)12 Duration (java.time.Duration)12 OffsetDateTime (java.time.OffsetDateTime)11