diff --git a/php_timecop.h b/php_timecop.h index e75af7c..5cbf23f 100644 --- a/php_timecop.h +++ b/php_timecop.h @@ -63,6 +63,7 @@ PHP_FUNCTION(timecop_strftime); PHP_FUNCTION(timecop_gmstrftime); PHP_FUNCTION(timecop_unixtojd); PHP_FUNCTION(timecop_date_create); +PHP_FUNCTION(timecop_date_create_from_format); PHP_METHOD(TimecopDateTime, __construct); diff --git a/tests/overload_13.phpt b/tests/overload_13.phpt index 9e60c79..fdc38a0 100644 --- a/tests/overload_13.phpt +++ b/tests/overload_13.phpt @@ -24,6 +24,8 @@ timecop_freeze(timecop_orig_strtotime("2012-02-29 01:23:45")); // checking class name of instance $dt0 = new DateTime(); var_dump(get_class($dt0)); +$dt1 = DateTime::createFromFormat('U', 0); +var_dump(get_class($dt1)); $dts = array( // constuctor with 0 argument @@ -57,6 +59,7 @@ foreach ($dts as $dt) { --EXPECT-- string(15) "TimecopDateTime" +string(15) "TimecopDateTime" string(25) "2012-02-29T01:23:45-08:00" string(25) "2012-02-29T01:23:45-08:00" string(25) "2012-02-29T01:23:45-08:00" @@ -64,4 +67,4 @@ string(25) "2012-03-31T12:34:56-07:00" string(25) "2012-03-03T01:23:45-08:00" string(25) "1970-01-01T19:00:00-05:00" string(25) "1970-01-02T00:00:00+00:00" -string(25) "2012-02-29T18:23:45+09:00" \ No newline at end of file +string(25) "2012-02-29T18:23:45+09:00" diff --git a/tests/overload_15.phpt b/tests/overload_15.phpt new file mode 100644 index 0000000..543f6c3 --- /dev/null +++ b/tests/overload_15.phpt @@ -0,0 +1,56 @@ +--TEST-- +Function overrideing test for date_create_from_format +--SKIPIF-- +format("c")); + var_dump($dt->getTimezone()->getName()); +} + +--EXPECT-- +string(15) "TimecopDateTime" +string(25) "2012-03-31T12:34:56-07:00" +string(19) "America/Los_Angeles" +string(25) "1970-01-01T22:00:00-05:00" +string(3) "EST" +string(25) "1970-01-02T08:00:00+00:00" +string(6) "+00:00" +string(25) "2012-04-01T16:00:00+09:00" +string(10) "Asia/Tokyo" diff --git a/timecop.c b/timecop.c index 3f18faa..206a5b8 100644 --- a/timecop.c +++ b/timecop.c @@ -66,6 +66,7 @@ static const struct timecop_override_def timecop_ovld_func[] = { {"gmstrftime", "timecop_gmstrftime", "timecop_orig_gmstrftime"}, {"unixtojd", "timecop_unixtojd", "timecop_orig_unixtojd"}, {"date_create", "timecop_date_create", "timecop_orig_date_create"}, + {"date_create_from_format", "timecop_date_create_from_format", "timecop_orig_date_create_from_format"}, {NULL, NULL, NULL} }; /* }}} */ @@ -157,6 +158,12 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_timecop_date_create, 0, 0, 0) ZEND_ARG_INFO(0, object) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_timecop_date_create_from_format, 0, 0, 2) +ZEND_ARG_INFO(0, format) +ZEND_ARG_INFO(0, time) +ZEND_ARG_INFO(0, object) +ZEND_END_ARG_INFO() + #if !defined(PHP_VERSION_ID) || PHP_VERSION_ID < 50300 ZEND_BEGIN_ARG_INFO_EX(arginfo_timecop_date_method_timestamp_set, 0, 0, 1) ZEND_ARG_INFO(0, unixtimestamp) @@ -184,6 +191,7 @@ const zend_function_entry timecop_functions[] = { PHP_FE(timecop_gmstrftime, arginfo_timecop_gmstrftime) PHP_FE(timecop_unixtojd, arginfo_timecop_unixtojd) PHP_FE(timecop_date_create, arginfo_timecop_date_create) + PHP_FE(timecop_date_create_from_format, arginfo_timecop_date_create_from_format) {NULL, NULL, NULL} }; /* }}} */ @@ -194,6 +202,8 @@ const zend_function_entry timecop_functions[] = { static zend_function_entry timecop_datetime_class_functions[] = { PHP_ME(TimecopDateTime, __construct, arginfo_timecop_date_create, ZEND_ACC_CTOR | ZEND_ACC_PUBLIC) + PHP_ME_MAPPING(createFromFormat, timecop_date_create_from_format, arginfo_timecop_date_create_from_format, + ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) #if !defined(PHP_VERSION_ID) || PHP_VERSION_ID < 50300 PHP_ME(TimecopDateTime, getTimestamp, arginfo_timecop_date_method_timestamp_get, 0) @@ -881,6 +891,29 @@ PHP_FUNCTION(timecop_date_create) } /* }}} */ +/* {{{ proto TimecopDateTime timecop_date_create_from_format(string format, string time[, DateTimeZone object]) + Returns new TimecopDateTime object + */ +PHP_FUNCTION(timecop_date_create_from_format) +{ + zval *datetime_obj, *time = NULL, *format = NULL, *timezone; + zend_class_entry *ce; + char *format_string = "Y-m-d H:i:s"; + + MAKE_STD_ZVAL(format); + ZVAL_STRING(format, format_string, 0); + + _timecop_call_function(INTERNAL_FUNCTION_PARAM_PASSTHRU, ORIG_FUNC_NAME("date_create_from_format"), &datetime_obj, 0); + zend_call_method_with_1_params(&datetime_obj, NULL, NULL, "format", &time, format); + zend_call_method_with_0_params(&datetime_obj, NULL, NULL, "getTimezone", &timezone); + + php_timecop_date_instantiate(TIMECOP_G(ce_TimecopDateTime), return_value TSRMLS_CC); + /* call TimecopDateTime::__constuctor() */ + ce = TIMECOP_G(ce_TimecopDateTime); + zend_call_method_with_2_params(&return_value, ce, &ce->constructor, "__construct", NULL, time, timezone); +} +/* }}} */ + /* {{{ proto TimecopDateTime::__construct([string time[, DateTimeZone object]]) Creates new TimecopDateTime object */