Bug 18552 – std.datetime.date.Date strips year int argument to short

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-03-04T00:37:27Z
Last change time
2024-07-25T12:11:31Z
Assigned to
No Owner
Creator
feklushkin.denis

Comments

Comment #0 by feklushkin.denis — 2018-03-04T00:37:27Z
std.datetime.date.Date accepts int year as argument but silently strips it to short: this(int year, int month, int day) @safe pure { enforceValid!"months"(cast(Month) month); enforceValid!"days"(year, cast(Month) month, day); _year = cast(short) year; _month = cast(Month) month; _day = cast(ubyte) day; } It is need to add bounds check or change argument type.
Comment #1 by issues.dlang — 2018-03-04T00:58:48Z
What is the use case where this matters? The various constructors in std.datetime accepts int in order to avoid forcing casts when calling the constructor (especially since when it was first added to Phobos, VRP didn't exist, so passing integer literals to a short was an error even if they fit). The documenattion makes it pretty clear that Date does not support values beyond what a short can hold, and it would be highly abnormal to pass anything which did not fit in that range. Why should Date care about checking that it isn't given a ridiculous number for the year, especially when the result is still a perfectly valid Date?
Comment #2 by feklushkin.denis — 2018-03-04T03:23:11Z
> What is the use case where this matters? RDBMSes (at least Postgres) supports int type of year. With restrictions associated with Julian day (4713 BC boundary) it can represent up to 294276 AD. When converting test values to and from Postgres I found mismatch for such big numbers. > The documenattion makes it pretty clear that Date does not support values beyond what a short can hold Does not overrides necessity of assert, at least
Comment #3 by feklushkin.denis — 2024-07-25T12:11:31Z