Skip to content

Commit 95e12ab

Browse files
committed
Added DateTime[Immutable]::createFromTimestamp / date_create_[immutable]_from_timestamp
1 parent 0ba24a5 commit 95e12ab

File tree

4 files changed

+557
-2
lines changed

4 files changed

+557
-2
lines changed

ext/date/php_date.c

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2500,6 +2500,40 @@ PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, siz
25002500
return 1;
25012501
} /* }}} */
25022502

2503+
PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec) /* {{{ */
2504+
{
2505+
dateobj->time = timelib_time_ctor();
2506+
dateobj->time->zone_type = TIMELIB_ZONETYPE_OFFSET;
2507+
dateobj->time->z = (timelib_sll)0;
2508+
dateobj->time->have_relative = 0;
2509+
2510+
timelib_unixtime2gmt(dateobj->time, (timelib_sll)sec);
2511+
timelib_update_ts(dateobj->time, NULL);
2512+
php_date_set_time_fraction(dateobj->time, usec);
2513+
} /* }}} */
2514+
2515+
PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts) /* {{{ */
2516+
{
2517+
zend_long sec;
2518+
int usec;
2519+
2520+
if (UNEXPECTED(isnan(ts) || !ZEND_DOUBLE_FITS_LONG(ts))) {
2521+
return 0;
2522+
}
2523+
2524+
sec = (zend_long)ts;
2525+
usec = (int)(fmod(ts, 1) * 1000000);
2526+
2527+
if (UNEXPECTED(usec < 0)) {
2528+
sec = sec - 1;
2529+
usec = 1000000 + usec;
2530+
}
2531+
2532+
php_date_initialize_from_ts_long(dateobj, sec, usec);
2533+
2534+
return 1;
2535+
} /* }}} */
2536+
25032537
/* {{{ Returns new DateTime object */
25042538
PHP_FUNCTION(date_create)
25052539
{
@@ -2564,7 +2598,7 @@ PHP_FUNCTION(date_create_from_format)
25642598
}
25652599
/* }}} */
25662600

2567-
/* {{{ Returns new DateTime object formatted according to the specified format */
2601+
/* {{{ Returns new DateTimeImmutable object formatted according to the specified format */
25682602
PHP_FUNCTION(date_create_immutable_from_format)
25692603
{
25702604
zval *timezone_object = NULL;
@@ -2586,6 +2620,66 @@ PHP_FUNCTION(date_create_immutable_from_format)
25862620
}
25872621
/* }}} */
25882622

2623+
/* {{{ Returns new DateTime object from given unix timetamp */
2624+
PHP_FUNCTION(date_create_from_timestamp)
2625+
{
2626+
php_date_obj *dateobj;
2627+
zval *value;
2628+
2629+
ZEND_PARSE_PARAMETERS_START(1, 1)
2630+
Z_PARAM_NUMBER(value)
2631+
ZEND_PARSE_PARAMETERS_END();
2632+
2633+
php_date_instantiate(date_ce_date, return_value);
2634+
dateobj = Z_PHPDATE_P(return_value);
2635+
2636+
switch (Z_TYPE_P(value)) {
2637+
case IS_LONG:
2638+
php_date_initialize_from_ts_long(dateobj, Z_LVAL_P(value), 0);
2639+
break;
2640+
2641+
case IS_DOUBLE:
2642+
if (!php_date_initialize_from_ts_double(dateobj, Z_DVAL_P(value))) {
2643+
zval_ptr_dtor(return_value);
2644+
RETURN_FALSE;
2645+
}
2646+
break;
2647+
2648+
EMPTY_SWITCH_DEFAULT_CASE();
2649+
}
2650+
}
2651+
/* }}} */
2652+
2653+
/* {{{ Returns new DateTimeImmutable object from given unix timestamp */
2654+
PHP_FUNCTION(date_create_immutable_from_timestamp)
2655+
{
2656+
php_date_obj *dateobj;
2657+
zval *value;
2658+
2659+
ZEND_PARSE_PARAMETERS_START(1, 1)
2660+
Z_PARAM_NUMBER(value)
2661+
ZEND_PARSE_PARAMETERS_END();
2662+
2663+
php_date_instantiate(date_ce_immutable, return_value);
2664+
dateobj = Z_PHPDATE_P(return_value);
2665+
2666+
switch (Z_TYPE_P(value)) {
2667+
case IS_LONG:
2668+
php_date_initialize_from_ts_long(dateobj, Z_LVAL_P(value), 0);
2669+
break;
2670+
2671+
case IS_DOUBLE:
2672+
if (!php_date_initialize_from_ts_double(dateobj, Z_DVAL_P(value))) {
2673+
zval_ptr_dtor(return_value);
2674+
RETURN_FALSE;
2675+
}
2676+
break;
2677+
2678+
EMPTY_SWITCH_DEFAULT_CASE();
2679+
}
2680+
}
2681+
/* }}} */
2682+
25892683
/* {{{ Creates new DateTime object */
25902684
PHP_METHOD(DateTime, __construct)
25912685
{

ext/date/php_date.stub.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ function date_create_from_format(
159159
function date_create_immutable_from_format(
160160
string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false {}
161161

162+
/** @refcount 1 */
163+
function date_create_from_timestamp(int|float $timestamp): DateTime|false {}
164+
165+
/** @refcount 1 */
166+
function date_create_immutable_from_timestamp(int|float $timestamp): DateTimeImmutable|false {}
167+
162168
/**
163169
* @return array<string, mixed>
164170
* @refcount 1
@@ -362,6 +368,12 @@ public static function createFromInterface(DateTimeInterface $object): DateTime
362368
*/
363369
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false {}
364370

371+
/**
372+
* @tentative-return-type
373+
* @alias date_create_from_timestamp
374+
*/
375+
public static function createFromTimestamp(int|float $timestamp): DateTime|false {}
376+
365377
/**
366378
* @return array<string, int|array>|false
367379
* @tentative-return-type
@@ -466,6 +478,12 @@ public static function __set_state(array $array): DateTimeImmutable {}
466478
*/
467479
public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false {}
468480

481+
/**
482+
* @tentative-return-type
483+
* @alias date_create_immutable_from_timestamp
484+
*/
485+
public static function createFromTimestamp(int|float $timestamp): DateTimeImmutable|false {}
486+
469487
/**
470488
* @return array<string, int|array>|false
471489
* @tentative-return-type

ext/date/php_date_arginfo.h

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)