6 ms·
That comes from C’s standard library (and presumably from somewhere else before that). Classic bad design, took a very long time for people to figure out it was
by iainmerrick 4y ago
That comes from C’s standard library (and presumably from somewhere else before that). Classic bad design, took a very long time for people to figure out it was unhelpful and dangerous.
- jefftk 4y agohttps://linux.die.net/man/3/localtime https://linux.die.net/man/3/localtime: tm_mday: The day of the month, in the range 1 to 31. tm_mon: The number of months since January, in the range 0 to 11. I wonder where C got it? It goes back to at least 1973's V4: https://github.com/dspinellis/unix-history-repo/commit/92779735baa444db17a716db25ccb5dba7d1bf65 https://github.com/dspinellis/unix-history-repo/commit/92779...
- Jtsummers 4y agohttps://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s4/ctime.c https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/... Convenience. Look at how the days in the months are stored and accessed. Using 1-based months would introduce an extra calculation (-1) on all searches or an unused value in the 0-index. Also look at how printing is handled for weekday and month names. They, again, take advantage of 0-based indexing. Day and year are already represented as numbers, so it's natural to keep them as the "correct" (conventional) number as used by most people. Since the months aren't being stored as strings but as an index, this saves them from having useless data (entries in 0) or doing an extra calculation.
- jefftk 4y agoSubtracting 1 from the user provided index isn't that expensive. Or, if you're willing to give up three bytes, index into "ErrSunMonTueWedThuFriSat". Or, and this is mildly insane but perhaps in keeping with early C, have your pointer be three bytes before the beginning of "SunMonTueWedThuFriSat" and use one-based indexing.
- deleted 4y ago[deleted]
- Jtsummers 4y agoIt all boils down to the fact that C's memory model is meant to map closely to the physical memory model, and the C and Unix developers preferred simplicity of implementation over simplicity of interface. Leaky abstractions (like that the weekday or months start at 0 so that they can reference into arrays of names) are fine given that prioritization. Not everyone shares that preference.