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
30struct DateTime : SDL_DateTime
31{
37 constexpr DateTime(const SDL_DateTime& dateTime = {})
38 : SDL_DateTime(dateTime)
39 {
40 }
41
55 constexpr DateTime(int year,
56 int month,
57 int day,
58 int hour,
59 int minute,
60 int second,
61 int nanosecond,
62 int day_of_week,
63 int utc_offset)
64 : SDL_DateTime{year,
65 month,
66 day,
67 hour,
68 minute,
69 second,
70 nanosecond,
71 day_of_week,
72 utc_offset}
73 {
74 }
75
88 DateTime(Time ticks, bool localTime = true)
89 {
90 CheckError(SDL_TimeToDateTime(ticks.ToNS(), this, localTime));
91 }
92
98 constexpr explicit operator bool() const
99 {
100 return year != 0 || month != 0 || day != 0 || hour != 0 || minute != 0 ||
101 second != 0 || nanosecond != 0;
102 }
103
109 constexpr int GetYear() const { return year; }
110
117 constexpr DateTime& SetYear(int newYear)
118 {
119 year = newYear;
120 return *this;
121 }
122
128 constexpr int GetMonth() const { return month; }
129
136 constexpr DateTime& SetMonth(int newMonth)
137 {
138 month = newMonth;
139 return *this;
140 }
141
147 constexpr int GetDay() const { return day; }
148
155 constexpr DateTime& SetDay(int newDay)
156 {
157 day = newDay;
158 return *this;
159 }
160
166 constexpr int GetHour() const { return hour; }
167
174 constexpr DateTime& SetHour(int newHour)
175 {
176 hour = newHour;
177 return *this;
178 }
179
185 constexpr int GetMinute() const { return minute; }
186
193 constexpr DateTime& SetMinute(int newMinute)
194 {
195 minute = newMinute;
196 return *this;
197 }
198
204 constexpr int GetSecond() const { return second; }
205
212 constexpr DateTime& SetSecond(int newSecond)
213 {
214 second = newSecond;
215 return *this;
216 }
217
223 constexpr int GetNanosecond() const { return nanosecond; }
224
231 constexpr DateTime& SetNanosecond(int newNanosecond)
232 {
233 nanosecond = newNanosecond;
234 return *this;
235 }
236
242 constexpr int GetDay_of_week() const { return day_of_week; }
243
250 constexpr DateTime& SetDay_of_week(int newDay_of_week)
251 {
252 day_of_week = newDay_of_week;
253 return *this;
254 }
255
261 constexpr int GetUtc_offset() const { return utc_offset; }
262
269 constexpr DateTime& SetUtc_offset(int newUtc_offset)
270 {
271 utc_offset = newUtc_offset;
272 return *this;
273 }
274
286 operator Time() const
287 {
288 SDL_Time t;
289 CheckError(SDL_DateTimeToTime(this, &t));
290 return Time::FromNS(t);
291 }
292};
293
301using DateFormat = SDL_DateFormat;
302
304 SDL_DATE_FORMAT_YYYYMMDD;
305
307 SDL_DATE_FORMAT_DDMMYYYY;
308
310 SDL_DATE_FORMAT_MMDDYYYY;
311
319using TimeFormat = SDL_TimeFormat;
320
321constexpr TimeFormat TIME_FORMAT_24HR = SDL_TIME_FORMAT_24HR;
322
323constexpr TimeFormat TIME_FORMAT_12HR = SDL_TIME_FORMAT_12HR;
324
342 TimeFormat* timeFormat)
343{
344 CheckError(SDL_GetDateTimeLocalePreferences(dateFormat, timeFormat));
345}
346
356{
357 SDL_Time t;
358 CheckError(SDL_GetCurrentTime(&t));
359 return Time::FromNS(t);
360}
361
375inline void Time::ToWindows(Uint32* dwLowDateTime, Uint32* dwHighDateTime) const
376{
377 SDL_TimeToWindows(ToNS(), dwLowDateTime, dwHighDateTime);
378}
379
393inline Time Time::FromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
394{
395 return Time::FromNS(SDL_TimeFromWindows(dwLowDateTime, dwHighDateTime));
396}
397
408inline int GetDaysInMonth(int year, int month)
409{
410 return CheckError(SDL_GetDaysInMonth(year, month), -1);
411}
412
424inline int GetDayOfYear(int year, int month, int day)
425{
426 return CheckError(SDL_GetDayOfYear(year, month, day), -1);
427}
428
440inline int GetDayOfWeek(int year, int month, int day)
441{
442 return CheckError(SDL_GetDayOfWeek(year, month, day), -1);
443}
444
446
447} // namespace SDL
448
449#endif /* SDL3PP_TIME_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition SDL3pp_stdinc.h:261
constexpr Sint64 ToNS() const
Converts to nanoseconds Sint64.
Definition SDL3pp_stdinc.h:297
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition SDL3pp_stdinc.h:291
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:319
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:393
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:375
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:355
constexpr DateFormat DATE_FORMAT_DDMMYYYY
Day/Month/Year.
Definition SDL3pp_time.h:306
constexpr TimeFormat TIME_FORMAT_12HR
12 hour time
Definition SDL3pp_time.h:323
constexpr DateFormat DATE_FORMAT_MMDDYYYY
Month/Day/Year.
Definition SDL3pp_time.h:309
SDL_DateFormat DateFormat
The preferred date format of the current system locale.
Definition SDL3pp_time.h:301
constexpr DateFormat DATE_FORMAT_YYYYMMDD
Year/Month/Day.
Definition SDL3pp_time.h:303
constexpr TimeFormat TIME_FORMAT_24HR
24 hour time
Definition SDL3pp_time.h:321
int GetDayOfYear(int year, int month, int day)
Get the day of year for a calendar date.
Definition SDL3pp_time.h:424
int GetDaysInMonth(int year, int month)
Get the number of days in a month for a given year.
Definition SDL3pp_time.h:408
int GetDayOfWeek(int year, int month, int day)
Get the day of week for a calendar date.
Definition SDL3pp_time.h:440
void GetDateTimeLocalePreferences(DateFormat *dateFormat, TimeFormat *timeFormat)
Gets the current preferred date and time format for the system locale.
Definition SDL3pp_time.h:341
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:31
constexpr DateTime & SetMonth(int newMonth)
Set the month.
Definition SDL3pp_time.h:136
constexpr DateTime & SetUtc_offset(int newUtc_offset)
Set the utc_offset.
Definition SDL3pp_time.h:269
constexpr DateTime & SetMinute(int newMinute)
Set the minute.
Definition SDL3pp_time.h:193
constexpr DateTime & SetDay_of_week(int newDay_of_week)
Set the day_of_week.
Definition SDL3pp_time.h:250
constexpr int GetMonth() const
Get the month.
Definition SDL3pp_time.h:128
constexpr DateTime(const SDL_DateTime &dateTime={})
Wraps DateTime.
Definition SDL3pp_time.h:37
constexpr int GetYear() const
Get the year.
Definition SDL3pp_time.h:109
constexpr int GetSecond() const
Get the second.
Definition SDL3pp_time.h:204
constexpr int GetUtc_offset() const
Get the utc_offset.
Definition SDL3pp_time.h:261
constexpr int GetMinute() const
Get the minute.
Definition SDL3pp_time.h:185
constexpr int GetHour() const
Get the hour.
Definition SDL3pp_time.h:166
constexpr int GetNanosecond() const
Get the nanosecond.
Definition SDL3pp_time.h:223
constexpr DateTime & SetDay(int newDay)
Set the day.
Definition SDL3pp_time.h:155
constexpr int GetDay() const
Get the day.
Definition SDL3pp_time.h:147
constexpr DateTime & SetNanosecond(int newNanosecond)
Set the nanosecond.
Definition SDL3pp_time.h:231
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:88
constexpr DateTime & SetYear(int newYear)
Set the year.
Definition SDL3pp_time.h:117
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:55
constexpr int GetDay_of_week() const
Get the day_of_week.
Definition SDL3pp_time.h:242
constexpr DateTime & SetSecond(int newSecond)
Set the second.
Definition SDL3pp_time.h:212
constexpr DateTime & SetHour(int newHour)
Set the hour.
Definition SDL3pp_time.h:174