News for hackers. Stuff that matters.
Re:JavaScript Temporal Is Coming by stevoski on Thursday January 30, 2025 @11:08AM (#42879056)
Going back even further in time, Java.util.Date was was heavily inspired by a C library.

That’s the origin of getMonth() in Java (and therefore in JS) returning a value from 0-11 and not 1-12 as many coders initially expect.

What was the origin for this peculiarity in C? That I don’t know, but I’m curious to find out if anyone knows.

Re:JavaScript Temporal Is Coming by wanderingstan on Thursday January 30, 2025 @11:20AM (#42879237)
Just spitballing, but in C it was always the convention to use zero-based indexing. Probably because you were often adding these indexes to pointers (literal memory addresses) to index into an array, so you needed a zero to index into the first slot of the array.
Re:JavaScript Temporal Is Coming by 8n4vidtmkvmk on Thursday January 30, 2025 @10:57PM (#42884581)
What I don't get is why the day of the month doesn't also start at 0?
Re:JavaScript Temporal Is Coming by andyjohnson0 on Thursday January 30, 2025 @04:57PM (#42882581)
C uses zero-based array indexing. A C array variable is a pointer to its first element, and an element index is just an offset from that. Element 0 is at start + 0, element 1 is at start + (1 * element size) etc.

Which why the fields of the 'tm' structure [1] (used to represent dates and times) are zero based§. Makes it easy to index into eg an array of day names using tm_wday. I guess at one time Java.util.Date was a leaky abstraction layer on-top of such an api.

§ Except for the tm_mday element, which curiously is 1-based. I've always assumed that this is because it is unlikely to be used as an array index. A long time ago I'm ashamed to admit that I used tm_mday == 0 as a quick check for an invalid tm value.

[1] https://en.cppreference.com/w/c/chrono/tm