AceTime  2.2.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 <Arduino.h> // strncpy_P()
13 #include "LocalDate.h"
14 #include "LocalTime.h"
15 
16 class Print;
17 class __FlashStringHelper;
18 
19 namespace ace_time {
20 
32  public:
33 
45  static LocalDateTime forComponents(int16_t year, uint8_t month,
46  uint8_t day, uint8_t hour, uint8_t minute, uint8_t second,
47  uint8_t fold = 0) {
50  return LocalDateTime(ld, lt);
51  }
52 
65  acetime_t epochSeconds, uint8_t fold = 0) {
66  if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
67  return forError();
68  }
69 
70  // Integer floor-division towards -infinity
71  int32_t days = (epochSeconds < 0)
72  ? (epochSeconds + 1) / 86400 - 1
73  : epochSeconds / 86400;
74 
75  // Avoid % operator, because it's slow on an 8-bit process and because
76  // epochSeconds could be negative.
77  int32_t seconds = epochSeconds - 86400 * days;
78 
80  LocalTime lt = LocalTime::forSeconds(seconds, fold);
81  return LocalDateTime(ld, lt);
82  }
83 
92  int64_t unixSeconds, uint8_t fold = 0) {
93  if (unixSeconds == LocalDate::kInvalidUnixSeconds64) {
94  return forError();
95  }
96 
97  int64_t epochSeconds64 = unixSeconds
99 
100  // Integer floor-division towards -infinity
101  int32_t days = (epochSeconds64 < 0)
102  ? (epochSeconds64 + 1) / 86400 - 1
103  : epochSeconds64 / 86400;
104  int32_t seconds = epochSeconds64 - (int64_t) 86400 * days;
105 
107  LocalTime lt = LocalTime::forSeconds(seconds, fold);
108  return LocalDateTime(ld, lt);
109  }
110 
127  static LocalDateTime forDateString(const char* dateString);
128 
136  static LocalDateTime forDateStringChainable(const char*& dateString);
137 
142  static LocalDateTime forDateString(const __FlashStringHelper* dateString) {
143  // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
144  // ESP8266 do not have strlcpy_P(). We need +1 for the '\0' character and
145  // another +1 to determine if the dateString is too long to fit.
146  char buffer[kDateTimeStringLength + 2];
147  strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
148  buffer[kDateTimeStringLength + 1] = 0;
149 
150  // check if the original F() was too long
151  size_t len = strlen(buffer);
152  if (len > kDateTimeStringLength) {
153  return forError();
154  }
155 
156  return forDateString(buffer);
157  }
158 
162  }
163 
165  explicit LocalDateTime() {}
166 
168  bool isError() const {
169  return mLocalDate.isError() || mLocalTime.isError();
170  }
171 
173  int16_t year() const { return mLocalDate.year(); }
174 
176  void year(int16_t year) { mLocalDate.year(year); }
177 
179  uint8_t month() const { return mLocalDate.month(); }
180 
182  void month(uint8_t month) { mLocalDate.month(month); }
183 
185  uint8_t day() const { return mLocalDate.day(); }
186 
188  void day(uint8_t day) { mLocalDate.day(day); }
189 
191  uint8_t hour() const { return mLocalTime.hour(); }
192 
194  void hour(uint8_t hour) { mLocalTime.hour(hour); }
195 
197  uint8_t minute() const { return mLocalTime.minute(); }
198 
200  void minute(uint8_t minute) { mLocalTime.minute(minute); }
201 
203  uint8_t second() const { return mLocalTime.second(); }
204 
206  void second(uint8_t second) { mLocalTime.second(second); }
207 
209  uint8_t fold() const { return mLocalTime.fold(); }
210 
212  void fold(uint8_t fold) { mLocalTime.fold(fold); }
213 
215  uint8_t dayOfWeek() const { return mLocalDate.dayOfWeek(); }
216 
218  const LocalDate& localDate() const { return mLocalDate; }
219 
221  const LocalTime& localTime() const { return mLocalTime; }
222 
228  int32_t toEpochDays() const {
230  return mLocalDate.toEpochDays();
231  }
232 
234  int32_t toUnixDays() const {
237  }
238 
249  int32_t days = mLocalDate.toEpochDays();
250  int32_t seconds = mLocalTime.toSeconds();
251  return (int32_t) 86400 * days + seconds;
252  }
253 
262  int64_t toUnixSeconds64() const {
264  int32_t days = toUnixDays();
265  int32_t seconds = mLocalTime.toSeconds();
266  return (int64_t) 86400 * days + seconds;
267  }
268 
275  int8_t compareTo(const LocalDateTime& that) const {
276  int8_t dateCompare = localDate().compareTo(that.localDate());
277  if (dateCompare != 0) return dateCompare;
278  int8_t timeCompare = localTime().compareTo(that.localTime());
279  if (timeCompare != 0) return timeCompare;
280  return 0;
281  }
282 
288  void printTo(Print& printer) const;
289 
290  // Use default copy constructor and assignment operator.
291  LocalDateTime(const LocalDateTime&) = default;
292  LocalDateTime& operator=(const LocalDateTime&) = default;
293 
294  private:
295  friend bool operator==(const LocalDateTime& a, const LocalDateTime& b);
296 
298  static const uint8_t kDateTimeStringLength = 19;
299 
301  explicit LocalDateTime(const LocalDate& ld, const LocalTime& lt):
302  mLocalDate(ld),
303  mLocalTime(lt) {}
304 
305  LocalDate mLocalDate;
306  LocalTime mLocalTime;
307 };
308 
314 inline bool operator==(const LocalDateTime& a, const LocalDateTime& b) {
315  return a.mLocalDate == b.mLocalDate
316  && a.mLocalTime == b.mLocalTime;
317 }
318 
320 inline bool operator!=(const LocalDateTime& a, const LocalDateTime& b) {
321  return ! (a == b);
322 }
323 
324 }
325 
326 #endif
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:57
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:68
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:31
static LocalDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
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 ...
void hour(uint8_t hour)
Set the hour.
void printTo(Print &printer) const
Print LocalDateTime to 'printer' in ISO 8601 format.
static LocalDateTime forEpochSeconds(acetime_t epochSeconds, uint8_t fold=0)
Factory method.
Definition: LocalDateTime.h:64
LocalDateTime()
Constructor.
void fold(uint8_t fold)
Set the fold.
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:45
void minute(uint8_t minute)
Set the minute.
uint8_t day() const
Return the day of the month.
const LocalDate & localDate() const
Return the LocalDate.
bool isError() const
Return true if any component indicates an error condition.
int8_t compareTo(const LocalDateTime &that) const
Compare 'this' LocalDateTime with 'that' LocalDateTime, and return (<0, 0, >0) according to whether '...
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:91
void month(uint8_t month)
Set the month.
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
uint8_t month() const
Return the month with January=1, December=12.
uint8_t fold() const
Return the fold.
static LocalDateTime forDateString(const char *dateString)
Factory method.
uint8_t second() const
Return the second.
void day(uint8_t day)
Set the day of the month.
uint8_t minute() const
Return the minute.
void year(int16_t year)
Set the year.
int32_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
const LocalTime & localTime() const
Return the LocalTime.
int32_t toEpochDays() const
Return number of whole days since AceTime epoch.
uint8_t hour() const
Return the hour.
static LocalDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
friend bool operator==(const LocalDateTime &a, const LocalDateTime &b)
Return true if two LocalDateTime objects are equal in all components.
void second(uint8_t second)
Set the second.
acetime_t toEpochSeconds() const
Return seconds since the current AceTime epoch defined by Epoch::currentEpochYear().
int16_t year() const
Return the year.
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:49
static LocalDate forComponents(int16_t year, uint8_t month, uint8_t day)
Factory method using separated year, month and day fields.
Definition: LocalDate.h:156
static const int32_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:84
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:340
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:294
static const int64_t kInvalidUnixSeconds64
Sentinel unixSeconds64 which indicates an error.
Definition: LocalDate.h:90
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:327
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:393
static const int32_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:87
int16_t year() const
Return the year.
Definition: LocalDate.h:304
int32_t toEpochDays() const
Return number of days since the current epoch year sCurrentEpochYear.
Definition: LocalDate.h:355
static LocalDate forEpochDays(int32_t epochDays)
Factory method using the number of days since the current epoch (usually 2000-01-01).
Definition: LocalDate.h:169
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:310
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:316
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:27
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
uint8_t fold() const
Return the fold.
Definition: LocalTime.h:136
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:145
static LocalTime forError()
Factory method that returns an instance which indicates an error condition.
Definition: LocalTime.h:95
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:118
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:124
static LocalTime forSeconds(acetime_t seconds, uint8_t fold=0)
Factory method.
Definition: LocalTime.h:56
uint8_t second() const
Return the second.
Definition: LocalTime.h:130
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
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
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24