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
24
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
96 DateTime(Time ticks, bool localTime = true);
97
103 constexpr explicit operator bool() const noexcept
104 {
105 return year != 0 || month != 0 || day != 0 || hour != 0 || minute != 0 ||
106 second != 0 || nanosecond != 0;
107 }
108
114 constexpr int GetYear() const noexcept { return year; }
115
122 constexpr DateTime& SetYear(int newYear) noexcept
123 {
124 year = newYear;
125 return *this;
126 }
127
133 constexpr int GetMonth() const noexcept { return month; }
134
141 constexpr DateTime& SetMonth(int newMonth) noexcept
142 {
143 month = newMonth;
144 return *this;
145 }
146
152 constexpr int GetDay() const noexcept { return day; }
153
160 constexpr DateTime& SetDay(int newDay) noexcept
161 {
162 day = newDay;
163 return *this;
164 }
165
171 constexpr int GetHour() const noexcept { return hour; }
172
179 constexpr DateTime& SetHour(int newHour) noexcept
180 {
181 hour = newHour;
182 return *this;
183 }
184
190 constexpr int GetMinute() const noexcept { return minute; }
191
198 constexpr DateTime& SetMinute(int newMinute) noexcept
199 {
200 minute = newMinute;
201 return *this;
202 }
203
209 constexpr int GetSecond() const noexcept { return second; }
210
217 constexpr DateTime& SetSecond(int newSecond) noexcept
218 {
219 second = newSecond;
220 return *this;
221 }
222
228 constexpr int GetNanosecond() const noexcept { return nanosecond; }
229
236 constexpr DateTime& SetNanosecond(int newNanosecond) noexcept
237 {
238 nanosecond = newNanosecond;
239 return *this;
240 }
241
247 constexpr int GetDay_of_week() const noexcept { return day_of_week; }
248
255 constexpr DateTime& SetDay_of_week(int newDay_of_week) noexcept
256 {
257 day_of_week = newDay_of_week;
258 return *this;
259 }
260
266 constexpr int GetUtc_offset() const noexcept { return utc_offset; }
267
274 constexpr DateTime& SetUtc_offset(int newUtc_offset) noexcept
275 {
276 utc_offset = newUtc_offset;
277 return *this;
278 }
279
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
346 TimeFormat* timeFormat)
347{
348 CheckError(SDL_GetDateTimeLocalePreferences(dateFormat, timeFormat));
349}
350
351inline Time Time::Current()
352{
353 SDL_Time t;
354 CheckError(SDL_GetCurrentTime(&t));
355 return Time::FromNS(t);
356}
357
373inline DateTime TimeToDateTime(Time ticks, bool localTime = true)
374{
375 return DateTime(ticks, localTime);
376}
377
378inline DateTime::DateTime(Time ticks, bool localTime)
379{
380 CheckError(SDL_TimeToDateTime(ticks.ToNS(), this, localTime));
381}
382
398{
399 SDL_Time t;
400 CheckError(SDL_DateTimeToTime(&dt, &t));
401 return Time::FromNS(t);
402}
403
404inline DateTime::operator Time() const { return SDL::DateTimeToTime(*this); }
405
406inline void Time::ToWindows(Uint32* dwLowDateTime, Uint32* dwHighDateTime) const
407{
408 SDL_TimeToWindows(ToNS(), dwLowDateTime, dwHighDateTime);
409}
410
411inline Time Time::FromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
412{
413 return Time::FromNS(SDL_TimeFromWindows(dwLowDateTime, dwHighDateTime));
414}
415
428inline int GetDaysInMonth(int year, int month)
429{
430 return CheckError(SDL_GetDaysInMonth(year, month), -1);
431}
432
446inline int GetDayOfYear(int year, int month, int day)
447{
448 return CheckError(SDL_GetDayOfYear(year, month, day), -1);
449}
450
464inline int GetDayOfWeek(int year, int month, int day)
465{
466 return CheckError(SDL_GetDayOfWeek(year, month, day), -1);
467}
468
470
471} // namespace SDL
472
473#endif /* SDL3PP_TIME_H_ */
SDL times are signed, 64-bit integers representing nanoseconds since the Unix epoch (Jan 1,...
Definition SDL3pp_stdinc.h:363
constexpr Sint64 ToNS() const
Converts to nanoseconds Sint64.
Definition SDL3pp_stdinc.h:417
static constexpr Time FromNS(Sint64 time)
Create from a nanoseconds Sint64.
Definition SDL3pp_stdinc.h:411
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:290
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:411
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:406
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:351
SDL_DateFormat DateFormat
The preferred date format of the current system locale.
Definition SDL3pp_time.h:303
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
constexpr DateFormat DATE_FORMAT_YYYYMMDD
Year/Month/Day.
Definition SDL3pp_time.h:305
SDL_TimeFormat TimeFormat
The preferred time format of the current system locale.
Definition SDL3pp_time.h:321
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:373
Time DateTimeToTime(const DateTimeRaw &dt)
Converts a calendar time to an Time in nanoseconds since the epoch.
Definition SDL3pp_time.h:397
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:446
int GetDaysInMonth(int year, int month)
Get the number of days in a month for a given year.
Definition SDL3pp_time.h:428
SDL_DateTime DateTimeRaw
Alias to raw representation for DateTime.
Definition SDL3pp_time.h:26
int GetDayOfWeek(int year, int month, int day)
Get the day of week for a calendar date.
Definition SDL3pp_time.h:464
void GetDateTimeLocalePreferences(DateFormat *dateFormat, TimeFormat *timeFormat)
Gets the current preferred date and time format for the system locale.
Definition SDL3pp_time.h:345
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:255
constexpr DateTime & SetUtc_offset(int newUtc_offset) noexcept
Set the utc_offset.
Definition SDL3pp_time.h:274
constexpr int GetNanosecond() const noexcept
Get the nanosecond.
Definition SDL3pp_time.h:228
constexpr int GetUtc_offset() const noexcept
Get the utc_offset.
Definition SDL3pp_time.h:266
constexpr DateTime & SetHour(int newHour) noexcept
Set the hour.
Definition SDL3pp_time.h:179
constexpr DateTime & SetDay(int newDay) noexcept
Set the day.
Definition SDL3pp_time.h:160
constexpr DateTime & SetMonth(int newMonth) noexcept
Set the month.
Definition SDL3pp_time.h:141
constexpr DateTime & SetMinute(int newMinute) noexcept
Set the minute.
Definition SDL3pp_time.h:198
constexpr int GetDay_of_week() const noexcept
Get the day_of_week.
Definition SDL3pp_time.h:247
constexpr DateTime & SetNanosecond(int newNanosecond) noexcept
Set the nanosecond.
Definition SDL3pp_time.h:236
constexpr int GetMinute() const noexcept
Get the minute.
Definition SDL3pp_time.h:190
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:209
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:217
constexpr DateTime & SetYear(int newYear) noexcept
Set the year.
Definition SDL3pp_time.h:122
constexpr int GetHour() const noexcept
Get the hour.
Definition SDL3pp_time.h:171
constexpr int GetDay() const noexcept
Get the day.
Definition SDL3pp_time.h:152
constexpr int GetYear() const noexcept
Get the year.
Definition SDL3pp_time.h:114
constexpr int GetMonth() const noexcept
Get the month.
Definition SDL3pp_time.h:133