/** * {@inheritdoc} */ public function expand($className, $value, array $options = []) { try { switch ($className) { case Duration::class: return Duration::parse($value); case LocalDate::class: return LocalDate::parse($value); case LocalTime::class: return LocalTime::parse($value); case LocalDateTime::class: return LocalDateTime::parse($value); case TimeZoneOffset::class: return TimeZoneOffset::parse($value); case TimeZoneRegion::class: return TimeZoneRegion::parse($value); case YearMonth::class: return YearMonth::parse($value); case ZonedDateTime::class: return ZonedDateTime::parse($value); } } catch (DateTimeParseException $e) { throw new ObjectNotConvertibleException($e->getMessage(), $e->getCode(), $e); } return null; }
/** * @param Duration $duration * * @return Instant */ public function minus(Duration $duration) { if ($duration->isZero()) { return $this; } return $this->plus($duration->negated()); }
/** * Returns a copy of this LocalDateTime with the specific Duration subtracted. * * @param Duration $duration * * @return LocalDateTime */ public function minusDuration(Duration $duration) { return $this->plusDuration($duration->negated()); }
/** * @dataProvider providerToString * * @param integer $seconds * @param integer $nanos * @param string $expected */ public function testToString($seconds, $nanos, $expected) { $this->assertSame($expected, (string) Duration::ofSeconds($seconds, $nanos)); }
/** * @dataProvider providerPlus * * @param integer $second The base second. * @param integer $nano The base nano-of-second. * @param integer $plusSeconds The seconds of the duration to add. * @param integer $plusNanos The nanos of the duration to add. * @param integer $expectedSecond The expected second of the result. * @param integer $expectedNano The expected nano of the result. */ public function testMinus($second, $nano, $plusSeconds, $plusNanos, $expectedSecond, $expectedNano) { $result = Instant::of($second, $nano)->minus(Duration::ofSeconds(-$plusSeconds, -$plusNanos)); $this->assertReadableInstantIs($expectedSecond, $expectedNano, $result); }
/** * @dataProvider providerOffsetClock * * @param integer $second The epoch second to set the base clock to. * @param integer $nano The nano to set the base clock to. * @param string $duration A parsable duration string. * @param integer $expectedSecond The expected epoch second returned by the clock. * @param integer $expectedNano The expected nano returned by the clock. */ public function testOffsetClock($second, $nano, $duration, $expectedSecond, $expectedNano) { $baseClock = new FixedClock(Instant::of($second, $nano)); $clock = new OffsetClock($baseClock, Duration::parse($duration)); $this->assertReadableInstantIs($expectedSecond, $expectedNano, $clock->getTime()); }
/** * @dataProvider providerDuration * * @param integer $h The base hour. * @param integer $m The base minute. * @param integer $s The base second. * @param integer $n The base nano. * @param integer $ds The number of seconds in the duration. * @param integer $dn The nano adjustment of the duration. * @param integer $eh The expected hour of the result time. * @param integer $em The expected minute of the result time. * @param integer $es The expected second of the result time. * @param integer $en The expected nano of the result time. */ public function testMinusDuration($h, $m, $s, $n, $ds, $dn, $eh, $em, $es, $en) { $localTime = LocalTime::of($h, $m, $s, $n); $duration = Duration::ofSeconds(-$ds, -$dn); $this->assertLocalTimeIs($eh, $em, $es, $en, $localTime->MinusDuration($duration)); }
/** * @dataProvider providerDuration * * @param integer $ds The seconds of the duration. * @param integer $dn The nano adjustment of the duration. * @param integer $y The expected year. * @param integer $m The expected month. * @param integer $d The expected day. * @param integer $h The exepected hour. * @param integer $i The expected minute. * @param integer $s The expected second. * @param integer $n The expected nano. */ public function testMinusDuration($ds, $dn, $y, $m, $d, $h, $i, $s, $n) { $localDateTime = LocalDate::of(2001, 2, 3)->atTime(LocalTime::of(4, 5, 6, 123456789)); $duration = Duration::ofSeconds(-$ds, -$dn); $this->assertLocalDateTimeIs($y, $m, $d, $h, $i, $s, $n, $localDateTime->minusDuration($duration)); }
/** * @param integer $seconds The expected seconds. * @param integer $nanos The expected nanos. * @param Duration $duration The duration to test. */ protected function assertDurationIs($seconds, $nanos, Duration $duration) { $this->compare([$seconds, $nanos], [$duration->getSeconds(), $duration->getNanos()]); }