"Why is day of the month 1-indexed but the month is 0-indexed in C?" This Twitter thread from November 2020[1] and its HackerNews discussion[2] seem relevant. 1: https://twitter.com/hillelogram/status/1329228419628998665 2: https://news.ycombinator.com/item?id=25195287
Why don't we enumerate days throughout the year?
History of Zero-Based Months?
11–20 of 30 posts
Re: History of Zero-Based Months?
#12Earlier quoted context omitted.
Why don't we enumerate days throughout the year?
We enumerate milliseconds since 1 Jan 1970 12am UTC instead. You can convert to all the other formats.
Re: History of Zero-Based Months?
#13Re: History of Zero-Based Months?
#14Earlier quoted context omitted.
We enumerate milliseconds since 1 Jan 1970 12am UTC instead. You can convert to all the other formats.
Well, given a database of leap seconds you can. And you can't convent times in the future between unix time and calendar time.
Re: History of Zero-Based Months?
#15 const MONTHS = ["Jan", "Feb", "Mar", ..., "Dec"];
console.log(MONTHS[d.getMonth()]);Re: History of Zero-Based Months?
#16Hence, dealing with months is perhaps a bit more like indexing variable-sized objects, and for that purpose traditionally, the array-of-pointers approach uses zero-based pointer arithmetic, which is what the designers might have been thinking.
Really however, the months should probably be treated more like structs, with the number of days in that month being a data member. This would allow sanity checks, i.e. entering Feb 30 should raise an error, but not May 30.
Re: History of Zero-Based Months?
#17Zero-based months is awkward when printing as a number, but convenient when indexing an array: const MONTHS = ["Jan", "Feb", "Mar", ..., "Dec"]; console.log(MONTHS[d.getMonth()]);
Re: History of Zero-Based Months?
#18At least in Perl, the rationale for this (apart from copying C) is that it's very easy to reference a list (array) of month names if the month indices are zero-based.
How difficult is it to either subtract 1 before indexing your array, or have a 13 element array with a dummy value in element 0? Deprecate time.h and adopt ISO 8601 already.
Re: History of Zero-Based Months?
#19Zero-based months is awkward when printing as a number, but convenient when indexing an array: const MONTHS = ["Jan", "Feb", "Mar", ..., "Dec"]; console.log(MONTHS[d.getMonth()]);
Re: History of Zero-Based Months?
#20Earlier quoted context omitted.
Well, given a database of leap seconds you can. And you can't convent times in the future between unix time and calendar time.
Times in the future are always unreliable, even using the "normal" system there aren't any guarantees that specific time will even exist or if it does you still need to think about "is it an absolute event or a relative event and does it need to have the time updated as a result" just the other way around.