Skip to content

Commit 5fadb1a

Browse files
committed
Refactor UTCDateTime::toDateTime functions
1 parent dd6afab commit 5fadb1a

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/BSON/UTCDateTime.c

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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,45 +230,17 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __toString)
211230
/* Returns a DateTime object representing this UTCDateTime */
212231
static 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);
225-
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);
229-
230-
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
235+
php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_date_ce());
231236
}
232237

233-
/* Returns a DateTime object representing this UTCDateTime */
238+
/* Returns a DateTimeImmutable object representing this UTCDateTime */
234239
static PHP_METHOD(MongoDB_BSON_UTCDateTime, toDateTimeImmutable)
235240
{
236-
php_phongo_utcdatetime_t* intern;
237-
php_date_obj* datetime_obj;
238-
char* sec;
239-
size_t sec_len;
240-
241-
intern = Z_UTCDATETIME_OBJ_P(getThis());
242-
243241
PHONGO_PARSE_PARAMETERS_NONE();
244242

245-
object_init_ex(return_value, php_date_get_immutable_ce());
246-
datetime_obj = Z_PHPDATE_P(return_value);
247-
248-
sec_len = spprintf(&sec, 0, "@%" PRId64, intern->milliseconds / 1000);
249-
php_date_initialize(datetime_obj, sec, sec_len, NULL, NULL, 0);
250-
efree(sec);
251-
252-
datetime_obj->time->us = (intern->milliseconds % 1000) * 1000;
243+
php_phongo_utcdatetime_to_php_date(return_value, getThis(), php_date_get_immutable_ce());
253244
}
254245

255246
static PHP_METHOD(MongoDB_BSON_UTCDateTime, jsonSerialize)

0 commit comments

Comments
 (0)