Update: Translations from eints
[openttd-github.git] / src / script / api / script_date.cpp
blob3ff39b6232dfc20bbd0e5721c14ed0d2dfc7c2ba
1 /*
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/>.
6 */
8 /** @file script_date.cpp Implementation of ScriptDate. */
10 #include "../../stdafx.h"
11 #include "script_date.hpp"
12 #include "../../timer/timer_game_economy.h"
14 #include <time.h>
16 #include "../../safeguards.h"
18 /* static */ bool ScriptDate::IsValidDate(Date date)
20 return date >= 0;
23 /* static */ ScriptDate::Date ScriptDate::GetCurrentDate()
25 return (ScriptDate::Date)TimerGameEconomy::date.base();
28 /* static */ SQInteger ScriptDate::GetYear(ScriptDate::Date date)
30 if (date < 0) return DATE_INVALID;
32 ::TimerGameEconomy::YearMonthDay ymd = ::TimerGameEconomy::ConvertDateToYMD(::TimerGameEconomy::Date{date});
33 return ymd.year.base();
36 /* static */ SQInteger ScriptDate::GetMonth(ScriptDate::Date date)
38 if (date < 0) return DATE_INVALID;
40 ::TimerGameEconomy::YearMonthDay ymd = ::TimerGameEconomy::ConvertDateToYMD(::TimerGameEconomy::Date{date});
41 return ymd.month + 1;
44 /* static */ SQInteger ScriptDate::GetDayOfMonth(ScriptDate::Date date)
46 if (date < 0) return DATE_INVALID;
48 ::TimerGameEconomy::YearMonthDay ymd = ::TimerGameEconomy::ConvertDateToYMD(::TimerGameEconomy::Date{date});
49 return ymd.day;
52 /* static */ ScriptDate::Date ScriptDate::GetDate(SQInteger year, SQInteger month, SQInteger day_of_month)
54 if (month < 1 || month > 12) return DATE_INVALID;
55 if (day_of_month < 1 || day_of_month > 31) return DATE_INVALID;
57 ::TimerGameEconomy::Year timer_year{ClampTo<int32_t>(year)};
58 if (timer_year < EconomyTime::MIN_YEAR || timer_year > EconomyTime::MAX_YEAR) return DATE_INVALID;
60 return static_cast<ScriptDate::Date>(::TimerGameEconomy::ConvertYMDToDate(timer_year, month - 1, day_of_month).base());
63 /* static */ SQInteger ScriptDate::GetSystemTime()
65 time_t t;
66 time(&t);
67 return t;