1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#include "SDL_time_c.h"
24
25/* The following algorithms are based on those of Howard Hinnant and are in the public domain.
26 *
27 * http://howardhinnant.github.io/date_algorithms.html
28 */
29
30/* Given a calendar date, returns days since Jan 1 1970, and optionally
31 * the day of the week [0-6, 0 is Sunday] and day of the year [0-365].
32 */
33Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year)
34{
35
36 year -= month <= 2;
37 const int era = (year >= 0 ? year : year - 399) / 400;
38 const unsigned yoe = (unsigned)(year - era * 400); // [0, 399]
39 const unsigned doy = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1; // [0, 365]
40 const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
41 const Sint64 z = (Sint64)(era) * 146097 + (Sint64)(doe)-719468;
42
43 if (day_of_week) {
44 *day_of_week = (int)(z >= -4 ? (z + 4) % 7 : (z + 5) % 7 + 6);
45 }
46 if (day_of_year) {
47 // This algorithm considers March 1 to be the first day of the year, so offset by Jan + Feb.
48 if (doy > 305) {
49 // Day 0 is the first day of the year.
50 *day_of_year = doy - 306;
51 } else {
52 const int doy_offset = 59 + (!(year % 4) && ((year % 100) || !(year % 400)));
53 *day_of_year = doy + doy_offset;
54 }
55 }
56
57 return z;
58}
59
60bool SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat)
61{
62 // Default to ISO 8061 date format, as it is unambiguous, and 24 hour time.
63 if (dateFormat) {
64 *dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
65 }
66 if (timeFormat) {
67 *timeFormat = SDL_TIME_FORMAT_24HR;
68 }
69
70 SDL_GetSystemTimeLocalePreferences(dateFormat, timeFormat);
71
72 return true;
73}
74
75int SDL_GetDaysInMonth(int year, int month)
76{
77 static const int DAYS_IN_MONTH[] = {
78 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
79 };
80
81 if (month < 1 || month > 12) {
82 SDL_SetError("Month out of range [1-12], requested: %i", month);
83 return -1;
84 }
85
86 int days = DAYS_IN_MONTH[month - 1];
87
88 /* A leap year occurs every 4 years...
89 * but not every 100 years...
90 * except for every 400 years.
91 */
92 if (month == 2 && (!(year % 4) && ((year % 100) || !(year % 400)))) {
93 ++days;
94 }
95
96 return days;
97}
98
99int SDL_GetDayOfYear(int year, int month, int day)
100{
101 int dayOfYear;
102
103 if (month < 1 || month > 12) {
104 SDL_SetError("Month out of range [1-12], requested: %i", month);
105 return -1;
106 }
107 if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
108 SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
109 return -1;
110 }
111
112 SDL_CivilToDays(year, month, day, NULL, &dayOfYear);
113 return dayOfYear;
114}
115
116int SDL_GetDayOfWeek(int year, int month, int day)
117{
118 int dayOfWeek;
119
120 if (month < 1 || month > 12) {
121 SDL_SetError("Month out of range [1-12], requested: %i", month);
122 return -1;
123 }
124 if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
125 SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
126 return -1;
127 }
128
129 SDL_CivilToDays(year, month, day, &dayOfWeek, NULL);
130 return dayOfWeek;
131}
132
133static bool SDL_DateTimeIsValid(const SDL_DateTime *dt)
134{
135 if (dt->month < 1 || dt->month > 12) {
136 SDL_SetError("Malformed SDL_DateTime: month out of range [1-12], current: %i", dt->month);
137 return false;
138 }
139
140 const int daysInMonth = SDL_GetDaysInMonth(dt->year, dt->month);
141 if (dt->day < 1 || dt->day > daysInMonth) {
142 SDL_SetError("Malformed SDL_DateTime: day of month out of range [1-%i], current: %i", daysInMonth, dt->month);
143 return false;
144 }
145 if (dt->hour < 0 || dt->hour > 23) {
146 SDL_SetError("Malformed SDL_DateTime: hour out of range [0-23], current: %i", dt->hour);
147 return false;
148 }
149 if (dt->minute < 0 || dt->minute > 59) {
150 SDL_SetError("Malformed SDL_DateTime: minute out of range [0-59], current: %i", dt->minute);
151 return false;
152 }
153 if (dt->second < 0 || dt->second > 60) {
154 SDL_SetError("Malformed SDL_DateTime: second out of range [0-60], current: %i", dt->second);
155 return false; // 60 accounts for a possible leap second.
156 }
157 if (dt->nanosecond < 0 || dt->nanosecond >= SDL_NS_PER_SECOND) {
158 SDL_SetError("Malformed SDL_DateTime: nanosecond out of range [0-999999999], current: %i", dt->nanosecond);
159 return false;
160 }
161
162 return true;
163}
164
165bool SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks)
166{
167 static const Sint64 max_seconds = SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1;
168 static const Sint64 min_seconds = SDL_NS_TO_SECONDS(SDL_MIN_TIME) + 1;
169 bool result = true;
170
171 if (!dt) {
172 return SDL_InvalidParamError("dt");
173 }
174 if (!ticks) {
175 return SDL_InvalidParamError("ticks");
176 }
177 if (!SDL_DateTimeIsValid(dt)) {
178 // The validation function sets the error string.
179 return false;
180 }
181
182 *ticks = SDL_CivilToDays(dt->year, dt->month, dt->day, NULL, NULL) * SDL_SECONDS_PER_DAY;
183 *ticks += (((dt->hour * 60) + dt->minute) * 60) + dt->second - dt->utc_offset;
184 if (*ticks > max_seconds || *ticks < min_seconds) {
185 *ticks = SDL_clamp(*ticks, min_seconds, max_seconds);
186 result = SDL_SetError("Date out of range for SDL_Time representation; SDL_Time value clamped");
187 }
188 *ticks = SDL_SECONDS_TO_NS(*ticks) + dt->nanosecond;
189
190 return result;
191}
192
193#define DELTA_EPOCH_1601_100NS (11644473600ll * 10000000ll) // [100 ns] (100 ns units between 1601-01-01 and 1970-01-01, 11644473600 seconds)
194
195void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
196{
197 /* Convert nanoseconds to Win32 ticks.
198 * SDL_Time has a range of roughly 292 years, so even SDL_MIN_TIME can't underflow the Win32 epoch.
199 */
200 const Uint64 wtime = (Uint64)((ticks / 100) + DELTA_EPOCH_1601_100NS);
201
202 if (dwLowDateTime) {
203 *dwLowDateTime = (Uint32)wtime;
204 }
205
206 if (dwHighDateTime) {
207 *dwHighDateTime = (Uint32)(wtime >> 32);
208 }
209}
210
211SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
212{
213 static const Uint64 wintime_min = (Uint64)((SDL_MIN_TIME / 100) + DELTA_EPOCH_1601_100NS);
214 static const Uint64 wintime_max = (Uint64)((SDL_MAX_TIME / 100) + DELTA_EPOCH_1601_100NS);
215
216 Uint64 wtime = (((Uint64)dwHighDateTime << 32) | dwLowDateTime);
217
218 // Clamp the windows time range to the SDL_Time min/max
219 wtime = SDL_clamp(wtime, wintime_min, wintime_max);
220
221 return (SDL_Time)(wtime - DELTA_EPOCH_1601_100NS) * 100;
222}
223