public function equals($obj) { if ($obj instanceof OffsetClock) { return $this->baseClock->equals($obj->baseClock) && $this->offset->equals($obj->offset); } return false; }
public function equals($obj) { if ($obj instanceof TickClock) { return $this->baseClock->equals($obj->baseClock) && $this->tickNanos == $obj->tickNanos; } else { return false; } }
public function test_now_Clock() { $instant = OffsetDateTime::ofDateAndTime(LocalDate::of(2010, 12, 31), LocalTime::of(0, 0), ZoneOffset::UTC())->toInstant(); $clock = Clock::fixed($instant, ZoneOffset::UTC()); $test = Year::nowof($clock); $this->assertEquals($test->getValue(), 2010); }
public function test__equals() { $a = Clock::tick(Clock::system(self::PARIS()), Duration::ofMillis(500)); $b = Clock::tick(Clock::system(self::PARIS()), Duration::ofMillis(500)); $this->assertEquals($a->equals($a), true); $this->assertEquals($a->equals($b), true); $this->assertEquals($b->equals($a), true); $this->assertEquals($b->equals($b), true); $c = Clock::tick(Clock::system(self::MOSCOW()), Duration::ofMillis(500)); $this->assertEquals($a->equals($c), false); $d = Clock::tick(Clock::system(self::PARIS()), Duration::ofMillis(499)); $this->assertEquals($a->equals($d), false); $this->assertEquals($a->equals(null), false); $this->assertEquals($a->equals("other type"), false); $this->assertEquals($a->equals(Clock::systemUTC()), false); }
/** * Obtains the current time from the specified clock. * <p> * This will query the specified clock to obtain the current time. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param Clock $clock the clock to use, not null * @return LocalTime the current time, not null */ public static function nowOf(Clock $clock) { // inline OffsetTime factory to avoid creating object and InstantProvider checks $now = $clock->instant(); // called once $offset = $clock->getZone()->getRules()->getOffset($now); $localSecond = $now->getEpochSecond() + $offset->getTotalSeconds(); // overflow caught later $secsOfDay = (int) Math::floorMod($localSecond, self::SECONDS_PER_DAY); return self::ofNanoOfDay($secsOfDay * self::NANOS_PER_SECOND + $now->getNano()); }
/** * Obtains the current date from the specified clock. * <p> * This will query the specified clock to obtain the current date - today. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param Clock $clock the clock to use, not null * @return LocalDate the current date, not null */ public static function nowOf(Clock $clock) { // inline to avoid creating object and Instant checks $now = $clock->instant(); // called once $offset = $clock->getZone()->getRules()->getOffset($now); $epochSec = $now->getEpochSecond() + $offset->getTotalSeconds(); // overflow caught later $epochDay = Math::floorDiv($epochSec, LocalTime::SECONDS_PER_DAY); return LocalDate::ofEpochDay($epochDay); }
public function test_now_Clock_offsets() { $base = ZonedDateTime::ofDateTime(LocalDateTime::of(1970, 1, 1, 12, 0), ZoneOffset::UTC()); for ($i = -9; $i < 15; $i++) { $offset = ZoneOffset::ofHours($i); $clock = Clock::fixed($base->toInstant(), $offset); $test = ZonedDateTime::nowOf($clock); $this->assertEquals($test->getHour(), (12 + $i) % 24); $this->assertEquals($test->getMinute(), 0); $this->assertEquals($test->getSecond(), 0); $this->assertEquals($test->getNano(), 0); $this->assertEquals($test->getOffset(), $offset); $this->assertEquals($test->getZone(), $offset); } }
/** * Obtains the current instant from the specified clock. * <p> * This will query the specified clock to obtain the current time. * <p> * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param Clock $clock the clock to use, not null * @return Instant the current instant, not null */ public static function nowOf(Clock $clock) { return $clock->instant(); }
/** * @expectedException \Celest\DateTimeException */ public function test_now_Clock_tooLow() { $clock = Clock::fixed($this->MIN_INSTANT->minusNanos(1), ZoneOffset::UTC()); LocalDateTime::nowOf($clock); }
public function test_now_Clock_min() { $clock = Clock::fixed(Instant::MIN(), ZoneOffset::UTC()); $test = LocalTime::nowOf($clock); $this->assertEquals($test->getHour(), 0); $this->assertEquals($test->getMinute(), 0); $this->assertEquals($test->getSecond(), 0); $this->assertEquals($test->getNano(), 0); }
/** * Obtains the current {@code ThaiBuddhistDate} from the system clock in the specified time-zone. * <p> * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date. * Specifying the time-zone avoids dependence on the default time-zone. * <p> * Using this method will prevent the ability to use an alternate clock for testing * because the clock is hard-coded. * * @param ZoneId $zone the zone ID to use, not null * @return ThaiBuddhistDate the current date using the system clock, not null */ public static function nowFrom(ZoneId $zone) { return self::nowOf(Clock::system($zone)); }
public function test_now_Clock() { $instant = LocalDateTime::of(2010, 12, 31, 0, 0)->toInstant(ZoneOffset::UTC()); $clock = Clock::fixed($instant, ZoneOffset::UTC()); $test = YearMonth::nowOf($clock); $this->assertEquals($test->getYear(), 2010); $this->assertEquals($test->getMonth(), Month::DECEMBER()); }
/** * @expectedException \Celest\DateTimeException */ public function test_now_Clock_tooLow() { $clock = Clock::fixed(self::MIN_INSTANT()->minusNanos(1), ZoneOffset::UTC()); LocalDate::nowOf($clock); }
/** * Obtains the current date-time from the specified clock. * <p> * This will query the specified clock to obtain the current date-time. * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param Clock $clock the clock to use, not null * @return LocalDateTime the current date-time, not null */ public static function nowOf(Clock $clock) { $now = $clock->instant(); // called once $offset = $clock->getZone()->getRules()->getOffset($now); return self::ofEpochSecond($now->getEpochSecond(), $now->getNano(), $offset); }
public function dateNowIn(ZoneId $zone) { return $this->dateNowOf(Clock::system($zone)); }
/** * Obtains the current date-time from the specified clock. * <p> * This will query the specified clock to obtain the current date-time. * The offset will be calculated from the time-zone in the clock. * <p> * Using this method allows the use of an alternate clock for testing. * The alternate clock may be introduced using {@link Clock dependency injection}. * * @param Clock $clock the clock to use, not null * @return OffsetDateTime the current date-time, not null */ public static function nowOf(Clock $clock) { $now = $clock->instant(); // called once return self::ofInstant($now, $clock->getZone()->getRules()->getOffset($now)); }
/** * @group long */ public function test_now_Clock_allSecsInDay_beforeEpoch() { for ($i = -1; $i >= -(24 * 60 * 60); $i--) { $expected = Instant::ofEpochSecond($i)->plusNanos(123456789); $clock = Clock::fixed($expected, ZoneOffset::UTC()); $test = Instant::nowOf($clock); $this->assertEquals($test, $expected); } }