AceTime 2.4.0 Date and time classes for Arduino that support timezones from the TZ Database.

LocalDateTime.h
1 /*
2 * MIT License
3 * Copyright (c) 2018 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_LOCAL_DATE_TIME_H
7 #define ACE_TIME_LOCAL_DATE_TIME_H
8
9 #include <stddef.h> // size_t
10 #include <stdint.h> // uint8_t, etc
11 #include <string.h> // strlen()
12 #include "LocalDate.h"
13 #include "LocalTime.h"
14
15 class Print;
16 class __FlashStringHelper;
17
18 namespace ace_time {
19
30 class LocalDateTime {
31 public:
32
44 static LocalDateTime forComponents(int16_t year, uint8_t month,
45 uint8_t day, uint8_t hour, uint8_t minute, uint8_t second,
46 uint8_t fold = 0) {
47 LocalDate ld = LocalDate::forComponents(year, month, day);
48 LocalTime lt = LocalTime::forComponents(hour, minute, second, fold);
49 return LocalDateTime(ld, lt);
50 }
51
63 static LocalDateTime forEpochSeconds(
64 acetime_t epochSeconds, uint8_t fold = 0) {
65 if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
66 return forError();
67 }
68
69 // Integer floor-division towards -infinity
70 int32_t days = (epochSeconds < 0)
71 ? (epochSeconds + 1) / 86400 - 1
72 : epochSeconds / 86400;
73
74 // Avoid % operator, because it's slow on an 8-bit process and because
75 // epochSeconds could be negative.
76 int32_t seconds = epochSeconds - 86400 * days;
77
78 LocalDate ld = LocalDate::forEpochDays(days);
79 LocalTime lt = LocalTime::forSeconds(seconds, fold);
80 return LocalDateTime(ld, lt);
81 }
82
90 static LocalDateTime forUnixSeconds64(
91 int64_t unixSeconds, uint8_t fold = 0) {
92 if (unixSeconds == LocalDate::kInvalidUnixSeconds64) {
93 return forError();
94 }
95
96 int64_t epochSeconds64 = unixSeconds
97 - Epoch::secondsToCurrentEpochFromUnixEpoch64();
98
99 // Integer floor-division towards -infinity
100 int32_t days = (epochSeconds64 < 0)
101 ? (epochSeconds64 + 1) / 86400 - 1
102 : epochSeconds64 / 86400;
103 int32_t seconds = epochSeconds64 - (int64_t) 86400 * days;
104
105 LocalDate ld = LocalDate::forEpochDays(days);
106 LocalTime lt = LocalTime::forSeconds(seconds, fold);
107 return LocalDateTime(ld, lt);
108 }
109
125 static LocalDateTime forDateString(const char* dateString);
126
131 static LocalDateTime forDateString(const __FlashStringHelper* dateString);
132
140 static LocalDateTime forDateStringChainable(const char*& dateString);
141
143 static LocalDateTime forError() {
144 return LocalDateTime(LocalDate::forError(), LocalTime::forError());
145 }
146
148 explicit LocalDateTime() {}
149
151 bool isError() const {
152 return mLocalDate.isError() || mLocalTime.isError();
153 }
154
156 int16_t year() const { return mLocalDate.year(); }
157
159 void year(int16_t year) { mLocalDate.year(year); }
160
162 uint8_t month() const { return mLocalDate.month(); }
163
165 void month(uint8_t month) { mLocalDate.month(month); }
166
168 uint8_t day() const { return mLocalDate.day(); }
169
171 void day(uint8_t day) { mLocalDate.day(day); }
172
174 uint8_t hour() const { return mLocalTime.hour(); }
175
177 void hour(uint8_t hour) { mLocalTime.hour(hour); }
178
180 uint8_t minute() const { return mLocalTime.minute(); }
181
183 void minute(uint8_t minute) { mLocalTime.minute(minute); }
184
186 uint8_t second() const { return mLocalTime.second(); }
187
189 void second(uint8_t second) { mLocalTime.second(second); }
190
192 uint8_t fold() const { return mLocalTime.fold(); }
193
195 void fold(uint8_t fold) { mLocalTime.fold(fold); }
196
198 uint8_t dayOfWeek() const { return mLocalDate.dayOfWeek(); }
199
201 const LocalDate& localDate() const { return mLocalDate; }
202
204 const LocalTime& localTime() const { return mLocalTime; }
205
211 int32_t toEpochDays() const {
212 if (isError()) return LocalDate::kInvalidEpochDays;
213 return mLocalDate.toEpochDays();
214 }
215
217 int32_t toUnixDays() const {
218 if (isError()) return LocalDate::kInvalidEpochDays;
219 return toEpochDays() + Epoch::daysToCurrentEpochFromUnixEpoch();
220 }
221
230 acetime_t toEpochSeconds() const {
231 if (isError()) return LocalDate::kInvalidEpochSeconds;
232 int32_t days = mLocalDate.toEpochDays();
233 int32_t seconds = mLocalTime.toSeconds();
234 return (int32_t) 86400 * days + seconds;
235 }
236
245 int64_t toUnixSeconds64() const {
246 if (isError()) return LocalDate::kInvalidUnixSeconds64;
247 int32_t days = toUnixDays();
248 int32_t seconds = mLocalTime.toSeconds();
249 return (int64_t) 86400 * days + seconds;
250 }
251
258 int8_t compareTo(const LocalDateTime& that) const {
259 int8_t dateCompare = localDate().compareTo(that.localDate());
260 if (dateCompare != 0) return dateCompare;
261 int8_t timeCompare = localTime().compareTo(that.localTime());
262 if (timeCompare != 0) return timeCompare;
263 return 0;
264 }
265
271 void printTo(Print& printer) const;
272
273 // Use default copy constructor and assignment operator.
274 LocalDateTime(const LocalDateTime&) = default;
275 LocalDateTime& operator=(const LocalDateTime&) = default;
276
277 private:
278 friend bool operator==(const LocalDateTime& a, const LocalDateTime& b);
279
281 static const uint8_t kDateTimeStringLength = 19;
282
284 explicit LocalDateTime(const LocalDate& ld, const LocalTime& lt):
285 mLocalDate(ld),
286 mLocalTime(lt) {}
287
288 LocalDate mLocalDate;
289 LocalTime mLocalTime;
290 };
291
297 inline bool operator==(const LocalDateTime& a, const LocalDateTime& b) {
298 return a.mLocalDate == b.mLocalDate
299 && a.mLocalTime == b.mLocalTime;
300 }
301
303 inline bool operator!=(const LocalDateTime& a, const LocalDateTime& b) {
304 return ! (a == b);
305 }
306
307 }
308
309 #endif
ace_time::Epoch::daysToCurrentEpochFromUnixEpoch
static int32_t daysToCurrentEpochFromUnixEpoch()
Return the number of days from the Unix epoch (1970-01-01T00:00:00) to the current epoch.
Definition: Epoch.h:58
ace_time::Epoch::secondsToCurrentEpochFromUnixEpoch64
static int64_t secondsToCurrentEpochFromUnixEpoch64()
Return the number of seconds from the Unix epoch (1970-01-01T00:00:00) to the current epoch.
Definition: Epoch.h:69
ace_time::LocalDateTime
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
ace_time::LocalDateTime::toUnixSeconds64
int64_t toUnixSeconds64() const
Return 64-bit seconds from Unix epoch 1970-01-01 00:00:00 UTC, after assuming that the date and time ...
Definition: LocalDateTime.h:245
ace_time::LocalDateTime::hour
void hour(uint8_t hour)
Set the hour.
Definition: LocalDateTime.h:177
ace_time::LocalDateTime::printTo
void printTo(Print &printer) const
Print LocalDateTime to 'printer' in ISO 8601 format.
Definition: LocalDateTime.cpp:16
ace_time::LocalDateTime::forEpochSeconds
static LocalDateTime forEpochSeconds(acetime_t epochSeconds, uint8_t fold=0)
Factory method.
Definition: LocalDateTime.h:63
ace_time::LocalDateTime::LocalDateTime
LocalDateTime()
Constructor.
Definition: LocalDateTime.h:148
ace_time::LocalDateTime::fold
void fold(uint8_t fold)
Set the fold.
Definition: LocalDateTime.h:195
ace_time::LocalDateTime::forComponents
static LocalDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint8_t fold=0)
Factory method using separated date and time components.
Definition: LocalDateTime.h:44
ace_time::LocalDateTime::minute
void minute(uint8_t minute)
Set the minute.
Definition: LocalDateTime.h:183
ace_time::LocalDateTime::day
uint8_t day() const
Return the day of the month.
Definition: LocalDateTime.h:168
ace_time::LocalDateTime::localDate
const LocalDate & localDate() const
Return the LocalDate.
Definition: LocalDateTime.h:201
ace_time::LocalDateTime::isError
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDateTime.h:151
ace_time::LocalDateTime::compareTo
int8_t compareTo(const LocalDateTime &that) const
Compare 'this' LocalDateTime with 'that' LocalDateTime, and return (<0, 0, >0) according to whether '...
Definition: LocalDateTime.h:258
ace_time::LocalDateTime::forUnixSeconds64
static LocalDateTime forUnixSeconds64(int64_t unixSeconds, uint8_t fold=0)
Factory method that takes the 64-bit number of seconds since Unix Epoch of 1970-01-01.
Definition: LocalDateTime.h:90
ace_time::LocalDateTime::month
void month(uint8_t month)
Set the month.
Definition: LocalDateTime.h:165
ace_time::LocalDateTime::forError
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
Definition: LocalDateTime.h:143
ace_time::LocalDateTime::month
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDateTime.h:162
ace_time::LocalDateTime::fold
uint8_t fold() const
Return the fold.
Definition: LocalDateTime.h:192
ace_time::LocalDateTime::forDateString
static LocalDateTime forDateString(const char *dateString)
Factory method.
Definition: LocalDateTime.cpp:40
ace_time::LocalDateTime::second
uint8_t second() const
Return the second.
Definition: LocalDateTime.h:186
ace_time::LocalDateTime::day
void day(uint8_t day)
Set the day of the month.
Definition: LocalDateTime.h:171
ace_time::LocalDateTime::minute
uint8_t minute() const
Return the minute.
Definition: LocalDateTime.h:180
ace_time::LocalDateTime::year
void year(int16_t year)
Set the year.
Definition: LocalDateTime.h:159
ace_time::LocalDateTime::toUnixDays
int32_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
Definition: LocalDateTime.h:217
ace_time::LocalDateTime::localTime
const LocalTime & localTime() const
Return the LocalTime.
Definition: LocalDateTime.h:204
ace_time::LocalDateTime::toEpochDays
int32_t toEpochDays() const
Return number of whole days since AceTime epoch.
Definition: LocalDateTime.h:211
ace_time::LocalDateTime::hour
uint8_t hour() const
Return the hour.
Definition: LocalDateTime.h:174
ace_time::LocalDateTime::forDateStringChainable
static LocalDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
Definition: LocalDateTime.cpp:65
ace_time::LocalDateTime::dayOfWeek
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
Definition: LocalDateTime.h:198
ace_time::LocalDateTime::operator==
friend bool operator==(const LocalDateTime &a, const LocalDateTime &b)
Return true if two LocalDateTime objects are equal in all components.
Definition: LocalDateTime.h:297
ace_time::LocalDateTime::second
void second(uint8_t second)
Set the second.
Definition: LocalDateTime.h:189
ace_time::LocalDateTime::toEpochSeconds
acetime_t toEpochSeconds() const
Return seconds since the current AceTime epoch defined by Epoch::currentEpochYear().
Definition: LocalDateTime.h:230
ace_time::LocalDateTime::year
int16_t year() const
Return the year.
Definition: LocalDateTime.h:156
ace_time::LocalDate
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:46
ace_time::LocalDate::forComponents
static LocalDate forComponents(int16_t year, uint8_t month, uint8_t day)
Factory method using separated year, month and day fields.
Definition: LocalDate.h:153
ace_time::LocalDate::kInvalidEpochDays
static const int32_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:81
ace_time::LocalDate::isError
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:337
ace_time::LocalDate::forError
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:291
ace_time::LocalDate::kInvalidUnixSeconds64
static const int64_t kInvalidUnixSeconds64
Sentinel unixSeconds64 which indicates an error.
Definition: LocalDate.h:87
ace_time::LocalDate::dayOfWeek
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:324
ace_time::LocalDate::compareTo
int8_t compareTo(const LocalDate &that) const
Compare 'this' LocalDate to 'that' LocalDate, returning (<0, 0, >0) according to whether 'this' occur...
Definition: LocalDate.h:390
ace_time::LocalDate::kInvalidEpochSeconds
static const int32_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:84
ace_time::LocalDate::year
int16_t year() const
Return the year.
Definition: LocalDate.h:301
ace_time::LocalDate::toEpochDays
int32_t toEpochDays() const
Return number of days since the current epoch year sCurrentEpochYear.
Definition: LocalDate.h:352
ace_time::LocalDate::forEpochDays
static LocalDate forEpochDays(int32_t epochDays)
Factory method using the number of days since the current epoch (usually 2000-01-01).
Definition: LocalDate.h:166
ace_time::LocalDate::month
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:307
ace_time::LocalDate::day
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:313
ace_time::LocalTime
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:27
ace_time::LocalTime::forComponents
static LocalTime forComponents(uint8_t hour, uint8_t minute, uint8_t second, uint8_t fold=0)
Factory method using separated date, time, and time zone fields.
Definition: LocalTime.h:43
ace_time::LocalTime::fold
uint8_t fold() const
Return the fold.
Definition: LocalTime.h:136
ace_time::LocalTime::toSeconds
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:145
ace_time::LocalTime::forError
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:95
ace_time::LocalTime::hour
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:118
ace_time::LocalTime::minute
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:124
ace_time::LocalTime::forSeconds
static LocalTime forSeconds(acetime_t seconds, uint8_t fold=0)
Factory method.
Definition: LocalTime.h:56
ace_time::LocalTime::second
uint8_t second() const
Return the second.
Definition: LocalTime.h:130
ace_time::LocalTime::isError
bool isError() const
Return true if any component is outside the normal time range of 00:00:00 to 23:59:59.
Definition: LocalTime.h:108
ace_time::LocalTime::compareTo
int8_t compareTo(const LocalTime &that) const
Compare 'this' LocalTime with 'that' LocalTime, and return (<0, 0, >0) according to whether 'this' oc...
Definition: LocalTime.h:162
ace_time::acetime_t
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24