#include "SDL_internal.h"
#include "SDL_time_c.h"
Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year)
{
year -= month <= 2;
const int era = (year >= 0 ? year : year - 399) / 400;
const unsigned yoe = (unsigned)(year - era * 400); const unsigned doy = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1; const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; const Sint64 z = (Sint64)(era) * 146097 + (Sint64)(doe)-719468;
if (day_of_week) {
*day_of_week = (int)(z >= -4 ? (z + 4) % 7 : (z + 5) % 7 + 6);
}
if (day_of_year) {
if (doy > 305) {
*day_of_year = doy - 306;
} else {
const int doy_offset = 59 + (!(year % 4) && ((year % 100) || !(year % 400)));
*day_of_year = doy + doy_offset;
}
}
return z;
}
bool SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat)
{
if (dateFormat) {
*dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
}
if (timeFormat) {
*timeFormat = SDL_TIME_FORMAT_24HR;
}
SDL_GetSystemTimeLocalePreferences(dateFormat, timeFormat);
return true;
}
int SDL_GetDaysInMonth(int year, int month)
{
static const int DAYS_IN_MONTH[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
if (month < 1 || month > 12) {
SDL_SetError("Month out of range [1-12], requested: %i", month);
return -1;
}
int days = DAYS_IN_MONTH[month - 1];
if (month == 2 && (!(year % 4) && ((year % 100) || !(year % 400)))) {
++days;
}
return days;
}
int SDL_GetDayOfYear(int year, int month, int day)
{
int dayOfYear;
if (month < 1 || month > 12) {
SDL_SetError("Month out of range [1-12], requested: %i", month);
return -1;
}
if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
return -1;
}
SDL_CivilToDays(year, month, day, NULL, &dayOfYear);
return dayOfYear;
}
int SDL_GetDayOfWeek(int year, int month, int day)
{
int dayOfWeek;
if (month < 1 || month > 12) {
SDL_SetError("Month out of range [1-12], requested: %i", month);
return -1;
}
if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
return -1;
}
SDL_CivilToDays(year, month, day, &dayOfWeek, NULL);
return dayOfWeek;
}
static bool SDL_DateTimeIsValid(const SDL_DateTime *dt)
{
if (dt->month < 1 || dt->month > 12) {
SDL_SetError("Malformed SDL_DateTime: month out of range [1-12], current: %i", dt->month);
return false;
}
const int daysInMonth = SDL_GetDaysInMonth(dt->year, dt->month);
if (dt->day < 1 || dt->day > daysInMonth) {
SDL_SetError("Malformed SDL_DateTime: day of month out of range [1-%i], current: %i", daysInMonth, dt->month);
return false;
}
if (dt->hour < 0 || dt->hour > 23) {
SDL_SetError("Malformed SDL_DateTime: hour out of range [0-23], current: %i", dt->hour);
return false;
}
if (dt->minute < 0 || dt->minute > 59) {
SDL_SetError("Malformed SDL_DateTime: minute out of range [0-59], current: %i", dt->minute);
return false;
}
if (dt->second < 0 || dt->second > 60) {
SDL_SetError("Malformed SDL_DateTime: second out of range [0-60], current: %i", dt->second);
return false; }
if (dt->nanosecond < 0 || dt->nanosecond >= SDL_NS_PER_SECOND) {
SDL_SetError("Malformed SDL_DateTime: nanosecond out of range [0-999999999], current: %i", dt->nanosecond);
return false;
}
return true;
}
bool SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks)
{
static const Sint64 max_seconds = SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1;
static const Sint64 min_seconds = SDL_NS_TO_SECONDS(SDL_MIN_TIME) + 1;
bool result = true;
CHECK_PARAM(!dt) {
return SDL_InvalidParamError("dt");
}
CHECK_PARAM(!ticks) {
return SDL_InvalidParamError("ticks");
}
CHECK_PARAM(!SDL_DateTimeIsValid(dt)) {
return false;
}
*ticks = SDL_CivilToDays(dt->year, dt->month, dt->day, NULL, NULL) * SDL_SECONDS_PER_DAY;
*ticks += (((dt->hour * 60) + dt->minute) * 60) + dt->second - dt->utc_offset;
if (*ticks > max_seconds || *ticks < min_seconds) {
*ticks = SDL_clamp(*ticks, min_seconds, max_seconds);
result = SDL_SetError("Date out of range for SDL_Time representation; SDL_Time value clamped");
}
*ticks = SDL_SECONDS_TO_NS(*ticks) + dt->nanosecond;
return result;
}
#define DELTA_EPOCH_1601_100NS (11644473600ll * 10000000ll)
void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
{
const Uint64 wtime = (Uint64)((ticks / 100) + DELTA_EPOCH_1601_100NS);
if (dwLowDateTime) {
*dwLowDateTime = (Uint32)wtime;
}
if (dwHighDateTime) {
*dwHighDateTime = (Uint32)(wtime >> 32);
}
}
SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
{
static const Uint64 wintime_min = (Uint64)((SDL_MIN_TIME / 100) + DELTA_EPOCH_1601_100NS);
static const Uint64 wintime_max = (Uint64)((SDL_MAX_TIME / 100) + DELTA_EPOCH_1601_100NS);
Uint64 wtime = (((Uint64)dwHighDateTime << 32) | dwLowDateTime);
wtime = SDL_clamp(wtime, wintime_min, wintime_max);
return (SDL_Time)(wtime - DELTA_EPOCH_1601_100NS) * 100;
}