2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-30 00:15:35 +01:00
|
|
|
/** @file script_date.cpp Implementation of ScriptDate. */
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2011-01-22 11:33:16 +01:00
|
|
|
#include "../../stdafx.h"
|
2011-11-30 00:07:38 +01:00
|
|
|
#include "script_date.hpp"
|
2023-04-24 17:56:01 +02:00
|
|
|
#include "../../timer/timer_game_calendar.h"
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2021-04-29 14:26:08 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2014-02-06 20:50:34 +01:00
|
|
|
/* static */ bool ScriptDate::IsValidDate(Date date)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2014-02-06 20:50:34 +01:00
|
|
|
return date >= 0;
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 20:50:34 +01:00
|
|
|
/* static */ ScriptDate::Date ScriptDate::GetCurrentDate()
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2024-03-23 17:41:13 +01:00
|
|
|
return (ScriptDate::Date)TimerGameCalendar::date.base();
|
2014-02-06 20:50:34 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 18:40:44 +01:00
|
|
|
/* static */ SQInteger ScriptDate::GetYear(ScriptDate::Date date)
|
2014-02-06 20:50:34 +01:00
|
|
|
{
|
|
|
|
if (date < 0) return DATE_INVALID;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2024-03-23 17:41:13 +01:00
|
|
|
::TimerGameCalendar::YearMonthDay ymd = ::TimerGameCalendar::ConvertDateToYMD(date);
|
2023-11-06 21:29:35 +01:00
|
|
|
return ymd.year.base();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 18:40:44 +01:00
|
|
|
/* static */ SQInteger ScriptDate::GetMonth(ScriptDate::Date date)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2014-02-06 20:50:34 +01:00
|
|
|
if (date < 0) return DATE_INVALID;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2024-03-23 17:41:13 +01:00
|
|
|
::TimerGameCalendar::YearMonthDay ymd = ::TimerGameCalendar::ConvertDateToYMD(date);
|
2009-01-12 18:11:45 +01:00
|
|
|
return ymd.month + 1;
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:40:44 +01:00
|
|
|
/* static */ SQInteger ScriptDate::GetDayOfMonth(ScriptDate::Date date)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2014-02-06 20:50:34 +01:00
|
|
|
if (date < 0) return DATE_INVALID;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2024-03-23 17:41:13 +01:00
|
|
|
::TimerGameCalendar::YearMonthDay ymd = ::TimerGameCalendar::ConvertDateToYMD(date);
|
2009-01-12 18:11:45 +01:00
|
|
|
return ymd.day;
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:40:44 +01:00
|
|
|
/* static */ ScriptDate::Date ScriptDate::GetDate(SQInteger year, SQInteger month, SQInteger day_of_month)
|
2009-01-12 18:11:45 +01:00
|
|
|
{
|
2014-02-06 20:50:34 +01:00
|
|
|
if (month < 1 || month > 12) return DATE_INVALID;
|
|
|
|
if (day_of_month < 1 || day_of_month > 31) return DATE_INVALID;
|
2023-08-16 15:43:31 +02:00
|
|
|
if (year < 0 || year > CalendarTime::MAX_YEAR) return DATE_INVALID;
|
2009-01-12 18:11:45 +01:00
|
|
|
|
2024-03-23 17:41:13 +01:00
|
|
|
return (ScriptDate::Date)::TimerGameCalendar::ConvertYMDToDate(year, month - 1, day_of_month).base();
|
2009-01-12 18:11:45 +01:00
|
|
|
}
|
2011-12-19 21:59:12 +01:00
|
|
|
|
2023-02-10 18:40:44 +01:00
|
|
|
/* static */ SQInteger ScriptDate::GetSystemTime()
|
2011-12-19 21:59:12 +01:00
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
time(&t);
|
|
|
|
return t;
|
|
|
|
}
|