Skip to content

Commit

Permalink
fix: simplify hook_strtotime
Browse files Browse the repository at this point in the history
  • Loading branch information
t-matsuno-777 committed Sep 19, 2024
1 parent 0b9098a commit 49cbbe4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
platform: ['linux/amd64', 'linux/arm64/v8', 'linux/s390x']
version: ['8.1', '8.2', '8.3']
type: ['cli', 'zts']
distro: ['bookworm', 'alpine']
# platform: ['linux/amd64', 'linux/arm64/v8', 'linux/s390x']
platform: ['linux/amd64']
# version: ['8.1', '8.2', '8.3']
version: ['8.3']
# type: ['cli', 'zts']
type: ['cli']
# distro: ['bookworm', 'alpine']
distro: ['bookworm']
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
strategy:
matrix:
version: ['8.3']
type: ['cli', 'zts']
# type: ['cli', 'zts']
type: ['cli']
distro: ['bookworm']
outputs:
matrix: ${{ toJson(matrix) }}
Expand Down
28 changes: 9 additions & 19 deletions ext/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,25 +699,15 @@ static void hook_strtotime(INTERNAL_FUNCTION_PARAMETERS)
return;
}

if (is_fixed_ret == 2) {
CALL_ORIGINAL_FUNCTION(strtotime);
} else {
/* Call original function with params. */
zval *params = NULL;
uint32_t param_count = 0;
zend_parse_parameters(ZEND_NUM_ARGS(), "+", &params, &param_count);
ZVAL_LONG(&params[1], get_shifted_time(NULL));
CALL_ORIGINAL_FUNCTION_WITH_PARAMS(strtotime, params, param_count);
}

/* Apply interval. */
timelib_time *t = timelib_time_ctor();
timelib_rel_time interval;
timelib_unixtime2gmt(t, Z_LVAL_P(return_value));
get_shift_interval(&interval);
apply_interval(&t, &interval);
RETVAL_LONG(timelib_date_to_int(t, NULL));
timelib_time_dtor(t);
/* Call original function based on shifted time */
zval params[2];
uint32_t param_count = 0;
zend_parse_parameters(ZEND_NUM_ARGS(), "+", &params, &param_count);
ZVAL_STRING(&params[0], ZSTR_VAL(times));
ZVAL_LONG(&params[1], get_shifted_time(NULL));
CALL_ORIGINAL_FUNCTION_WITH_PARAMS(strtotime, params, 2);
zval_ptr_dtor(&params[0]);
zval_ptr_dtor(&params[1]);
}

#if HAVE_GETTIMEOFDAY
Expand Down
2 changes: 1 addition & 1 deletion ext/tests/functions/strtotime.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if ($before_now != $after_now && $before_fixed === $after_fixed && 4 > $interval
die('success');
}

die('failed');
die('failed, before_now = ' . $before_now . ', after_now = ' . $after_now . ', before_fixed = ' . $before_fixed . ', after_fixed = ' . $after_fixed . ', interval->days = ' . $interval->days . ', interval->invert = ' . $interval->invert);
?>
--EXPECT--
success
9 changes: 6 additions & 3 deletions ext/tests/functions/strtotime_extra.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ $after_two = new \DateTime('@' . \strtotime('now'));

$interval = $after_one->diff($before);

if ($before == $after_one || $before == $after_two) {
die('failed');
if ($before->getTimestamp() == $after_one->getTimestamp()) {
die('failed, $before == $after_one');
}
if ($before->getTimestamp() == $after_two->getTimestamp()) {
die('failed, $before == $after_two');
}

/* Note: Sometime valgrind makes flaky: $interval->y !== 2 */
if (($interval->y > 2 && $interval->y !== 0) || $interval->invert !== 0) {
die('failed');
die('failed, $interval->y = ' . $interval->y . ', $interval->invert = ' . $interval->invert);
}

die('success');
Expand Down

0 comments on commit 49cbbe4

Please sign in to comment.