Skip to content

Commit

Permalink
override DateTime::createFromFormat(), date_create_from_format()
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Nov 6, 2013
1 parent 61c2753 commit 004368a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions php_timecop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion tests/overload_13.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,11 +59,12 @@ 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"
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"
string(25) "2012-02-29T18:23:45+09:00"
56 changes: 56 additions & 0 deletions tests/overload_15.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
Function overrideing test for date_create_from_format
--SKIPIF--
<?php
extension_loaded('timecop') or die('skip timecop not available');
$required_func = array("date_create_from_format");
foreach ($required_func as $func_name) {
if (!function_exists($func_name)) {
die("skip $func_name() function is not available.");
}
}
$required_class = array("datetime");
foreach ($required_class as $class_name) {
if (!class_exists($class_name)) {
die("skip $class_name class is not available.");
}
}
--INI--
date.timezone=America/Los_Angeles
--FILE--
<?php

// checking class name of instance
$dt0 = new DateTime();
var_dump(get_class($dt0));

$dts = array(
// constuctor with 2 argument(absolute format)
date_create_from_format("Y-m-d H:i:s", "2012-03-31 12:34:56"),

// constuctor with 2 argument(including timezone info)
date_create_from_format("Y-m-d H:i:s T", "1970-01-01 19:00:00 EST"),

// constuctor with 2 argument(unix time)
date_create_from_format("U", "86400"),

// constuctor with 3 argument
date_create_from_format("Y-m-d H:i:s", "2012-04-01 00:00:00", new DateTimezone("Asia/Tokyo")),

);

foreach ($dts as $dt) {
var_dump($dt->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"
33 changes: 33 additions & 0 deletions timecop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};
/* }}} */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}
};
/* }}} */
Expand All @@ -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)
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 004368a

Please sign in to comment.