views:

349

answers:

4

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?
+4  A: 

When we address a date. It has 3 components Year, Month, Day. Year is a numeric component. 2009. Thats why we use a number to display it. Day is a numeric component. 21. Thats why we use a number to display it. But Month is not a numeric thing. There is a label attached to it. Like January, February. Thats why we use index based system, when we display it as number. (starting from 0)

Krishna Kant Sharma
+1, well said
Jason
I think the poster is asking this question because we *do* use months as numbers, quite often in fact, and when we do we always start counting with 1, not 0.
Adam Batkin
That's a good theory but doesn't gel with "tm_yday [0,365]".
paxdiablo
Yes sure. When you are thinking of it as number then you can start from 1. Jasnuary is the 1st month. But I think index based system is more fitting here. Index based system must be used in all things, where numbers are representing another things.Like: array, constants, enumerations, months.
Krishna Kant Sharma
@Krishna: So why not represent _days_ of the month the same way?
Robert L
I'm pretty sure that referring to months with numbers is a consequence of the advent of computing - people were just vocalising what computers were displaying. (looking for citation)
annakata
@annakata: I am skeptical. I don't think the idea of calling the first month of the year "month 1" is a new one. I can't imagine someone didn't need a shorthand way of writing out the full date, in a ledger or whatever.
Brian
@Robert: If I were the decision maker I would have chosen day as o-based, If they had name. Alpha. Beta. My birthday is on 2009 January Gamma. :)
Krishna Kant Sharma
@Brian: *really?* If I asked you to say "01-01-1970", would you say "first of January, nineteen-seventy" or "one of one nineteen-seventy"? I can't find anything to cite this unfortunately but I know I've read it *somewhere*. Certainly, if you look at old court ledgers or parish documents (in the UK) the months are abbreviated as letters. It was a really good book :(
annakata
+1  A: 

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".

paxdiablo
At least one person (not me) had defined "getRealMonth" and "setRealMonth" functions for use in his scripts. I don't blame him one bit.
Robert L
+4  A: 

Yes, the Romans had problems with zero as well.

This is just a [non-intuitive] consequence of mathematics (being a strong component of programming, especially early programming) defining zero as the first (problematic term that one) real, positive* natural number, and since an array is indexed with real, natural numbers the "first" element is at index 0.

Months are really named values in an array, where days and years are numbered values - it would perhaps be more useful to think of days/years as being in arrays which look like { "1", "2", "3", ... } themselves.

As to why this is so common (apart from being mathematically correct) well all the languages you listed descend from a common origin for one thing...

Edit:

Looking further into it, this wikipedia link details several good and interesting reasons for zero indexing (which does not directly speak to why months are zero-indexed but I think that's covered already), and this SO link has answered the question before.

Looks like the prevailing opinion is either "historical accident" or "because months are not numbers so cannot be compared to day/year storage" depending on who you ask.

* Sorry, sorry, physics!=maths coming back to bite me there. Off to iron my hands now.

annakata
Zero is not a positive number. It is also not a negative number.
Robert L
Good point, perhaps you should change it to "natural number".
paxdiablo
Interesting, I had always thought that C arrays start at 0 to simplify the pointer arithmetic: `a[0]` == `*(a + 0)`.
too much php
Actually, in computing, you can have a positive and negative zero if you use the Ones' complement system. Fortunately, that system is almost never used again. See http://en.wikipedia.org/wiki/Ones'_complement#Ones.27_complement
Workshop Alex
A: 

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.

Workshop Alex