AceTime  0.6.1
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 
145  int8_t yearTiny() const { return mOffsetDateTime.yearTiny(); }
146 
148  void yearTiny(int8_t yearTiny) { mOffsetDateTime.yearTiny(yearTiny); }
149 
151  uint8_t month() const { return mOffsetDateTime.month(); }
152 
154  void month(uint8_t month) { mOffsetDateTime.month(month); }
155 
157  uint8_t day() const { return mOffsetDateTime.day(); }
158 
160  void day(uint8_t day) { mOffsetDateTime.day(day); }
161 
163  uint8_t hour() const { return mOffsetDateTime.hour(); }
164 
166  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
167 
169  uint8_t minute() const { return mOffsetDateTime.minute(); }
170 
172  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
173 
175  uint8_t second() const { return mOffsetDateTime.second(); }
176 
178  void second(uint8_t second) { mOffsetDateTime.second(second); }
179 
184  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
185 
187  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
188 
190  const TimeZone& timeZone() const { return mTimeZone; }
191 
196  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
197 
199  const LocalDateTime& localDateTime() const {
200  return mOffsetDateTime.localDateTime();
201  }
202 
208  acetime_t epochSeconds = toEpochSeconds();
209  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
210  }
211 
216  acetime_t toEpochDays() const {
217  return mOffsetDateTime.toEpochDays();
218  }
219 
221  acetime_t toUnixDays() const {
224  }
225 
234  acetime_t toEpochSeconds() const {
235  return mOffsetDateTime.toEpochSeconds();
236  }
237 
245  acetime_t toUnixSeconds() const {
246  return mOffsetDateTime.toUnixSeconds();
247  }
248 
256  int8_t compareTo(const ZonedDateTime& that) const {
257  return mOffsetDateTime.compareTo(that.mOffsetDateTime);
258  }
259 
265  void printTo(Print& printer) const;
266 
267  // Use default copy constructor and assignment operator.
268  ZonedDateTime(const ZonedDateTime&) = default;
269  ZonedDateTime& operator=(const ZonedDateTime&) = default;
270 
271  private:
273  static const uint8_t kDateStringLength = 25;
274 
275  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
276 
278  ZonedDateTime(const OffsetDateTime& offsetDateTime, const TimeZone& tz):
279  mOffsetDateTime(offsetDateTime),
280  mTimeZone(tz) {}
281 
282  OffsetDateTime mOffsetDateTime;
283  TimeZone mTimeZone;
284 };
285 
293 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
294  return a.mOffsetDateTime == b.mOffsetDateTime
295  && a.mTimeZone == b.mTimeZone;
296 }
297 
299 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
300  return ! (a == b);
301 }
302 
303 }
304 
305 #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:57
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:63
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:48
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:51
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:242
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:58
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.