Skip to content

Commit 6bf195e

Browse files
committed
Added DateTime[Immutable]::[get|set]Microseconds
1 parent 78364ef commit 6bf195e

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
@@ -3835,12 +3835,74 @@ PHP_METHOD(DateTimeImmutable, setTimestamp)
38353835
}
38363836
/* }}} */
38373837

3838+
/* {{{ */
3839+
PHP_METHOD(DateTimeImmutable, setMicroseconds)
3840+
{
3841+
zval *object, new_object;
3842+
php_date_obj *dateobj, *new_dateobj;
3843+
zend_long us;
3844+
3845+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3846+
RETURN_THROWS();
3847+
}
3848+
3849+
if (UNEXPECTED(us < 0 || us > 999999)) {
3850+
zend_throw_error(
3851+
date_ce_date_range_error,
3852+
"Microseconds must be between 0 and 999999, "ZEND_LONG_FMT" given",
3853+
us
3854+
);
3855+
RETURN_THROWS();
3856+
}
3857+
3858+
object = ZEND_THIS;
3859+
dateobj = Z_PHPDATE_P(object);
3860+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3861+
3862+
date_clone_immutable(object, &new_object);
3863+
new_dateobj = Z_PHPDATE_P(&new_object);
3864+
3865+
php_date_set_time_fraction(new_dateobj->time, (int)us);
3866+
3867+
RETURN_OBJ(Z_OBJ(new_object));
3868+
}
3869+
/* }}} */
3870+
3871+
/* {{{ */
3872+
PHP_METHOD(DateTime, setMicroseconds)
3873+
{
3874+
zval *object;
3875+
php_date_obj *dateobj;
3876+
zend_long us;
3877+
3878+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3879+
RETURN_THROWS();
3880+
}
3881+
3882+
if (UNEXPECTED(us < 0 || us > 999999)) {
3883+
zend_throw_error(
3884+
date_ce_date_range_error,
3885+
"Microseconds must be between 0 and 999999, "ZEND_LONG_FMT" given",
3886+
us
3887+
);
3888+
RETURN_THROWS();
3889+
}
3890+
3891+
object = ZEND_THIS;
3892+
dateobj = Z_PHPDATE_P(object);
3893+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3894+
php_date_set_time_fraction(dateobj->time, (int)us);
3895+
3896+
RETURN_OBJ_COPY(Z_OBJ_P(object));
3897+
}
3898+
/* }}} */
3899+
38383900
/* {{{ Gets the Unix timestamp. */
38393901
PHP_FUNCTION(date_timestamp_get)
38403902
{
38413903
zval *object;
38423904
php_date_obj *dateobj;
3843-
zend_long timestamp;
3905+
zend_long timestamp;
38443906
int epoch_does_not_fit_in_zend_long;
38453907

38463908
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
@@ -3864,6 +3926,21 @@ PHP_FUNCTION(date_timestamp_get)
38643926
}
38653927
/* }}} */
38663928

3929+
PHP_METHOD(DateTime, getMicroseconds) /* {{{ */
3930+
{
3931+
zval *object;
3932+
php_date_obj *dateobj;
3933+
3934+
ZEND_PARSE_PARAMETERS_NONE();
3935+
3936+
object = ZEND_THIS;
3937+
dateobj = Z_PHPDATE_P(object);
3938+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3939+
3940+
RETURN_LONG((zend_long)dateobj->time->us);
3941+
}
3942+
/* }}} */
3943+
38673944
/* {{{ Returns the difference between two DateTime objects. */
38683945
PHP_FUNCTION(date_diff)
38693946
{

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)