Date in date_type.h

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
LurkerL
Engineer
Engineer
Posts: 10
Joined: 16 Jan 2010 06:57

Date in date_type.h

Post 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!
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4766
Joined: 09 Sep 2007 05:03
Location: home

Re: Date in date_type.h

Post 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.
LurkerL
Engineer
Engineer
Posts: 10
Joined: 16 Jan 2010 06:57

Re: Date in date_type.h

Post 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).]
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Date in date_type.h

Post 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.
LurkerL
Engineer
Engineer
Posts: 10
Joined: 16 Jan 2010 06:57

Re: Date in date_type.h

Post 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:
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4766
Joined: 09 Sep 2007 05:03
Location: home

Re: Date in date_type.h

Post 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.
LurkerL
Engineer
Engineer
Posts: 10
Joined: 16 Jan 2010 06:57

Re: Date in date_type.h

Post 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.
Eddi
Tycoon
Tycoon
Posts: 8289
Joined: 17 Jan 2007 00:14

Re: Date in date_type.h

Post 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
LurkerL
Engineer
Engineer
Posts: 10
Joined: 16 Jan 2010 06:57

Re: Date in date_type.h

Post 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.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest