Skip to content

fix: preserve sub-second precision in BaseModel datetime timestamps - #10561

Open
wakqasahmed wants to merge 1 commit into
codeigniter4:developfrom
wakqasahmed:fix/basemodel-setdate-microseconds-v2
Open

wakqasahmed wants to merge 1 commit into
codeigniter4:developfrom
wakqasahmed:fix/basemodel-setdate-microseconds-v2

Conversation

@wakqasahmed

Copy link
Copy Markdown

Replaces #10541, which I can't update in place — the branch there has two commits with the wrong git identity from an earlier session, and this environment blocks both rewriting that history and force-pushing over it, even to my own fork. Same diff, plus the changelog entry @michalsn asked for.


When a model's $dateFormat is 'datetime' and the connection's dateFormat['datetime'] mask includes .v/.u (millisecond/microsecond precision), auto-generated timestamps for created_at/updated_at always rendered .000000 instead of the real sub-second value.

setDate() converted the current Time instance to a Unix timestamp integer (Time::now()->getTimestamp()) before formatting it, which discards anything below whole-second precision. timeToDate() already formats a Time object directly and correctly retains sub-second precision (Time extends DateTimeImmutable), but nothing routed the "no explicit date passed" case through it.

Changed setDate() so that when no explicit $userDate is given and the model's format is 'datetime', it calls timeToDate(Time::now()) instead of going through intToDate(). The 'int' and 'date' formats, and the case where an explicit integer timestamp is passed, are unchanged.

Added a test in GeneralModelTest that pins the current time to a value with microsecond precision and asserts both .v and .u format masks render correctly, plus the changelog entry in v4.7.5.rst.

Fixes #10540

When a model's $dateFormat is 'datetime' and the connection's
dateFormat['datetime'] mask includes .v/.u (millisecond/microsecond
precision), auto-generated created_at/updated_at timestamps always
rendered .000000 instead of the real sub-second value.

setDate() converted the current Time instance to a Unix timestamp
integer before formatting it, which discards anything below
whole-second precision. timeToDate() already formats a Time object
directly and retains sub-second precision, but nothing routed the
no-explicit-date case through it.

Fixes codeigniter4#10540
@mergeable

mergeable Bot commented Sep 14, 2026

Copy link
Copy Markdown

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
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 14, 2026
@paulbalandan paulbalandan changed the title fix: preserve sub-second precision in BaseModel datetime timestamps fix: preserve sub-second precision in BaseModel datetime timestamps Sep 18, 2026
@carson-codeigniter4 carson-codeigniter4 Bot added the stale Pull requests with conflicts label Sep 18, 2026
@carson-codeigniter4

Copy link
Copy Markdown

👋 Hi, @wakqasahmed!

We detected conflicts in your PR against the base branch 🙊
You may want to sync 🔄 your branch with upstream!

Ref: Syncing Your Branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them stale Pull requests with conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Database dateFormat['datetime'] does not support millisecond or microsecond values

5 participants