Skip to content

Commit 9c7c420

Browse files
committed
Added DateTime[Immutable]::[get|set]Microseconds
1 parent d04854b commit 9c7c420

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

ext/date/php_date.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3726,12 +3726,70 @@ PHP_METHOD(DateTimeImmutable, setTimestamp)
37263726
}
37273727
/* }}} */
37283728

3729+
/* {{{ */
3730+
PHP_METHOD(DateTimeImmutable, setMicroseconds)
3731+
{
3732+
zval *object, new_object;
3733+
php_date_obj *dateobj, *new_dateobj;
3734+
zend_long us;
3735+
3736+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3737+
RETURN_THROWS();
3738+
}
3739+
3740+
if (UNEXPECTED(us < 0 || us > 999999)) {
3741+
zend_throw_error(date_ce_date_range_error,
3742+
"Microseconds must be between 0 and 999999, "ZEND_LONG_FMT" given",
3743+
us);
3744+
RETURN_THROWS();
3745+
}
3746+
3747+
object = ZEND_THIS;
3748+
dateobj = Z_PHPDATE_P(object);
3749+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3750+
3751+
date_clone_immutable(object, &new_object);
3752+
new_dateobj = Z_PHPDATE_P(&new_object);
3753+
3754+
php_date_set_time_fraction(new_dateobj->time, (int)us);
3755+
3756+
RETURN_OBJ(Z_OBJ(new_object));
3757+
}
3758+
/* }}} */
3759+
3760+
/* {{{ */
3761+
PHP_METHOD(DateTime, setMicroseconds)
3762+
{
3763+
zval *object;
3764+
php_date_obj *dateobj;
3765+
zend_long us;
3766+
3767+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &us) == FAILURE) {
3768+
RETURN_THROWS();
3769+
}
3770+
3771+
if (UNEXPECTED(us < 0 || us > 999999)) {
3772+
zend_throw_error(date_ce_date_range_error,
3773+
"Microseconds must be between 0 and 999999, "ZEND_LONG_FMT" given",
3774+
us);
3775+
RETURN_THROWS();
3776+
}
3777+
3778+
object = ZEND_THIS;
3779+
dateobj = Z_PHPDATE_P(object);
3780+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3781+
php_date_set_time_fraction(dateobj->time, (int)us);
3782+
3783+
RETURN_OBJ_COPY(Z_OBJ_P(object));
3784+
}
3785+
/* }}} */
3786+
37293787
/* {{{ Gets the Unix timestamp. */
37303788
PHP_FUNCTION(date_timestamp_get)
37313789
{
37323790
zval *object;
37333791
php_date_obj *dateobj;
3734-
zend_long timestamp;
3792+
zend_long timestamp;
37353793
int epoch_does_not_fit_in_zend_long;
37363794

37373795
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
@@ -3755,6 +3813,21 @@ PHP_FUNCTION(date_timestamp_get)
37553813
}
37563814
/* }}} */
37573815

3816+
PHP_METHOD(DateTime, getMicroseconds) /* {{{ */
3817+
{
3818+
zval *object;
3819+
php_date_obj *dateobj;
3820+
3821+
ZEND_PARSE_PARAMETERS_NONE();
3822+
3823+
object = ZEND_THIS;
3824+
dateobj = Z_PHPDATE_P(object);
3825+
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(object));
3826+
3827+
RETURN_LONG((zend_long)dateobj->time->us);
3828+
}
3829+
/* }}} */
3830+
37583831
/* {{{ Returns the difference between two DateTime objects. */
37593832
PHP_FUNCTION(date_diff)
37603833
{

ext/date/php_date.stub.php

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

412+
/** @tentative-return-type */
413+
public function getMicroseconds(): int {}
414+
412415
/**
413416
* @tentative-return-type
414417
* @alias date_time_set
@@ -433,6 +436,9 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTime {
433436
*/
434437
public function setTimestamp(int $timestamp): DateTime {}
435438

439+
/** @tentative-return-type */
440+
public function setMicroseconds(int $microseconds): static {}
441+
436442
/**
437443
* @tentative-return-type
438444
* @alias date_timestamp_get
@@ -497,6 +503,12 @@ public function getOffset(): int {}
497503
*/
498504
public function getTimestamp(): int {}
499505

506+
/**
507+
* @alias DateTime::getMicroseconds
508+
* @tentative-return-type
509+
*/
510+
public function getMicroseconds(): int {}
511+
500512
/**
501513
* @tentative-return-type
502514
* @alias date_diff
@@ -527,6 +539,9 @@ public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeIm
527539
/** @tentative-return-type */
528540
public function setTimestamp(int $timestamp): DateTimeImmutable {}
529541

542+
/** @tentative-return-type */
543+
public function setMicroseconds(int $microseconds): static {}
544+
530545
/** @tentative-return-type */
531546
public static function createFromMutable(DateTime $object): static {}
532547

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)