From 18f055b7913f9bba7b93b8a23cbf81d31567b74e Mon Sep 17 00:00:00 2001
From: Thomas Rothe
Date: Sun, 10 Jun 2012 22:01:43 +0200
Subject: [PATCH] Request #41187 DateTime::setDefaultFormat and __toString()
- added static protected DateTime::$defaultFormat
- added bool DateTime::setDefaultFormat($format)
- added string DateTime::getDefaultFormat()
- added DateTime::__toString()
- DateTime::$defaultFormat supports LSB
---
ext/date/php_date.c | 77 ++++++++++++++++
ext/date/php_date.h | 3 +
ext/date/tests/DateTime_toString.phpt | 15 ++++
.../tests/DateTime_toString_inheritance.phpt | 20 +++++
ext/date/tests/DateTime_verify.phpt | 89 ++++++++++++-------
5 files changed, 170 insertions(+), 34 deletions(-)
create mode 100644 ext/date/tests/DateTime_toString.phpt
create mode 100644 ext/date/tests/DateTime_toString_inheritance.phpt
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index cd48de2731bdb..1b48628272b34 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -362,6 +362,10 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_date_interval_construct, 0, 0, 0)
ZEND_ARG_INFO(0, interval_spec)
ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_date_set_default_format, 0, 0, 1)
+ ZEND_ARG_INFO(0, format)
+ZEND_END_ARG_INFO();
/* }}} */
/* {{{ Function table */
@@ -432,6 +436,9 @@ const zend_function_entry date_funcs_date[] = {
PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC)
PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+ PHP_ME(DateTime, __toString, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(DateTime, setDefaultFormat, arginfo_date_set_default_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+ PHP_ME(DateTime, getDefaultFormat, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(format, date_format, arginfo_date_method_format, 0)
@@ -1899,6 +1906,12 @@ static void date_register_classes(TSRMLS_D)
REGISTER_DATE_CLASS_CONST_STRING("RSS", DATE_FORMAT_RFC1123);
REGISTER_DATE_CLASS_CONST_STRING("W3C", DATE_FORMAT_RFC3339);
+#define REGISTER_DATE_CLASS_PROPERTY_PROTECTED_STRING(const_name, value, access_type ) \
+ zend_declare_property_string(date_ce_date, const_name, sizeof(const_name)-1, value, access_type)
+
+ REGISTER_DATE_CLASS_PROPERTY_PROTECTED_STRING("defaultFormat", DATE_FORMAT_ISO8601,
+ ZEND_ACC_STATIC|ZEND_ACC_PROTECTED TSRMLS_DC);
+
INIT_CLASS_ENTRY(ce_timezone, "DateTimeZone", date_funcs_timezone);
ce_timezone.create_object = date_object_new_timezone;
@@ -2535,6 +2548,70 @@ PHP_METHOD(DateTime, __wakeup)
php_date_initialize_from_hash(&return_value, &dateobj, myht TSRMLS_CC);
}
/* }}} */
+/* {{{ proto DateTime::__toString()
+ */PHP_METHOD(DateTime, __toString) {
+ php_date_obj *dateobj;
+ zval *format;
+ zval *object = getThis();
+
+ char *property = "defaultFormat";
+ zend_class_entry* ce = 0L;
+
+ if (EG(called_scope)) {
+ ce = EG(called_scope);
+ } else if (!EG(scope)) {
+ ce = EG(scope);
+ }
+
+ dateobj = (php_date_obj *) zend_object_store_get_object(object TSRMLS_CC);
+ DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
+
+ format = zend_read_static_property(ce, property, strlen(property), 0);
+
+
+ RETURN_STRING(date_format(Z_STRVAL_P(format), Z_STRLEN_P(format), dateobj->time, dateobj->time->is_localtime),0);
+}
+/* }}} */
+
+/* {{{ proto DateTime::setDefaultFormat(string format)
+ */PHP_METHOD(DateTime, setDefaultFormat) {
+ zval *object = getThis();
+ char *property = "defaultFormat";
+ zend_class_entry* ce = 0L;
+
+ char *format;
+ int format_len;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &format, &format_len) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (EG(called_scope)) {
+ ce = EG(called_scope);
+ } else if (!EG(scope)) {
+ ce = EG(scope);
+ }
+ zend_update_static_property_stringl(ce, property, strlen(property), format, format_len);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto DateTime::getDefaultFormat()
+ */PHP_METHOD(DateTime, getDefaultFormat) {
+ zval *object = getThis();
+ char *property = "defaultFormat";
+ zend_class_entry* ce = 0L;
+ zval *format;
+
+ if (EG(called_scope)) {
+ ce = EG(called_scope);
+ } else if (!EG(scope)) {
+ ce = EG(scope);
+ }
+ format = zend_read_static_property(ce, property, strlen(property), 0);
+ RETURN_STRINGL(Z_STRVAL_P(format), Z_STRLEN_P(format), 1);
+}
+/* }}} */
/* Helper function used to add an associative array of warnings and errors to a zval */
static void zval_from_error_container(zval *z, timelib_error_container *error)
diff --git a/ext/date/php_date.h b/ext/date/php_date.h
index b2f4e9fd27f39..35a797d213e34 100644
--- a/ext/date/php_date.h
+++ b/ext/date/php_date.h
@@ -50,6 +50,9 @@ PHP_FUNCTION(getdate);
PHP_METHOD(DateTime, __construct);
PHP_METHOD(DateTime, __wakeup);
PHP_METHOD(DateTime, __set_state);
+PHP_METHOD(DateTime, __toString);
+PHP_METHOD(DateTime, setDefaultFormat);
+PHP_METHOD(DateTime, getDefaultFormat);
PHP_FUNCTION(date_create);
PHP_FUNCTION(date_create_from_format);
PHP_FUNCTION(date_parse);
diff --git a/ext/date/tests/DateTime_toString.phpt b/ext/date/tests/DateTime_toString.phpt
new file mode 100644
index 0000000000000..b5f03fb9a1922
--- /dev/null
+++ b/ext/date/tests/DateTime_toString.phpt
@@ -0,0 +1,15 @@
+--TEST--
+DateTime::__toString() -- Inheritance
+--CREDITS--
+Thomas Rothe
+--FILE--
+
+--EXPECT--
+07.06.2012 22:18
+d.m.Y H:i
diff --git a/ext/date/tests/DateTime_toString_inheritance.phpt b/ext/date/tests/DateTime_toString_inheritance.phpt
new file mode 100644
index 0000000000000..5a59d22792873
--- /dev/null
+++ b/ext/date/tests/DateTime_toString_inheritance.phpt
@@ -0,0 +1,20 @@
+--TEST--
+DateTime::__toString()
+--CREDITS--
+Thomas Rothe
+--FILE--
+
+--EXPECT--
+07.06.2012
+d.m.Y
+Y-m-d\TH:i:sO
\ No newline at end of file
diff --git a/ext/date/tests/DateTime_verify.phpt b/ext/date/tests/DateTime_verify.phpt
index a03911f5c8298..9e18506009a29 100644
--- a/ext/date/tests/DateTime_verify.phpt
+++ b/ext/date/tests/DateTime_verify.phpt
@@ -22,133 +22,154 @@ var_dump($constants);
--EXPECTF--
*** Verify DateTime class ***
Verify DateTime class registered OK
-object(ReflectionClass)#%d (1) {
+object(ReflectionClass)#1 (1) {
["name"]=>
string(8) "DateTime"
}
..and get names of all its methods
-array(18) {
+array(21) {
[0]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#2 (2) {
["name"]=>
string(11) "__construct"
["class"]=>
string(8) "DateTime"
}
[1]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#3 (2) {
["name"]=>
string(8) "__wakeup"
["class"]=>
string(8) "DateTime"
}
[2]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#4 (2) {
["name"]=>
string(11) "__set_state"
["class"]=>
string(8) "DateTime"
}
[3]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#5 (2) {
["name"]=>
- string(16) "createFromFormat"
+ string(10) "__toString"
["class"]=>
string(8) "DateTime"
}
[4]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#6 (2) {
["name"]=>
- string(13) "getLastErrors"
+ string(16) "setDefaultFormat"
["class"]=>
string(8) "DateTime"
}
[5]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#7 (2) {
["name"]=>
- string(6) "format"
+ string(16) "getDefaultFormat"
["class"]=>
string(8) "DateTime"
}
[6]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#8 (2) {
["name"]=>
- string(6) "modify"
+ string(16) "createFromFormat"
["class"]=>
string(8) "DateTime"
}
[7]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#9 (2) {
["name"]=>
- string(3) "add"
+ string(13) "getLastErrors"
["class"]=>
string(8) "DateTime"
}
[8]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#10 (2) {
["name"]=>
- string(3) "sub"
+ string(6) "format"
["class"]=>
string(8) "DateTime"
}
[9]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#11 (2) {
["name"]=>
- string(11) "getTimezone"
+ string(6) "modify"
["class"]=>
string(8) "DateTime"
}
[10]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#12 (2) {
["name"]=>
- string(11) "setTimezone"
+ string(3) "add"
["class"]=>
string(8) "DateTime"
}
[11]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#13 (2) {
["name"]=>
- string(9) "getOffset"
+ string(3) "sub"
["class"]=>
string(8) "DateTime"
}
[12]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#14 (2) {
["name"]=>
- string(7) "setTime"
+ string(11) "getTimezone"
["class"]=>
string(8) "DateTime"
}
[13]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#15 (2) {
["name"]=>
- string(7) "setDate"
+ string(11) "setTimezone"
["class"]=>
string(8) "DateTime"
}
[14]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#16 (2) {
["name"]=>
- string(10) "setISODate"
+ string(9) "getOffset"
["class"]=>
string(8) "DateTime"
}
[15]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#17 (2) {
["name"]=>
- string(12) "setTimestamp"
+ string(7) "setTime"
["class"]=>
string(8) "DateTime"
}
[16]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#18 (2) {
["name"]=>
- string(12) "getTimestamp"
+ string(7) "setDate"
["class"]=>
string(8) "DateTime"
}
[17]=>
- &object(ReflectionMethod)#%d (2) {
+ &object(ReflectionMethod)#19 (2) {
+ ["name"]=>
+ string(10) "setISODate"
+ ["class"]=>
+ string(8) "DateTime"
+ }
+ [18]=>
+ &object(ReflectionMethod)#20 (2) {
+ ["name"]=>
+ string(12) "setTimestamp"
+ ["class"]=>
+ string(8) "DateTime"
+ }
+ [19]=>
+ &object(ReflectionMethod)#21 (2) {
+ ["name"]=>
+ string(12) "getTimestamp"
+ ["class"]=>
+ string(8) "DateTime"
+ }
+ [20]=>
+ &object(ReflectionMethod)#22 (2) {
["name"]=>
string(4) "diff"
["class"]=>