Files
OpenRCT2-Web/src/openrct2/localisation/Localisation.Date.cpp
T

129 lines
3.0 KiB
C++
Raw Normal View History

2014-04-09 11:16:35 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2014-04-09 11:16:35 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2014-04-09 11:16:35 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-04-09 11:16:35 +01:00
*****************************************************************************/
2017-11-30 18:17:06 +01:00
#include "../Game.h"
2022-01-18 10:21:20 -08:00
#include "../profiling/Profiling.h"
#include "../util/Math.hpp"
2018-01-06 18:32:25 +01:00
#include "Date.h"
#include "StringIds.h"
2014-04-09 11:16:35 +01:00
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-06-22 23:01:56 +02:00
#include <time.h>
uint16_t gDateMonthTicks;
2020-08-25 19:06:38 -03:00
int32_t gDateMonthsElapsed;
2016-09-03 17:40:23 +00:00
// rct2: 0x00993988
2021-10-17 15:31:56 +02:00
const int16_t days_in_month[MONTH_COUNT] = {
31, 30, 31, 30, 31, 31, 30, 31,
};
2022-07-31 14:22:58 +02:00
const StringId DateFormatStringIds[] = {
2017-06-06 23:24:18 +02:00
STR_DATE_FORMAT_DAY_MONTH_YEAR,
STR_DATE_FORMAT_MONTH_DAY_YEAR,
STR_DATE_FORMAT_YEAR_MONTH_DAY,
2021-10-17 15:31:56 +02:00
STR_DATE_FORMAT_YEAR_DAY_MONTH,
};
2022-07-31 14:22:58 +02:00
const StringId DateFormatStringFormatIds[] = {
2017-06-06 23:24:18 +02:00
STR_DATE_FORMAT_DMY,
STR_DATE_FORMAT_MDY,
STR_DATE_FORMAT_YMD,
2021-10-17 15:31:56 +02:00
STR_DATE_FORMAT_YDM,
};
openrct2_timeofday gRealTimeOfDay;
int32_t date_get_month(int32_t months)
2014-04-09 11:16:35 +01:00
{
2017-06-06 23:24:18 +02:00
return months % MONTH_COUNT;
2014-04-09 11:16:35 +01:00
}
int32_t date_get_year(int32_t months)
2014-04-09 11:16:35 +01:00
{
2017-06-06 23:24:18 +02:00
return months / MONTH_COUNT;
2014-04-09 11:16:35 +01:00
}
int32_t date_get_total_months(int32_t month, int32_t year)
2014-04-10 03:23:12 +01:00
{
2017-06-06 23:24:18 +02:00
return (year - 1) * MONTH_COUNT + month;
2014-04-10 03:23:12 +01:00
}
2014-04-09 11:16:35 +01:00
/**
*
* rct2: 0x006C4494
*/
void date_reset()
{
2017-06-06 23:24:18 +02:00
gDateMonthsElapsed = 0;
gDateMonthTicks = 0;
gCurrentRealTimeTicks = 0;
}
void date_set(int32_t year, int32_t month, int32_t day)
{
2019-03-31 19:48:51 +02:00
year = std::clamp(year, 1, MAX_YEAR);
2020-04-22 17:09:29 +02:00
month = std::clamp(month, 1, static_cast<int>(MONTH_COUNT));
day = std::clamp(day, 1, static_cast<int>(days_in_month[month - 1]));
gDateMonthsElapsed = (year - 1) * MONTH_COUNT + month - 1;
gDateMonthTicks = TICKS_PER_MONTH / days_in_month[month - 1] * (day - 1) + 4;
}
void date_update()
{
2022-01-18 10:21:20 -08:00
PROFILED_FUNCTION();
int32_t monthTicks = gDateMonthTicks + 4;
if (monthTicks >= TICKS_PER_MONTH)
{
gDateMonthTicks = 0;
gDateMonthsElapsed++;
}
else
{
2020-04-22 17:09:29 +02:00
gDateMonthTicks = floor2(static_cast<uint16_t>(monthTicks), 4);
}
}
void date_update_real_time_of_day()
{
2018-01-07 21:43:07 +01:00
time_t timestamp = time(nullptr);
2018-06-22 23:01:56 +02:00
struct tm* now = localtime(&timestamp);
2017-06-06 23:24:18 +02:00
gRealTimeOfDay.second = now->tm_sec;
gRealTimeOfDay.minute = now->tm_min;
gRealTimeOfDay.hour = now->tm_hour;
}
bool date_is_day_start(int32_t monthTicks)
{
if (monthTicks < 4)
{
return false;
}
int32_t prevMonthTick = monthTicks - 4;
int32_t currentMonth = date_get_month(gDateMonthsElapsed);
int32_t currentDaysInMonth = days_in_month[currentMonth];
return ((currentDaysInMonth * monthTicks) >> 16 != (currentDaysInMonth * prevMonthTick) >> 16);
}
bool date_is_week_start(int32_t monthTicks)
{
return (monthTicks & 0x3FFF) == 0;
}
bool date_is_fortnight_start(int32_t monthTicks)
{
return (monthTicks & 0x7FFF) == 0;
}
bool date_is_month_start(int32_t monthTicks)
{
return (monthTicks == 0);
}