month of the macro function "getDateAndTime()"

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

month of the macro function "getDateAndTime()"

Jürgen Gluch
Hello,

I noticed today (2012 Aug 09) that the macro:

  getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second,
msec);
  print(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);

gives

  2012 7 4 9 12 39 15 910

as output. So the month is 7 instead of the expected 8. So the month
variable starts at 0 while all other variables start at 1.
What's the purpose, or is it a bug?  I use Fiji/ImageJ 1.46j.

Thanks for help, Jürgen.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: month of the macro function "getDateAndTime()"

Volker Baecker
Hello,
I didn't verify so this is just a guess. It probably uses the class
Calendar from java that behaves in the same way.
see
http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#MONTH

And why does months start with 0 in Calendar?
Look here for a discussion see
http://stackoverflow.com/questions/344380/why-is-january-month-0-in-java-calendar
or
http://stackoverflow.com/questions/1755199/calendar-returns-wrong-month

It seems to be historical, as the citation from the above link states:

"As several people have pointed out, months returned by the Calendar
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html> and Date
<http://java.sun.com/javase/6/docs/api/java/util/Date.html> classes in
Java are indexed from 0 instead of 1. So 0 is January, and the current
month, November, is 10.

You might wonder why this is the case. The origins lie with the POSIX
standard functions |ctime|, |gmtime| and |localtime|, which accept or
return a |time_t| structure with the following fields (from man 3 ctime
<http://linux.die.net/man/3/ctime>):

|int tm_mday;    /* day of month (1 - 31) */
int tm_mon;     /* month of year (0 - 11) */
int tm_year;    /* year - 1900 */
|

This API was copied pretty much exactly into the Java Date class in Java
1.0, and from there mostly intact into the Calendar class in Java 1.1.
Sun fixed the most glaring problem when they introduced Calendar – the
fact that the year 2001 in the Gregorian calendar was represented by the
value 101 in their Date class. But I'm not sure why they didn't change
the day and month values to at least both be consistent in their
indexing, either from zero or one. This inconsistency and related
confusion still exists in Java (and C) to this day."

Volker



On 09/08/12 12:46, Jürgen Gluch wrote:

> Hello,
>
> I noticed today (2012 Aug 09) that the macro:
>
>   getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second,
> msec);
>   print(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
>
> gives
>
>   2012 7 4 9 12 39 15 910
>
> as output. So the month is 7 instead of the expected 8. So the month
> variable starts at 0 while all other variables start at 1.
> What's the purpose, or is it a bug?  I use Fiji/ImageJ 1.46j.
>
> Thanks for help, Jürgen.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: month of the macro function "getDateAndTime()"

Jürgen Gluch
Thanks Volker,

It still seems awkward to me, but now I how to deal with it.

Jürgen

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html