AceTime  3.0.0
Date and time classes for Arduino that support timezones from the TZ Database.
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 "OffsetDateTime.h"
11 #include "TimeZone.h"
12 
13 class Print;
14 
15 namespace ace_time {
16 
37  public:
56  int16_t year, uint8_t month, uint8_t day,
57  uint8_t hour, uint8_t minute, uint8_t second,
58  const TimeZone& timeZone, uint8_t fold = 0) {
61  return forLocalDateTime(ldt, timeZone);
62  }
63 
76  const LocalDateTime& ldt,
77  const TimeZone& timeZone) {
78  auto odt = timeZone.getOffsetDateTime(ldt);
79  return ZonedDateTime(odt, timeZone);
80  }
94  const TimeZone& timeZone) {
95  OffsetDateTime odt = (epochSeconds == LocalDate::kInvalidEpochSeconds)
97  : timeZone.getOffsetDateTime(epochSeconds);
98  return ZonedDateTime(odt, timeZone);
99  }
100 
120  int64_t unixSeconds, const TimeZone& timeZone) {
121  acetime_t epochSeconds;
122  if (unixSeconds == LocalDate::kInvalidUnixSeconds64) {
123  epochSeconds = LocalDate::kInvalidEpochSeconds;
124  } else {
125  epochSeconds = unixSeconds
127  }
128  return forEpochSeconds(epochSeconds, timeZone);
129  }
130 
144  static ZonedDateTime forDateString(const char* dateString) {
147  }
148 
153  static ZonedDateTime forDateString(const __FlashStringHelper* dateString) {
156  }
157 
161  }
162 
164  explicit ZonedDateTime() {}
165 
167  bool isError() const { return mOffsetDateTime.isError(); }
168 
170  int16_t year() const { return mOffsetDateTime.year(); }
171 
173  void year(int16_t year) { mOffsetDateTime.year(year); }
174 
176  uint8_t month() const { return mOffsetDateTime.month(); }
177 
179  void month(uint8_t month) { mOffsetDateTime.month(month); }
180 
182  uint8_t day() const { return mOffsetDateTime.day(); }
183 
185  void day(uint8_t day) { mOffsetDateTime.day(day); }
186 
188  uint8_t hour() const { return mOffsetDateTime.hour(); }
189 
191  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
192 
194  uint8_t minute() const { return mOffsetDateTime.minute(); }
195 
197  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
198 
200  uint8_t second() const { return mOffsetDateTime.second(); }
201 
203  void second(uint8_t second) { mOffsetDateTime.second(second); }
204 
206  uint8_t fold() const { return mOffsetDateTime.fold(); }
207 
209  void fold(uint8_t fold) { mOffsetDateTime.fold(fold); }
210 
215  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
216 
218  const TimeZone& timeZone() const { return mTimeZone; }
219 
224  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
225 
227  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
228 
230  const LocalDateTime& localDateTime() const {
231  return mOffsetDateTime.localDateTime();
232  }
233 
236  return mOffsetDateTime;
237  }
238 
255  void normalize() {
256  mOffsetDateTime = mTimeZone.getOffsetDateTime(localDateTime());
257  }
258 
264  acetime_t epochSeconds = toEpochSeconds();
265  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
266  }
267 
273  int32_t toEpochDays() const {
274  return mOffsetDateTime.toEpochDays();
275  }
276 
278  int32_t toUnixDays() const {
279  return mOffsetDateTime.toUnixDays();
280  }
281 
288  return mOffsetDateTime.toEpochSeconds();
289  }
290 
298  int64_t toUnixSeconds64() const {
299  return mOffsetDateTime.toUnixSeconds64();
300  }
301 
318  int8_t compareTo(const ZonedDateTime& that) const {
319  return mOffsetDateTime.compareTo(that.mOffsetDateTime);
320  }
321 
327  void printTo(Print& printer) const;
328 
329  // Use default copy constructor and assignment operator.
330  ZonedDateTime(const ZonedDateTime&) = default;
331  ZonedDateTime& operator=(const ZonedDateTime&) = default;
332 
333  private:
335  static const uint8_t kDateStringLength = 25;
336 
337  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
338 
341  mOffsetDateTime(offsetDateTime),
342  mTimeZone(tz) {}
343 
344  OffsetDateTime mOffsetDateTime;
345  TimeZone mTimeZone;
346 };
347 
355 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
356  return a.mOffsetDateTime == b.mOffsetDateTime
357  && a.mTimeZone == b.mTimeZone;
358 }
359 
361 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
362  return ! (a == b);
363 }
364 
365 }
366 
367 #endif
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
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
static const int64_t kInvalidUnixSeconds64
Sentinel unixSeconds64 which indicates an error.
Definition: LocalDate.h:87
static const int32_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:84
The date (year, month, day), time (hour, minute, second) and fixed offset from UTC (timeOffset).
uint8_t day() const
Return the day of the month.
TimeOffset timeOffset() const
Return the UTC offset of the OffsetDateTime.
int64_t toUnixSeconds64() const
Return the 64-bit number of seconds from Unix epoch 1970-01-01 00:00:00 UTC.
bool isError() const
Return true if any component indicates an error condition.
const LocalDateTime & localDateTime() const
Return the LocalDateTime.
uint8_t hour() const
Return the hour.
uint8_t month() const
Return the month with January=1, December=12.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch taking into account the UTC offset.
static OffsetDateTime forError()
Factory method that returns an instance whose isError() is true.
uint8_t fold() const
Return the fold.
int8_t compareTo(const OffsetDateTime &that) const
Compare 'this' OffsetDateTime with 'that' OffsetDateTime, and return (<0, 0, >0) according to whether...
uint8_t minute() const
Return the minute.
int16_t year() const
Return the year.
static OffsetDateTime forDateString(const char *dateString)
Factory method.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
int32_t toEpochDays() const
Return number of whole days since AceTime epoch taking into account the UTC offset.
int32_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
uint8_t second() const
Return the second.
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
Class that describes a time zone.
Definition: TimeZone.h:86
OffsetDateTime getOffsetDateTime(const LocalDateTime &ldt) const
Return the best estimate of the OffsetDateTime at the given LocalDateTime for the current TimeZone.
Definition: TimeZone.h:386
static TimeZone forTimeOffset(TimeOffset stdOffset, TimeOffset dstOffset=TimeOffset())
Factory method to create from a UTC offset and an optional DST offset.
Definition: TimeZone.h:115
The date (year, month, day), time (hour, minute, second), and a timeZone object that supports the zon...
Definition: ZonedDateTime.h:36
static ZonedDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, const TimeZone &timeZone, uint8_t fold=0)
Factory method using separated date, time, and time zone fields.
Definition: ZonedDateTime.h:55
int8_t compareTo(const ZonedDateTime &that) const
Compare 'this' ZonedDateTime with 'that' ZonedDateTime, and return (<0, 0, >0) according to whether t...
const TimeZone & timeZone() const
Return the time zone of the ZonedDateTime.
uint8_t hour() const
Return the hour.
void fold(uint8_t fold)
Set the fold.
int32_t toEpochDays() const
Return number of whole days since AceTime epoch taking into account the time zone.
int16_t year() const
Return the year.
void printTo(Print &printer) const
Print ZonedDateTime to 'printer'.
uint8_t dayOfWeek() const
Return the day of the week using ISO 8601 numbering where Monday=1 and Sunday=7.
static ZonedDateTime forLocalDateTime(const LocalDateTime &ldt, const TimeZone &timeZone)
Factory method using LocalDateTime and time zone fields.
Definition: ZonedDateTime.h:75
friend bool operator==(const ZonedDateTime &a, const ZonedDateTime &b)
Return true if two ZonedDateTime objects are equal in all components.
static ZonedDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
uint8_t fold() const
Return the fold.
ZonedDateTime convertToTimeZone(const TimeZone &timeZone) const
Create a ZonedDateTime in a different time zone (with the same epochSeconds).
bool isError() const
Return true if any component indicates an error condition.
void day(uint8_t day)
Set the day of the month.
void minute(uint8_t minute)
Set the minute.
int64_t toUnixSeconds64() const
Return the 64-bit number of seconds from Unix epoch 1970-01-01 00:00:00 UTC.
uint8_t day() const
Return the day of the month.
static ZonedDateTime forUnixSeconds64(int64_t unixSeconds, const TimeZone &timeZone)
Factory method to create a ZonedDateTime using the 64-bit number of seconds from Unix epoch.
void second(uint8_t second)
Set the second.
uint8_t second() const
Return the second.
static ZonedDateTime forError()
Return an instance whose isError() returns true.
static ZonedDateTime forEpochSeconds(acetime_t epochSeconds, const TimeZone &timeZone)
Factory method.
Definition: ZonedDateTime.h:93
void year(int16_t year)
Set the year given the full year.
void hour(uint8_t hour)
Set the hour.
ZonedDateTime()
Default constructor.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch taking into account the time zone.
uint8_t minute() const
Return the minute.
void timeZone(const TimeZone &timeZone)
Set the time zone.
void normalize()
Normalize the ZonedDateTime after mutation.
void month(uint8_t month)
Set the month.
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
int32_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
static ZonedDateTime forDateString(const char *dateString)
Factory method.
const LocalDateTime & localDateTime() const
Return the LocalDateTime of the components.
uint8_t month() const
Return the month with January=1, December=12.
const OffsetDateTime & offsetDateTime() const
Return the OffsetDateTime of the components.
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24