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

126 lines
3.0 KiB
C++
Raw Normal View History

2014-04-09 11:16:35 +01:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 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"
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;
uint16_t gDateMonthsElapsed;
2016-09-03 17:40:23 +00:00
// rct2: 0x00993988
const int16_t days_in_month[MONTH_COUNT] = { 31, 30, 31, 30, 31, 31, 30, 31 };
2018-01-10 16:14:36 +01:00
// clang-format off
const rct_string_id 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,
STR_DATE_FORMAT_YEAR_DAY_MONTH
};
const rct_string_id DateFormatStringFormatIds[] = {
2017-06-06 23:24:18 +02:00
STR_DATE_FORMAT_DMY,
STR_DATE_FORMAT_MDY,
STR_DATE_FORMAT_YMD,
STR_DATE_FORMAT_YDM
};
2018-01-10 16:14:36 +01:00
// clang-format on
openrct_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;
gCurrentTicks = 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);
2018-08-12 12:50:40 +01:00
month = std::clamp(month, 1, (int)MONTH_COUNT);
day = std::clamp(day, 1, (int)days_in_month[month - 1]);
gDateMonthsElapsed = (year - 1) * MONTH_COUNT + month - 1;
gDateMonthTicks = 0x10000 / days_in_month[month - 1] * (day - 1) + 4;
}
void date_update()
{
int32_t monthTicks = gDateMonthTicks + 4;
if (monthTicks >= 0x10000)
{
gDateMonthTicks = 0;
gDateMonthsElapsed++;
}
else
{
gDateMonthTicks = floor2((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);
}