SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_time.h
1#ifndef SDL3PP_TIME_H_
2#define SDL3PP_TIME_H_
3
4#include <SDL3/SDL_time.h>
5#include "SDL3pp_stdinc.h"
6
7namespace SDL {
8
26using DateTimeRaw = SDL_DateTime;
27
28// Forward decl
29struct DateTime;
30
37{
43 constexpr DateTime(const DateTimeRaw& dateTime = {}) noexcept
44 : DateTimeRaw(dateTime)
45 {
46 }
47
61 constexpr DateTime(int year,
62 int month,
63 int day,
64 int hour,
65 int minute,
66 int second,
67 int nanosecond,
68 int day_of_week,
69 int utc_offset) noexcept
70 : DateTimeRaw{year,
71 month,
72 day,
73 hour,
74 minute,
75 second,
76 nanosecond,
77 day_of_week,
78 utc_offset}
79 {
80 }
81
94 DateTime(Time ticks, bool localTime = true)
95 {
96 CheckError(SDL_TimeToDateTime(ticks.ToNS(), this, localTime));
97 }
98
104 constexpr explicit operator bool() const noexcept
105 {
106 return year != 0 || month != 0 || day != 0 || hour != 0 || minute != 0 ||
107 second != 0 || nanosecond != 0;
108 }
109
115 constexpr int GetYear() const noexcept { return year; }
116
123 constexpr DateTime& SetYear(int newYear) noexcept
124 {
125 year = newYear;
126 return *this;
127 }
128
134 constexpr int GetMonth() const noexcept { return month; }
135
142 constexpr DateTime& SetMonth(int newMonth) noexcept
143 {
144 month = newMonth;
145 return *this;
146 }
147
153 constexpr int GetDay() const noexcept { return day; }
154
161 constexpr DateTime& SetDay(int newDay) noexcept
162 {
163 day = newDay;
164 return *this;
165 }
166
172 constexpr int GetHour() const noexcept { return hour; }
173
180 constexpr DateTime& SetHour(int newHour) noexcept
181 {
182 hour = newHour;
183 return *this;
184 }
185
191 constexpr int GetMinute() const noexcept { return minute; }
192
199 constexpr DateTime& SetMinute(int newMinute) noexcept
200 {
201 minute = newMinute;
202 return *this;
203 }
204
210 constexpr int GetSecond() const noexcept { return second; }
211
218 constexpr DateTime& SetSecond(int newSecond) noexcept
219 {
220 second = newSecond;
221 return *this;
222 }
223
229 constexpr int GetNanosecond() const noexcept { return nanosecond; }
230
237 constexpr DateTime& SetNanosecond(int newNanosecond) noexcept
238 {
239 nanosecond = newNanosecond;
240 return *this;
241 }
242
248 constexpr int GetDay_of_week() const noexcept { return day_of_week; }
249
256 constexpr DateTime& SetDay_of_week(int newDay_of_week) noexcept
257 {
258 day_of_week = newDay_of_week;
259 return *this;
260 }
261
267 constexpr int GetUtc_offset() const noexcept { return utc_offset; }
268
275 constexpr DateTime& SetUtc_offset(int newUtc_offset) noexcept
276 {
277 utc_offset = newUtc_offset;
278 return *this;
279 }
280
292 operator Time() const;
293};
294
302using DateFormat = SDL_DateFormat;
303
305 SDL_DATE_FORMAT_YYYYMMDD;
306
308 SDL_DATE_FORMAT_DDMMYYYY;
309
311 SDL_DATE_FORMAT_MMDDYYYY;
312
320using TimeFormat = SDL_TimeFormat;
321
322constexpr TimeFormat TIME_FORMAT_24HR = SDL_TIME_FORMAT_24HR;
323
324constexpr TimeFormat TIME_FORMAT_12HR = SDL_TIME_FORMAT_12HR;
325
343 TimeFormat* timeFormat)
344{
345 CheckError(SDL_GetDateTimeLocalePreferences(dateFormat, timeFormat));
346}
347
349{
350 SDL_Time t;
351 CheckError(SDL_GetCurrentTime(&t));
352 return Time::FromNS(t);
353}
354
368inline DateTime TimeToDateTime(Time ticks, bool localTime = true)
369{
370 return DateTime(ticks, localTime);
371}
372
386{
387 SDL_Time t;
388 CheckError(SDL_DateTimeToTime(&dt, &t));
389 return Time::FromNS(t);
390}
391
392inline DateTime::operator Time() const { return SDL::DateTimeToTime(*this); }
393
394inline void Time::ToWindows(Uint32* dwLowDateTime, Uint32* dwHighDateTime) const
395{
396 SDL_TimeToWindows(ToNS(), dwLowDateTime, dwHighDateTime);
397}
398
399inline Time Time::FromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
400{
401 return Time::FromNS(SDL_TimeFromWindows(dwLowDateTime, dwHighDateTime));
402}
403
414inline int GetDaysInMonth(int year, int month)
415{
416 return CheckError(SDL_GetDaysInMonth(year, month), -1);
417}
418
430inline int GetDayOfYear(int year, int month, int day)
431{
432 return CheckError(SDL_GetDayOfYear(year, month, day), -1);
433}
434
446inline int GetDayOfWeek(int year, int month, int day)
447{
448 return CheckError(SDL_GetDayOfWeek(year, month, day), -1);
449}
450
452
453} // namespace SDL
454
455#endif /* SDL3PP_TIME_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition: SDL3pp_stdinc.h:414
constexpr Sint64 ToNS() const
Converts to nanoseconds Sint64.
Definition: SDL3pp_stdinc.h:466
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition: SDL3pp_stdinc.h:460
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
SDL_TimeFormat TimeFormat
The preferred time format of the current system locale.
Definition: SDL3pp_time.h:320
static Time FromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
Converts a Windows FILETIME (100-nanosecond intervals since January 1, 1601) to an SDL time.
Definition: SDL3pp_time.h:399
void ToWindows(Uint32 *dwLowDateTime, Uint32 *dwHighDateTime) const
Converts an SDL time into a Windows FILETIME (100-nanosecond intervals since January 1,...
Definition: SDL3pp_time.h:394
static Time Current()
Gets the current value of the system realtime clock in nanoseconds since Jan 1, 1970 in Universal Coo...
Definition: SDL3pp_time.h:348
SDL_DateTime DateTimeRaw
Alias to raw representation for DateTime.
Definition: SDL3pp_time.h:26
constexpr DateFormat DATE_FORMAT_DDMMYYYY
Day/Month/Year.
Definition: SDL3pp_time.h:307
constexpr TimeFormat TIME_FORMAT_12HR
12 hour time
Definition: SDL3pp_time.h:324
constexpr DateFormat DATE_FORMAT_MMDDYYYY
Month/Day/Year.
Definition: SDL3pp_time.h:310
SDL_DateFormat DateFormat
The preferred date format of the current system locale.
Definition: SDL3pp_time.h:302
constexpr DateFormat DATE_FORMAT_YYYYMMDD
Year/Month/Day.
Definition: SDL3pp_time.h:304
DateTime TimeToDateTime(Time ticks, bool localTime=true)
Converts an Time in nanoseconds since the epoch to a calendar time in the DateTime format.
Definition: SDL3pp_time.h:368
Time DateTimeToTime(const DateTimeRaw &dt)
Converts a calendar time to an Time in nanoseconds since the epoch.
Definition: SDL3pp_time.h:385
constexpr TimeFormat TIME_FORMAT_24HR
24 hour time
Definition: SDL3pp_time.h:322
int GetDayOfYear(int year, int month, int day)
Get the day of year for a calendar date.
Definition: SDL3pp_time.h:430
int GetDaysInMonth(int year, int month)
Get the number of days in a month for a given year.
Definition: SDL3pp_time.h:414
int GetDayOfWeek(int year, int month, int day)
Get the day of week for a calendar date.
Definition: SDL3pp_time.h:446
void GetDateTimeLocalePreferences(DateFormat *dateFormat, TimeFormat *timeFormat)
Gets the current preferred date and time format for the system locale.
Definition: SDL3pp_time.h:342
Main include header for the SDL3pp library.
A structure holding a calendar date and time broken down into its components.
Definition: SDL3pp_time.h:37
constexpr DateTime & SetDay_of_week(int newDay_of_week) noexcept
Set the day_of_week.
Definition: SDL3pp_time.h:256
constexpr DateTime & SetUtc_offset(int newUtc_offset) noexcept
Set the utc_offset.
Definition: SDL3pp_time.h:275
constexpr int GetNanosecond() const noexcept
Get the nanosecond.
Definition: SDL3pp_time.h:229
constexpr int GetUtc_offset() const noexcept
Get the utc_offset.
Definition: SDL3pp_time.h:267
constexpr DateTime & SetHour(int newHour) noexcept
Set the hour.
Definition: SDL3pp_time.h:180
constexpr DateTime & SetDay(int newDay) noexcept
Set the day.
Definition: SDL3pp_time.h:161
constexpr DateTime & SetMonth(int newMonth) noexcept
Set the month.
Definition: SDL3pp_time.h:142
constexpr DateTime & SetMinute(int newMinute) noexcept
Set the minute.
Definition: SDL3pp_time.h:199
constexpr int GetDay_of_week() const noexcept
Get the day_of_week.
Definition: SDL3pp_time.h:248
constexpr DateTime & SetNanosecond(int newNanosecond) noexcept
Set the nanosecond.
Definition: SDL3pp_time.h:237
constexpr int GetMinute() const noexcept
Get the minute.
Definition: SDL3pp_time.h:191
constexpr DateTime(int year, int month, int day, int hour, int minute, int second, int nanosecond, int day_of_week, int utc_offset) noexcept
Constructs from its fields.
Definition: SDL3pp_time.h:61
constexpr int GetSecond() const noexcept
Get the second.
Definition: SDL3pp_time.h:210
constexpr DateTime(const DateTimeRaw &dateTime={}) noexcept
Wraps DateTime.
Definition: SDL3pp_time.h:43
constexpr DateTime & SetSecond(int newSecond) noexcept
Set the second.
Definition: SDL3pp_time.h:218
constexpr DateTime & SetYear(int newYear) noexcept
Set the year.
Definition: SDL3pp_time.h:123
DateTime(Time ticks, bool localTime=true)
Converts an Time in nanoseconds since the epoch to a calendar time in the DateTime format.
Definition: SDL3pp_time.h:94
constexpr int GetHour() const noexcept
Get the hour.
Definition: SDL3pp_time.h:172
constexpr int GetDay() const noexcept
Get the day.
Definition: SDL3pp_time.h:153
constexpr int GetYear() const noexcept
Get the year.
Definition: SDL3pp_time.h:115
constexpr int GetMonth() const noexcept
Get the month.
Definition: SDL3pp_time.h:134