AceTime  0.3
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 #ifndef ACE_TIME_ZONED_DATE_TIME_H
2 #define ACE_TIME_ZONED_DATE_TIME_H
3 
4 #include <stdint.h>
5 #include "common/flash.h"
6 #include "OffsetDateTime.h"
7 #include "ExtendedZoneSpecifier.h"
8 #include "TimeZone.h"
9 
10 class Print;
11 
12 namespace ace_time {
13 
29  public:
47  static ZonedDateTime forComponents(int16_t year, uint8_t month, uint8_t day,
48  uint8_t hour, uint8_t minute, uint8_t second,
49  const TimeZone& timeZone) {
51  year, month, day, hour, minute, second);
52  auto odt = timeZone.getOffsetDateTime(ldt);
53  return ZonedDateTime(odt, timeZone);
54  }
55 
67  static ZonedDateTime forEpochSeconds(acetime_t epochSeconds,
68  const TimeZone& timeZone) {
69  OffsetDateTime odt;
70  if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
72  } else {
73  TimeOffset timeOffset = timeZone.getUtcOffset(epochSeconds);
74  odt = OffsetDateTime::forEpochSeconds(epochSeconds, timeOffset);
75  }
76  return ZonedDateTime(odt, timeZone);
77  }
78 
88  static ZonedDateTime forUnixSeconds(acetime_t unixSeconds,
89  const TimeZone& timeZone) {
90  acetime_t epochSeconds = (unixSeconds == LocalDate::kInvalidEpochSeconds)
91  ? unixSeconds
92  : unixSeconds - LocalDate::kSecondsSinceUnixEpoch;
93  return forEpochSeconds(epochSeconds, timeZone);
94  }
95 
109  static ZonedDateTime forDateString(const char* dateString) {
112  }
113 
118  static ZonedDateTime forDateString(const __FlashStringHelper* dateString) {
121  }
122 
126  }
127 
129  explicit ZonedDateTime() {}
130 
132  bool isError() const { return mOffsetDateTime.isError(); }
133 
135  int16_t year() const { return mOffsetDateTime.year(); }
136 
138  void year(int16_t year) { mOffsetDateTime.year(year); }
139 
141  int8_t yearTiny() const { return mOffsetDateTime.yearTiny(); }
142 
144  void yearTiny(int8_t yearTiny) { mOffsetDateTime.yearTiny(yearTiny); }
145 
147  uint8_t month() const { return mOffsetDateTime.month(); }
148 
150  void month(uint8_t month) { mOffsetDateTime.month(month); }
151 
153  uint8_t day() const { return mOffsetDateTime.day(); }
154 
156  void day(uint8_t day) { mOffsetDateTime.day(day); }
157 
159  uint8_t hour() const { return mOffsetDateTime.hour(); }
160 
162  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
163 
165  uint8_t minute() const { return mOffsetDateTime.minute(); }
166 
168  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
169 
171  uint8_t second() const { return mOffsetDateTime.second(); }
172 
174  void second(uint8_t second) { mOffsetDateTime.second(second); }
175 
180  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
181 
183  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
184 
186  const TimeZone& timeZone() const { return mTimeZone; }
187 
192  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
193 
195  const LocalDateTime& localDateTime() const {
196  return mOffsetDateTime.localDateTime();
197  }
198 
204  acetime_t epochSeconds = toEpochSeconds();
205  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
206  }
207 
212  acetime_t toEpochDays() const {
213  return mOffsetDateTime.toEpochDays();
214  }
215 
217  acetime_t toUnixDays() const {
220  }
221 
230  acetime_t toEpochSeconds() const {
231  return mOffsetDateTime.toEpochSeconds();
232  }
233 
241  acetime_t toUnixSeconds() const {
242  return mOffsetDateTime.toUnixSeconds();
243  }
244 
252  int8_t compareTo(const ZonedDateTime& that) const {
253  return mOffsetDateTime.compareTo(that.mOffsetDateTime);
254  }
255 
261  void printTo(Print& printer) const;
262 
263  // Use default copy constructor and assignment operator.
264  ZonedDateTime(const ZonedDateTime&) = default;
265  ZonedDateTime& operator=(const ZonedDateTime&) = default;
266 
267  private:
269  static const uint8_t kDateStringLength = 25;
270 
271  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
272 
274  ZonedDateTime(const OffsetDateTime& offsetDateTime, const TimeZone& tz):
275  mOffsetDateTime(offsetDateTime),
276  mTimeZone(tz) {}
277 
278  OffsetDateTime mOffsetDateTime;
279  TimeZone mTimeZone;
280 };
281 
289 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
290  return a.mOffsetDateTime == b.mOffsetDateTime
291  && a.mTimeZone == b.mTimeZone;
292 }
293 
295 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
296  return ! (a == b);
297 }
298 
299 }
300 
301 #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:51
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:57
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:42
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:45
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:157
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:26
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:47
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...
static TimeZone forTimeOffset(TimeOffset offset)
Factory method to create from a fixed UTC offset.
Definition: TimeZone.h:108
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
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), 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:53
static ZonedDateTime forEpochSeconds(acetime_t epochSeconds, const TimeZone &timeZone)
Factory method.
Definition: ZonedDateTime.h:67
Class that describes a time zone.
Definition: TimeZone.h:96
The date (year, month, day) and time (hour, minute, second) fields representing an instant in time...
Definition: ZonedDateTime.h:28
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:88
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.
TimeOffset getUtcOffset(acetime_t epochSeconds) const
Return the total UTC offset at epochSeconds, including DST offset.
Definition: TimeZone.h:134
int16_t year() const
Return the year.
static OffsetDateTime forDateString(const char *dateString)
Factory method.