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
38{
44 constexpr DateTime(const DateTimeRaw& dateTime = {})
45 : DateTimeRaw(dateTime)
46 {
47 }
48
62 constexpr DateTime(int year,
63 int month,
64 int day,
65 int hour,
66 int minute,
67 int second,
68 int nanosecond,
69 int day_of_week,
70 int utc_offset)
71 : DateTimeRaw{year,
72 month,
73 day,
74 hour,
75 minute,
76 second,
77 nanosecond,
78 day_of_week,
79 utc_offset}
80 {
81 }
82
95 DateTime(Time ticks, bool localTime = true)
96 {
97 CheckError(SDL_TimeToDateTime(ticks.ToNS(), this, localTime));
98 }
99
105 constexpr explicit operator bool() const
106 {
107 return year != 0 || month != 0 || day != 0 || hour != 0 || minute != 0 ||
108 second != 0 || nanosecond != 0;
109 }
110
116 constexpr int GetYear() const { return year; }
117
124 constexpr DateTime& SetYear(int newYear)
125 {
126 year = newYear;
127 return *this;
128 }
129
135 constexpr int GetMonth() const { return month; }
136
143 constexpr DateTime& SetMonth(int newMonth)
144 {
145 month = newMonth;
146 return *this;
147 }
148
154 constexpr int GetDay() const { return day; }
155
162 constexpr DateTime& SetDay(int newDay)
163 {
164 day = newDay;
165 return *this;
166 }
167
173 constexpr int GetHour() const { return hour; }
174
181 constexpr DateTime& SetHour(int newHour)
182 {
183 hour = newHour;
184 return *this;
185 }
186
192 constexpr int GetMinute() const { return minute; }
193
200 constexpr DateTime& SetMinute(int newMinute)
201 {
202 minute = newMinute;
203 return *this;
204 }
205
211 constexpr int GetSecond() const { return second; }
212
219 constexpr DateTime& SetSecond(int newSecond)
220 {
221 second = newSecond;
222 return *this;
223 }
224
230 constexpr int GetNanosecond() const { return nanosecond; }
231
238 constexpr DateTime& SetNanosecond(int newNanosecond)
239 {
240 nanosecond = newNanosecond;
241 return *this;
242 }
243
249 constexpr int GetDay_of_week() const { return day_of_week; }
250
257 constexpr DateTime& SetDay_of_week(int newDay_of_week)
258 {
259 day_of_week = newDay_of_week;
260 return *this;
261 }
262
268 constexpr int GetUtc_offset() const { return utc_offset; }
269
276 constexpr DateTime& SetUtc_offset(int newUtc_offset)
277 {
278 utc_offset = newUtc_offset;
279 return *this;
280 }
281
293 operator Time() const;
294};
295
303using DateFormat = SDL_DateFormat;
304
306 SDL_DATE_FORMAT_YYYYMMDD;
307
309 SDL_DATE_FORMAT_DDMMYYYY;
310
312 SDL_DATE_FORMAT_MMDDYYYY;
313
321using TimeFormat = SDL_TimeFormat;
322
323constexpr TimeFormat TIME_FORMAT_24HR = SDL_TIME_FORMAT_24HR;
324
325constexpr TimeFormat TIME_FORMAT_12HR = SDL_TIME_FORMAT_12HR;
326
344 TimeFormat* timeFormat)
345{
346 CheckError(SDL_GetDateTimeLocalePreferences(dateFormat, timeFormat));
347}
348
350{
351 SDL_Time t;
352 CheckError(SDL_GetCurrentTime(&t));
353 return Time::FromNS(t);
354}
355
369inline DateTime TimeToDateTime(Time ticks, bool localTime = true)
370{
371 return DateTime(ticks, localTime);
372}
373
387{
388 SDL_Time t;
389 CheckError(SDL_DateTimeToTime(&dt, &t));
390 return Time::FromNS(t);
391}
392
393inline DateTime::operator Time() const { return SDL::DateTimeToTime(*this); }
394
395inline void Time::ToWindows(Uint32* dwLowDateTime, Uint32* dwHighDateTime) const
396{
397 SDL_TimeToWindows(ToNS(), dwLowDateTime, dwHighDateTime);
398}
399
400inline Time Time::FromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
401{
402 return Time::FromNS(SDL_TimeFromWindows(dwLowDateTime, dwHighDateTime));
403}
404
415inline int GetDaysInMonth(int year, int month)
416{
417 return CheckError(SDL_GetDaysInMonth(year, month), -1);
418}
419
431inline int GetDayOfYear(int year, int month, int day)
432{
433 return CheckError(SDL_GetDayOfYear(year, month, day), -1);
434}
435
447inline int GetDayOfWeek(int year, int month, int day)
448{
449 return CheckError(SDL_GetDayOfWeek(year, month, day), -1);
450}
451
453
454} // namespace SDL
455
456#endif /* SDL3PP_TIME_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition: SDL3pp_stdinc.h:383
constexpr Sint64 ToNS() const
Converts to nanoseconds Sint64.
Definition: SDL3pp_stdinc.h:435
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition: SDL3pp_stdinc.h:429
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:310
SDL_TimeFormat TimeFormat
The preferred time format of the current system locale.
Definition: SDL3pp_time.h:321
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:400
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:395
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:349
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:308
constexpr TimeFormat TIME_FORMAT_12HR
12 hour time
Definition: SDL3pp_time.h:325
constexpr DateFormat DATE_FORMAT_MMDDYYYY
Month/Day/Year.
Definition: SDL3pp_time.h:311
SDL_DateFormat DateFormat
The preferred date format of the current system locale.
Definition: SDL3pp_time.h:303
constexpr DateFormat DATE_FORMAT_YYYYMMDD
Year/Month/Day.
Definition: SDL3pp_time.h:305
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:369
Time DateTimeToTime(const DateTimeRaw &dt)
Converts a calendar time to an Time in nanoseconds since the epoch.
Definition: SDL3pp_time.h:386
constexpr TimeFormat TIME_FORMAT_24HR
24 hour time
Definition: SDL3pp_time.h:323
int GetDayOfYear(int year, int month, int day)
Get the day of year for a calendar date.
Definition: SDL3pp_time.h:431
int GetDaysInMonth(int year, int month)
Get the number of days in a month for a given year.
Definition: SDL3pp_time.h:415
int GetDayOfWeek(int year, int month, int day)
Get the day of week for a calendar date.
Definition: SDL3pp_time.h:447
void GetDateTimeLocalePreferences(DateFormat *dateFormat, TimeFormat *timeFormat)
Gets the current preferred date and time format for the system locale.
Definition: SDL3pp_time.h:343
Main include header for the SDL3pp library.
A structure holding a calendar date and time broken down into its components.
Definition: SDL3pp_time.h:38
constexpr DateTime & SetMonth(int newMonth)
Set the month.
Definition: SDL3pp_time.h:143
constexpr DateTime & SetUtc_offset(int newUtc_offset)
Set the utc_offset.
Definition: SDL3pp_time.h:276
constexpr DateTime & SetMinute(int newMinute)
Set the minute.
Definition: SDL3pp_time.h:200
constexpr DateTime & SetDay_of_week(int newDay_of_week)
Set the day_of_week.
Definition: SDL3pp_time.h:257
constexpr int GetMonth() const
Get the month.
Definition: SDL3pp_time.h:135
constexpr int GetYear() const
Get the year.
Definition: SDL3pp_time.h:116
constexpr DateTime(const DateTimeRaw &dateTime={})
Wraps DateTime.
Definition: SDL3pp_time.h:44
constexpr int GetSecond() const
Get the second.
Definition: SDL3pp_time.h:211
constexpr int GetUtc_offset() const
Get the utc_offset.
Definition: SDL3pp_time.h:268
constexpr int GetMinute() const
Get the minute.
Definition: SDL3pp_time.h:192
constexpr int GetHour() const
Get the hour.
Definition: SDL3pp_time.h:173
constexpr int GetNanosecond() const
Get the nanosecond.
Definition: SDL3pp_time.h:230
constexpr DateTime & SetDay(int newDay)
Set the day.
Definition: SDL3pp_time.h:162
constexpr int GetDay() const
Get the day.
Definition: SDL3pp_time.h:154
constexpr DateTime & SetNanosecond(int newNanosecond)
Set the nanosecond.
Definition: SDL3pp_time.h:238
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:95
constexpr DateTime & SetYear(int newYear)
Set the year.
Definition: SDL3pp_time.h:124
constexpr DateTime(int year, int month, int day, int hour, int minute, int second, int nanosecond, int day_of_week, int utc_offset)
Constructs from its fields.
Definition: SDL3pp_time.h:62
constexpr int GetDay_of_week() const
Get the day_of_week.
Definition: SDL3pp_time.h:249
constexpr DateTime & SetSecond(int newSecond)
Set the second.
Definition: SDL3pp_time.h:219
constexpr DateTime & SetHour(int newHour)
Set the hour.
Definition: SDL3pp_time.h:181