1
0
Fork 0

(svn r18291) -Codechange: rework the calculation of the 'days till year' macro a bit so it can be properly reused and add a MAX_DAY

release/1.0
rubidium 2009-11-25 23:28:04 +00:00
parent d3a925af42
commit 1a8c203d6e
2 changed files with 32 additions and 14 deletions

View File

@ -140,22 +140,13 @@ void ConvertDateToYMD(Date date, YearMonthDay *ymd)
*/
Date ConvertYMDToDate(Year year, Month month, Day day)
{
/*
* Each passed leap year adds one day to the 'day count'.
*
* A special case for the year 0 as no year has been passed,
* but '(year - 1) / 4' does not yield '-1' to counteract the
* '+1' at the end of the formula as divisions round to zero.
*/
int nr_of_leap_years = (year == 0) ? 0 : ((year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1);
/* Day-offset in a leap year */
int days = _accum_days_for_month[month] + day - 1;
/* Account for the missing of the 29th of February in non-leap years */
if (!IsLeapYear(year) && days >= ACCUM_MAR) days--;
return year * DAYS_IN_YEAR + nr_of_leap_years + days;
return DAYS_TILL(year) + days;
}
/** Functions used by the IncreaseDate function */

View File

@ -38,18 +38,45 @@ enum {
/** The maximum year of the original TTD */
#define ORIGINAL_MAX_YEAR 2090
/**
* Calculate the number of leap years till a given year.
*
* Each passed leap year adds one day to the 'day count'.
*
* A special case for the year 0 as no year has been passed,
* but '(year - 1) / 4' does not yield '-1' to counteract the
* '+1' at the end of the formula as divisions round to zero.
*
* @param year the year to get the leap years till.
* @return the number of leap years.
*/
#define LEAP_YEARS_TILL(year) ((year) == 0 ? 0 : ((year) - 1) / 4 - ((year) - 1) / 100 + ((year) - 1) / 400 + 1)
/**
* Calculate the date of the first day of a given year.
* @param year the year to get the first day of.
* @return the date.
*/
#define DAYS_TILL(year) (DAYS_IN_YEAR * (year) + LEAP_YEARS_TILL(year))
/**
* The offset in days from the '_date == 0' till
* 'ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)'
*/
#define DAYS_TILL_ORIGINAL_BASE_YEAR (DAYS_IN_YEAR * ORIGINAL_BASE_YEAR + ORIGINAL_BASE_YEAR / 4 - ORIGINAL_BASE_YEAR / 100 + ORIGINAL_BASE_YEAR / 400)
#define DAYS_TILL_ORIGINAL_BASE_YEAR DAYS_TILL(ORIGINAL_BASE_YEAR)
/* The absolute minimum & maximum years in OTTD */
/** The absolute minimum & maximum years in OTTD */
#define MIN_YEAR 0
/* MAX_YEAR, nicely rounded value of the number of years that can
* be encoded in a single 32 bits date, about 2^31 / 366 years. */
/**
* MAX_YEAR, nicely rounded value of the number of years that can
* be encoded in a single 32 bits date, about 2^31 / 366 years.
*/
#define MAX_YEAR 5000000
/** The number of days till the last day */
#define MAX_DAY DAYS_TILL(MAX_YEAR + 1) - 1
typedef int32 Date; ///< The type to store our dates in
typedef uint16 DateFract; ///< The fraction of a date we're in, i.e. the number of ticks since the last date changeover
typedef int32 Ticks; ///< The type to store ticks in