AceTime  2.1.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 "common/compat.h"
11 #include "OffsetDateTime.h"
12 #include "TimeZone.h"
13 
14 class Print;
15 
16 namespace ace_time {
17 
38  public:
57  int16_t year, uint8_t month, uint8_t day,
58  uint8_t hour, uint8_t minute, uint8_t second,
59  const TimeZone& timeZone, uint8_t fold = 0) {
62  return forLocalDateTime(ldt, timeZone);
63  }
64 
77  const LocalDateTime& ldt,
78  const TimeZone& timeZone) {
79  auto odt = timeZone.getOffsetDateTime(ldt);
80  return ZonedDateTime(odt, timeZone);
81  }
95  const TimeZone& timeZone) {
96  OffsetDateTime odt = (epochSeconds == LocalDate::kInvalidEpochSeconds)
98  : timeZone.getOffsetDateTime(epochSeconds);
99  return ZonedDateTime(odt, timeZone);
100  }
101 
121  int64_t unixSeconds, const TimeZone& timeZone) {
122  acetime_t epochSeconds;
123  if (unixSeconds == LocalDate::kInvalidUnixSeconds64) {
124  epochSeconds = LocalDate::kInvalidEpochSeconds;
125  } else {
126  epochSeconds = unixSeconds
128  }
129  return forEpochSeconds(epochSeconds, timeZone);
130  }
131 
145  static ZonedDateTime forDateString(const char* dateString) {
148  }
149 
154  static ZonedDateTime forDateString(const __FlashStringHelper* dateString) {
157  }
158 
162  }
163 
165  explicit ZonedDateTime() {}
166 
168  bool isError() const { return mOffsetDateTime.isError(); }
169 
171  int16_t year() const { return mOffsetDateTime.year(); }
172 
174  void year(int16_t year) { mOffsetDateTime.year(year); }
175 
177  uint8_t month() const { return mOffsetDateTime.month(); }
178 
180  void month(uint8_t month) { mOffsetDateTime.month(month); }
181 
183  uint8_t day() const { return mOffsetDateTime.day(); }
184 
186  void day(uint8_t day) { mOffsetDateTime.day(day); }
187 
189  uint8_t hour() const { return mOffsetDateTime.hour(); }
190 
192  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
193 
195  uint8_t minute() const { return mOffsetDateTime.minute(); }
196 
198  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
199 
201  uint8_t second() const { return mOffsetDateTime.second(); }
202 
204  void second(uint8_t second) { mOffsetDateTime.second(second); }
205 
207  uint8_t fold() const { return mOffsetDateTime.fold(); }
208 
210  void fold(uint8_t fold) { mOffsetDateTime.fold(fold); }
211 
216  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
217 
219  const TimeZone& timeZone() const { return mTimeZone; }
220 
225  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
226 
228  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
229 
231  const LocalDateTime& localDateTime() const {
232  return mOffsetDateTime.localDateTime();
233  }
234 
251  void normalize() {
252  mOffsetDateTime = mTimeZone.getOffsetDateTime(localDateTime());
253  }
254 
260  acetime_t epochSeconds = toEpochSeconds();
261  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
262  }
263 
269  int32_t toEpochDays() const {
270  return mOffsetDateTime.toEpochDays();
271  }
272 
274  int32_t toUnixDays() const {
275  return mOffsetDateTime.toUnixDays();
276  }
277 
284  return mOffsetDateTime.toEpochSeconds();
285  }
286 
294  int64_t toUnixSeconds64() const {
295  return mOffsetDateTime.toUnixSeconds64();
296  }
297 
314  int8_t compareTo(const ZonedDateTime& that) const {
315  return mOffsetDateTime.compareTo(that.mOffsetDateTime);
316  }
317 
323  void printTo(Print& printer) const;
324 
325  // Use default copy constructor and assignment operator.
326  ZonedDateTime(const ZonedDateTime&) = default;
327  ZonedDateTime& operator=(const ZonedDateTime&) = default;
328 
329  private:
331  static const uint8_t kDateStringLength = 25;
332 
333  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
334 
336  ZonedDateTime(const OffsetDateTime& offsetDateTime, const TimeZone& tz):
337  mOffsetDateTime(offsetDateTime),
338  mTimeZone(tz) {}
339 
340  OffsetDateTime mOffsetDateTime;
341  TimeZone mTimeZone;
342 };
343 
351 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
352  return a.mOffsetDateTime == b.mOffsetDateTime
353  && a.mTimeZone == b.mTimeZone;
354 }
355 
357 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
358  return ! (a == b);
359 }
360 
361 }
362 
363 #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:68
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:31
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:45
static const int64_t kInvalidUnixSeconds64
Sentinel unixSeconds64 which indicates an error.
Definition: LocalDate.h:90
static const int32_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:87
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:382
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:37
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:56
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:76
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:94
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.
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24
Macros and definitions that provide a consistency layer among the various Arduino boards for compatib...