AceTime  3.0.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 
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) {
49  return LocalDateTime(ld, lt);
50  }
51 
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 
79  LocalTime lt = LocalTime::forSeconds(seconds, fold);
80  return LocalDateTime(ld, lt);
81  }
82 
91  int64_t unixSeconds, uint8_t fold = 0) {
92  if (unixSeconds == LocalDate::kInvalidUnixSeconds64) {
93  return forError();
94  }
95 
96  int64_t epochSeconds64 = unixSeconds
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 
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 
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 {
213  return mLocalDate.toEpochDays();
214  }
215 
217  int32_t toUnixDays() const {
220  }
221 
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 {
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
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
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
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
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:63
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:44
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:90
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:46
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
static const int32_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:81
bool isError() const
Return true if any component indicates an error condition.
Definition: LocalDate.h:337
static LocalDate forError()
Factory method that returns a LocalDate which represents an error condition.
Definition: LocalDate.h:291
static const int64_t kInvalidUnixSeconds64
Sentinel unixSeconds64 which indicates an error.
Definition: LocalDate.h:87
uint8_t dayOfWeek() const
Calculate the day of week given the (year, month, day).
Definition: LocalDate.h:324
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:406
static const int32_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:84
int16_t year() const
Return the year.
Definition: LocalDate.h:301
int32_t toEpochDays() const
Return number of days since the current epoch year sCurrentEpochYear.
Definition: LocalDate.h:352
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
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDate.h:307
uint8_t day() const
Return the day of the month.
Definition: LocalDate.h:313
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