@@ -126,6 +126,25 @@ static HashTable* php_phongo_utcdatetime_get_properties_hash(phongo_compat_objec
126126 return props ;
127127}
128128
129+ static void php_phongo_utcdatetime_to_php_date (zval * return_value , const zval * this , zend_class_entry * ce )
130+ {
131+ php_phongo_utcdatetime_t * intern ;
132+ php_date_obj * datetime_obj ;
133+ char * sec ;
134+ size_t sec_len ;
135+
136+ intern = Z_UTCDATETIME_OBJ_P (this );
137+
138+ object_init_ex (return_value , ce );
139+ datetime_obj = Z_PHPDATE_P (return_value );
140+
141+ sec_len = spprintf (& sec , 0 , "@%" PRId64 , intern -> milliseconds / 1000 );
142+ php_date_initialize (datetime_obj , sec , sec_len , NULL , NULL , 0 );
143+ efree (sec );
144+
145+ datetime_obj -> time -> us = (intern -> milliseconds % 1000 ) * 1000 ;
146+ }
147+
129148/* Construct a new BSON UTCDateTime type from either the current time,
130149 milliseconds since the epoch, or a DateTimeInterface object. Defaults to the
131150 current time. */
@@ -211,23 +230,17 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __toString)
211230/* Returns a DateTime object representing this UTCDateTime */
212231static PHP_METHOD (MongoDB_BSON_UTCDateTime , toDateTime )
213232{
214- php_phongo_utcdatetime_t * intern ;
215- php_date_obj * datetime_obj ;
216- char * sec ;
217- size_t sec_len ;
218-
219- intern = Z_UTCDATETIME_OBJ_P (getThis ());
220-
221233 PHONGO_PARSE_PARAMETERS_NONE ();
222234
223- object_init_ex (return_value , php_date_get_date_ce ());
224- datetime_obj = Z_PHPDATE_P ( return_value );
235+ php_phongo_utcdatetime_to_php_date (return_value , getThis () , php_date_get_date_ce ());
236+ }
225237
226- sec_len = spprintf (& sec , 0 , "@%" PRId64 , intern -> milliseconds / 1000 );
227- php_date_initialize (datetime_obj , sec , sec_len , NULL , NULL , 0 );
228- efree (sec );
238+ /* Returns a DateTimeImmutable object representing this UTCDateTime */
239+ static PHP_METHOD (MongoDB_BSON_UTCDateTime , toDateTimeImmutable )
240+ {
241+ PHONGO_PARSE_PARAMETERS_NONE ();
229242
230- datetime_obj -> time -> us = ( intern -> milliseconds % 1000 ) * 1000 ;
243+ php_phongo_utcdatetime_to_php_date ( return_value , getThis (), php_date_get_immutable_ce ()) ;
231244}
232245
233246static PHP_METHOD (MongoDB_BSON_UTCDateTime , jsonSerialize )
0 commit comments