AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized 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:
26  static LocalDateTime forComponents(int16_t year, uint8_t month,
27  uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
28  return LocalDateTime(year, month, day, hour, minute, second);
29  }
30 
39  static LocalDateTime forEpochSeconds(acetime_t epochSeconds) {
40  if (epochSeconds == LocalDate::kInvalidEpochSeconds) return forError();
41 
42  // Integer floor-division towards -infinity
43  acetime_t days = (epochSeconds < 0)
44  ? (epochSeconds + 1) / 86400 - 1
45  : epochSeconds / 86400;
46 
47  // Avoid % operator, because it's slow on an 8-bit process and because
48  // epochSeconds could be negative.
49  acetime_t seconds = epochSeconds - 86400 * days;
50 
52  LocalTime lt = LocalTime::forSeconds(seconds);
53 
54  return LocalDateTime(ld, lt);
55  }
56 
62  static LocalDateTime forUnixSeconds(acetime_t unixSeconds) {
63  if (unixSeconds == LocalDate::kInvalidEpochSeconds) {
64  return forEpochSeconds(unixSeconds);
65  } else {
67  }
68  }
69 
87  static LocalDateTime forDateString(const char* dateString);
88 
93  static LocalDateTime forDateString(const __FlashStringHelper* dateString) {
94  // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
95  // ESP8266 do not have strlcpy_P().
96  char buffer[kDateTimeStringLength + 2];
97  strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
98  buffer[kDateTimeStringLength + 1] = 0;
99 
100  // check if the original F() was too long
101  size_t len = strlen(buffer);
102  if (len > kDateTimeStringLength) {
103  return LocalDateTime::forError();
104  }
105 
106  return forDateString(buffer);
107  }
108 
112  }
113 
115  explicit LocalDateTime() {}
116 
118  bool isError() const {
119  return mLocalDate.isError() || mLocalTime.isError();
120  }
121 
123  int16_t year() const { return mLocalDate.year(); }
124 
126  void year(int16_t year) { mLocalDate.year(year); }
127 
129  int8_t yearTiny() const { return mLocalDate.yearTiny(); }
130 
132  void yearTiny(int8_t yearTiny) { mLocalDate.yearTiny(yearTiny); }
133 
135  uint8_t month() const { return mLocalDate.month(); }
136 
138  void month(uint8_t month) { mLocalDate.month(month); }
139 
141  uint8_t day() const { return mLocalDate.day(); }
142 
144  void day(uint8_t day) { mLocalDate.day(day); }
145 
147  uint8_t hour() const { return mLocalTime.hour(); }
148 
150  void hour(uint8_t hour) { mLocalTime.hour(hour); }
151 
153  uint8_t minute() const { return mLocalTime.minute(); }
154 
156  void minute(uint8_t minute) { mLocalTime.minute(minute); }
157 
159  uint8_t second() const { return mLocalTime.second(); }
160 
162  void second(uint8_t second) { mLocalTime.second(second); }
163 
165  uint8_t dayOfWeek() const { return mLocalDate.dayOfWeek(); }
166 
168  const LocalDate& localDate() const { return mLocalDate; }
169 
171  const LocalTime& localTime() const { return mLocalTime; }
172 
177  void printTo(Print& printer) const;
178 
182  acetime_t toEpochDays() const {
184  return mLocalDate.toEpochDays();
185  }
186 
188  acetime_t toUnixDays() const {
191  }
192 
196  acetime_t toEpochSeconds() const {
198 
199  acetime_t days = mLocalDate.toEpochDays();
200  acetime_t seconds = mLocalTime.toSeconds();
201  return days * 86400 + seconds;
202  }
203 
211  acetime_t toUnixSeconds() const {
214  }
215 
220  int8_t compareTo(const LocalDateTime& that) const {
221  acetime_t thisSeconds = toEpochSeconds();
222  acetime_t thatSeconds = that.toEpochSeconds();
223  if (thisSeconds < thatSeconds) return -1;
224  if (thisSeconds > thatSeconds) return 1;
225  return 0;
226  }
227 
228  // Use default copy constructor and assignment operator.
229  LocalDateTime(const LocalDateTime&) = default;
230  LocalDateTime& operator=(const LocalDateTime&) = default;
231 
232  private:
233  friend class OffsetDateTime; // constructor, forDateStringChainable()
234  friend class ExtendedZoneSpecifier; // getLocalDate()
235  friend class BasicZoneSpecifier; // getLocalDate()
236  friend bool operator==(const LocalDateTime& a, const LocalDateTime& b);
237 
239  static const uint8_t kDateTimeStringLength = 19;
240 
248  static LocalDateTime forDateStringChainable(const char*& dateString);
249 
260  explicit LocalDateTime(int16_t year, uint8_t month, uint8_t day,
261  uint8_t hour, uint8_t minute, uint8_t second):
262  mLocalDate(year - LocalDate::kEpochYear, month, day),
263  mLocalTime(hour, minute, second) {}
264 
266  explicit LocalDateTime(const LocalDate& ld, const LocalTime& lt):
267  mLocalDate(ld),
268  mLocalTime(lt) {}
269 
270  const LocalDate& getLocalDate() const { return mLocalDate; }
271 
272  LocalDate mLocalDate;
273  LocalTime mLocalTime;
274 };
275 
281 inline bool operator==(const LocalDateTime& a, const LocalDateTime& b) {
282  return a.mLocalDate == b.mLocalDate
283  && a.mLocalTime == b.mLocalTime;
284 }
285 
287 inline bool operator!=(const LocalDateTime& a, const LocalDateTime& b) {
288  return ! (a == b);
289 }
290 
291 }
292 
293 #endif
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:59
static LocalDate forEpochDays(acetime_t epochDays)
Factory method using the number of days since AceTime epoch of 2000-01-01.
Definition: LocalDate.h:113
uint8_t hour() const
Return the hour.
int8_t compareTo(const LocalDateTime &that) const
Compare this LocalDateTime with another LocalDateTime, and return (<0, 0, >0) according to whether th...
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:65
uint8_t month() const
Return the month with January=1, December=12.
static LocalDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
Definition: LocalDateTime.h:93
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:184
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:50
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:53
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:20
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:215
uint8_t second() const
Return the second.
uint8_t minute() const
Return the minute.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:242
An implementation of ZoneSpecifier that works for all zones defined by the TZ Database (with some zon...
static LocalDateTime forEpochSeconds(acetime_t epochSeconds)
Factory method.
Definition: LocalDateTime.h:39
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:120
bool isError() const
Return true if any component indicates an error condition.
const LocalDate & localDate() const
Return the LocalDate.
void minute(uint8_t minute)
Set the minute.
void hour(uint8_t hour)
Set the hour.
static LocalDateTime forDateString(const char *dateString)
Factory method.
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, time.
Definition: LocalDateTime.h:26
int16_t year() const
Return the year.
void printTo(Print &printer) const
Print LocalDateTime to &#39;printer&#39; in ISO 8601 format.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z).
uint8_t day() const
Return the day of the month.
int16_t year() const
Return the full year instead of just the last 2 digits.
Definition: LocalDate.h:203
acetime_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:221
void year(int16_t year)
Set the year.
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z).
static LocalDateTime forUnixSeconds(acetime_t unixSeconds)
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01.
Definition: LocalDateTime.h:62
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:76
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:32
friend bool operator==(const LocalDateTime &a, const LocalDateTime &b)
Return true if two LocalDateTime objects are equal in all components.
uint8_t hour() const
Return the hour.
Definition: LocalTime.h:99
const LocalTime & localTime() const
Return the LocalTime.
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:35
void month(uint8_t month)
Set the month.
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:232
uint8_t second() const
Return the second.
Definition: LocalTime.h:111
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
Definition: LocalDate.h:209
LocalDateTime()
Constructor.
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:89
uint8_t minute() const
Return the minute.
Definition: LocalTime.h:105
acetime_t toEpochDays() const
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
Definition: LocalDate.h:263
static LocalTime forSeconds(acetime_t seconds)
Factory method.
Definition: LocalTime.h:48
An implementation of ZoneSpecifier that supports a subset of the zones containing in the TZ Database...
void second(uint8_t second)
Set the second.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
void day(uint8_t day)
Set the day of the month.