Max OS 0.3
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1
9#ifndef MAXOS_COMMON_TIME_H
10#define MAXOS_COMMON_TIME_H
11
12#include <cstdint>
13
14
15namespace MaxOS::common {
16
24 typedef struct Time {
25
29
33
38 [[nodiscard]] bool is_leap_year() const {
39 return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
40 }
41
42 } time_t;
43
45 static const char* Months[] = {
46 "January",
47 "February",
48 "March",
49 "April",
50 "May",
51 "June",
52 "July",
53 "August",
54 "September",
55 "October",
56 "November",
57 "December"
58 };
59
61 static const char* Days[] = {
62 "Sunday",
63 "Monday",
64 "Tuesday",
65 "Wednesday",
66 "Thursday",
67 "Friday",
68 "Saturday"
69 };
70
72 constexpr uint8_t DAYS_IN_MONTH[] = {
73 31, // January
74 28, // February
75 31, // March
76 30, // April
77 31, // May
78 30, // June
79 31, // July
80 31, // August
81 30, // September
82 31, // October
83 30, // November
84 31 // December
85 };
86
87 constexpr uint16_t DAYS_PER_YEAR = 365;
88 constexpr uint16_t DAYS_PER_LEAP_YEAR = 366;
89
96 static uint64_t time_to_epoch(Time time) {
97 uint64_t epoch = 0;
98
99 // Add the number of years
100 for(uint16_t year = 1970; year < time.year; year++)
101 epoch += (time.is_leap_year() ? DAYS_PER_LEAP_YEAR : DAYS_PER_YEAR);
102
103
104 // Add the number of days in the current year
105 for(uint8_t month = 0; month < time.month - 1; month++)
106 epoch += DAYS_IN_MONTH[month];
107
108 // Add the number of days in the current month
109 epoch += time.day - 1;
110
111 // Add the number of hours
112 epoch *= 24;
113 epoch += time.hour;
114
115 // Add the number of minutes
116 epoch *= 60;
117 epoch += time.minute;
118
119 // Add the number of seconds
120 epoch *= 60;
121 epoch += time.second;
122
123 return epoch;
124 }
125
126}
127
128
129#endif //MAXOS_COMMON_TIME_H
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Stores the year, month, day, hour, minute and second of a time.
Definition time.h:24
uint8_t minute
The minute (0-59)
Definition time.h:31
uint8_t month
The month (1-12)
Definition time.h:27
bool is_leap_year() const
Checks if the year is a leap year.
Definition time.h:38
uint16_t year
The year.
Definition time.h:26
uint8_t second
The second (0-59)
Definition time.h:32
uint8_t day
The day (1-31)
Definition time.h:28
uint8_t hour
The hour (0-23)
Definition time.h:30
constexpr uint16_t DAYS_PER_LEAP_YEAR
Number of days in a leap year.
Definition time.h:88
constexpr uint8_t DAYS_IN_MONTH[]
Number of days in each month indexed by month number - 1.
Definition time.h:72
constexpr uint16_t DAYS_PER_YEAR
Number of days in a non-leap year.
Definition time.h:87