Commit 88102eae authored by Bo Yang's avatar Bo Yang

Replace private timelib_update_ts with public date_timestamp_get

parent 9f6aceaa
......@@ -1133,12 +1133,24 @@ PHP_METHOD(Timestamp, fromDateTime) {
return;
}
php_date_obj* dateobj = UNBOX(php_date_obj, datetime);
if (!dateobj->time->sse_uptodate) {
timelib_update_ts(dateobj->time, NULL);
// Get timestamp from Datetime object.
zval* retval_ptr;
zval* function_name;
int64_t timestamp;
MAKE_STD_ZVAL(retval_ptr);
MAKE_STD_ZVAL(function_name);
ZVAL_STRING(function_name, "date_timestamp_get", 1);
if (call_user_function(EG(function_table), NULL,
function_name, retval_ptr, 1,
&datetime TSRMLS_CC) == SUCCESS) {
protobuf_convert_to_int64(retval_ptr, &timestamp);
}
int64_t timestamp = dateobj->time->sse;
zval_ptr_dtor(&retval_ptr);
zval_ptr_dtor(&function_name);
// Set seconds
MessageHeader* self = UNBOX(MessageHeader, getThis());
......@@ -1146,13 +1158,15 @@ PHP_METHOD(Timestamp, fromDateTime) {
upb_msgdef_ntofz(self->descriptor->msgdef, "seconds");
void* storage = message_data(self);
void* memory = slot_memory(self->descriptor->layout, storage, field);
*(int64_t*)memory = dateobj->time->sse;
*(int64_t*)memory = timestamp;
// Set nanos
field = upb_msgdef_ntofz(self->descriptor->msgdef, "nanos");
storage = message_data(self);
memory = slot_memory(self->descriptor->layout, storage, field);
*(int32_t*)memory = 0;
RETURN_NULL();
#else
zend_error(E_USER_ERROR, "fromDateTime needs date extension.");
#endif
......
......@@ -126,6 +126,18 @@ $from = new \Google\Protobuf\Timestamp();
$from->setSeconds(1);
assert(1, $from->getSeconds());
$timestamp = new \Google\Protobuf\Timestamp();
date_default_timezone_set('UTC');
$from = new DateTime('2011-01-01T15:03:01.012345UTC');
$timestamp->fromDateTime($from);
assert($from->format('U'), $timestamp->getSeconds());
assert(0, $timestamp->getNanos());
$to = $timestamp->toDateTime();
assert(\DateTime::class, get_class($to));
assert($from->format('U'), $to->format('U'));
$from = new \Google\Protobuf\Value();
$from->setNumberValue(1);
assert(1, $from->getNumberValue());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment