public function testAddWeekdaysPositive() { $dt = Date::create(2012, 1, 4, 13, 2, 1)->addWeekdays(9); $this->assertSame(17, $dt->getDay()); // test for phpbug id 54909 $this->assertSame(13, $dt->getHour()); $this->assertSame(2, $dt->getMinute()); $this->assertSame(1, $dt->getSecond()); }
private function createAndPersistTestEntity() { Date::setTestNow(Date::create(2020, 01, 01, 12, 30, 20)); $entity = new TestEntity(Date::create(1980, 04, 14, 13, 37)); Date::setTestNow(); $em = $this->doctrine->getManager(); $em->persist($entity); $em->flush(); $em->clear(); }
public function testSecondsUntilEndOfDay() { $d = Date::today()->endOfDay(); $this->assertSame(0, $d->secondsUntilEndOfDay()); $d = Date::today()->endOfDay()->subSeconds(60); $this->assertSame(60, $d->secondsUntilEndOfDay()); $d = Date::create(2014, 10, 24, 12, 34, 56); $this->assertSame(41103, $d->secondsUntilEndOfDay()); $d = Date::create(2014, 10, 24, 0, 0, 0); $this->assertSame(86399, $d->secondsUntilEndOfDay()); }
public function testCreateWithTimeZoneString() { $d = Date::create(2012, 1, 1, 0, 0, 0, 'Europe/London'); $this->assertDate($d, 2012, 1, 1, 0, 0, 0); $this->assertSame('Europe/London', $d->getTimezoneName()); }
public function testTimestampGetter() { $d = Date::create()->setTimezone('GMT')->setDateTime(1970, 1, 1, 0, 0, 0)->getTimestamp(); $this->assertSame(0, $d); }
public function testEndOfWeek() { $d = Date::create(1980, 8, 7, 11, 12, 13)->endOfWeek(); $this->assertDate($d, 1980, 8, 10, 23, 59, 59); }
public function testFarthestWithEquals() { $instance = Date::create(2015, 5, 28, 12, 0, 0); $dt1 = Date::create(2015, 5, 28, 12, 0, 0); $dt2 = Date::create(2015, 5, 28, 14, 0, 0); $Farthest = $instance->farthest($dt1, $dt2); $this->assertEquals($dt2, $Farthest); }
public function testCreateReturnsDatingInstance() { $d = Date::createFromTimestamp(Date::create(1975, 5, 21, 22, 32, 5)->getTimestamp()); $this->assertDate($d, 1975, 5, 21, 22, 32, 5); }
public function testDiffInHoursVsDefaultNow() { $scope = $this; $this->wrapWithTestNow(function () use($scope) { $scope->assertSame(48, Date::now()->subDays(2)->diffInHours()); }, Date::create(2012, 1, 15)); }
/** * Transforms a localized date into a normalized date. * * @param array $value Localized date * * @return Date Normalized date * * @throws TransformationFailedException If the given value is not an array, * if the value could not be transformed * or if the input timezone is not * supported. */ public function reverseTransform($value) { if (null === $value) { return; } if (!is_array($value)) { throw new TransformationFailedException('Expected an array.'); } if ('' === implode('', $value)) { return; } $emptyFields = []; foreach ($this->fields as $field) { if (!isset($value[$field])) { $emptyFields[] = $field; } } if (count($emptyFields) > 0) { throw new TransformationFailedException(sprintf('The fields "%s" should not be empty', implode('", "', $emptyFields))); } if (isset($value['month']) && !ctype_digit((string) $value['month'])) { throw new TransformationFailedException('This month is invalid'); } if (isset($value['day']) && !ctype_digit((string) $value['day'])) { throw new TransformationFailedException('This day is invalid'); } if (isset($value['year']) && !ctype_digit((string) $value['year'])) { throw new TransformationFailedException('This year is invalid'); } if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month'], $value['day'], $value['year'])) { throw new TransformationFailedException('This is an invalid date'); } if (isset($value['hour']) && !ctype_digit((string) $value['hour'])) { throw new TransformationFailedException('This hour is invalid'); } if (isset($value['minute']) && !ctype_digit((string) $value['minute'])) { throw new TransformationFailedException('This minute is invalid'); } if (isset($value['second']) && !ctype_digit((string) $value['second'])) { throw new TransformationFailedException('This second is invalid'); } try { $date = Date::create(empty($value['year']) ? 1970 : (int) $value['year'], empty($value['month']) ? 1 : (int) $value['month'], empty($value['day']) ? 1 : (int) $value['day'], empty($value['hour']) ? 0 : (int) $value['hour'], empty($value['minute']) ? 0 : (int) $value['minute'], empty($value['second']) ? 0 : (int) $value['second'], new \DateTimeZone($this->outputTimezone)); if ($this->inputTimezone !== $this->outputTimezone) { $date->setTimezone(new \DateTimeZone($this->inputTimezone)); } } catch (\Exception $e) { throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); } return $date; }
public function testToW3cString() { $d = Date::create(1975, 12, 25, 14, 15, 16); $this->assertSame('1975-12-25T14:15:16-05:00', $d->toW3cString()); }
public function testCreateFromDateWithDefaults() { $d = Date::createFromDate(); $this->assertSame($d->getTimestamp(), Date::create(null, null, null, null, null, null)->getTimestamp()); }