Update readme.md
[openttd-joker.git] / src / script / api / script_date.cpp
blob645384151c7ecf8d679a58795210cf6776096e63
1 /* $Id: script_date.cpp 26307 2014-02-06 19:50:34Z zuu $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file script_date.cpp Implementation of ScriptDate. */
12 #include <time.h>
13 #include "../../stdafx.h"
14 #include "script_date.hpp"
15 #include "../../date_func.h"
17 #include "../../safeguards.h"
19 /* static */ bool ScriptDate::IsValidDate(Date date)
21 return date >= 0;
24 /* static */ ScriptDate::Date ScriptDate::GetCurrentDate()
26 return (ScriptDate::Date)_date;
29 /* static */ int32 ScriptDate::GetYear(ScriptDate::Date date)
31 if (date < 0) return DATE_INVALID;
33 ::YearMonthDay ymd;
34 ::ConvertDateToYMD(date, &ymd);
35 return ymd.year;
38 /* static */ int32 ScriptDate::GetMonth(ScriptDate::Date date)
40 if (date < 0) return DATE_INVALID;
42 ::YearMonthDay ymd;
43 ::ConvertDateToYMD(date, &ymd);
44 return ymd.month + 1;
47 /* static */ int32 ScriptDate::GetDayOfMonth(ScriptDate::Date date)
49 if (date < 0) return DATE_INVALID;
51 ::YearMonthDay ymd;
52 ::ConvertDateToYMD(date, &ymd);
53 return ymd.day;
56 /* static */ ScriptDate::Date ScriptDate::GetDate(int32 year, int32 month, int32 day_of_month)
58 if (month < 1 || month > 12) return DATE_INVALID;
59 if (day_of_month < 1 || day_of_month > 31) return DATE_INVALID;
60 if (year < 0 || year > MAX_YEAR) return DATE_INVALID;
62 return (ScriptDate::Date)::ConvertYMDToDate(year, month - 1, day_of_month);
65 /* static */ int32 ScriptDate::GetSystemTime()
67 time_t t;
68 time(&t);
69 return t;