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
31struct DateTime : SDL_DateTime
32{
38 constexpr DateTime(const SDL_DateTime& dateTime = {})
39 : SDL_DateTime(dateTime)
40 {
41 }
42
56 constexpr DateTime(int year,
57 int month,
58 int day,
59 int hour,
60 int minute,
61 int second,
62 int nanosecond,
63 int day_of_week,
64 int utc_offset)
65 : SDL_DateTime{year,
66 month,
67 day,
68 hour,
69 minute,
70 second,
71 nanosecond,
72 day_of_week,
73 utc_offset}
74 {
75 }
76
89 DateTime(Time ticks, bool localTime = true)
90 {
91 CheckError(SDL_TimeToDateTime(ticks.ToNS(), this, localTime));
92 }
93
99 constexpr explicit operator bool() const
100 {
101 return year != 0 || month != 0 || day != 0 || hour != 0 || minute != 0 ||
102 second != 0 || nanosecond != 0;
103 }
104
110 constexpr int GetYear() const { return year; }
111
118 constexpr DateTime& SetYear(int newYear)
119 {
120 year = newYear;
121 return *this;
122 }
123
129 constexpr int GetMonth() const { return month; }
130
137 constexpr DateTime& SetMonth(int newMonth)
138 {
139 month = newMonth;
140 return *this;
141 }
142
148 constexpr int GetDay() const { return day; }
149
156 constexpr DateTime& SetDay(int newDay)
157 {
158 day = newDay;
159 return *this;
160 }
161
167 constexpr int GetHour() const { return hour; }
168
175 constexpr DateTime& SetHour(int newHour)
176 {
177 hour = newHour;
178 return *this;
179 }
180
186 constexpr int GetMinute() const { return minute; }
187
194 constexpr DateTime& SetMinute(int newMinute)
195 {
196 minute = newMinute;
197 return *this;
198 }
199
205 constexpr int GetSecond() const { return second; }
206
213 constexpr DateTime& SetSecond(int newSecond)
214 {
215 second = newSecond;
216 return *this;
217 }
218
224 constexpr int GetNanosecond() const { return nanosecond; }
225
232 constexpr DateTime& SetNanosecond(int newNanosecond)
233 {
234 nanosecond = newNanosecond;
235 return *this;
236 }
237
243 constexpr int GetDay_of_week() const { return day_of_week; }
244
251 constexpr DateTime& SetDay_of_week(int newDay_of_week)
252 {
253 day_of_week = newDay_of_week;
254 return *this;
255 }
256
262 constexpr int GetUtc_offset() const { return utc_offset; }
263
270 constexpr DateTime& SetUtc_offset(int newUtc_offset)
271 {
272 utc_offset = newUtc_offset;
273 return *this;
274 }
275
287 operator Time() const
288 {
289 SDL_Time t;
290 CheckError(SDL_DateTimeToTime(this, &t));
291 return Time::FromNS(t);
292 }
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
357{
358 SDL_Time t;
359 CheckError(SDL_GetCurrentTime(&t));
360 return Time::FromNS(t);
361}
362
376inline void Time::ToWindows(Uint32* dwLowDateTime, Uint32* dwHighDateTime) const
377{
378 SDL_TimeToWindows(ToNS(), dwLowDateTime, dwHighDateTime);
379}
380
394inline Time Time::FromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
395{
396 return Time::FromNS(SDL_TimeFromWindows(dwLowDateTime, dwHighDateTime));
397}
398
409inline int GetDaysInMonth(int year, int month)
410{
411 return CheckError(SDL_GetDaysInMonth(year, month), -1);
412}
413
425inline int GetDayOfYear(int year, int month, int day)
426{
427 return CheckError(SDL_GetDayOfYear(year, month, day), -1);
428}
429
441inline int GetDayOfWeek(int year, int month, int day)
442{
443 return CheckError(SDL_GetDayOfWeek(year, month, day), -1);
444}
445
447
448} // namespace SDL
449
450#endif /* SDL3PP_TIME_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition SDL3pp_stdinc.h:293
constexpr Sint64 ToNS() const
Converts to nanoseconds Sint64.
Definition SDL3pp_stdinc.h:329
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition SDL3pp_stdinc.h:323
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
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:394
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:376
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:356
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
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:425
int GetDaysInMonth(int year, int month)
Get the number of days in a month for a given year.
Definition SDL3pp_time.h:409
int GetDayOfWeek(int year, int month, int day)
Get the day of week for a calendar date.
Definition SDL3pp_time.h:441
void GetDateTimeLocalePreferences(DateFormat *dateFormat, TimeFormat *timeFormat)
Gets the current preferred date and time format for the system locale.
Definition SDL3pp_time.h:342
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
A structure holding a calendar date and time broken down into its components.
Definition SDL3pp_time.h:32
constexpr DateTime & SetMonth(int newMonth)
Set the month.
Definition SDL3pp_time.h:137
constexpr DateTime & SetUtc_offset(int newUtc_offset)
Set the utc_offset.
Definition SDL3pp_time.h:270
constexpr DateTime & SetMinute(int newMinute)
Set the minute.
Definition SDL3pp_time.h:194
constexpr DateTime & SetDay_of_week(int newDay_of_week)
Set the day_of_week.
Definition SDL3pp_time.h:251
constexpr int GetMonth() const
Get the month.
Definition SDL3pp_time.h:129
constexpr DateTime(const SDL_DateTime &dateTime={})
Wraps DateTime.
Definition SDL3pp_time.h:38
constexpr int GetYear() const
Get the year.
Definition SDL3pp_time.h:110
constexpr int GetSecond() const
Get the second.
Definition SDL3pp_time.h:205
constexpr int GetUtc_offset() const
Get the utc_offset.
Definition SDL3pp_time.h:262
constexpr int GetMinute() const
Get the minute.
Definition SDL3pp_time.h:186
constexpr int GetHour() const
Get the hour.
Definition SDL3pp_time.h:167
constexpr int GetNanosecond() const
Get the nanosecond.
Definition SDL3pp_time.h:224
constexpr DateTime & SetDay(int newDay)
Set the day.
Definition SDL3pp_time.h:156
constexpr int GetDay() const
Get the day.
Definition SDL3pp_time.h:148
constexpr DateTime & SetNanosecond(int newNanosecond)
Set the nanosecond.
Definition SDL3pp_time.h:232
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:89
constexpr DateTime & SetYear(int newYear)
Set the year.
Definition SDL3pp_time.h:118
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:56
constexpr int GetDay_of_week() const
Get the day_of_week.
Definition SDL3pp_time.h:243
constexpr DateTime & SetSecond(int newSecond)
Set the second.
Definition SDL3pp_time.h:213
constexpr DateTime & SetHour(int newHour)
Set the hour.
Definition SDL3pp_time.h:175