Fix contructors when TZ is set #43
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thank you for Timecop, it really makes testing as thorough as can be!
The Problem:
When Timecop was active and
new DateTime('2026-01-30', new DateTimeZone('UTC'))(orDateTimeImmutable) was called with a date, the time was incorrectly interpreted as in the default timezone (e.g.,Europe/Paris) and then converted to UTC, resulting in2026-01-29 23:00:00 UTCinstead of the correct2026-01-30 00:00:00 UTC.The Fix (in
timecop.c):Modified the
get_formatted_mock_time()function to temporarily set the default timezone to the provided timezone before callingstrtotime(), then restore the original default timezone afterwards. This ensures date strings without explicit time information are correctly interpreted in the specified timezone. The fix also handles edge cases where offset-based timezones (like+00:00) are used, skipping the timezone switch for those sincedate_default_timezone_set()doesn't accept them.Files Changed:
timecop.c- Core fix inget_formatted_mock_time()functiontests/issue_timezone_datetime.phpt- New test case for this fixNote : the problem still exists when using offset-based timezones. (
+3:00etc.)Minimal PHP test to see the problem:
Output :