3333#include <php_ini.h>
3434#include <ext/standard/info.h>
3535#include <Zend/zend_interfaces.h>
36+ #include <ext/date/php_date.h>
3637#include <ext/spl/spl_iterators.h>
3738#include <ext/date/php_date.h>
3839/* Our Compatability header */
3940#include "phongo_compat.h"
4041
42+ #ifdef PHP_WIN32
43+ #include "win32/time.h"
44+ #endif
45+
4146/* Our stuffz */
4247#include "php_phongo.h"
4348#include "php_bson.h"
@@ -47,17 +52,60 @@ PHONGO_API zend_class_entry *php_phongo_utcdatetime_ce;
4752
4853zend_object_handlers php_phongo_handler_utcdatetime ;
4954
50- /* {{{ proto BSON\UTCDateTime UTCDateTime::__construct(integer $milliseconds)
55+ static void php_phongo_utcdatetime_init_from_current_time (php_phongo_utcdatetime_t * intern )
56+ {
57+ int64_t sec , usec ;
58+ struct timeval cur_time ;
59+
60+ gettimeofday (& cur_time , NULL );
61+ sec = cur_time .tv_sec ;
62+ usec = cur_time .tv_usec ;
63+
64+ intern -> milliseconds = (sec * 1000 ) + (usec / 1000 );
65+ }
66+
67+ static void php_phongo_utcdatetime_init_from_date (php_phongo_utcdatetime_t * intern , php_date_obj * datetime_obj )
68+ {
69+ int64_t sec , usec ;
70+
71+ /* The following assignments use the same logic as date_format() in php_date.c */
72+ sec = datetime_obj -> time -> sse ;
73+ usec = (int64_t ) floor (datetime_obj -> time -> f * 1000000 + 0.5 );
74+
75+ intern -> milliseconds = (sec * 1000 ) + (usec / 1000 );
76+ }
77+
78+ /* {{{ proto BSON\UTCDateTime UTCDateTime::__construct([integer|DateTimeInterface $milliseconds = null])
5179 Construct a new UTCDateTime */
5280PHP_METHOD (UTCDateTime , __construct )
5381{
5482 php_phongo_utcdatetime_t * intern ;
55- zend_error_handling error_handling ;
56-
83+ zend_error_handling error_handling ;
84+ zval * datetime = NULL ;
5785
5886 zend_replace_error_handling (EH_THROW , phongo_exception_from_phongo_domain (PHONGO_ERROR_INVALID_ARGUMENT ), & error_handling TSRMLS_CC );
5987 intern = Z_UTCDATETIME_OBJ_P (getThis ());
6088
89+ if (zend_parse_parameters_ex (ZEND_PARSE_PARAMS_QUIET , ZEND_NUM_ARGS () TSRMLS_CC , "|o!" , & datetime ) == SUCCESS ) {
90+ if (datetime == NULL ) {
91+ php_phongo_utcdatetime_init_from_current_time (intern );
92+ } else if (instanceof_function (Z_OBJCE_P (datetime ), php_date_get_date_ce () TSRMLS_CC )) {
93+ php_phongo_utcdatetime_init_from_date (intern , Z_PHPDATE_P (datetime ));
94+ #if PHP_VERSION_ID >= 50500
95+ } else if (instanceof_function (Z_OBJCE_P (datetime ), php_date_get_immutable_ce () TSRMLS_CC )) {
96+ php_phongo_utcdatetime_init_from_date (intern , Z_PHPDATE_P (datetime ));
97+ #endif
98+ } else {
99+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC ,
100+ "Expected instance of DateTimeInterface, %s given" ,
101+ phongo_str (Z_OBJCE_P (datetime )-> name )
102+ );
103+ }
104+
105+ zend_restore_error_handling (& error_handling TSRMLS_CC );
106+ return ;
107+ }
108+
61109#if SIZEOF_PHONGO_LONG == 8
62110 {
63111 phongo_long milliseconds ;
0 commit comments