Skip to content

Commit

Permalink
Merge v1.19 into master (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
mongodb-php-bot authored Sep 4, 2024
2 parents a7091e8 + 9e9b082 commit f9aa6c0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <math.h>
#include <inttypes.h>

#include <php.h>
#include <zend_smart_str.h>
Expand Down Expand Up @@ -130,19 +131,32 @@ static void php_phongo_utcdatetime_to_php_date(zval* return_value, const zval* t
{
php_phongo_utcdatetime_t* intern;
php_date_obj* datetime_obj;
char* sec;
char* sec_str;
size_t sec_len;
int64_t sec, usec;

intern = Z_UTCDATETIME_OBJ_P(this);

object_init_ex(return_value, ce);
datetime_obj = Z_PHPDATE_P(return_value);

sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000);
php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0);
efree(sec);
sec = intern->milliseconds / 1000;
usec = (llabs(intern->milliseconds) % 1000) * 1000;
if (intern->milliseconds < 0 && usec != 0) {
/* For dates before the unix epoch, we need to subtract the microseconds from the timestamp.
* Since we can't directly pass microseconds when calling php_date_initialize due to a bug in PHP,
* we manually decrement the timestamp and subtract the number of microseconds from a full seconds
* to store in the us field. */
sec--;
usec = 1000000 - usec;
}

/* TODO PHP 8.1.7+: microseconds can be included in the format string */
sec_len = spprintf(&sec_str, 0, "@%" PRId64, sec);
php_date_initialize(datetime_obj, sec_str, sec_len, NULL, NULL, 0);
efree(sec_str);

datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
datetime_obj->time->us = usec;
}

/* Construct a new BSON UTCDateTime type from either the current time,
Expand Down
33 changes: 33 additions & 0 deletions tests/bson/bson-utcdatetime-todatetime-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
MongoDB\BSON\UTCDateTime::toDateTime() with dates before the Unix epoch
--INI--
date.timezone=UTC
--FILE--
<?php

$dates = [
'1960-01-01 12:12:12.1',
'1969-12-31 23:59:59.999',
];

foreach ($dates as $date) {
$dateTime = new \DateTimeImmutable($date);
echo $dateTime->format(DateTimeInterface::RFC3339_EXTENDED), PHP_EOL;

$utcDateTime = new MongoDB\BSON\UTCDateTime($dateTime);

$newDate = $utcDateTime->toDateTime();
echo $newDate->format(DateTimeInterface::RFC3339_EXTENDED), PHP_EOL, PHP_EOL;
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
1960-01-01T12:12:12.100+00:00
1960-01-01T12:12:12.100+00:00

1969-12-31T23:59:59.999+00:00
1969-12-31T23:59:59.999+00:00

===DONE===
33 changes: 33 additions & 0 deletions tests/bson/bson-utcdatetime-todatetimeimmutable-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
MongoDB\BSON\UTCDateTime::toDateTimeImmutable() with dates before the Unix epoch
--INI--
date.timezone=UTC
--FILE--
<?php

$dates = [
'1960-01-01 12:12:12.1',
'1969-12-31 23:59:59.999',
];

foreach ($dates as $date) {
$dateTime = new \DateTimeImmutable($date);
echo $dateTime->format(DateTimeInterface::RFC3339_EXTENDED), PHP_EOL;

$utcDateTime = new MongoDB\BSON\UTCDateTime($dateTime);

$newDate = $utcDateTime->toDateTimeImmutable();
echo $newDate->format(DateTimeInterface::RFC3339_EXTENDED), PHP_EOL, PHP_EOL;
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
1960-01-01T12:12:12.100+00:00
1960-01-01T12:12:12.100+00:00

1969-12-31T23:59:59.999+00:00
1969-12-31T23:59:59.999+00:00

===DONE===

0 comments on commit f9aa6c0

Please sign in to comment.