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.
OffsetDateTime.h
1 #ifndef ACE_TIME_OFFSET_DATE_TIME_H
2 #define ACE_TIME_OFFSET_DATE_TIME_H
3 
4 #include <stdint.h>
5 #include "TimeOffset.h"
6 #include "LocalDateTime.h"
7 
8 class Print;
9 
10 namespace ace_time {
11 
31  public:
47  static OffsetDateTime forComponents(int16_t year, uint8_t month,
48  uint8_t day, uint8_t hour, uint8_t minute, uint8_t second,
50  return OffsetDateTime(year, month, day, hour, minute, second, timeOffset);
51  }
52 
62  static OffsetDateTime forEpochSeconds(acetime_t epochSeconds,
64  OffsetDateTime dt;
65  if (epochSeconds == LocalDate::kInvalidEpochSeconds) return forError();
66 
67  // Get the real epochSeconds
68  dt.mTimeOffset = timeOffset;
69  epochSeconds += timeOffset.toSeconds();
70 
71  dt.mLocalDateTime = LocalDateTime::forEpochSeconds(epochSeconds);
72  return dt;
73  }
74 
80  static OffsetDateTime forUnixSeconds(acetime_t unixSeconds,
82  if (unixSeconds == LocalDate::kInvalidEpochSeconds) {
83  return forEpochSeconds(unixSeconds, timeOffset);
84  } else {
86  timeOffset);
87  }
88  }
89 
109  static OffsetDateTime forDateString(const char* dateString);
110 
115  static OffsetDateTime forDateString(const __FlashStringHelper* dateString) {
116  // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
117  // ESP8266 do not have strlcpy_P().
118  char buffer[kDateStringLength + 2];
119  strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
120  buffer[kDateStringLength + 1] = 0;
121 
122  // check if the original F() was too long
123  size_t len = strlen(buffer);
124  if (len > kDateStringLength) {
125  return forError();
126  }
127 
128  return forDateString(buffer);
129  }
130 
134  }
135 
137  explicit OffsetDateTime() {}
138 
140  bool isError() const {
141  // Check mTimeOffset first because it's expected to be invalid more often.
142  return mTimeOffset.isError() || mLocalDateTime.isError();
143  }
144 
146  int16_t year() const { return mLocalDateTime.year(); }
147 
149  void year(int16_t year) { mLocalDateTime.year(year); }
150 
152  int8_t yearTiny() const { return mLocalDateTime.yearTiny(); }
153 
155  void yearTiny(int8_t yearTiny) { mLocalDateTime.yearTiny(yearTiny); }
156 
158  uint8_t month() const { return mLocalDateTime.month(); }
159 
161  void month(uint8_t month) { mLocalDateTime.month(month); }
162 
164  uint8_t day() const { return mLocalDateTime.day(); }
165 
167  void day(uint8_t day) { mLocalDateTime.day(day); }
168 
170  uint8_t hour() const { return mLocalDateTime.hour(); }
171 
173  void hour(uint8_t hour) { mLocalDateTime.hour(hour); }
174 
176  uint8_t minute() const { return mLocalDateTime.minute(); }
177 
179  void minute(uint8_t minute) { mLocalDateTime.minute(minute); }
180 
182  uint8_t second() const { return mLocalDateTime.second(); }
183 
185  void second(uint8_t second) { mLocalDateTime.second(second); }
186 
188  uint8_t dayOfWeek() const { return mLocalDateTime.dayOfWeek(); }
189 
191  const LocalDate& localDate() const { return mLocalDateTime.localDate(); }
192 
194  const LocalTime& localTime() const { return mLocalDateTime.localTime(); }
195 
197  TimeOffset timeOffset() const { return mTimeOffset; }
198 
200  void timeOffset(TimeOffset timeOffset) { mTimeOffset = timeOffset; }
201 
207  acetime_t epochSeconds = toEpochSeconds();
208  return OffsetDateTime::forEpochSeconds(epochSeconds, timeOffset);
209  }
210 
215  void printTo(Print& printer) const;
216 
221  acetime_t toEpochDays() const {
223 
224  acetime_t epochDays = mLocalDateTime.localDate().toEpochDays();
225 
226  // Increment or decrement the day count depending on the offset zone.
227  acetime_t timeOffset = mLocalDateTime.localTime().toSeconds()
228  - mTimeOffset.toSeconds();
229  if (timeOffset >= 86400) return epochDays + 1;
230  if (timeOffset < 0) return epochDays - 1;
231  return epochDays;
232  }
233 
235  acetime_t toUnixDays() const {
238  }
239 
244  acetime_t toEpochSeconds() const {
246  return mLocalDateTime.toEpochSeconds() - mTimeOffset.toSeconds();
247  }
248 
256  acetime_t toUnixSeconds() const {
259  }
260 
267  int8_t compareTo(const OffsetDateTime& that) const {
268  acetime_t thisSeconds = toEpochSeconds();
269  acetime_t thatSeconds = that.toEpochSeconds();
270  if (thisSeconds < thatSeconds) return -1;
271  if (thisSeconds > thatSeconds) return 1;
272  return 0;
273  }
274 
275  // Use default copy constructor and assignment operator.
276  OffsetDateTime(const OffsetDateTime&) = default;
277  OffsetDateTime& operator=(const OffsetDateTime&) = default;
278 
279  private:
280  friend bool operator==(const OffsetDateTime& a, const OffsetDateTime& b);
281  friend class ZonedDateTime; // constructor
282  friend class BasicZoneSpecifier; // constructor
283 
285  static const uint8_t kDateStringLength = 25;
286 
294  static OffsetDateTime forDateStringChainable(const char*& dateString);
295 
310  explicit OffsetDateTime(int16_t year, uint8_t month, uint8_t day,
311  uint8_t hour, uint8_t minute, uint8_t second,
313  mLocalDateTime(year, month, day, hour, minute, second),
314  mTimeOffset(timeOffset) {}
315 
317  explicit OffsetDateTime(const LocalDateTime& ldt, TimeOffset timeOffset):
318  mLocalDateTime(ldt),
319  mTimeOffset(timeOffset) {}
320 
321  LocalDateTime mLocalDateTime;
322  TimeOffset mTimeOffset; // offset from UTC
323 };
324 
330 inline bool operator==(const OffsetDateTime& a, const OffsetDateTime& b) {
331  return a.mLocalDateTime == b.mLocalDateTime
332  && a.mTimeOffset == b.mTimeOffset;
333 }
334 
336 inline bool operator!=(const OffsetDateTime& a, const OffsetDateTime& b) {
337  return ! (a == b);
338 }
339 
340 }
341 
342 #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 TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:90
uint8_t hour() const
Return the hour.
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
void minute(uint8_t minute)
Set the minute.
uint8_t month() const
Return the month with January=1, December=12.
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:50
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.
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
static OffsetDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, TimeOffset timeOffset=TimeOffset())
Factory method using separated date, time, and UTC offset fields.
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.
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.
void year(int16_t year)
Set the year.
const LocalDate & localDate() const
Return the LocalDate.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z), taking into account the offset zone...
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:133
const LocalTime & localTime() const
Return the LocalTime.
OffsetDateTime()
Constructor.
void second(uint8_t second)
Set the second.
const LocalDate & localDate() const
Return the LocalDate.
int16_t year() const
Return the year.
uint8_t month() const
Return the month with January=1, December=12.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z).
void timeOffset(TimeOffset timeOffset)
Set the offset zone.
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
friend bool operator==(const OffsetDateTime &a, const OffsetDateTime &b)
Return true if two OffsetDateTime objects are equal in all components.
uint8_t day() const
Return the day of the month.
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z), taking into account the offse...
void month(uint8_t month)
Set the month.
static OffsetDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
uint8_t hour() const
Return the hour.
OffsetDateTime convertToTimeOffset(TimeOffset timeOffset) const
Create a OffsetDateTime in a different offset zone code (with the same epochSeconds).
int8_t compareTo(const OffsetDateTime &that) const
Compare this OffsetDateTime with another OffsetDateTime, and return (<0, 0, >0) according to whether ...
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
bool isError() const
Return true if any component indicates an error condition.
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
int16_t year() const
Return the year.
static OffsetDateTime forEpochSeconds(acetime_t epochSeconds, TimeOffset timeOffset=TimeOffset())
Factory method.
uint8_t day() const
Return the day of the month.
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:32
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
The date (year, month, day) and time (hour, minute, second) fields representing an instant in time...
Definition: ZonedDateTime.h:28
uint8_t second() const
Return the second.
const LocalTime & localTime() const
Return the LocalTime.
void hour(uint8_t hour)
Set the hour.
static OffsetDateTime forUnixSeconds(acetime_t unixSeconds, TimeOffset timeOffset=TimeOffset())
Factory method that takes the number of seconds since Unix Epoch of 1970-01-01.
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:120
acetime_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
void printTo(Print &printer) const
Print OffsetDateTime to &#39;printer&#39; in ISO 8601 format.
uint8_t minute() const
Return the minute.
acetime_t toEpochDays() const
Return number of days since AceTime epoch (2000-01-01 00:00:00Z).
Definition: LocalDate.h:263
An implementation of ZoneSpecifier that supports a subset of the zones containing in the TZ Database...
static OffsetDateTime forError()
Factory method that returns an instance whose isError() is true.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
static OffsetDateTime forDateString(const char *dateString)
Factory method.