Skip to content

Commit

Permalink
Simplify timecop_date_create_from_format() / Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hnw committed Apr 29, 2017
1 parent f23313a commit d512546
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
2 changes: 2 additions & 0 deletions tests/date_override_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $tests_args = array(
array("Y-m-d H:i:s T", "1970-01-01 19:00:00 EST"),
array("U", "86400"),
array("Y-m-d H:i:s", "2012-04-01 00:00:00", new DateTimezone("Asia/Tokyo")),
array("m-d|", "03-31"),
);

$dt0 = date_create_from_format("Y-m-d H:i:s.u", "2010-01-02 03:04:05.678000");
Expand Down Expand Up @@ -65,3 +66,4 @@ string\(32\) "1970-01-01 12:34:56\.000000-08:00"
string\(32\) "1970-01-01 19:00:00\.000000-05:00"
string\(32\) "1970-01-02 00:00:00\.000000\+00:00"
string\(32\) "2012-04-01 00:00:00\.000000\+09:00"
string\(32\) "1970-03-31 00:00:00\.000000\-08:00"
4 changes: 2 additions & 2 deletions tests/date_override_008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $tests_args = array(
array("Y-m-d H:i:s T", "1970-01-01 19:00:00 EST"),
array("U", "86400"),
array("Y-m-d H:i:s", "2012-04-01 00:00:00", new DateTimezone("Asia/Tokyo")),
array("Y-m-d|", "2012-03-31"),
array("m-d|", "03-31"),
);

$dt0 = DateTime::createFromFormat("Y-m-d H:i:s.u", "2010-01-02 03:04:05.678000");
Expand Down Expand Up @@ -67,4 +67,4 @@ string\(32\) "1970-01-01 12:34:56\.000000-08:00"
string\(32\) "1970-01-01 19:00:00\.000000-05:00"
string\(32\) "1970-01-02 00:00:00\.000000\+00:00"
string\(32\) "2012-04-01 00:00:00\.000000\+09:00"
string\(32\) "2012-03-31 00:00:00\.000000\-07:00"
string\(32\) "1970-03-31 00:00:00\.000000\-08:00"
2 changes: 2 additions & 0 deletions tests/immutable_override_007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $tests_args = array(
array("Y-m-d H:i:s T", "1970-01-01 19:00:00 EST"),
array("U", "86400"),
array("Y-m-d H:i:s", "2012-04-01 00:00:00", new DateTimezone("Asia/Tokyo")),
array("m-d|", "03-31"),
);

$dt0 = date_create_immutable_from_format("Y-m-d H:i:s.u", "2010-01-02 03:04:05.678");
Expand Down Expand Up @@ -65,3 +66,4 @@ string\(32\) "1970-01-01 12:34:56\.000000-08:00"
string\(32\) "1970-01-01 19:00:00\.000000-05:00"
string\(32\) "1970-01-02 00:00:00\.000000\+00:00"
string\(32\) "2012-04-01 00:00:00\.000000\+09:00"
string\(32\) "1970-03-31 00:00:00\.000000\-08:00"
2 changes: 2 additions & 0 deletions tests/immutable_override_008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $tests_args = array(
array("Y-m-d H:i:s T", "1970-01-01 19:00:00 EST"),
array("U", "86400"),
array("Y-m-d H:i:s", "2012-04-01 00:00:00", new DateTimezone("Asia/Tokyo")),
array("m-d|", "03-31"),
);

$dt0 = DateTimeImmutable::createFromFormat("Y-m-d H:i:s.u", "2010-01-02 03:04:05.678");
Expand Down Expand Up @@ -66,3 +67,4 @@ string\(32\) "1970-01-01 12:34:56\.000000-08:00"
string\(32\) "1970-01-01 19:00:00\.000000-05:00"
string\(32\) "1970-01-02 00:00:00\.000000\+00:00"
string\(32\) "2012-04-01 00:00:00\.000000\+09:00"
string\(32\) "1970-03-31 00:00:00\.000000\-08:00"
19 changes: 8 additions & 11 deletions timecop_php5.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,9 +1498,8 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
RETURN_FALSE;
}

