|
1 | 1 | #include <Arduino.h>
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * @class Time |
| 5 | + * @brief Represents a point in time with year, month, day, hour, minute, second, and offset. |
| 6 | + * |
| 7 | + * The Time class provides methods to manipulate and retrieve information about a specific point in time. |
| 8 | + * It supports conversion to and from ISO8601 format and UNIX timestamp. |
| 9 | + */ |
3 | 10 | class Time {
|
4 |
| -public: |
5 |
| - Time(int year, int month, int day, int hour, int minute, int second, int offset = 0) { |
6 |
| - this->year = year; |
7 |
| - this->month = month; |
8 |
| - this->day = day; |
9 |
| - this->hour = hour; |
10 |
| - this->minute = minute; |
11 |
| - this->second = second; |
12 |
| - this->offset = offset; |
13 |
| - } |
14 |
| - |
15 |
| - Time(){ |
16 |
| - |
17 |
| - } |
18 |
| - |
19 |
| - void fromISO8601(String ISO8601) { |
20 |
| - parseISO8601(ISO8601); |
21 |
| - } |
22 |
| - |
23 |
| - void fromUNIXTimestamp(String UNIXTimestamp) { |
24 |
| - parseUNIXTimestamp(UNIXTimestamp.toInt()); |
25 |
| - } |
26 |
| - |
27 |
| - void fromComponents(int year, int month, int day, int hour, int minute, int second, int offset = 0) { |
28 |
| - this->year = year; |
29 |
| - this->month = month; |
30 |
| - this->day = day; |
31 |
| - this->hour = hour; |
32 |
| - this->minute = minute; |
33 |
| - this->second = second; |
34 |
| - this->offset = offset; |
35 |
| - } |
36 |
| - |
37 |
| - |
38 |
| - |
39 |
| - |
40 |
| - String getISO8601() { |
41 |
| - char buffer[25]; |
42 |
| - sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:00", year, month, day, hour, minute, second, offset); |
43 |
| - return String(buffer); |
44 |
| - } |
45 |
| - |
46 |
| - String getUNIXTimestampString() { |
47 |
| - // Simple conversion to UNIX timestamp, not accounting for leap years or time zones |
48 |
| - long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926; |
49 |
| - return String(timestamp); |
50 |
| - } |
51 |
| - |
52 |
| - unsigned long getUNIXTimestamp() { |
53 |
| - // Simple conversion to UNIX timestamp, not accounting for leap years or time zones |
54 |
| - unsigned long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926; |
55 |
| - return timestamp; |
56 |
| - } |
57 |
| - |
58 |
| - void parseISO8601(String iso8601) { |
59 |
| - // Simplified parsing, assuming format "YYYY-MM-DDTHH:MM:SS+00:00" |
60 |
| - year = iso8601.substring(0, 4).toInt(); |
61 |
| - month = iso8601.substring(5, 7).toInt(); |
62 |
| - day = iso8601.substring(8, 10).toInt(); |
63 |
| - hour = iso8601.substring(11, 13).toInt(); |
64 |
| - minute = iso8601.substring(14, 16).toInt(); |
65 |
| - second = iso8601.substring(17, 19).toInt(); |
66 |
| - // This does not parse the timezone offset correctly, just a placeholder |
67 |
| - offset = 0; |
68 |
| - // TODO: don't use substrings, use index of comma, dash etc |
69 |
| - } |
70 |
| - |
71 |
| - void parseUNIXTimestamp(long unixTimestamp) { |
72 |
| - // Simplified conversion, not accounting for leap years or time zones |
73 |
| - second = unixTimestamp % 60; |
74 |
| - minute = (unixTimestamp / 60) % 60; |
75 |
| - hour = (unixTimestamp / 3600) % 24; |
76 |
| - day = (unixTimestamp / 86400) % 30; // Simplification |
77 |
| - month = (unixTimestamp / 2629743) % 12 + 1; // Approximation |
78 |
| - year = (unixTimestamp / 31556926) + 1970; // Approximation |
79 |
| - // This does not set the timezone offset, as UNIX timestamp is typically considered UTC |
80 |
| - offset = 0; |
81 |
| - } |
82 |
| - |
83 |
| - void setYear(int year) { this->year = year; } |
84 |
| - void setMonth(int month) { this->month = month; } |
85 |
| - void setDay(int day) { this->day = day; } |
86 |
| - void setHour(int hour) { this->hour = hour; } |
87 |
| - void setMinute(int minute) { this->minute = minute; } |
88 |
| - void setSecond(int second) { this->second = second; } |
89 |
| - void setOffset(int offset) { this->offset = offset; } |
90 |
| - |
91 |
| - int getYear() { return year; } |
92 |
| - int getMonth() { return month; } |
93 |
| - int getDay() { return day; } |
94 |
| - int getHour() { return hour; } |
95 |
| - int getMinute() { return minute; } |
96 |
| - int getSecond() { return second; } |
97 |
| - int getOffset() { return offset; } |
| 11 | + public: |
| 12 | + /** |
| 13 | + * Constructor for Time class. |
| 14 | + * Initializes the year, month, day, hour, minute, second, and offset to zero. |
| 15 | + * @param year The year component of the time. |
| 16 | + * @param month The month component of the time. |
| 17 | + * @param day The day component of the time. |
| 18 | + * @param hour The hour component of the time. |
| 19 | + * @param minute The minute component of the time. |
| 20 | + * @param second The second component of the time. |
| 21 | + * @param offset The timezone offset in hours (default is 0). |
| 22 | + */ |
| 23 | + Time(int year, int month, int day, int hour, int minute, int second, int offset = 0) { |
| 24 | + this->year = year; |
| 25 | + this->month = month; |
| 26 | + this->day = day; |
| 27 | + this->hour = hour; |
| 28 | + this->minute = minute; |
| 29 | + this->second = second; |
| 30 | + this->offset = offset; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Default constructor for Time class. |
| 35 | + * Initializes the year, month, day, hour, minute, second, and offset to zero. |
| 36 | + */ |
| 37 | + Time(){ |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Parses an ISO8601 formatted string and sets the time components accordingly. |
| 43 | + * @param ISO8601 The ISO8601 formatted string to parse. |
| 44 | + */ |
| 45 | + void fromISO8601(String ISO8601) { |
| 46 | + parseISO8601(ISO8601); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Parses a UNIX timestamp and sets the time components accordingly. |
| 51 | + * @param UNIXTimestamp The UNIX timestamp to parse. |
| 52 | + */ |
| 53 | + void fromUNIXTimestamp(String UNIXTimestamp) { |
| 54 | + parseUNIXTimestamp(UNIXTimestamp.toInt()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Initialises the time components with the given values. |
| 59 | + */ |
| 60 | + void fromComponents(int year, int month, int day, int hour, int minute, int second, int offset = 0) { |
| 61 | + this->year = year; |
| 62 | + this->month = month; |
| 63 | + this->day = day; |
| 64 | + this->hour = hour; |
| 65 | + this->minute = minute; |
| 66 | + this->second = second; |
| 67 | + this->offset = offset; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the time in ISO8601 format. |
| 74 | + * @return The time in ISO8601 format. |
| 75 | + */ |
| 76 | + String getISO8601() { |
| 77 | + char buffer[25]; |
| 78 | + sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:00", year, month, day, hour, minute, second, offset); |
| 79 | + return String(buffer); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the time in UNIX timestamp format. |
| 84 | + * @return The time in UNIX timestamp format. |
| 85 | + */ |
| 86 | + String getUNIXTimestampString() { |
| 87 | + // Simple conversion to UNIX timestamp, not accounting for leap years or time zones |
| 88 | + long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926; |
| 89 | + return String(timestamp); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the time in UNIX timestamp format. |
| 94 | + * @return The time in UNIX timestamp format. |
| 95 | + */ |
| 96 | + unsigned long getUNIXTimestamp() { |
| 97 | + // Simple conversion to UNIX timestamp, not accounting for leap years or time zones |
| 98 | + unsigned long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926; |
| 99 | + return timestamp; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns the year component of the time. |
| 104 | + * @return The year component of the time. |
| 105 | + */ |
| 106 | + void parseISO8601(String iso8601) { |
| 107 | + // Simplified parsing, assuming format "YYYY-MM-DDTHH:MM:SS+00:00" |
| 108 | + year = iso8601.substring(0, 4).toInt(); |
| 109 | + month = iso8601.substring(5, 7).toInt(); |
| 110 | + day = iso8601.substring(8, 10).toInt(); |
| 111 | + hour = iso8601.substring(11, 13).toInt(); |
| 112 | + minute = iso8601.substring(14, 16).toInt(); |
| 113 | + second = iso8601.substring(17, 19).toInt(); |
| 114 | + // This does not parse the timezone offset correctly, just a placeholder |
| 115 | + offset = 0; |
| 116 | + // TODO: don't use substrings, use index of comma, dash etc |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Parses a UNIX timestamp and sets the time components accordingly. |
| 121 | + * @param unixTimestamp The UNIX timestamp to parse. |
| 122 | + */ |
| 123 | + void parseUNIXTimestamp(long unixTimestamp) { |
| 124 | + // Simplified conversion, not accounting for leap years or time zones |
| 125 | + second = unixTimestamp % 60; |
| 126 | + minute = (unixTimestamp / 60) % 60; |
| 127 | + hour = (unixTimestamp / 3600) % 24; |
| 128 | + day = (unixTimestamp / 86400) % 30; // Simplification |
| 129 | + month = (unixTimestamp / 2629743) % 12 + 1; // Approximation |
| 130 | + year = (unixTimestamp / 31556926) + 1970; // Approximation |
| 131 | + // This does not set the timezone offset, as UNIX timestamp is typically considered UTC |
| 132 | + offset = 0; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the year component of the time. |
| 137 | + * @param year The year component of the time. |
| 138 | + */ |
| 139 | + void setYear(int year) { this->year = year; } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the month component of the time. |
| 143 | + * @param month The month component of the time. |
| 144 | + */ |
| 145 | + void setMonth(int month) { this->month = month; } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the day component of the time. |
| 149 | + * @param day The day component of the time. |
| 150 | + */ |
| 151 | + void setDay(int day) { this->day = day; } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the hour component of the time. |
| 155 | + * @param hour The hour component of the time. |
| 156 | + */ |
| 157 | + void setHour(int hour) { this->hour = hour; } |
| 158 | + |
| 159 | + /** |
| 160 | + * Sets the minute component of the time. |
| 161 | + * @param minute The minute component of the time. |
| 162 | + */ |
| 163 | + void setMinute(int minute) { this->minute = minute; } |
| 164 | + |
| 165 | + /** |
| 166 | + * Sets the second component of the time. |
| 167 | + * @param second The second component of the time. |
| 168 | + */ |
| 169 | + void setSecond(int second) { this->second = second; } |
| 170 | + |
| 171 | + /** |
| 172 | + * Sets the timezone offset of the time. |
| 173 | + * @param offset The timezone offset of the time. |
| 174 | + */ |
| 175 | + void setOffset(int offset) { this->offset = offset; } |
| 176 | + |
| 177 | + /** |
| 178 | + * Returns the year component of the time. |
| 179 | + * @return The year component of the time. |
| 180 | + */ |
| 181 | + int getYear() { return year; } |
| 182 | + |
| 183 | + /** |
| 184 | + * Returns the month component of the time. |
| 185 | + * @return The month component of the time. |
| 186 | + */ |
| 187 | + int getMonth() { return month; } |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns the day component of the time. |
| 191 | + * @return The day component of the time. |
| 192 | + */ |
| 193 | + int getDay() { return day; } |
| 194 | + |
| 195 | + /** |
| 196 | + * Returns the hour component of the time. |
| 197 | + * @return The hour component of the time. |
| 198 | + */ |
| 199 | + int getHour() { return hour; } |
| 200 | + |
| 201 | + /** |
| 202 | + * Returns the minute component of the time. |
| 203 | + * @return The minute component of the time. |
| 204 | + */ |
| 205 | + int getMinute() { return minute; } |
| 206 | + |
| 207 | + /** |
| 208 | + * Returns the second component of the time. |
| 209 | + * @return The second component of the time. |
| 210 | + */ |
| 211 | + int getSecond() { return second; } |
| 212 | + |
| 213 | + /** |
| 214 | + * Returns the timezone offset of the time. |
| 215 | + * @return The timezone offset of the time. |
| 216 | + */ |
| 217 | + int getOffset() { return offset; } |
98 | 218 |
|
99 | 219 | private:
|
100 | 220 | int year, month, day, hour, minute, second, offset;
|
|
0 commit comments