Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions Zend/Optimizer/zend_func_infos.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ static const func_info_t func_infos[] = {
F1("date_create_immutable", MAY_BE_OBJECT|MAY_BE_FALSE),
F1("date_create_from_format", MAY_BE_OBJECT|MAY_BE_FALSE),
F1("date_create_immutable_from_format", MAY_BE_OBJECT|MAY_BE_FALSE),
F1("date_create_from_timestamp", MAY_BE_OBJECT),
F1("date_create_immutable_from_timestamp", MAY_BE_OBJECT),
F1("date_parse", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),
F1("date_parse_from_format", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_ANY),
F1("date_get_last_errors", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_FALSE),
Expand Down
107 changes: 106 additions & 1 deletion ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -2500,6 +2500,45 @@ 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 +2603,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 All @@ -2586,6 +2625,72 @@ PHP_FUNCTION(date_create_immutable_from_format)
}
/* }}} */

/* {{{ Returns new DateTime object from given unix timetamp */
PHP_FUNCTION(date_create_from_timestamp)
{
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));
}
/* }}} */

/* {{{ Returns new DateTimeImmutable object from given unix timestamp */
PHP_FUNCTION(date_create_immutable_from_timestamp)
{
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));
}
/* }}} */

/* {{{ Creates new DateTime object */
PHP_METHOD(DateTime, __construct)
{
Expand Down
18 changes: 18 additions & 0 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ function date_create_from_format(
function date_create_immutable_from_format(
string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false {}

/** @refcount 1 */
function date_create_from_timestamp(int|float $timestamp): DateTime {}

/** @refcount 1 */
function date_create_immutable_from_timestamp(int|float $timestamp): DateTimeImmutable {}

/**
* @return array<string, mixed>
* @refcount 1
Expand Down Expand Up @@ -362,6 +368,12 @@ public static function createFromInterface(DateTimeInterface $object): DateTime
*/
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false {}

/**
* @tentative-return-type
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tentative-return-type makes Carbon compatible with deprecation message

* @alias date_create_from_timestamp
*/
public static function createFromTimestamp(int|float $timestamp): static {}

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

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

/**
* @return array<string, int|array>|false
* @tentative-return-type
Expand Down
22 changes: 21 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