Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zero-based month numbering [closed]

Some popular programming languages use month numbering which is off by 1 -- JavaScript comes to mind, as does Java, and if memory serves, C is another. I have some questions:

  • If you are going to be ignoring the month numbering used by laypeople, then why not for the sake of consistency also ignore the day numbering used by laypeople, and number the days in each month starting from 0 ?
  • Why is this so common?
  • Whose idea was this in the first place?
like image 903
Robert L Avatar asked Sep 21 '09 05:09

Robert L


People also ask

Why do computers start counting at 0?

Counting arrays from 0 simplifies the computation of the memory address of each element. Not a huge difference but it adds an unnecessary subtraction for each access.

Why are indexes zero based?

In addition, some languages like C and C++ use pointers to store data, therefore it makes sense for indices to start at zero as well, because the memory address is held by the program counter at 0 first by default, thus making compilation easier.

What is zero indexed array?

Showing the position of elements in an array We can simply number the elements of our array starting at zero: So if we wanted to create a new array with the following letters: h, e, l, l, o we would have the following: here the array starts with a zero'th element too. Here is the thing. All arrays are zero'th indexed.


2 Answers

The use of zero to start counting is actually an optimization trick from Assembly programmers. Instead of assigning 1 to the count register, they XOR'ed the register with itself, which was slightly faster in CPU cycles. This meant that counting would start with 0 and would always be up to the length of elements, excluding the last one.

Also, the use of zero is also popular with pointer arithmetics where you would use one base pointer pointing at some allocated memory, plus a second pointer which would be at an offset from this base pointer. Here, using the value zero makes a lot of sense to point the offset to the base of the memory block. (General array logic tends to be the base address plus the offset x record size.)

And zero-based month numbers? Often, many programming environments calculate the data as a number of days since some default data. December 31, 1899 is a popular date, although there have been plenty of other dates used as base date. All other dates are offset from this base, and will just be stored as one single number. Fractions would be used to indicate hours, minutes and seconds, where 0.25 would be 24/4 = 6 hours. Thus, to transform a date into a real date, all the environment has to do is transform this number into a real date.

However, the combination of zero-based arrays and 1-based month values does bring a problem. To get the month name of month 9, you would have to get item 8 from the month array. Some developers would be happy with decreasing the month number before getting it's name. Others preferred to change the month into something zero-based since people just want to know the name, not the number. It's a personal view.

like image 117
Wim ten Brink Avatar answered Oct 13 '22 23:10

Wim ten Brink


It is what it is, and the huge weight of software built to that assumption means it's going to be around for a while.

My opinion is that it was the fault of C, and all those other Johnie-come-lately languages just conformed with it.

You get some funny situations from people who don't know better. One of the few Y2K bugs our team found was a web site proudly proclaiming the year was 19100 simply because they prefixed the struct tm year with the literal "19".

like image 39
paxdiablo Avatar answered Oct 13 '22 22:10

paxdiablo