diff --git a/generator/tests/DateTimeImmutableTest.php b/generator/tests/DateTimeImmutableTest.php new file mode 100644 index 00000000..fbe38b49 --- /dev/null +++ b/generator/tests/DateTimeImmutableTest.php @@ -0,0 +1,30 @@ +expectException(DatetimeException::class); + $datetime = DateTimeImmutable::createFromFormat('lol', 'super'); + } + + public function testSafeDatetimeImmutablePreserveTimeAndTimezone(): void + { + $timezone = new \DateTimeZone('Pacific/Chatham'); + $datetime = DateTimeImmutable::createFromFormat('d-m-Y', '20-03-2006', $timezone); + $this->assertEquals('20-03-2006', $datetime->format('d-m-Y')); + $this->assertEquals($timezone, $datetime->getTimezone()); + } +} \ No newline at end of file diff --git a/generator/tests/DateTimeTest.php b/generator/tests/DateTimeTest.php new file mode 100644 index 00000000..113db921 --- /dev/null +++ b/generator/tests/DateTimeTest.php @@ -0,0 +1,30 @@ +expectException(DatetimeException::class); + $datetime = DateTime::createFromFormat('lol', 'super'); + } + + public function testSafeDatetimePreserveTimeAndTimezone(): void + { + $timezone = new \DateTimeZone('Pacific/Chatham'); + $datetime = DateTime::createFromFormat('d-m-Y', '20-03-2006', $timezone); + $this->assertEquals('20-03-2006', $datetime->format('d-m-Y')); + $this->assertEquals($timezone, $datetime->getTimezone()); + } +} \ No newline at end of file diff --git a/generator/tests/SpecialCasesTest.php b/generator/tests/SpecialCasesTest.php index 35474214..707e1158 100644 --- a/generator/tests/SpecialCasesTest.php +++ b/generator/tests/SpecialCasesTest.php @@ -7,6 +7,7 @@ class SpecialCasesTest extends TestCase { + public function testPregReplace() { require_once __DIR__.'/../../lib/special_cases.php'; diff --git a/lib/DateTime.php b/lib/DateTime.php new file mode 100644 index 00000000..51449028 --- /dev/null +++ b/lib/DateTime.php @@ -0,0 +1,75 @@ +setTimestamp($datetime->getTimestamp()); + $safeDatetime->setTimezone($datetime->getTimezone()); + return $safeDatetime; + } + + public static function createFromFormat($format, $time, DateTimeZone $timezone = null): self + { + $datetime = parent::createFromFormat($format, $time, $timezone); + if ($datetime === false) { + throw DatetimeException::createFromPhpError(); + } + return self::createFromRegular($datetime); + } + + /** + * @param DateTimeInterface $datetime2 The date to compare to. + * @param boolean $absolute [optional] Whether to return absolute difference. + * @return DateInterval The DateInterval object representing the difference between the two dates. + */ + public function diff($datetime2, $absolute = false): DateInterval + { + /** @var \DateInterval|false $result */ + $result = parent::diff($datetime2, $absolute); + if ($result === false) { + throw DatetimeException::createFromPhpError(); + } + return $result; + } + + /** + * @param string $modify A date/time string. Valid formats are explained in Date and Time Formats. + * @return DateTime Returns the DateTime object for method chaining. + */ + public function modify($modify): self + { + /** @var DateTime|false $result */ + $result = parent::modify($modify); + if ($result === false) { + throw DatetimeException::createFromPhpError(); + } + return $result; + } + + /** + * @param int $year + * @param int $month + * @param int $day + * @return DateTime + */ + public function setDate($year, $month, $day): self + { + /** @var DateTime|false $result */ + $result = parent::setDate($year, $month, $day); + if ($result === false) { + throw DatetimeException::createFromPhpError(); + } + return $result; + } +} diff --git a/lib/DateTimeImmutable.php b/lib/DateTimeImmutable.php new file mode 100644 index 00000000..32c3383f --- /dev/null +++ b/lib/DateTimeImmutable.php @@ -0,0 +1,78 @@ +setTimestamp($datetime->getTimestamp()); + $safeDatetime = $safeDatetime->setTimezone($datetime->getTimezone()); + return $safeDatetime; + } + + public static function createFromFormat($format, $time, DateTimeZone $timezone = null): self + { + $datetime = parent::createFromFormat($format, $time, $timezone); + if ($datetime === false) { + throw DatetimeException::createFromPhpError(); + } + return self::createFromRegular($datetime); + } + + /** + * @param DateTimeInterface $datetime2
The date to compare to.
+ * @param bool $absolute [optional]Should the interval be forced to be positive?
+ * @return DateInterval + */ + public function diff($datetime2, $absolute = false): DateInterval + { + /** @var \DateInterval|false $result */ + $result = parent::diff($datetime2, $absolute); + if ($result === false) { + throw DatetimeException::createFromPhpError(); + } + return $result; + } + + /** + * @param string $modifyA date/time string. Valid formats are explained in + * {@link https://secure.php.net/manual/en/datetime.formats.php Date and Time Formats}.
+ * @return DateTimeImmutable + */ + public function modify($modify): self + { + /** @var \DateTimeImmutable|false $result */ + $result = parent::modify($modify); + if ($result === false) { + throw DatetimeException::createFromPhpError(); + } + return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable + } + + /** + * @param int $yearYear of the date.
+ * @param int $monthMonth of the date.
+ * @param int $dayDay of the date.
+ * @return DateTimeImmutable + * + */ + public function setDate($year, $month, $day): self + { + /** @var \DateTimeImmutable|false $result */ + $result = parent::setDate($year, $month, $day); + if ($result === false) { + throw DatetimeException::createFromPhpError(); + } + return self::createFromRegular($result); //we have to recreate a safe datetime because modify create a new instance of \DateTimeImmutable + } +}