SQLite User Forum

Odd date rounding behavior since SQLite 3.45
Login

Odd date rounding behavior since SQLite 3.45

(1) By jeremysheeley on 2025-01-21 16:18:29 [source]

I suspect that there is a problem with dealing with datetimes that end in 23:59:59.9995. When using SQLite functions to interrogate the date, the day is rounded up to the next day. But time related functions do not round up (as stated by the docs).

To be clear, rounding up to the next day is within reason (but not what I would prefer), but reporting a datetime combination that is off by 24 hours does not seem right. This behavior was introduced in 3.45.

select date('2024-12-31 23:59:59.9995');
--2025-01-01 (rounds up)

select time('2024-12-31 23:59:59.9995');
--23:59:59 (does not round up)

select datetime('2024-12-31 23:59:59.9995');
--2025-01-01 23:59:59 (rounds up date, but not time)

select strftime('%Y-%m-%d %H:%M:%S', '2024-12-31 23:59:59.9995');
--2025-01-01 23:59:59 (rounds up date, but not time)

select julianday('2024-12-31 23:59:59.9995');
--2460676.5 (rounds up)

select julianday('2024-12-31 23:59:59.9994');
--2460676.49999999 (rounds down, correctly)

select unixepoch('2024-12-31 23:59:59.9995');
--1735689600 (rounds up)

select unixepoch('2024-12-31 23:59:59.9994');
--1735689599 (rounds down, correctly)

select timediff('2024-12-31 23:59:59.9995', '2024-12-31 23:59:59.9994');
--+0000-00-00 00:00:00.001 (rounds time portion up)

This somewhat matches the C# MaxDate ('9999-12-31 23:59:59.9999999') issue reported on the forum, but that issue started in an earlier version than this one.

Year 9999 issue

(2) By Richard Hipp (drh) on 2025-01-21 17:42:26 in reply to 1 [link] [source]

Thank you for the report. The problem should now be resolved on trunk and on branch-3.48.

(3) By jeremysheeley on 2025-01-21 17:55:42 in reply to 2 [link] [source]

Thank you!