AceTime  1.0
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.
ZonedDateTime.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_ZONED_DATE_TIME_H
7 #define ACE_TIME_ZONED_DATE_TIME_H
8 
9 #include <stdint.h>
10 #include "common/compat.h"
11 #include "OffsetDateTime.h"
12 #include "TimeZone.h"
13 
14 class Print;
15 
16 namespace ace_time {
17 
33  public:
51  static ZonedDateTime forComponents(int16_t year, uint8_t month, uint8_t day,
52  uint8_t hour, uint8_t minute, uint8_t second,
53  const TimeZone& timeZone) {
55  year, month, day, hour, minute, second);
56  auto odt = timeZone.getOffsetDateTime(ldt);
57  return ZonedDateTime(odt, timeZone);
58  }
59 
71  static ZonedDateTime forEpochSeconds(acetime_t epochSeconds,
72  const TimeZone& timeZone) {
73  OffsetDateTime odt;
74  if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
76  } else {
77  TimeOffset timeOffset = timeZone.getUtcOffset(epochSeconds);
78  odt = OffsetDateTime::forEpochSeconds(epochSeconds, timeOffset);
79  }
80  return ZonedDateTime(odt, timeZone);
81  }
82 
92  static ZonedDateTime forUnixSeconds(acetime_t unixSeconds,
93  const TimeZone& timeZone) {
94  acetime_t epochSeconds = (unixSeconds == LocalDate::kInvalidEpochSeconds)
95  ? unixSeconds
96  : unixSeconds - LocalDate::kSecondsSinceUnixEpoch;
97  return forEpochSeconds(epochSeconds, timeZone);
98  }
99 
113  static ZonedDateTime forDateString(const char* dateString) {
116  }
117 
122  static ZonedDateTime forDateString(const __FlashStringHelper* dateString) {
125  }
126 
130  }
131 
133  explicit ZonedDateTime() {}
134 
136  bool isError() const { return mOffsetDateTime.isError(); }
137 
139  int16_t year() const { return mOffsetDateTime.year(); }
140 
142  void year(int16_t year) { mOffsetDateTime.year(year); }
143 
149  int8_t yearTiny() const { return mOffsetDateTime.yearTiny(); }
150 
156  void yearTiny(int8_t yearTiny) { mOffsetDateTime.yearTiny(yearTiny); }
157 
159  uint8_t month() const { return mOffsetDateTime.month(); }
160 
162  void month(uint8_t month) { mOffsetDateTime.month(month); }
163 
165  uint8_t day() const { return mOffsetDateTime.day(); }
166 
168  void day(uint8_t day) { mOffsetDateTime.day(day); }
169 
171  uint8_t hour() const { return mOffsetDateTime.hour(); }
172 
174  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
175 
177  uint8_t minute() const { return mOffsetDateTime.minute(); }
178 
180  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
181 
183  uint8_t second() const { return mOffsetDateTime.second(); }
184 
186  void second(uint8_t second) { mOffsetDateTime.second(second); }
187 
192  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
193 
195  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
196 
198  const TimeZone& timeZone() const { return mTimeZone; }
199 
204  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
205 
207  const LocalDateTime& localDateTime() const {
208  return mOffsetDateTime.localDateTime();
209  }
210 
216  acetime_t epochSeconds = toEpochSeconds();
217  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
218  }
219 
224  acetime_t toEpochDays() const {
225  return mOffsetDateTime.toEpochDays();
226  }
227 
229  acetime_t toUnixDays() const {
232  }
233 
242  acetime_t toEpochSeconds() const {
243  return mOffsetDateTime.toEpochSeconds();
244  }
245 
253  acetime_t toUnixSeconds() const {
254  return mOffsetDateTime.toUnixSeconds();
255  }
256 
264  int8_t compareTo(const ZonedDateTime& that) const {
265  return mOffsetDateTime.compareTo(that.mOffsetDateTime);
266  }
267 
273  void printTo(Print& printer) const;
274 
275  // Use default copy constructor and assignment operator.
276  ZonedDateTime(const ZonedDateTime&) = default;
277  ZonedDateTime& operator=(const ZonedDateTime&) = default;
278 
279  private:
281  static const uint8_t kDateStringLength = 25;
282 
283  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
284 
286  ZonedDateTime(const OffsetDateTime& offsetDateTime, const TimeZone& tz):
287  mOffsetDateTime(offsetDateTime),
288  mTimeZone(tz) {}
289 
290  OffsetDateTime mOffsetDateTime;
291  TimeZone mTimeZone;
292 };
293 
301 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
302  return a.mOffsetDateTime == b.mOffsetDateTime
303  && a.mTimeZone == b.mTimeZone;
304 }
305 
307 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
308  return ! (a == b);
309 }
310 
311 }
312 
313 #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:69
uint8_t dayOfWeek() const
Return the day of the week using ISO 8601 numbering where Monday=1 and Sunday=7.
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:75
uint8_t second() const
Return the second.
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:60
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z), taking into account the offse...
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:63
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z), taking into account the time zone...
uint8_t month() const
Return the month with January=1, December=12.
uint8_t hour() const
Return the hour.
uint8_t day() const
Return the day of the month.
void second(uint8_t second)
Set the second.
void hour(uint8_t hour)
Set the hour.
acetime_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
void day(uint8_t day)
Set the day of the month.
acetime_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
int8_t compareTo(const OffsetDateTime &that) const
Compare this OffsetDateTime with another OffsetDateTime, and return (<0, 0, >0) according to whether ...
static ZonedDateTime forError()
Return an instance whose isError() returns true.
OffsetDateTime getOffsetDateTime(const LocalDateTime &ldt) const
Return the best estimate of the OffsetDateTime at the given LocalDateTime for the current TimeZone...
Definition: TimeZone.h:285
static OffsetDateTime forEpochSeconds(acetime_t epochSeconds, TimeOffset timeOffset)
Factory method.
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:40
const TimeZone & timeZone() const
Return the time zone of the ZonedDateTime.
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
static ZonedDateTime forDateString(const char *dateString)
Factory method.
void year(int16_t year)
Set the year given the full year.
static ZonedDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, const TimeZone &timeZone)
Factory method using separated date, time, and time zone fields.
Definition: ZonedDateTime.h:51
int16_t year() const
Return the year.
const LocalDateTime & localDateTime() const
Return the LocalDateTime of the components.
uint8_t hour() const
Return the hour.
const LocalDateTime & localDateTime() const
Return the LocalDateTime.
int8_t compareTo(const ZonedDateTime &that) const
Compare this ZonedDateTime with another ZonedDateTime, and return (<0, 0, >0) according to whether th...
The date (year, month, day), time (hour, minute, second) and offset from UTC (timeOffset).
uint8_t month() const
Return the month with January=1, December=12.
Macros and definitions that provide a consistency layer among the various Arduino boards for compatib...
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 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:56
static ZonedDateTime forEpochSeconds(acetime_t epochSeconds, const TimeZone &timeZone)
Factory method.
Definition: ZonedDateTime.h:71
static TimeZone forTimeOffset(TimeOffset stdOffset, TimeOffset dstOffset=TimeOffset())
Factory method to create from a UTC offset and an optional DST offset.
Definition: TimeZone.h:104
Class that describes a time zone.
Definition: TimeZone.h:82
The date (year, month, day), time (hour, minute, second), and a timeZone representing an instant in t...
Definition: ZonedDateTime.h:32
void printTo(Print &printer) const
Print ZonedDateTime to &#39;printer&#39;.
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z), taking into account the time ...
ZonedDateTime convertToTimeZone(const TimeZone &timeZone) const
Create a ZonedDateTime in a different time zone (with the same epochSeconds).
void minute(uint8_t minute)
Set the minute.
static ZonedDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
static ZonedDateTime forUnixSeconds(acetime_t unixSeconds, const TimeZone &timeZone)
Factory method to create a ZonedDateTime using the number of seconds from Unix epoch.
Definition: ZonedDateTime.h:92
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
friend bool operator==(const ZonedDateTime &a, const ZonedDateTime &b)
Return true if two ZonedDateTime objects are equal in all components.
uint8_t second() const
Return the second.
uint8_t day() const
Return the day of the month.
void timeZone(const TimeZone &timeZone)
Set the time zone.
uint8_t minute() const
Return the minute.
ZonedDateTime()
Default constructor.
bool isError() const
Return true if any component indicates an error condition.
void month(uint8_t month)
Set the month.
static OffsetDateTime forError()
Factory method that returns an instance whose isError() is true.
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:27
TimeOffset getUtcOffset(acetime_t epochSeconds) const
Return the total UTC offset at epochSeconds, including DST offset.
Definition: TimeZone.h:191
int16_t year() const
Return the year.
static OffsetDateTime forDateString(const char *dateString)
Factory method.