Skip to content

Commit aa19dd1

Browse files
committed
Added DateTime[Immutable]::[get|set]Microseconds
1 parent 1fc85a3 commit aa19dd1

File tree

4 files changed

+308
-2
lines changed

4 files changed

+308
-2
lines changed

ext/date/php_date.c

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3844,12 +3844,74 @@ 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_throw_error(
3860+
date_ce_date_range_error,
3861+
"Microseconds must be between 0 and 999999, "ZEND_LONG_FMT" given",
3862+
us
3863+
);
3864+
RETURN_THROWS();
3865+
}
3866+
3867+
object = ZEND_THIS;
3868+
dateobj = Z_PHPDATE_P(object);
3869+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3870+
3871+
date_clone_immutable(object, &new_object);
3872+
new_dateobj = Z_PHPDATE_P(&new_object);
3873+
3874+
php_date_set_time_fraction(new_dateobj->time, (int)us);
3875+
3876+
RETURN_OBJ(Z_OBJ(new_object));
3877+
}
3878+
/* }}} */
3879+
3880+
/* {{{ */
3881+
PHP_METHOD(DateTime, setMicroseconds)
3882+
{
3883+
zval *object;
3884+
php_date_obj *dateobj;
3885+
zend_long us;
3886+
3887+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3888+
RETURN_THROWS();
3889+
}
3890+
3891+
if (UNEXPECTED(us < 0 || us > 999999)) {
3892+
zend_throw_error(
3893+
date_ce_date_range_error,
3894+
"Microseconds must be between 0 and 999999, "ZEND_LONG_FMT" given",
3895+
us
3896+
);
3897+
RETURN_THROWS();
3898+
}
3899+
3900+
object = ZEND_THIS;
3901+
dateobj = Z_PHPDATE_P(object);
3902+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3903+
php_date_set_time_fraction(dateobj->time, (int)us);
3904+
3905+
RETURN_OBJ_COPY(Z_OBJ_P(object));
3906+
}
3907+
/* }}} */
3908+
38473909
/* {{{ Gets the Unix timestamp. */
38483910
PHP_FUNCTION(date_timestamp_get)
38493911
{
38503912
zval *object;
38513913
php_date_obj *dateobj;
3852-
zend_long timestamp;
3914+
zend_long timestamp;
38533915
int epoch_does_not_fit_in_zend_long;
38543916

38553917
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
@@ -3873,6 +3935,21 @@ PHP_FUNCTION(date_timestamp_get)
38733935
}
38743936
/* }}} */
38753937

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

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)