better datetime handling
authorMichael Wallner <mike@php.net>
Wed, 24 Apr 2013 15:45:52 +0000 (17:45 +0200)
committerMichael Wallner <mike@php.net>
Wed, 24 Apr 2013 15:45:52 +0000 (17:45 +0200)
src/php_pq_misc.c
src/php_pq_misc.h
src/php_pq_module.c
src/php_pqres.c

index 943aa57ffce27eaee0791adf0cc89b4b8196d28e..8c7ec6b241bc988cb667ce938c8efd6fc9183a3f 100644 (file)
 
 #include <php.h>
 #include <ext/date/php_date.h>
+#if defined(HAVE_JSON) && !defined(COMPILE_DL_JSON)
+#      include <ext/json/php_json.h>
+#endif
+
+#include <Zend/zend_interfaces.h>
+
 #include <libpq/libpq-fs.h>
 
 #include "php_pq.h"
@@ -174,7 +180,26 @@ Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
 }
 */
 
-zval *php_pq_date_from_string(char *datetime_str, size_t datetime_len, zval *zv TSRMLS_DC)
+zend_class_entry *php_pqdt_class_entry;
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(pqdt, __toString)
+{
+       zval *rv;
+
+       zend_call_method_with_1_params(&getThis(), php_pqdt_class_entry, NULL, "format", &rv,
+                       zend_read_property(php_pqdt_class_entry, getThis(), ZEND_STRL("format"), 0 TSRMLS_CC));
+       RETVAL_ZVAL(rv, 1, 1);
+}
+
+static zend_function_entry php_pqdt_methods[] = {
+       PHP_ME(pqdt, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
+       PHP_MALIAS(pqdt, jsonSerialize, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
+       {0}
+};
+
+zval *php_pqdt_from_string(char *dt_str, size_t dt_len, char *fmt, zval *zv TSRMLS_DC)
 {
        php_date_obj *dobj;
 
@@ -182,16 +207,35 @@ zval *php_pq_date_from_string(char *datetime_str, size_t datetime_len, zval *zv
                MAKE_STD_ZVAL(zv);
        }
 
-       php_date_instantiate(php_date_get_date_ce(), zv TSRMLS_CC);
+       php_date_instantiate(php_pqdt_class_entry, zv TSRMLS_CC);
        dobj = zend_object_store_get_object(zv TSRMLS_CC);
-       if (!php_date_initialize(dobj, datetime_str, datetime_len, NULL, NULL, 1 TSRMLS_CC)) {
+       if (!php_date_initialize(dobj, dt_str, dt_len, NULL, NULL, 1 TSRMLS_CC)) {
                zval_dtor(zv);
                ZVAL_NULL(zv);
+       } else if (fmt) {
+               zend_update_property_string(php_pqdt_class_entry, zv, ZEND_STRL("format"), fmt TSRMLS_CC);
        }
 
        return zv;
 }
 
+PHP_MINIT_FUNCTION(pq_misc)
+{
+       zend_class_entry **json, ce = {0};
+
+       INIT_NS_CLASS_ENTRY(ce ,"pq", "DateTime", php_pqdt_methods);
+       php_pqdt_class_entry = zend_register_internal_class_ex(&ce, php_date_get_date_ce(), "DateTime" TSRMLS_CC);
+
+       zend_declare_property_stringl(php_pqdt_class_entry, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.u"), ZEND_ACC_PUBLIC TSRMLS_CC);
+
+       /* stop reading this file right here! */
+       if (SUCCESS == zend_hash_find(CG(class_table), ZEND_STRS("jsonserializable"), (void *) &json)) {
+               zend_class_implements(php_pqdt_class_entry TSRMLS_CC, 1, *json);
+       }
+
+       return SUCCESS;
+}
+
 /*
  * Local variables:
  * tab-width: 4
index 3ce172d666d20083e08f79facd6e60409f9f0ed6..3bb1702a236f582c9ab5e353b7c803fba95d8fd0 100644 (file)
@@ -33,7 +33,10 @@ int compare_index(const void *lptr, const void *rptr TSRMLS_DC);
 int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC);
 int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC);
 
-zval *php_pq_date_from_string(char *datetime_str, size_t datetime_len, zval *zv TSRMLS_DC);
+zend_class_entry *php_pqdt_class_entry;
+zval *php_pqdt_from_string(char *datetime_str, size_t datetime_len, char *fmt, zval *zv TSRMLS_DC);
+
+PHP_MINIT_FUNCTION(pq_misc);
 
 #endif
 
index 12a6e4b110fec42f039b78531e5123a812eb92a9..02cd6dbc8ccc89dee746abf9023b4319acf33bd7 100644 (file)
@@ -52,6 +52,7 @@
 
 static PHP_MINIT_FUNCTION(pq)
 {
+       PHP_MINIT_CALL(pq_misc);
        PHP_MINIT_CALL(pqexc);
 
        PHP_MINIT_CALL(pqconn);
index 75355154acc971ed4aa79ffc1f767707ddef1d24..03317113f01ba68e35cc3b00f087321402ccf343 100644 (file)
@@ -155,11 +155,20 @@ zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch
                                        ZVAL_DOUBLE(zv, zend_strtod(val, NULL));
                                        break;
 
-                               case PHP_PQ_OID_ABSTIME:
                                case PHP_PQ_OID_DATE:
+                                       php_pqdt_from_string(val, len, "Y-m-d", zv TSRMLS_CC);
+                                       break;
+
+                               case PHP_PQ_OID_ABSTIME:
+                                       php_pqdt_from_string(val, len, "Y-m-d H:i:s", zv TSRMLS_CC);
+                                       break;
+
                                case PHP_PQ_OID_TIMESTAMP:
+                                       php_pqdt_from_string(val, len, "Y-m-d H:i:s.u", zv TSRMLS_CC);
+                                       break;
+
                                case PHP_PQ_OID_TIMESTAMPTZ:
-                                       php_pq_date_from_string(val, len, zv TSRMLS_CC);
+                                       php_pqdt_from_string(val, len, "Y-m-d H:i:s.uO", zv TSRMLS_CC);
                                        break;