Skip to content

Fix createFromFormat with Unix timestamp format 'U'#502

Merged
dereuromark merged 1 commit into3.xfrom
fix-createfromformat-unix-timestamp
Mar 14, 2026
Merged

Fix createFromFormat with Unix timestamp format 'U'#502
dereuromark merged 1 commit into3.xfrom
fix-createfromformat-unix-timestamp

Conversation

@dereuromark
Copy link
Member

Summary

The 3.3.2 fix for respecting testNow in createFromFormat (#494) incorrectly applied testNow values when using the Unix timestamp format 'U'. Since 'U' sets all date/time components from the Unix timestamp value, testNow should not be applied.

Problem

Chronos::setTestNow(new Chronos('2020-12-01 14:30:45'));
$d = Chronos::createFromFormat('U', '0');
// Expected: 1970-01-01 00:00:00 (Unix epoch)
// Actual (broken): 2020-12-01 14:30:45 (testNow value)

This broke CakePHP's test suite after 3.3.2 was released.

Fix

Add 'U' to the list of format specifiers that should skip the testNow application, similar to '!' and '|'.

Test

Added test cases for Unix timestamp format with testNow set.

The 3.3.2 fix for respecting testNow in createFromFormat
incorrectly applied testNow values when using the Unix timestamp
format 'U'. Since 'U' sets all date/time components from the
Unix timestamp value, testNow should not be applied.

This caused createFromFormat('U', '0') to return the current
test time instead of the Unix epoch (1970-01-01 00:00:00).

The fix adds 'U' to the list of format specifiers that should
skip the testNow application, similar to '!' and '|'.
@dereuromark dereuromark merged commit 77a248b into 3.x Mar 14, 2026
6 checks passed
@dereuromark dereuromark deleted the fix-createfromformat-unix-timestamp branch March 14, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants