Page 1 of 1

Date in date_type.h

Posted: 22 May 2011 04:11
by LurkerL
How exactly does int32 Date work? Is it a "simple" counter of days passed since 1 Jan 0000 ? (ATM this is my understanding)

Neither Doxygen nor the Wiki help here!

Re: Date in date_type.h

Posted: 22 May 2011 08:55
by Alberth
A bit further down in the file is YearMonthDay, which says

Code: Select all

/**
 * Data structure to convert between Date and triplet (year, month, and day).
 * @see ConvertDateToYMD(), ConvertYMDToDate()
 */
I haven't looked what the functions do exactly, but maybe that's what you are looking for?

It usually helps to browse around, and check where (in this case) Date is used, to get a feel how to use it.
Of course, fixes for doxymentation are always welcome.

Re: Date in date_type.h

Posted: 22 May 2011 09:13
by LurkerL
Alberth wrote:... YearMonthDay ...
Actually that was the reason why I asked the question in the first place! That particular function has a funny way of dealing with leap years to my eyes. Playing around, I think that Date is a day counter from 0000-01-01, but then again the only advantage I can see with that is that you can use C++'s inbuilt plus/minus/times/divide rather than making new ones. (Wasted time from 0 to 1920, and we only use 1920 to 2050. 130 years, which is tiny compared to 1920 years!)

[My real goal is to rewrite the timekeeping code in order to implement economy changes (with the side effect that seasons, day-night cycles and so on are also solved at the same time).]

Re: Date in date_type.h

Posted: 22 May 2011 09:18
by planetmaker
LurkerL wrote:Wasted time from 0 to 1920, and we only use 1920 to 2050. 130 years, which is tiny compared to 1920 years!)
OpenTTD allows to start the game on 1st January 0 (or 1?) and counts years up to 5000000. Thus you can play a game for 5 million years. Quite a bit more than the assumd 130 years.

Re: Date in date_type.h

Posted: 22 May 2011 09:31
by LurkerL
1.

Perhaps I should just show what I want.

Original (for those who don't have the source right this moment)

Code: Select all

ConvertYMDToDate(Year year, Month month, Day day)
{
    int days = _accum_days_for_month[month] + day - 1;
    if (!IsLeapYear(year) && days >= ACCUM_MAR) days--;
    return DAYS_TILL(year) + days;
}
My try

Code: Select all

ConvertYMDToDate(Year year, Month month, Day day)
{
    TimeDate::Date date(year, month+1, ?);
    return DAYS_TILL(year) + date.DayOfYear();
}
(TimeDate::Date date(int year, int month, int day) deals with dates input in the "conventional" Gregorian way, e.g. date(1952, 02, 29) or date(1966, 01, 01) and so on.)

Where the "?" is I have no idea what to put in... (The latest I'm on is day-2*(!TimeDate::YearIsLeap(year)), which gives the correct date on leap years, but adds 2 days all other years).



2.
planetmaker wrote:... OpenTTD allows to start the game on 1st January 0 (or 1?) and counts years up to 5000000. Thus you can play a game for 5 million years. Quite a bit more than the assumd 130 years.
Yes, my bad :wink:

Re: Date in date_type.h

Posted: 22 May 2011 09:46
by Alberth
LurkerL wrote:
Alberth wrote:... YearMonthDay ...
Actually that was the reason why I asked the question in the first place! That particular function has a funny way of dealing with leap years to my eyes. ...
The source code is leading. Doxygen docs have been added much later without full knowledge of the details.
If you find new information, and supply a doxygen patch, we are happy to add it.

Re: Date in date_type.h

Posted: 22 May 2011 09:52
by LurkerL
Alberth wrote:
LurkerL wrote:
Alberth wrote:... YearMonthDay ...
Actually that was the reason why I asked the question in the first place! That particular function has a funny way of dealing with leap years to my eyes. ...
The source code is leading. Doxygen docs have been added much later without full knowledge of the details.
If you find new information, and supply a doxygen patch, we are happy to add it.
I think I shouldn't have been so blunt in my first post---it wasn't my intention to say bad things about the docs or the code, or to say I wasn't playing with the code to figure it out myself.

Re: Date in date_type.h

Posted: 22 May 2011 09:53
by Eddi
LurkerL wrote:Wasted time from 0 to 1920, and we only use 1920 to 2050. 130 years, which is tiny compared to 1920 years!
it used to be that way that days were counted from 1920-01-01, with a 16 bit counter, which allowed dates up to 2090-12-31. this was not enough for some people, so 32 bit date was introduced, starting in 0000-01-01 and ending in 5000000-12-31. [this is a normalised gregorian calendar with year 0]

leap years occur every 4 years, but every 100 years one is skipped, and every 400 years an additional one is added

Re: Date in date_type.h

Posted: 22 May 2011 12:02
by LurkerL
Thanks for the help, it's been figured out now. "Leap year offset" in ConvertYMDToDate was giving me the trouble: the day ordinal ("Date type") is shifted by 1 day.