Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

So I spent several hours in rage, figuring out why isn't my code writing to the /sys/class/rtc/rtc0/wakealarm correctly. The problem is that it doesn't return anything if the value is wrong. And finally I noticed this small

5

between the year and the day. Why isn't it counting days and years from zero for consistency?

For comparison: QCalendarWidget counts month from 1 to 12 (docs)

share|improve this question
1  
In future, instead of "spending several hours in a rage", take a minute to look at the API, which boldly says that for struct GtkCalendar`, "Note that month is zero-based (i.e it allowed values are 0-11) while selected_day is one-based (i.e. allowed values are 1-31)." –  izx Jun 27 '12 at 11:23
1  
it's not bold in GTK3 docs: developer.gnome.org/gtk3/stable/GtkCalendar.html –  int_ua Jun 27 '12 at 13:20

2 Answers 2

up vote 0 down vote accepted

It's the whole Inclusive or Exclusive discussion They probably used the system for counting how many months have passed.

For example if you say 1 month has passed, you would be in the second month of the year. So If you need a program to count how many months pass, and you are using the 1-12 system, it would report that you are in month 1 (January) not that 0 (January) months have passed , and would need to preform an extra function to determine how much time has passed.

Just like in the thought puzzle in Chrono Trigger, http://www.youtube.com/watch?v=Ll1uAla90BU&t=13m42s , depending on how you count, if you count from the start or if you count when you finish, it can be incredibly misleading. enter image description here

So for coding it is easier to start from 0 , In the diagram you count to three but in the first picture it adds up to only 2 complete cycles (months) and in the second you count to 3 but start with 0 adding up to 3 complete months.

This is also how everyone counts how old they are in years, you start with 0 and go to 1 once a year has passed.

As for why they didn't also do this for days and years. I would imagine the counting is separate for days because of the variable number of days that are in a month, this would also make it hard to count how many months have passed If you are going by the number of days. and you can get an accurate count of a passing of a year if you count how many months pass, i.e. if you count the months accurately you can count years accurately, there are 12 complete months in a year, start counting at 0 and once you hit 12 you cycle back to 0, so the numbers would be 10 11 0 1 2 3 ... like the picture below if you count to 2 then restart -

enter image description here

Anyways, in a nutshell, the programers just did what was easiest for them.

share|improve this answer

Doc for GtkCalendar: http://developer.gnome.org/gtk/2.24/GtkCalendar.html

General explaination for zero based:

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.

Moreover, there is more than a reason why one could think there is wrong with Date/Calendar:

  • Surprising bases (1900 as the year base in Date, admittedly for deprecated constructors; 0 as the month base in both)
  • Mutability - using immutable types makes it much simpler to work with what are really effectively values
  • An insufficient set of types: it's nice to have Date and Calendar as different things, but the separation of "local" vs "zoned" values is missing, as is date/time vs date vs time
  • An API which leads to ugly code with magic constants, instead of clearly named methods
  • An API which is very hard to reason about - all the business about when things are recomputed etc
  • The use of parameterless constructors to default to "now", which leads to hard-to-test code
share|improve this answer
    
I've used GTK3: developer.gnome.org/gtk3/stable/GtkCalendar.html –  int_ua Jun 27 '12 at 13:20
    
and didn't read the whole answer yet, will do it later. Thanks for understanding. –  int_ua Jun 27 '12 at 14:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.