Skip to content

PHPC-536: UTCDateTime construction from DateTime or current time #336

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

Merged
merged 3 commits into from
Jun 29, 2016

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Jun 21, 2016

@moderndeveloperllc
Copy link

Sorry for my programming ignorance, but I noticed that you were using DateTime in the code and tests. Is this code DateTimeInterface-compatible? It may also be advisable to throw in a pre-1970 date in the tests just to make sure that negative numbers are playing nicely. I'm looking forward to this landing soon!

/* Initialize UTCDateTime from current time */
struct timeval cur_time;

gettimeofday(&cur_time, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows doesn't have gettimeofday.

Copy link
Member Author

@jmikola jmikola Jun 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered the options in http://stackoverflow.com/q/10905892/162228, but then I found gettimeofday and the necessary structs defined in win32/time.h (going back to at least PHP 5.4). I also found evidence of that implementation being used in several core extensions.

The PHP implementation seems to be equivalent to the other solutions (i.e. it uses GetSystemTimeAsFileTime()).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@jmikola
Copy link
Member Author

jmikola commented Jun 21, 2016

Sorry for my programming ignorance, but I noticed that you were using DateTime in the code and tests. Is this code DateTimeInterface-compatible?

DateTimeInterface only exists in PHP 5.5, but we should be able to use that if it's available and fall back to DateTime for PHP 5.4.

@derick: I noticed that php_date.h only exports class entries for DateTime and DateTimeImmutable:

PHPAPI zend_class_entry *php_date_get_date_ce(void);
PHPAPI zend_class_entry *php_date_get_immutable_ce(void);

Can we get away with defining extern zend_class_entry * date_ce_interface and relying on variable in php_date.c (it's not declared static). Alternatively, do we need to use zend_fetch_class() and resolve the interface name string to a class entry?

It may also be advisable to throw in a pre-1970 date in the tests just to make sure that negative numbers are playing nicely. I'm looking forward to this landing soon!

Great idea. I'll work on getting these in and try to come up with some tests that exercise the range limits of PHP and BSON dates.

@jmikola
Copy link
Member Author

jmikola commented Jun 21, 2016

@derickr: Regarding the Travis failure, that appears to be bson-utcdatetime_error-001.phpt, which was asserting that UTCDateTime::__construct() threw when no argument was passed. Since that test case is no longer valid, I propose we delete it and decrement the other "-002" and "-003" tests.

@moderndeveloperllc
Copy link

DateTimeInterface only exists in PHP 5.5, but we should be able to use that if it's available and fall back to DateTime for PHP 5.4.

Sorry. That's just me forgetting that the driver is compatible back to 5.4. Being able to pass in DateTimeInterface classes would be great regardless.

@derickr
Copy link
Contributor

derickr commented Jun 27, 2016

Can we get away with defining extern zend_class_entry * date_ce_interface and relying on variable in php_date.c (it's not declared static).

No. This works on Linux, but won't work on Windows.

Alternatively, do we need to use zend_fetch_class() and resolve the interface name string to a class entry?

That sounds kinda crappy... I'd rather export the interface in PHP (from PHP 7.1).

@jmikola
Copy link
Member Author

jmikola commented Jun 27, 2016

Alternatively, do we need to use zend_fetch_class() and resolve the interface name string to a class entry?

That sounds kinda crappy... I'd rather export the interface in PHP (from PHP 7.1).

Of course it makes sense to start doing that in PHP 7.1, but zend_fetch_class() seems necessary to get this working today.

found_ce = zend_fetch_class("DateTimeInterface", sizeof("DateTimeInterface")-1, ZEND_FETCH_CLASS_AUTO|ZEND_FETCH_CLASS_NO_AUTOLOAD|ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
#endif

return found_ce ? found_ce : php_date_get_date_ce();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derickr: Would it make sense to memoize this function by saving found_ce in a static global? I also thought about initializing the constructors date_ce in UTCDateTime's MINIT, but that assumes we are always initialized after PHP's date extension. I'd like to respect the edge case where that may not be true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a dependency on it, and that should make PHP initialise datetime first. Instead of MINIT, I would put it in RINIT.

zend_restore_error_handling(&error_handling TSRMLS_CC);
return;
}
#endif
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into problems calling zend_fetch_class() to get the class entry for DateTimeInterface during MINIT. This ultimately seemed an easier solution, which uses nothing more than the date extension's public API.

Meanwhile, I've opened PHPC-718 to ensure that we start listing our extension dependencies.

--SKIPIF--
<?php if (defined("HHVM_VERSION_ID")) exit("skip HHVM handles parameter parsing differently"); ?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might have to put this one back in I suppose, if we find that it's still different with HHVM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it will be a problem. Your constructor accepts a mixed type and all validation is left to you, so it should be trivial to customize the exception message here.

@derickr
Copy link
Contributor

derickr commented Jun 29, 2016

LGTM

@jmikola jmikola merged commit 29a3729 into mongodb:master Jun 29, 2016
jmikola added a commit that referenced this pull request Jun 29, 2016
@jmikola jmikola deleted the phpc-536 branch June 29, 2016 18:25
GromNaN added a commit to GromNaN/mongodb-odm that referenced this pull request Feb 27, 2025
GromNaN added a commit to doctrine/mongodb-odm that referenced this pull request Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants