Skip to content
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

int|float for DateTime::setTimestamp #13383

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 38 additions & 25 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -3799,51 +3799,64 @@ PHP_METHOD(DateTimeImmutable, setISODate)
}
/* }}} */

static void php_date_timestamp_set(zval *object, zend_long timestamp, zval *return_value) /* {{{ */
{
php_date_obj *dateobj;

dateobj = Z_PHPDATE_P(object);
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
timelib_unixtime2local(dateobj->time, (timelib_sll)timestamp);
timelib_update_ts(dateobj->time, NULL);
php_date_set_time_fraction(dateobj->time, 0);
static void php_date_timestamp_set(zval *object, zval *timestamp, zval *return_value) /* {{{ */
{
php_date_obj *dateobj;
timelib_sll ts;
Copy link
Member

Choose a reason for hiding this comment

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

nit: the same spaces vs tab "issue" here as well


dateobj = Z_PHPDATE_P(object);
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));

if (Z_TYPE_P(timestamp) == IS_LONG) {
ts = (timelib_sll)Z_LVAL_P(timestamp);
} else {
ZEND_ASSERT(Z_TYPE_P(timestamp) == IS_DOUBLE); // zend_parse_parameters() should guarantee this.
ts = (timelib_sll)Z_DVAL_P(timestamp);
}

timelib_unixtime2local(dateobj->time, ts);
timelib_update_ts(dateobj->time, NULL);
if (Z_TYPE_P(timestamp) == IS_DOUBLE) {
const int fraction_microseconds = (int)((Z_DVAL_P(timestamp) - (double)ts) * 1000000);
php_date_set_time_fraction(dateobj->time, fraction_microseconds);
} else {
php_date_set_time_fraction(dateobj->time, 0);
}
} /* }}} */

/* {{{ Sets the date and time based on an Unix timestamp. */
PHP_FUNCTION(date_timestamp_set)
{
zval *object;
zend_long timestamp;
zval *object, *timestamp_zval;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &object, date_ce_date, &timestamp) == FAILURE) {
RETURN_THROWS();
}
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "On", &object, date_ce_date, &timestamp_zval) == FAILURE) {
RETURN_THROWS();
}

php_date_timestamp_set(object, timestamp, return_value);
php_date_timestamp_set(object, timestamp_zval, return_value);

RETURN_OBJ_COPY(Z_OBJ_P(object));
RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */

/* {{{ */
PHP_METHOD(DateTimeImmutable, setTimestamp)
{
zval *object, new_object;
zend_long timestamp;
zval *object, new_object, *timestamp_zval;

object = ZEND_THIS;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &timestamp) == FAILURE) {
RETURN_THROWS();
}
object = ZEND_THIS;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "n", &timestamp_zval) == FAILURE) {
RETURN_THROWS();
}

date_clone_immutable(object, &new_object);
php_date_timestamp_set(&new_object, timestamp, return_value);
date_clone_immutable(object, &new_object);
php_date_timestamp_set(&new_object, timestamp_zval, return_value);

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


/* {{{ */
PHP_METHOD(DateTimeImmutable, setMicroseconds)
{
Expand Down
6 changes: 3 additions & 3 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function date_date_set(DateTime $object, int $year, int $month, int $day): DateT

function date_isodate_set(DateTime $object, int $year, int $week, int $dayOfWeek = 1): DateTime {}

function date_timestamp_set(DateTime $object, int $timestamp): DateTime {}
function date_timestamp_set(DateTime $object, int|float $timestamp): DateTime {}

function date_timestamp_get(DateTimeInterface $object): int {}

Expand Down Expand Up @@ -437,7 +437,7 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTime {
* @tentative-return-type
* @alias date_timestamp_set
*/
public function setTimestamp(int $timestamp): DateTime {}
public function setTimestamp(int|float $timestamp): DateTime {}

/** @tentative-return-type */
public function setMicroseconds(int $microseconds): static {}
Expand Down Expand Up @@ -543,7 +543,7 @@ public function setDate(int $year, int $month, int $day): DateTimeImmutable {}
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeImmutable {}

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

/** @tentative-return-type */
public function setMicroseconds(int $microseconds): static {}
Expand Down
8 changes: 4 additions & 4 deletions ext/date/php_date_arginfo.h

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

5 changes: 5 additions & 0 deletions ext/date/tests/date-set-timestamp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ echo $d->format( "Y-m-d H:i e\n" );
$d = new DateTime();
$d->setTimestamp( 1217184864 );
echo $d->format( "Y-m-d H:i e\n" );

$d = new DateTime();
$d->setTimestamp(0.123456789);
echo $d->format( \DateTime::RFC3339_EXTENDED );
?>
--EXPECT--
2008-07-27 18:54 +00:00
2008-07-27 20:54 Europe/Oslo
1970-01-01T01:00:00.123+01:00
2 changes: 1 addition & 1 deletion ext/date/tests/date_timestamp_set_nullparam2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $dtms021 = date_create();
var_dump(date_timestamp_set($dtms021, null));
?>
--EXPECTF--
Deprecated: date_timestamp_set(): Passing null to parameter #2 ($timestamp) of type int is deprecated in %s on line %d
Deprecated: date_timestamp_set(): Passing null to parameter #2 ($timestamp) of type int|float is deprecated in %s on line %d
object(DateTime)#1 (3) {
["date"]=>
string(26) "1970-01-01 00:00:00.000000"
Expand Down