Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
346 changes: 262 additions & 84 deletions ext/date/php_date.c

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions ext/date/php_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ PHPAPI zend_class_entry *php_date_get_immutable_ce(void);
PHPAPI zend_class_entry *php_date_get_interface_ce(void);
PHPAPI zend_class_entry *php_date_get_timezone_ce(void);
PHPAPI zend_class_entry *php_date_get_interval_ce(void);
PHPAPI zend_class_entry *php_date_get_date_time_interval_ce(void);
PHPAPI zend_class_entry *php_date_get_date_time_string_interval_ce(void);
PHPAPI zend_class_entry *php_date_get_period_ce(void);

/* Functions for creating DateTime objects, and initializing them from a string */
Expand Down
26 changes: 24 additions & 2 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function timezone_abbreviations_list(): array {}
function timezone_version_get(): string {}

/** @refcount 1 */
function date_interval_create_from_date_string(string $datetime): DateInterval|false {}
function date_interval_create_from_date_string(string $datetime): DateTimeStringInterval|false {}

/** @refcount 1 */
function date_interval_format(DateInterval $object, string $format): string {}
Expand Down Expand Up @@ -668,12 +668,14 @@ public static function __set_state(array $array): DateTimeZone {}

class DateInterval
{
public readonly bool $from_string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're creating it, shouldn't it be $fromString now? And ditto with $date_string in DateTimeStringInterval?

I am also unsure as to the new names...

Copy link
Member Author

@kocsismate kocsismate Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I realized a bit too late that the consistent property name would be $fromString. On the other hand, this property is only kept for BC concerns (since from now on you'll know the type of the interval based on the class name), so renaming it defies the main purpose. I'm also fine with removing the property altogether if we think that its BC impact is negligible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the other names, I'm fine with finding better names/moving them under some namespace :)


public function __construct(string $duration) {}

/**
* @tentative-return-type
*/
public static function createFromDateString(string $datetime): DateInterval|false {}
public static function createFromDateString(string $datetime): DateTimeStringInterval|false {}

/**
* @tentative-return-type
Expand All @@ -692,6 +694,26 @@ public function __wakeup(): void {}
public static function __set_state(array $array): DateInterval {}
}

final class DateTimeStringInterval extends DateInterval
{
public readonly ?string $date_string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using camelCase was better, but this is not easily possible because DateTimeStringInterval extends the original class, and because of BC concerns. I'm fine to rename the property if @derickr is ok with it. We could also create a new $dateString property besides $date_string, and in the same time deprecate the latter.


public function __construct(string $datetime) {}
}

final class DateTimeInterval extends DateInterval
{
public readonly int $y;
public readonly int $m;
public readonly int $d;
public readonly int $h;
public readonly int $i;
public readonly int $s;
public readonly float $f;
public readonly int $invert;
public readonly int|false $days;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO -1 for false would be better than mixed types

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree here.

}

class DatePeriod implements IteratorAggregate
{
/**
Expand Down
110 changes: 107 additions & 3 deletions ext/date/php_date_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions ext/date/tests/DateInterval_serialize-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var_dump($now->sub($e));
?>
--EXPECTF--
Original object:
object(DateInterval)#1 (10) {
object(DateInterval)#1 (%d) {
["from_string"]=>
bool(false)
["y"]=>
int(2)
["m"]=>
Expand All @@ -45,17 +47,17 @@ object(DateInterval)#1 (10) {
int(0)
["days"]=>
bool(false)
["from_string"]=>
bool(false)
}


Serialised object:
string(164) "O:12:"DateInterval":10:{s:1:"y";i:2;s:1:"m";i:0;s:1:"d";i:4;s:1:"h";i:6;s:1:"i";i:8;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}"
string(164) "O:12:"DateInterval":10:{s:11:"from_string";b:0;s:1:"y";i:2;s:1:"m";i:0;s:1:"d";i:4;s:1:"h";i:6;s:1:"i";i:8;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;}"


Unserialised object:
object(DateInterval)#2 (10) {
object(DateInterval)#2 (%d) {
["from_string"]=>
bool(false)
["y"]=>
int(2)
["m"]=>
Expand All @@ -74,13 +76,13 @@ object(DateInterval)#2 (10) {
int(0)
["days"]=>
bool(false)
["from_string"]=>
bool(false)
}


Calling __serialize manually:
array(%d) {
["from_string"]=>
bool(false)
["y"]=>
int(2)
["m"]=>
Expand All @@ -99,21 +101,19 @@ array(%d) {
int(0)
["days"]=>
bool(false)
["from_string"]=>
bool(false)
}


Used serialised interval:
object(DateTimeImmutable)#4 (3) {
object(DateTimeImmutable)#4 (%d) {
["date"]=>
string(26) "2024-04-26 22:33:11.000000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(3) "BST"
}
object(DateTimeImmutable)#4 (3) {
object(DateTimeImmutable)#4 (%d) {
["date"]=>
string(26) "2020-04-18 10:17:11.000000"
["timezone_type"]=>
Expand Down
Loading