Skip to content

Commit 0016b30

Browse files
authored
Added DateTime[Immutable]::[get|set]Microseconds (#12557)
* Added DateTime[Immutable]::[get|set]Microseconds
1 parent f91833d commit 0016b30

File tree

6 files changed

+314
-2
lines changed

6 files changed

+314
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PHP NEWS
1919

2020
- Date:
2121
. Added DateTime[Immutable]::createFromTimestamp. (Marc Bennewitz)
22+
. Added DateTime[Immutable]::[get|set]Microseconds. (Marc Bennewitz)
2223

2324
- DOM:
2425
. Added DOMNode::compareDocumentPosition(). (nielsdos)

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ PHP 8.4 UPGRADE NOTES
146146
- Date:
147147
. Added static methods
148148
DateTime[Immutable]::createFromTimestamp(int|float $timestamp): static.
149+
. Added method DateTime[Immutable]::getMicroseconds(): int.
150+
. Added method
151+
DateTime[Immutable]::setMicroseconds(int $microseconds): static.
149152

150153
- DOM:
151154
. Added constant DOMNode::DOCUMENT_POSITION_DISCONNECTED.

ext/date/php_date.c

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3844,12 +3844,76 @@ PHP_METHOD(DateTimeImmutable, setTimestamp)
38443844
}
38453845
/* }}} */
38463846

3847+
/* {{{ */
3848+
PHP_METHOD(DateTimeImmutable, setMicroseconds)
3849+
{
3850+
zval *object, new_object;
3851+
php_date_obj *dateobj, *new_dateobj;
3852+
zend_long us;
3853+
3854+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3855+
RETURN_THROWS();
3856+
}
3857+
3858+
if (UNEXPECTED(us < 0 || us > 999999)) {
3859+
zend_argument_error(
3860+
date_ce_date_range_error,
3861+
1,
3862+
"must be between 0 and 999999, " ZEND_LONG_FMT " given",
3863+
us
3864+
);
3865+
RETURN_THROWS();
3866+
}
3867+
3868+
object = ZEND_THIS;
3869+
dateobj = Z_PHPDATE_P(object);
3870+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3871+
3872+
date_clone_immutable(object, &new_object);
3873+
new_dateobj = Z_PHPDATE_P(&new_object);
3874+
3875+
php_date_set_time_fraction(new_dateobj->time, (int)us);
3876+
3877+
RETURN_OBJ(Z_OBJ(new_object));
3878+
}
3879+
/* }}} */
3880+
3881+
/* {{{ */
3882+
PHP_METHOD(DateTime, setMicroseconds)
3883+
{
3884+
zval *object;
3885+
php_date_obj *dateobj;
3886+
zend_long us;
3887+
3888+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3889+
RETURN_THROWS();
3890+
}
3891+
3892+
if (UNEXPECTED(us < 0 || us > 999999)) {
3893+
zend_argument_error(
3894+
date_ce_date_range_error,
3895+
1,
3896+
"must be between 0 and 999999, " ZEND_LONG_FMT " given",
3897+
us
3898+
);
3899+
RETURN_THROWS();
3900+
}
3901+
3902+
object = ZEND_THIS;
3903+
dateobj = Z_PHPDATE_P(object);
3904+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3905+
php_date_set_time_fraction(dateobj->time, (int)us);
3906+
3907+
RETURN_OBJ_COPY(Z_OBJ_P(object));
3908+
}
3909+
/* }}} */
3910+
38473911
/* {{{ Gets the Unix timestamp. */
38483912
PHP_FUNCTION(date_timestamp_get)
38493913
{
38503914
zval *object;
38513915
php_date_obj *dateobj;
3852-
zend_long timestamp;
3916+
zend_long timestamp;
38533917
int epoch_does_not_fit_in_zend_long;
38543918

38553919
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
@@ -3873,6 +3937,21 @@ PHP_FUNCTION(date_timestamp_get)
38733937
}
38743938
/* }}} */
38753939

3940+
PHP_METHOD(DateTime, getMicroseconds) /* {{{ */
3941+
{
3942+
zval *object;
3943+
php_date_obj *dateobj;
3944+
3945+
ZEND_PARSE_PARAMETERS_NONE();
3946+
3947+
object = ZEND_THIS;
3948+
dateobj = Z_PHPDATE_P(object);
3949+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3950+
3951+
RETURN_LONG((zend_long)dateobj->time->us);
3952+
}
3953+
/* }}} */
3954+
38763955
/* {{{ Returns the difference between two DateTime objects. */
38773956
PHP_FUNCTION(date_diff)
38783957
{

ext/date/php_date.stub.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ public function setTimezone(DateTimeZone $timezone): DateTime {}
412412
*/
413413
public function getOffset(): int {}
414414

415+
/** @tentative-return-type */
416+
public function getMicroseconds(): int {}
417+
415418
/**
416419
* @tentative-return-type
417420
* @alias date_time_set
@@ -436,6 +439,9 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTime {
436439
*/
437440
public function setTimestamp(int $timestamp): DateTime {}
438441

442+
/** @tentative-return-type */
443+
public function setMicroseconds(int $microseconds): static {}
444+
439445
/**
440446
* @tentative-return-type
441447
* @alias date_timestamp_get
@@ -503,6 +509,12 @@ public function getOffset(): int {}
503509
*/
504510
public function getTimestamp(): int {}
505511

512+
/**
513+
* @alias DateTime::getMicroseconds
514+
* @tentative-return-type
515+
*/
516+
public function getMicroseconds(): int {}
517+
506518
/**
507519
* @tentative-return-type
508520
* @alias date_diff
@@ -533,6 +545,9 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeIm
533545
/** @tentative-return-type */
534546
public function setTimestamp(int $timestamp): DateTimeImmutable {}
535547

548+
/** @tentative-return-type */
549+
public function setMicroseconds(int $microseconds): static {}
550+
536551
/** @tentative-return-type */
537552
public static function createFromMutable(DateTime $object): static {}
538553

ext/date/php_date_arginfo.h

Lines changed: 18 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)