if (memchr(orig_format_str, '|', orig_format_len)) {
zval_ptr_dtor(&orig_format);
zval_ptr_dtor(&orig_time);
if (memchr(orig_format_str, '!', orig_format_len) ||
memchr(orig_format_str, '|', orig_format_len)) {
RETURN_ZVAL(dt, 1, 1);
}

Expand All @@ -1515,14 +1514,12 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "format", &fixed_time, &tmp);

INIT_ZVAL(fixed_format);
if (memchr(orig_format_str, '!', orig_format_len)) {
ZVAL_STRING(&fixed_format, "???\?-?\?-?? ??:??:??.??????", 0);
} else if (memchr(orig_format_str, 'g', orig_format_len) ||
memchr(orig_format_str, 'h', orig_format_len) ||
memchr(orig_format_str, 'G', orig_format_len) ||
memchr(orig_format_str, 'H', orig_format_len) ||
memchr(orig_format_str, 'i', orig_format_len) ||
memchr(orig_format_str, 's', orig_format_len)) {
if (memchr(orig_format_str, 'g', orig_format_len) ||
memchr(orig_format_str, 'h', orig_format_len) ||
memchr(orig_format_str, 'G', orig_format_len) ||
memchr(orig_format_str, 'H', orig_format_len) ||
memchr(orig_format_str, 'i', orig_format_len) ||
memchr(orig_format_str, 's', orig_format_len)) {
ZVAL_STRING(&fixed_format, "Y-m-d ??:??:??.??????", 0);
} else if (memchr(orig_format_str, 'Y', orig_format_len) ||
memchr(orig_format_str, 'y', orig_format_len) ||
Expand Down
17 changes: 8 additions & 9 deletions timecop_php7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,8 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
RETURN_FALSE;
}

if (memchr(orig_format_str, '|', orig_format_len)) {
if (memchr(orig_format_str, '!', orig_format_len) ||
memchr(orig_format_str, '|', orig_format_len)) {
zval_ptr_dtor(&orig_format);
zval_ptr_dtor(&orig_time);
RETURN_ZVAL(&dt, 1, 1);
Expand All @@ -1425,14 +1426,12 @@ static void _timecop_date_create_from_format(INTERNAL_FUNCTION_PARAMETERS, int i
call_php_method_with_1_params(&dt, TIMECOP_G(ce_DateTime), "format", &fixed_time, &tmp);
zval_ptr_dtor(&tmp);

if (memchr(orig_format_str, '!', orig_format_len)) {
ZVAL_STRING(&fixed_format, "???\?-?\?-?? ??:??:??.??????");
} else if (memchr(orig_format_str, 'g', orig_format_len) ||
memchr(orig_format_str, 'h', orig_format_len) ||
memchr(orig_format_str, 'G', orig_format_len) ||
memchr(orig_format_str, 'H', orig_format_len) ||
memchr(orig_format_str, 'i', orig_format_len) ||
memchr(orig_format_str, 's', orig_format_len)) {
if (memchr(orig_format_str, 'g', orig_format_len) ||
memchr(orig_format_str, 'h', orig_format_len) ||
memchr(orig_format_str, 'G', orig_format_len) ||
memchr(orig_format_str, 'H', orig_format_len) ||
memchr(orig_format_str, 'i', orig_format_len) ||
memchr(orig_format_str, 's', orig_format_len)) {
ZVAL_STRING(&fixed_format, "Y-m-d ??:??:??.??????");
} else if (memchr(orig_format_str, 'Y', orig_format_len) ||
memchr(orig_format_str, 'y', orig_format_len) ||
Expand Down

0 comments on commit d512546

Please sign in to comment.