Search in sources :

Example 46 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKOffsetDateTime method now_Clock_allSecsInDay_offset.

@Test
public void now_Clock_allSecsInDay_offset() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE);
        OffsetDateTime test = OffsetDateTime.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2);
        assertEquals(test.getHour(), (i / (60 * 60)) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
        assertEquals(test.getNano(), 123456789);
        assertEquals(test.getOffset(), OFFSET_PONE);
    }
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Instant(java.time.Instant) Clock(java.time.Clock) Test(org.testng.annotations.Test)

Example 47 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKOffsetDateTime method now_Clock_allSecsInDay_beforeEpoch.

@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
    LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L);
    for (int i = -1; i >= -(24 * 60 * 60); i--) {
        Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        OffsetDateTime test = OffsetDateTime.now(clock);
        assertEquals(test.getYear(), 1969);
        assertEquals(test.getMonth(), Month.DECEMBER);
        assertEquals(test.getDayOfMonth(), 31);
        expected = expected.minusSeconds(1);
        assertEquals(test.toLocalTime(), expected);
        assertEquals(test.getOffset(), ZoneOffset.UTC);
    }
}
Also used : LocalTime(java.time.LocalTime) OffsetDateTime(java.time.OffsetDateTime) Instant(java.time.Instant) Clock(java.time.Clock) Test(org.testng.annotations.Test)

Example 48 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKOffsetDateTime method now_Clock_allSecsInDay_utc.

//-----------------------------------------------------------------------
// now(Clock)
//-----------------------------------------------------------------------
@Test
public void now_Clock_allSecsInDay_utc() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        OffsetDateTime test = OffsetDateTime.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
        assertEquals(test.getHour(), (i / (60 * 60)) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
        assertEquals(test.getNano(), 123456789);
        assertEquals(test.getOffset(), ZoneOffset.UTC);
    }
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Instant(java.time.Instant) Clock(java.time.Clock) Test(org.testng.annotations.Test)

Example 49 with Clock

use of java.time.Clock in project jdk8u_jdk by JetBrains.

the class TCKOffsetDateTime method now_Clock_offsets.

@Test
public void now_Clock_offsets() {
    OffsetDateTime base = OffsetDateTime.of(1970, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC);
    for (int i = -9; i < 15; i++) {
        ZoneOffset offset = ZoneOffset.ofHours(i);
        Clock clock = Clock.fixed(base.toInstant(), offset);
        OffsetDateTime test = OffsetDateTime.now(clock);
        assertEquals(test.getHour(), (12 + i) % 24);
        assertEquals(test.getMinute(), 0);
        assertEquals(test.getSecond(), 0);
        assertEquals(test.getNano(), 0);
        assertEquals(test.getOffset(), offset);
    }
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Clock(java.time.Clock) ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Example 50 with Clock

use of java.time.Clock in project sonar-java by SonarSource.

the class A method foo.

void foo() {
    HijrahDate hijrahDate = HijrahDate.now();
    // Noncompliant {{Synchronize on a non-value-based object; synchronizing on a "HijrahDate" could lead to contention. (sonar.java.source not set. Assuming 8 or greater.)}}
    synchronized (hijrahDate) {
    }
    JapaneseDate japaneseDate = JapaneseDate.now();
    // Noncompliant
    synchronized (japaneseDate) {
    }
    MinguoDate minguoDate = MinguoDate.now();
    // Noncompliant
    synchronized (minguoDate) {
    }
    ThaiBuddhistDate thaiBuddhistDate = ThaiBuddhistDate.now();
    // Noncompliant
    synchronized (thaiBuddhistDate) {
    }
    LocalDateTime localDateTime = LocalDateTime.now();
    // Noncompliant
    synchronized (localDateTime) {
    }
    Optional<Object> optionalEmpty = Optional.empty();
    // Noncompliant {{Synchronize on a non-value-based object; synchronizing on a "Optional" could lead to contention. (sonar.java.source not set. Assuming 8 or greater.)}}
    synchronized (optionalEmpty) {
    }
    OptionalDouble optionalDouble = OptionalDouble.empty();
    // Noncompliant
    synchronized (optionalDouble) {
    }
    OptionalLong optionalLong = OptionalLong.empty();
    // Noncompliant
    synchronized (optionalLong) {
    }
    OptionalInt optionalInt = OptionalInt.empty();
    // Noncompliant
    synchronized (optionalInt) {
    }
    Clock clock = Clock.systemUTC();
    // Compliant
    synchronized (clock) {
    }
    Object object = new Object();
    // Compliant
    synchronized (object) {
    }
    DateTimeFormatterBuilder dateTimeFormatter = new DateTimeFormatterBuilder();
    // Compliant - not part of 'java.time' package but subpackage 'java.time.format'
    synchronized (dateTimeFormatter) {
    }
    // Compliant
    synchronized (unknownObject) {
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) HijrahDate(java.time.chrono.HijrahDate) MinguoDate(java.time.chrono.MinguoDate) ThaiBuddhistDate(java.time.chrono.ThaiBuddhistDate) OptionalLong(java.util.OptionalLong) JapaneseDate(java.time.chrono.JapaneseDate) OptionalInt(java.util.OptionalInt) Clock(java.time.Clock) OptionalDouble(java.util.OptionalDouble) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder)

Aggregations

Clock (java.time.Clock)209 Test (org.junit.Test)62 Instant (java.time.Instant)51 Test (org.testng.annotations.Test)43 LocalTime (java.time.LocalTime)15 ZonedDateTime (java.time.ZonedDateTime)12 LocalDateTime (java.time.LocalDateTime)11 ZoneId (java.time.ZoneId)11 LocalDate (java.time.LocalDate)10 OffsetDateTime (java.time.OffsetDateTime)8 Test (org.junit.jupiter.api.Test)7 HandlerTest (com.yahoo.vespa.config.server.http.HandlerTest)6 SessionHandlerTest (com.yahoo.vespa.config.server.http.SessionHandlerTest)6 IOException (java.io.IOException)6 SessionTest (com.yahoo.vespa.config.server.session.SessionTest)5 OffsetTime (java.time.OffsetTime)5 ZoneOffset (java.time.ZoneOffset)5 Chronology (java.time.chrono.Chronology)5 IsoChronology (java.time.chrono.IsoChronology)5 Date (java.util.Date)4