2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file script_date.cpp Implementation of ScriptDate. */
10 #include "../../stdafx.h"
11 #include "script_date.hpp"
12 #include "script_timemode.hpp"
13 #include "../../timer/timer_game_calendar.h"
14 #include "../../timer/timer_game_economy.h"
18 #include "../../safeguards.h"
20 /* static */ bool ScriptDate::IsValidDate(Date date
)
25 /* static */ ScriptDate::Date
ScriptDate::GetCurrentDate()
27 if (ScriptTimeMode::IsCalendarMode()) return (ScriptDate::Date
)TimerGameCalendar::date
.base();
29 return (ScriptDate::Date
)TimerGameEconomy::date
.base();
32 /* static */ SQInteger
ScriptDate::GetYear(ScriptDate::Date date
)
34 if (date
< 0) return DATE_INVALID
;
36 if (ScriptTimeMode::IsCalendarMode()) {
37 ::TimerGameCalendar::YearMonthDay ymd
= ::TimerGameCalendar::ConvertDateToYMD(date
);
38 return ymd
.year
.base();
41 ::TimerGameEconomy::YearMonthDay ymd
= ::TimerGameEconomy::ConvertDateToYMD(date
);
42 return ymd
.year
.base();
45 /* static */ SQInteger
ScriptDate::GetMonth(ScriptDate::Date date
)
47 if (date
< 0) return DATE_INVALID
;
49 if (ScriptTimeMode::IsCalendarMode()) {
50 ::TimerGameCalendar::YearMonthDay ymd
= ::TimerGameCalendar::ConvertDateToYMD(date
);
54 ::TimerGameEconomy::YearMonthDay ymd
= ::TimerGameEconomy::ConvertDateToYMD(date
);
58 /* static */ SQInteger
ScriptDate::GetDayOfMonth(ScriptDate::Date date
)
60 if (date
< 0) return DATE_INVALID
;
62 if (ScriptTimeMode::IsCalendarMode()) {
63 ::TimerGameCalendar::YearMonthDay ymd
= ::TimerGameCalendar::ConvertDateToYMD(date
);
67 ::TimerGameEconomy::YearMonthDay ymd
= ::TimerGameEconomy::ConvertDateToYMD(date
);
71 /* static */ ScriptDate::Date
ScriptDate::GetDate(SQInteger year
, SQInteger month
, SQInteger day_of_month
)
73 if (month
< 1 || month
> 12) return DATE_INVALID
;
74 if (day_of_month
< 1 || day_of_month
> 31) return DATE_INVALID
;
75 if (year
< 0 || year
> CalendarTime::MAX_YEAR
) return DATE_INVALID
;
77 if (ScriptTimeMode::IsCalendarMode()) return (ScriptDate::Date
)::TimerGameCalendar::ConvertYMDToDate(year
, month
- 1, day_of_month
).base();
79 return (ScriptDate::Date
)::TimerGameEconomy::ConvertYMDToDate(year
, month
- 1, day_of_month
).base();
82 /* static */ SQInteger
ScriptDate::GetSystemTime()