AceTime  1.11.7
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 
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, uint8_t fold = 0) {
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 = (epochSeconds == LocalDate::kInvalidEpochSeconds)
75  : timeZone.getOffsetDateTime(epochSeconds);
76  return ZonedDateTime(odt, timeZone);
77  }
78 
91  int32_t unixSeconds, const TimeZone& timeZone) {
92  acetime_t epochSeconds = (unixSeconds == LocalDate::kInvalidUnixSeconds)
94  : unixSeconds - LocalDate::kSecondsSinceUnixEpoch;
95  return forEpochSeconds(epochSeconds, timeZone);
96  }
97 
110  int64_t unixSeconds, const TimeZone& timeZone) {
111  acetime_t epochSeconds = (unixSeconds == LocalDate::kInvalidUnixSeconds64
112  || unixSeconds > LocalDate::kMaxValidUnixSeconds64
113  || unixSeconds < LocalDate::kMinValidUnixSeconds64)
115  : (acetime_t) (unixSeconds - LocalDate::kSecondsSinceUnixEpoch);
116  return forEpochSeconds(epochSeconds, timeZone);
117  }
118 
132  static ZonedDateTime forDateString(const char* dateString) {
135  }
136 
141  static ZonedDateTime forDateString(const __FlashStringHelper* dateString) {
144  }
145 
149  }
150 
152  explicit ZonedDateTime() {}
153 
155  bool isError() const { return mOffsetDateTime.isError(); }
156 
158  int16_t year() const { return mOffsetDateTime.year(); }
159 
161  void year(int16_t year) { mOffsetDateTime.year(year); }
162 
168  int8_t yearTiny() const { return mOffsetDateTime.yearTiny(); }
169 
175  void yearTiny(int8_t yearTiny) { mOffsetDateTime.yearTiny(yearTiny); }
176 
178  uint8_t month() const { return mOffsetDateTime.month(); }
179 
181  void month(uint8_t month) { mOffsetDateTime.month(month); }
182 
184  uint8_t day() const { return mOffsetDateTime.day(); }
185 
187  void day(uint8_t day) { mOffsetDateTime.day(day); }
188 
190  uint8_t hour() const { return mOffsetDateTime.hour(); }
191 
193  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
194 
196  uint8_t minute() const { return mOffsetDateTime.minute(); }
197 
199  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
200 
202  uint8_t second() const { return mOffsetDateTime.second(); }
203 
205  void second(uint8_t second) { mOffsetDateTime.second(second); }
206 
208  uint8_t fold() const { return mOffsetDateTime.fold(); }
209 
211  void fold(uint8_t fold) { mOffsetDateTime.fold(fold); }
212 
217  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
218 
220  const TimeZone& timeZone() const { return mTimeZone; }
221 
226  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
227 
229  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
230 
232  const LocalDateTime& localDateTime() const {
233  return mOffsetDateTime.localDateTime();
234  }
235 
252  void normalize() {
253  mOffsetDateTime = mTimeZone.getOffsetDateTime(localDateTime());
254  }
255 
261  acetime_t epochSeconds = toEpochSeconds();
262  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
263  }
264 
269  int32_t toEpochDays() const {
270  return mOffsetDateTime.toEpochDays();
271  }
272 
274  int32_t toUnixDays() const {
275  return mOffsetDateTime.toUnixDays();
276  }
277 
286  acetime_t toEpochSeconds() const {
287  return mOffsetDateTime.toEpochSeconds();
288  }
289 
297  int32_t toUnixSeconds() const {
298  return mOffsetDateTime.toUnixSeconds();
299  }
300 
308  int64_t toUnixSeconds64() const {
309  return mOffsetDateTime.toUnixSeconds64();
310  }
311 
328  int8_t compareTo(const ZonedDateTime& that) const {
329  return mOffsetDateTime.compareTo(that.mOffsetDateTime);
330  }
331 
337  void printTo(Print& printer) const;
338 
339  // Use default copy constructor and assignment operator.
340  ZonedDateTime(const ZonedDateTime&) = default;
341  ZonedDateTime& operator=(const ZonedDateTime&) = default;
342 
343  private:
345  static const uint8_t kDateStringLength = 25;
346 
347  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
348 
350  ZonedDateTime(const OffsetDateTime& offsetDateTime, const TimeZone& tz):
351  mOffsetDateTime(offsetDateTime),
352  mTimeZone(tz) {}
353 
354  OffsetDateTime mOffsetDateTime;
355  TimeZone mTimeZone;
356 };
357 
365 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
366  return a.mOffsetDateTime == b.mOffsetDateTime
367  && a.mTimeZone == b.mTimeZone;
368 }
369 
371 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
372  return ! (a == b);
373 }
374 
375 }
376 
377 #endif
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 int32_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:45
static const int64_t kMaxValidUnixSeconds64
Maximum 64-bit Unix seconds supported by acetime_t.
Definition: LocalDate.h:81
static const int64_t kInvalidUnixSeconds64
Sentinel 64-bit unixSeconds which indicates an error.
Definition: LocalDate.h:78
static const int64_t kMinValidUnixSeconds64
Minimum 64-bit Unix seconds supported by acetime_t.
Definition: LocalDate.h:85
static const int32_t kInvalidUnixSeconds
Sentinel unixSeconds which indicates an error.
Definition: LocalDate.h:75
static const int32_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:69
The date (year, month, day), time (hour, minute, second) and offset from UTC (timeOffset).
uint8_t day() const
Return the day of the month.
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
int64_t toUnixSeconds64() const
Return the 64-bit number of seconds from Unix epoch 1970-01-01 00:00:00Z.
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 (2000-01-01 00:00:00Z), taking into account the offset zone.
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.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
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 (2000-01-01 00:00:00Z), taking into account the offse...
int32_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
int32_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
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:85
OffsetDateTime getOffsetDateTime(const LocalDateTime &ldt) const
Return the best estimate of the OffsetDateTime at the given LocalDateTime for the current TimeZone.
Definition: TimeZone.h:361
static TimeZone forTimeOffset(TimeOffset stdOffset, TimeOffset dstOffset=TimeOffset())
Factory method to create from a UTC offset and an optional DST offset.
Definition: TimeZone.h:114
The date (year, month, day), time (hour, minute, second), and a timeZone representing an instant in t...
Definition: ZonedDateTime.h:32
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:51
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 (2000-01-01 00:00:00Z), taking into account the time ...
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.
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.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
void minute(uint8_t minute)
Set the minute.
int32_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
int64_t toUnixSeconds64() const
Return the 64-bit number of seconds from Unix epoch 1970-01-01 00:00:00Z.
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:71
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
void year(int16_t year)
Set the year given the full year.
void hour(uint8_t hour)
Set the hour.
ZonedDateTime()
Default constructor.
static ZonedDateTime forUnixSeconds(int32_t unixSeconds, const TimeZone &timeZone)
Factory method to create a ZonedDateTime using the number of seconds from Unix epoch.
Definition: ZonedDateTime.h:90
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z), 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.
Macros and definitions that provide a consistency layer among the various Arduino boards for compatib...