php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #59559 Support for returning PHP datetime objects back to V8 as Date objects
Submitted: 2011-01-05 09:17 UTC Modified: 2015-03-13 14:53 UTC
From: taneli at crasman dot fi Assigned: stesie (profile)
Status: Closed Package: v8js (PECL)
PHP Version: 5.3.2 OS: RHEL 5
Private report: No CVE-ID: None
 [2011-01-05 09:17 UTC] taneli at crasman dot fi
Description:
------------
It'd be nice to get DateTime objects back to V8 as Date -objects. Patch attached.

Reproduce code:
---------------
Code sample:
<?php 
{
  $v8 = new \V8Js();
  $dt = new \DateTime('now');
  var_dump($dt->format('r'));
  $v8->dt = $dt;
  var_dump($v8->executeString('PHP.dt;'));
}

Patch:
--- v8js-0.1.2/v8js_convert.cc  2010-12-31 10:09:38.000000000 +0200
+++ v8js-0.1.2-rosmo/v8js_convert.cc    2011-01-05 16:08:01.000000000 +0200
@@ -485,6 +485,7 @@
 v8::Handle<v8::Value> zval_to_v8js(zval *value TSRMLS_DC) /* {{{ */
 {
        v8::Handle<v8::Value> jsValue;
+       zval *dtval;
 
        switch (Z_TYPE_P(value))
        {
@@ -492,8 +493,17 @@
                        jsValue = php_v8js_hash_to_jsarr(value TSRMLS_CC);
                        break;
 
-               case IS_OBJECT:
-                       jsValue = php_v8js_hash_to_jsobj(value TSRMLS_CC);
+               case IS_OBJECT:
+                       if (instanceof_function(Z_OBJCE_P(value), php_date_get_date_ce() TSRMLS_CC))
+                       {
+                               zend_call_method_with_0_params(&value, NULL, NULL, "getTimestamp", &dtval);
+                               if (dtval)
+                                       jsValue = V8JS_DATE(((double)Z_LVAL_P(dtval) * 1000.0));
+                               else
+                                       jsValue = V8JS_NULL;
+                       } else {
+                               jsValue = php_v8js_hash_to_jsobj(value TSRMLS_CC);
+                       }
                        break;
 
                case IS_STRING:
--- v8js-0.1.2/php_v8js_macros.h        2010-12-31 10:09:38.000000000 +0200
+++ v8js-0.1.2-rosmo/php_v8js_macros.h  2011-01-05 16:06:11.000000000 +0200
@@ -34,6 +34,7 @@
 #define V8JS_INT(v)                    v8::Integer::New(v)
 #define V8JS_FLOAT(v)          v8::Number::New(v)
 #define V8JS_BOOL(v)           v8::Boolean::New(v)
+#define V8JS_DATE(v)           v8::Date::New(v)
 #define V8JS_NULL                      v8::Null()
 #define V8JS_MN(name)          v8js_method_##name
 #define V8JS_METHOD(name)      v8::Handle<v8::Value> V8JS_MN(name)(const v8::Arguments& args)


Expected result:
----------------
string(31) "Wed, 05 Jan 2011 16:15:12 +0200"
object(DateTime)#3 (3) {
  ["date"]=>
  string(19) "2011-01-05 16:15:12"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+02:00"
}

Actual result:
--------------
string(31) "Wed, 05 Jan 2011 16:16:31 +0200"
object(V8Object)#3 (18) {
  ["createFromFormat"]=>
  object(V8Function)#4 (0) {
  }
  ["getLastErrors"]=>
  object(V8Function)#5 (0) {
  }
  ["format"]=>
  object(V8Function)#6 (0) {
  }
  ["modify"]=>
  object(V8Function)#7 (0) {
  }
  ["add"]=>
  object(V8Function)#8 (0) {
  }
  ["sub"]=>
  object(V8Function)#9 (0) {
  }
  ["getTimezone"]=>
  object(V8Function)#10 (0) {
  }
  ["setTimezone"]=>
  object(V8Function)#11 (0) {
  }
  ["getOffset"]=>
  object(V8Function)#12 (0) {
  }
  ["setTime"]=>
  object(V8Function)#13 (0) {
  }
  ["setDate"]=>
  object(V8Function)#14 (0) {
  }
  ["setISODate"]=>
  object(V8Function)#15 (0) {
  }
  ["setTimestamp"]=>
  object(V8Function)#16 (0) {
  }
  ["getTimestamp"]=>
  object(V8Function)#17 (0) {
  }
  ["diff"]=>
  object(V8Function)#18 (0) {
  }
  ["date"]=>
  string(19) "2011-01-05 16:16:31"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "Europe/Helsinki"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-20 20:00 UTC] stesie at brokenpipe dot de
This was integrated into master branch over at Github with
https://github.com/preillyme/v8js/commit/ddbef50ea6bb267b8ee856589bdc95c83d7e39e7

cheers
  ~stesie
 [2015-03-13 14:53 UTC] stesie@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: stesie
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Jan 21 17:01:24 2020 UTC