-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHPC-2429: Fix UTCDateTime with negative timestamps #1623
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
*/ | ||
|
||
#include <math.h> | ||
#include <inttypes.h> | ||
|
||
#include <php.h> | ||
#include <zend_smart_str.h> | ||
|
@@ -213,8 +214,9 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime) | |
{ | ||
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(getThis()); | ||
|
||
|
@@ -223,11 +225,23 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTime) | |
object_init_ex(return_value, php_date_get_date_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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this code has worked for some time, but it does differ from the |
||
datetime_obj->time->us = usec; | ||
} | ||
|
||
static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=== |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to include
<inttypes.h>
for this (see: cppreference). I imagine it may be included already through some sequence of libmongoc/php includes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. It worked without, but I've added an explicit include so we don't have to rely on other headers including this for us.