Skip to content

Commit b7860cd

Browse files
committed
Implement More Appropriate Date/Time Exceptions RFC
1 parent 0cfc45b commit b7860cd

File tree

66 files changed

+1489
-261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1489
-261
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ PHP NEWS
2424
(ilutov)
2525
. Fix bug GH-10083 (Allow comments between & and parameter). (ilutov)
2626

27+
- Date:
28+
. Implement More Appropriate Date/Time Exceptions RFC. (Derick)
29+
2730
- Exif:
2831
. Removed unneeded codepaths in exif_process_TIFF_in_JPEG(). (nielsdos)
2932

ext/date/php_date.c

Lines changed: 213 additions & 79 deletions
Large diffs are not rendered by default.

ext/date/php_date.stub.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ public function format(string $format): string {}
377377

378378
/**
379379
* @tentative-return-type
380-
* @alias date_modify
381380
*/
382381
public function modify(string $modifier): DateTime|false {}
383382

@@ -389,7 +388,6 @@ public function add(DateInterval $interval): DateTime {}
389388

390389
/**
391390
* @tentative-return-type
392-
* @alias date_sub
393391
*/
394392
public function sub(DateInterval $interval): DateTime {}
395393

@@ -668,7 +666,6 @@ public function __construct(string $duration) {}
668666

669667
/**
670668
* @tentative-return-type
671-
* @alias date_interval_create_from_date_string
672669
*/
673670
public static function createFromDateString(string $datetime): DateInterval|false {}
674671

@@ -749,3 +746,66 @@ public static function __set_state(array $array): DatePeriod {}
749746

750747
public function getIterator(): Iterator {}
751748
}
749+
750+
/**
751+
* @strict-properties
752+
*/
753+
class DateError extends Error
754+
{
755+
}
756+
757+
/**
758+
* @strict-properties
759+
*/
760+
class DateObjectError extends DateError
761+
{
762+
}
763+
764+
/**
765+
* @strict-properties
766+
*/
767+
class DateRangeError extends DateRangeError
768+
{
769+
}
770+
771+
/**
772+
* @strict-properties
773+
*/
774+
class DateException extends Exception
775+
{
776+
}
777+
778+
/**
779+
* @strict-properties
780+
*/
781+
class DateInvalidTimeZoneException extends Exception
782+
{
783+
}
784+
785+
/**
786+
* @strict-properties
787+
*/
788+
class DateInvalidOperationException extends DateException
789+
{
790+
}
791+
792+
/**
793+
* @strict-properties
794+
*/
795+
class DateMalformedStringException extends DateException
796+
{
797+
}
798+
799+
/**
800+
* @strict-properties
801+
*/
802+
class DateMalformedIntervalStringException extends DateException
803+
{
804+
}
805+
806+
/**
807+
* @strict-properties
808+
*/
809+
class DateMalformedPeriodStringException extends DateException
810+
{
811+
}

ext/date/php_date_arginfo.h

Lines changed: 151 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/date/tests/68062.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ echo $tz->getOffset($dt), "\n";
1010
try {
1111
echo $tz->getOffset(1);
1212
} catch (TypeError $e) {
13-
echo $e->getMessage(), "\n";
13+
echo $e::class, ': ', $e->getMessage(), "\n";
1414
}
1515
?>
1616
--EXPECT--
1717
3600
18-
DateTimeZone::getOffset(): Argument #1 ($datetime) must be of type DateTimeInterface, int given
18+
TypeError: DateTimeZone::getOffset(): Argument #1 ($datetime) must be of type DateTimeInterface, int given
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
DateInterval constructor exceptions
3+
--INI--
4+
date.timezone=Europe/London
5+
--FILE--
6+
<?php
7+
function check(callable $c)
8+
{
9+
try {
10+
var_dump($c());
11+
} catch (\DateMalformedIntervalStringException $e) {
12+
echo $e::class, ': ', $e->getMessage(), "\n";
13+
}
14+
}
15+
16+
check(fn() => new DateInterval(""));
17+
check(fn() => new DateInterval("2007-05-11T15:30:00Z/"));
18+
?>
19+
--EXPECTF--
20+
DateMalformedIntervalStringException: Unknown or bad format ()
21+
DateMalformedIntervalStringException: Failed to parse interval (2007-05-11T15:30:00Z/)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test DateInterval::createFromDateString() function : nonsense data
3+
--FILE--
4+
<?php
5+
try {
6+
$i = DateInterval::createFromDateString("foobar");
7+
} catch (DateMalformedIntervalStringException $e) {
8+
echo $e::class, ': ', $e->getMessage(), "\n";
9+
}
10+
?>
11+
--EXPECTF--
12+
DateMalformedIntervalStringException: Unknown or bad format (foobar) at position 0 (f): The timezone could not be found in the database

ext/date/tests/DateInterval_serialize-003.phpt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ var_dump($d);
3232
echo "\n\nUsed serialised interval:\n";
3333
$now = new DateTimeImmutable("2022-04-22 16:25:11 BST");
3434
var_dump($now->add($e));
35-
var_dump($now->sub($e));
35+
try {
36+
var_dump($now->sub($e));
37+
} catch (DateInvalidOperationException $e) {
38+
echo $e::class, ': ', $e->getMessage(), "\n";
39+
}
3640
?>
3741
--EXPECTF--
3842
Original object:
@@ -84,13 +88,4 @@ object(DateTimeImmutable)#4 (3) {
8488
["timezone"]=>
8589
string(3) "BST"
8690
}
87-
88-
Warning: DateTimeImmutable::sub(): Only non-special relative time specifications are supported for subtraction in %s on line %d
89-
object(DateTimeImmutable)#4 (3) {
90-
["date"]=>
91-
string(26) "2022-04-22 16:25:11.000000"
92-
["timezone_type"]=>
93-
int(2)
94-
["timezone"]=>
95-
string(3) "BST"
96-
}
91+
DateInvalidOperationException: DateTimeImmutable::sub(): Only non-special relative time specifications are supported for subtraction
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
DateInterval invalid serialization data with date_string
3+
--FILE--
4+
<?php
5+
$propertySets = [
6+
'2023-01-16 17:01:19',
7+
'2023-01-16-foobar$*',
8+
];
9+
10+
foreach( $propertySets as $propertySet )
11+
{
12+
try {
13+
$d = DateInterval::__set_state( [ 'date_string' => $propertySet ] );
14+
echo "OK\n";
15+
} catch (\Error $e) {
16+
echo $e::class, ': ', $e->getMessage(), "\n";
17+
}
18+
}
19+
?>
20+
--EXPECT--
21+
OK
22+
Error: Unknown or bad format (2023-01-16-foobar$*) at position 10 (-) while unserializing: Unexpected character

0 commit comments

Comments
 (0)