Skip to content

Adds DateTime[Immutable]::createFromTimestamp #12413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
111 changes: 110 additions & 1 deletion ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,49 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
return 1;
} /* }}} */

PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec) /* {{{ */
{
dateobj->time = timelib_time_ctor();
dateobj->time->zone_type = TIMELIB_ZONETYPE_OFFSET;

timelib_unixtime2gmt(dateobj->time, (timelib_sll)sec);
timelib_update_ts(dateobj->time, NULL);
php_date_set_time_fraction(dateobj->time, usec);
} /* }}} */

PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts) /* {{{ */
{
double sec_dval = trunc(ts);
zend_long sec;
int usec;

if (UNEXPECTED(isnan(sec_dval)
|| sec_dval >= (double)TIMELIB_LONG_MAX
|| sec_dval < (double)TIMELIB_LONG_MIN
)) {
zend_throw_error(
date_ce_date_range_error,
"Seconds must be a finite number between " TIMELIB_LONG_FMT " and " TIMELIB_LONG_FMT ", %g given",
TIMELIB_LONG_MIN,
TIMELIB_LONG_MAX,
sec_dval
);
return false;
}

sec = (zend_long)sec_dval;
usec = (int)(fmod(ts, 1) * 1000000);

if (UNEXPECTED(usec < 0)) {
sec = sec - 1;
usec = 1000000 + usec;
}

php_date_initialize_from_ts_long(dateobj, sec, usec);

return true;
} /* }}} */

/* {{{ Returns new DateTime object */
PHP_FUNCTION(date_create)
{
Expand Down Expand Up @@ -2564,7 +2607,7 @@ PHP_FUNCTION(date_create_from_format)
}
/* }}} */

/* {{{ Returns new DateTime object formatted according to the specified format */
/* {{{ Returns new DateTimeImmutable object formatted according to the specified format */
PHP_FUNCTION(date_create_immutable_from_format)
{
zval *timezone_object = NULL;
Expand Down Expand Up @@ -2662,6 +2705,39 @@ PHP_METHOD(DateTime, createFromInterface)
}
/* }}} */

/* {{{ Creates new DateTime object from given unix timetamp */
PHP_METHOD(DateTime, createFromTimestamp)
{
zval *value;
zval new_object;
php_date_obj *new_dateobj;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_NUMBER(value)
ZEND_PARSE_PARAMETERS_END();

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_date, &new_object);
new_dateobj = Z_PHPDATE_P(&new_object);

switch (Z_TYPE_P(value)) {
case IS_LONG:
php_date_initialize_from_ts_long(new_dateobj, Z_LVAL_P(value), 0);
break;

case IS_DOUBLE:
if (!php_date_initialize_from_ts_double(new_dateobj, Z_DVAL_P(value))) {
zval_ptr_dtor(&new_object);
RETURN_THROWS();
}
break;

EMPTY_SWITCH_DEFAULT_CASE();
}

RETURN_OBJ(Z_OBJ(new_object));
}
/* }}} */

/* {{{ Creates new DateTimeImmutable object from an existing mutable DateTime object. */
PHP_METHOD(DateTimeImmutable, createFromMutable)
{
Expand Down Expand Up @@ -2704,6 +2780,39 @@ PHP_METHOD(DateTimeImmutable, createFromInterface)
}
/* }}} */

/* {{{ Creates new DateTimeImmutable object from given unix timestamp */
PHP_METHOD(DateTimeImmutable, createFromTimestamp)
{
zval *value;
zval new_object;
php_date_obj *new_dateobj;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_NUMBER(value)
ZEND_PARSE_PARAMETERS_END();

php_date_instantiate(execute_data->This.value.ce ? execute_data->This.value.ce : date_ce_immutable, &new_object);
new_dateobj = Z_PHPDATE_P(&new_object);

switch (Z_TYPE_P(value)) {
case IS_LONG:
php_date_initialize_from_ts_long(new_dateobj, Z_LVAL_P(value), 0);
break;

case IS_DOUBLE:
if (!php_date_initialize_from_ts_double(new_dateobj, Z_DVAL_P(value))) {
zval_ptr_dtor(&new_object);
RETURN_THROWS();
}
break;

EMPTY_SWITCH_DEFAULT_CASE();
}

RETURN_OBJ(Z_OBJ(new_object));
}
/* }}} */

static bool php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht)
{
zval *z_date;
Expand Down
3 changes: 2 additions & 1 deletion ext/date/php_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ PHPAPI zend_class_entry *php_date_get_period_ce(void);

PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object);
PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags);

PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec);
PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts);

#endif /* PHP_DATE_H */
6 changes: 6 additions & 0 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ public static function createFromInterface(DateTimeInterface $object): DateTime
*/
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false {}

/** @tentative-return-type */
public static function createFromTimestamp(int|float $timestamp): static {}

/**
* @return array<string, int|array>|false
* @tentative-return-type
Expand Down Expand Up @@ -466,6 +469,9 @@ public static function __set_state(array $array): DateTimeImmutable {}
*/
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false {}

/** @tentative-return-type */
public static function createFromTimestamp(int|float $timestamp): static {}

/**
* @return array<string, int|array>|false
* @tentative-return-type
Expand Down
12 changes: 11 additions & 1 deletion ext/date/php_date_arginfo.h

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

Loading