libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_date_time.hpp
1#ifndef GIMBAL_DATE_TIME_HPP
2#define GIMBAL_DATE_TIME_HPP
3
5
6namespace gbl {
7
8enum class Month: GblEnum {
9 January = GBL_MONTH_JANUARY = 1,
10 February = GBL_MONTH_FEBRUARY,
11 March = GBL_MONTH_MARCH,
12 April = GBL_MONTH_APRIL,
13 May = GBL_MONTH_MAY,
14 June = GBL_MONTH_JUNE,
15 July = GBL_MONTH_JULY,
16 August = GBL_MONTH_AUGUST,
17 September = GBL_MONTH_SEPTEMBER,
18 October = GBL_MONTH_OCTOBER,
19 November = GBL_MONTH_NOVEMBER,
20 December = GBL_MONTH_DECEMBER,
21 Count = GBL_MONTH_COUNT = 12
22};
23
24enum class WeekDay: GblEnum {
25 Sunday = GBL_WEEK_DAY_SUNDAY,
26 Monday = GBL_WEEK_DAY_MONDAY,
27 Tuesday = GBL_WEEK_DAY_TUESDAY,
28 Wednesday = GBL_WEEK_DAY_WEDNESDAY,
29 Thursday = GBL_WEEK_DAY_THURSDAY,
30 Friday = GBL_WEEK_DAY_FRIDAY,
31 Saturday = GBL_WEEK_DAY_SATURDAY,
32 Count = GBL_WEEK_DAY_COUNT
33};
34
35using Year = ::GblYear;
36using Day = ::GblDay;
37using NanoSecond = ::GblNanoSecond;
38using Second = ::GblSecond;
39using Minute = ::GblMinute;
40using Hour = ::GblHour;
41
42using TimeSpec = ::GblTimeSpec;
43
44TimeSpec operator- (const TimeSpec& lhs, const TimeSpec& rhs);
45TimeSpec& operator-=(TimeSpec& lhs, const TimeSpec& rhs);
46TimeSpec operator+ (const TimeSpec& lhs, const TimeSpec& rhs);
47Timespec& operator+=(TimeSpec& lhs, const TimeSpec& rhs);
48
49struct Date: public GblDate {
50 static bool isLeapYear(Year year);
51 static Day monthDays(Month month, Year year);
52 static const char* monthStr(Month month);
53 static const char* monthStrShort(Month month);
54 static const char* weekDayStr(WeekDay weekday);
55 static const char* weekDayStrShort(WeekDay weekday);
56
57 static Date fromJulian(Day julianDate);
58 static Date fromOrdinal(Year year, Day day);
59
60 void set(Year year, Month month, Day day);
61
62 bool isValid() const;
63 bool isDst() const;
64 Day toJulien() const;
65 WeekDay weekDay() const;
66 Day yearDay() const;
67 uint8_t yearWeek() const;
68};
69
70struct Time: public GblTime {
71 static const char* amPmStr(bool isPm);
72 static Second localUtcOffset();
73
74 void set(Hour hours, Minute mins, Second secs, NanoSecond nsecs);
75
76 bool isValid() const;
77 bool isPm() const;
78};
79
80struct DateTime: public GblDateTime {
81 static DateTime fromUnix(std::time_t epoch);
82 static DateTime fromSpecUtc(TimeSpec spec);
83 static DateTime fromLocal(const std::tm& local);
84 static DateTime fromUtc(const std::tm& utc);
85 static DateTime nowLocal();
86 static DateTime nowUtc();
87 static DateTime parse(const char* pString, const char* pFormat);
88
89 std::time_t toUnix() const;
90 TimeSpec toSpecUtc() const;
91 void toLocal(std::tm* pBrokenDown) const;
92 void toUtc(std::tm* pBrokenDown) const;
93 std::string toIso8601(void) const;
94 std::string format(const char* pFmt) const;
95
96 bool normalize();
97
98 void set(Year year,
99 Month month = Month::January,
100 Day day = 1,
101 Hour hours = 0,
102 Minute mins = 0,
103 Second secs = 0,
104 NanoSecond ns = 0,
105 Second tzOff = 0);
106
107 void setDate(const Date& other);
108 void setTime(const Time& other);
109
110 bool isValid() const;
111 bool isUtc() const;
112 bool isLocal() const;
113
114};
115
116}
117
118
119#endif // GIMBAL_DATE_TIME_HPP
int32_t GblNanoSecond
Represents a nanosecond within a second (0-1000000000.
struct timespec GblTimeSpec
Represents the difference between two GblTime instances.
int32_t GblMinute
Represents a minute in a 60-minute hour (0-59)
int32_t GblDay
Represents a 24-hour day within a month (0-31)
int32_t GblHour
Represents an hour within a 24-hour day (0-23)
int64_t GblSecond
Represents a second in a 60-second minute (0-59)
int32_t GblYear
Represents a calendar year.
@ GBL_WEEK_DAY_TUESDAY
Tuesday.
@ GBL_WEEK_DAY_MONDAY
Monday.
@ GBL_WEEK_DAY_SUNDAY
Sunday.
@ GBL_WEEK_DAY_THURSDAY
Thursday.
@ GBL_WEEK_DAY_SATURDAY
Saturday.
@ GBL_WEEK_DAY_FRIDAY
Friday.
@ GBL_WEEK_DAY_WEDNESDAY
Wednesday.
@ GBL_WEEK_DAY_COUNT
Days/Week.
@ GBL_MONTH_COUNT
Months/Year.
@ GBL_MONTH_OCTOBER
October.
@ GBL_MONTH_DECEMBER
December.
@ GBL_MONTH_FEBRUARY
February.
@ GBL_MONTH_NOVEMBER
November.
@ GBL_MONTH_MAY
May.
@ GBL_MONTH_JULY
July.
@ GBL_MONTH_MARCH
March.
@ GBL_MONTH_APRIL
April.
@ GBL_MONTH_JANUARY
January.
@ GBL_MONTH_SEPTEMBER
September.
@ GBL_MONTH_JUNE
June.
@ GBL_MONTH_AUGUST
August.