AceTime  0.3
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
LocalDateTime.h
1 #ifndef ACE_TIME_LOCAL_DATE_TIME_H
2 #define ACE_TIME_LOCAL_DATE_TIME_H
3 
4 #include <stdint.h>
5 #include "LocalDate.h"
6 #include "LocalTime.h"
7 
8 class Print;
9 class __FlashStringHelper;
10 
11 namespace ace_time {
12 
14  public:
15 
26  static LocalDateTime forComponents(int16_t year, uint8_t month,
27  uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
28  int8_t yearTiny = LocalDate::isYearValid(year)
29  ? year - LocalDate::kEpochYear
31  return forTinyComponents(yearTiny, month, day, hour, minute, second);
32  }
33 
36  uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
37  return LocalDateTime(
38  LocalDate::forTinyComponents(yearTiny, month, day),
39  LocalTime::forComponents(hour, minute, second));
40  }
41 
53  static LocalDateTime forEpochSeconds(acetime_t epochSeconds) {
54  LocalDate ld;
55  LocalTime lt;
56  if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
57  ld = LocalDate::forError();
58  lt = LocalTime::forError();
59  } else {
60  // Integer floor-division towards -infinity
61  acetime_t days = (epochSeconds < 0)
62  ? (epochSeconds + 1) / 86400 - 1
63  : epochSeconds / 86400;
64 
65  // Avoid % operator, because it's slow on an 8-bit process and because
66  // epochSeconds could be negative.
67  acetime_t seconds = epochSeconds - 86400 * days;
68  ld = LocalDate::forEpochDays(days);
69  lt = LocalTime::forSeconds(seconds);
70  }
71 
72  return LocalDateTime(ld, lt);
73  }
74 
83  static LocalDateTime forUnixSeconds(acetime_t unixSeconds) {
84  acetime_t epochSeconds = (unixSeconds == LocalDate::kInvalidEpochSeconds)
85  ? unixSeconds
86  : unixSeconds - LocalDate::kSecondsSinceUnixEpoch;
87  return forEpochSeconds(epochSeconds);
88  }
89 
106  static LocalDateTime forDateString(const char* dateString);
107 
115  static LocalDateTime forDateStringChainable(const char*& dateString);
116 
121  static LocalDateTime forDateString(const __FlashStringHelper* dateString) {
122  // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
123  // ESP8266 do not have strlcpy_P(). We need +1 for the '\0' character and
124  // another +1 to determine if the dateString is too long to fit.
125  char buffer[kDateTimeStringLength + 2];
126  strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
127  buffer[kDateTimeStringLength + 1] = 0;
128 
129  // check if the original F() was too long
130  size_t len = strlen(buffer);
131  if (len > kDateTimeStringLength) {
132  return forError();
133  }
134 
135  return forDateString(buffer);
136  }
137 
141  }
142 
144  explicit LocalDateTime() {}
145 
147  bool isError() const {
148  return mLocalDate.isError() || mLocalTime.isError();
149  }
150 
152  int16_t year() const { return mLocalDate.year(); }
153 
155  void year(int16_t year) { mLocalDate.year(year); }
156 
158  int8_t yearTiny() const { return mLocalDate.yearTiny(); }
159 
161  void yearTiny(int8_t yearTiny) { mLocalDate.yearTiny(yearTiny); }
162 
164  uint8_t month() const { return mLocalDate.month(); }
165 
167  void month(uint8_t month) { mLocalDate.month(month); }
168 
170  uint8_t day() const { return mLocalDate.day(); }
171 
173  void day(uint8_t day) { mLocalDate.day(day); }
174 
176  uint8_t hour() const { return mLocalTime.hour(); }
177 
179  void hour(uint8_t hour) { mLocalTime.hour(hour); }
180 
182  uint8_t minute() const { return mLocalTime.minute(); }
183 
185  void minute(uint8_t minute) { mLocalTime.minute(minute); }
186 
188  uint8_t second() const { return mLocalTime.second(); }
189 
191  void second(uint8_t second) { mLocalTime.second(second); }
192 
194  uint8_t dayOfWeek() const { return mLocalDate.dayOfWeek(); }
195 
197  const LocalDate& localDate() const { return mLocalDate; }
198 
200  const LocalTime& localTime() const { return mLocalTime; }
201 
205  acetime_t toEpochDays() const {
207  return mLocalDate.toEpochDays();
208  }
209 
211  acetime_t toUnixDays() const {
214  }
215 
221  acetime_t toEpochSeconds() const {
223 
224  acetime_t days = mLocalDate.toEpochDays();
225  acetime_t seconds = mLocalTime.toSeconds();
226  return days * 86400 + seconds;
227  }
228 
237  acetime_t toUnixSeconds() const {
240  }
241 
246  int8_t compareTo(const LocalDateTime& that) const {
247  acetime_t thisSeconds = toEpochSeconds();
248  acetime_t thatSeconds = that.toEpochSeconds();
249  if (thisSeconds < thatSeconds) return -1;
250  if (thisSeconds > thatSeconds) return 1;
251  return 0;
252  }
253 
259  void printTo(Print& printer) const;
260 
261  // Use default copy constructor and assignment operator.
262  LocalDateTime(const LocalDateTime&) = default;
263  LocalDateTime& operator=(const LocalDateTime&) = default;
264 
265  private:
266  friend bool operator==(const LocalDateTime& a, const LocalDateTime& b);
267 
269  static const uint8_t kDateTimeStringLength = 19;
270 
272  explicit LocalDateTime(const LocalDate& ld, const LocalTime& lt):
273  mLocalDate(ld),
274  mLocalTime(lt) {}
275 
276  LocalDate mLocalDate;
277  LocalTime mLocalTime;
278 };
279 
285 inline bool operator==(const LocalDateTime& a, const LocalDateTime& b) {
286  return a.mLocalDate == b.mLocalDate
287  && a.mLocalTime == b.mLocalTime;
288 }
289 
291 inline bool operator!=(const LocalDateTime& a, const LocalDateTime& b) {
292  return ! (a == b);
293 }
294 
295 }
296 
297 #endif
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:131
static const acetime_t kSecondsSinceUnixEpoch
Number of seconds from Unix epoch (1970-01-01 00:00:00Z) to the AceTime epoch (2000-01-01 00:00:00Z)...
Definition: LocalDate.h:51
static LocalDate forEpochDays(acetime_t epochDays)
Factory method using the number of days since AceTime epoch of 2000-01-01.
Definition: LocalDate.h:107
int16_t year() const
Return the year.
uint8_t minute() const
Return the minute.
static const acetime_t kDaysSinceUnixEpoch
Number of days from Unix epoch (1970-01-01 00:00:00Z) to the AceTime epoch (2000-01-01 00:00:00Z)...
Definition: LocalDate.h:57
static LocalDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:188
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:42
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
Definition: LocalDate.h:219
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:45
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:20
void printTo(Print &printer) const
Print LocalDateTime to &#39;printer&#39; in ISO 8601 format.
static LocalDateTime forTinyComponents(int8_t yearTiny, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
Factory method using components with an int8_t yearTiny.
Definition: LocalDateTime.h:35
acetime_t toEpochDays() const
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
Definition: LocalDate.h:274
uint8_t day() const
Return the day of the month.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
static LocalDateTime forEpochSeconds(acetime_t epochSeconds)
Factory method.
Definition: LocalDateTime.h:53
static LocalDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
static LocalDate forTinyComponents(int8_t yearTiny, uint8_t month, uint8_t day)
Factory method using components with an int8_t yearTiny.
Definition: LocalDate.h:95
void minute(uint8_t minute)
Set the minute.
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
void hour(uint8_t hour)
Set the hour.
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:252
acetime_t toUnixSeconds() const
Return seconds from Unix epoch 1970-01-01 00:00:00Z, after assuming that the date and time components...
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:231
static LocalDateTime forDateString(const char *dateString)
Factory method.
uint8_t month() const
Return the month with January=1, December=12.
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
static LocalDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
Factory method using separated date and time components.
Definition: LocalDateTime.h:26
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch 2000-01-01 00:00:00Z, after assuming that the date and time compon...
const LocalDate & localDate() const
Return the LocalDate.
const LocalTime & localTime() const
Return the LocalTime.
uint8_t hour() const
Return the hour.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
void year(int16_t year)
Set the year.
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:100
static const int8_t kInvalidYearTiny
Sentinel yearTiny which indicates an error condition or sometimes a year that &#39;does not exist&#39;...
Definition: LocalDate.h:39
uint8_t second() const
Return the second.
static LocalDateTime forUnixSeconds(acetime_t unixSeconds)
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01.
Definition: LocalDateTime.h:83
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:87
static LocalTime forComponents(uint8_t hour, uint8_t minute, uint8_t second)
Factory method using separated date, time, and time zone fields.
Definition: LocalTime.h:35
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:110
int8_t compareTo(const LocalDateTime &that) const
Compare this LocalDateTime with another LocalDateTime, and return (<0, 0, >0) according to whether th...
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:30
friend bool operator==(const LocalDateTime &a, const LocalDateTime &b)
Return true if two LocalDateTime objects are equal in all components.
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z).
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:33
void month(uint8_t month)
Set the month.
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:116
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:225
static bool isYearValid(int16_t year)
Return true if year is within valid range of [1873, 2127].
Definition: LocalDate.h:198
int16_t year() const
Return the full year instead of just the last 2 digits.
Definition: LocalDate.h:213
uint8_t second() const
Return the second.
Definition: LocalTime.h:122
LocalDateTime()
Constructor.
bool isError() const
Return true if any component indicates an error condition.
static LocalTime forSeconds(acetime_t seconds)
Factory method.
Definition: LocalTime.h:48
void second(uint8_t second)
Set the second.
void day(uint8_t day)
Set the day of the month.
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:242