fix: preserve sub-second precision in BaseModel datetime timestamps - #10541
fix: preserve sub-second precision in BaseModel datetime timestamps#10541wakqasahmed wants to merge 2 commits into
Conversation
|
Hi there, wakqasahmed! 👋 Thank you for sending this PR! We expect the following in all Pull Requests (PRs).
Important We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works. If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md Sincerely, the mergeable bot 🤖 |
|
Fixed the PHP CS Fixer failures (data provider naming/ordering convention — pushed a follow-up commit matching exactly what The "Check Signed Commit" failure is a real gap on my end unrelated to this fix — my commits are SSH-signed locally but the signing key isn't yet registered on my GitHub account for verification, so they show as unverified. Working on getting that registered; will follow up once resolved. |
michalsn
left a comment
There was a problem hiding this comment.
This looks good, thanks!
But to merge this, we need two things: signed commits and a changelog entry. The mergeable bot has already provided you with the links you can follow to fix this.
If you hit a wall with any of these, feel free to write here - we will try to help.
Description
When a model's
\$dateFormatis'datetime'and the connection'sdateFormat['datetime']mask includes.v/.u(millisecond/microsecond precision), auto-generated timestamps forcreated_at/updated_atalways rendered.000000instead of the real sub-second value.setDate()converted the currentTimeinstance to a Unix timestamp integer (Time::now()->getTimestamp()) before formatting it, which discards anything below whole-second precision.timeToDate()already formats aTimeobject directly and correctly retains sub-second precision (TimeextendsDateTimeImmutable), but nothing routed the "no explicit date passed" case through it.Changed
setDate()so that when no explicit\$userDateis given and the model's format is'datetime', it callstimeToDate(Time::now())instead of going throughintToDate(). The'int'and'date'formats, and the case where an explicit integer timestamp is passed, are unchanged — they still go throughintToDate()since an int timestamp has no sub-second data to preserve either way.Added a test in
GeneralModelTestthat pins the current time to a value with microsecond precision and asserts both.v(milliseconds) and.u(microseconds) format masks render correctly.PHP isn't available in the sandbox I used, so I traced this by hand against
intToDate()/timeToDate()'s actualmatcharms rather than running the suite — the command to verify isvendor/bin/phpunit --no-coverage --filter testCurrentTimestampPreservesSubseconds tests/system/Models/GeneralModelTest.php.Fixes #10540
Checklist: