AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized from an NTP server or an RTC chip.
ZonedDateTime.h
1 #ifndef ACE_TIME_DATE_TIME_H
2 #define ACE_TIME_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:
30  static const acetime_t kInvalidEpochSeconds = LocalTime::kInvalidSeconds;
31 
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 = TimeZone()) {
55  year, month, day, hour, minute, second);
57  return ZonedDateTime(odt, timeZone);
58  }
59 
73  static ZonedDateTime forEpochSeconds(acetime_t epochSeconds,
74  const TimeZone& timeZone = TimeZone()) {
75  ZonedDateTime dt;
76  if (epochSeconds == kInvalidEpochSeconds) return forError();
77 
79  dt.mOffsetDateTime = OffsetDateTime::forEpochSeconds(
80  epochSeconds, timeOffset);
81  dt.mTimeZone = timeZone;
82  return dt;
83  }
84 
89  static ZonedDateTime forUnixSeconds(acetime_t unixSeconds,
90  const TimeZone& timeZone = TimeZone()) {
91  if (unixSeconds == LocalDate::kInvalidEpochSeconds) {
92  return forEpochSeconds(unixSeconds, timeZone);
93  } else {
95  timeZone);
96  }
97  }
98 
112  static ZonedDateTime forDateString(const char* dateString) {
115  }
116 
121  static ZonedDateTime forDateString(const __FlashStringHelper* dateString) {
124  }
125 
129  }
130 
132  explicit ZonedDateTime() {}
133 
135  bool isError() const { return mOffsetDateTime.isError(); }
136 
138  int16_t year() const { return mOffsetDateTime.year(); }
139 
141  void year(int16_t year) { mOffsetDateTime.year(year); }
142 
144  int8_t yearTiny() const { return mOffsetDateTime.yearTiny(); }
145 
147  void yearTiny(int8_t yearTiny) { mOffsetDateTime.yearTiny(yearTiny); }
148 
150  uint8_t month() const { return mOffsetDateTime.month(); }
151 
153  void month(uint8_t month) { mOffsetDateTime.month(month); }
154 
156  uint8_t day() const { return mOffsetDateTime.day(); }
157 
159  void day(uint8_t day) { mOffsetDateTime.day(day); }
160 
162  uint8_t hour() const { return mOffsetDateTime.hour(); }
163 
165  void hour(uint8_t hour) { mOffsetDateTime.hour(hour); }
166 
168  uint8_t minute() const { return mOffsetDateTime.minute(); }
169 
171  void minute(uint8_t minute) { mOffsetDateTime.minute(minute); }
172 
174  uint8_t second() const { return mOffsetDateTime.second(); }
175 
177  void second(uint8_t second) { mOffsetDateTime.second(second); }
178 
183  uint8_t dayOfWeek() const { return mOffsetDateTime.dayOfWeek(); }
184 
186  TimeOffset timeOffset() const { return mOffsetDateTime.timeOffset(); }
187 
189  const TimeZone& timeZone() const { return mTimeZone; }
190 
195  void timeZone(const TimeZone& timeZone) { mTimeZone = timeZone; }
196 
202  acetime_t epochSeconds = toEpochSeconds();
203  return ZonedDateTime::forEpochSeconds(epochSeconds, timeZone);
204  }
205 
210  void printTo(Print& printer) const;
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 
260  // Use default copy constructor and assignment operator.
261  ZonedDateTime(const ZonedDateTime&) = default;
262  ZonedDateTime& operator=(const ZonedDateTime&) = default;
263 
264  private:
266  static const uint8_t kDateStringLength = 25;
267 
268  friend bool operator==(const ZonedDateTime& a, const ZonedDateTime& b);
269  friend bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b);
270 
272  ZonedDateTime(const OffsetDateTime& offsetDateTime, TimeZone tz):
273  mOffsetDateTime(offsetDateTime),
274  mTimeZone(tz) {}
275 
276  OffsetDateTime mOffsetDateTime;
277  TimeZone mTimeZone;
278 };
279 
287 inline bool operator==(const ZonedDateTime& a, const ZonedDateTime& b) {
288  return a.mOffsetDateTime == b.mOffsetDateTime
289  && a.mTimeZone == b.mTimeZone;
290 }
291 
293 inline bool operator!=(const ZonedDateTime& a, const ZonedDateTime& b) {
294  return ! (a == b);
295 }
296 
297 }
298 
299 #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:59
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
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:65
void yearTiny(int8_t yearTiny)
Set the single-byte year offset from year 2000.
acetime_t toUnixDays() const
Return the number of days since Unix epoch (1970-01-01 00:00:00).
static const acetime_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:50
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
static ZonedDateTime forEpochSeconds(acetime_t epochSeconds, const TimeZone &timeZone=TimeZone())
Factory method.
Definition: ZonedDateTime.h:73
static const acetime_t kInvalidEpochSeconds
Sentinel epochSeconds which indicates an error.
Definition: LocalDate.h:53
static const acetime_t kInvalidSeconds
An invalid seconds marker that indicates isError() true.
Definition: LocalTime.h:23
const TimeZone & timeZone() const
Return the time zone of the ZonedDateTime.
void printTo(Print &printer) const
Print ZonedDateTime to &#39;printer&#39;.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z), taking into account the time zone...
TimeOffset getUtcOffset(acetime_t epochSeconds) const
Return the total UTC offset at epochSeconds, including DST offset.
Definition: TimeZone.h:83
static TimeZone forTimeOffset(TimeOffset offset=TimeOffset())
Factory method to create from a fixed UTC offset.
Definition: TimeZone.h:60
void second(uint8_t second)
Set the second.
void hour(uint8_t hour)
Set the hour.
uint8_t dayOfWeek() const
Return the day of the week using ISO 8601 numbering where Monday=1 and Sunday=7.
void day(uint8_t day)
Set the day of the month.
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch (2000-01-01 00:00:00Z), taking into account the offset zone...
TimeOffset getUtcOffsetForDateTime(const LocalDateTime &ldt) const
Return the best guess of the UTC offset at the given LocalDateTime for the current TimeZone...
Definition: TimeZone.h:105
acetime_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
static ZonedDateTime forError()
Return an instance whose isError() returns true.
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, time.
Definition: LocalDateTime.h:26
uint8_t month() const
Return the month with January=1, December=12.
uint8_t hour() const
Return the hour.
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z), taking into account the offse...
static ZonedDateTime forDateString(const char *dateString)
Factory method.
void year(int16_t year)
Set the year given the full year.
ZonedDateTime convertToTimeZone(const TimeZone &timeZone) const
Create a ZonedDateTime in a different time zone (with the same epochSeconds).
uint8_t hour() const
Return the hour.
int8_t compareTo(const OffsetDateTime &that) const
Compare this OffsetDateTime with another OffsetDateTime, and return (<0, 0, >0) according to whether ...
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 not equal.
int8_t compareTo(const ZonedDateTime &that) const
Compare this ZonedDateTime with another ZonedDateTime, and return (<0, 0, >0) according to whether th...
uint8_t day() const
Return the day of the month.
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
bool isError() const
Return true if any component indicates an error condition.
static ZonedDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, const TimeZone &timeZone=TimeZone())
Factory method using separated date, time, and time zone fields.
Definition: ZonedDateTime.h:51
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
int16_t year() const
Return the year.
static OffsetDateTime forEpochSeconds(acetime_t epochSeconds, TimeOffset timeOffset=TimeOffset())
Factory method.
uint8_t day() const
Return the day of the month.
TimeOffset timeOffset() const
Return the offset zone of the OffsetDateTime.
int16_t year() const
Return the year.
Class that describes a time zone.
Definition: TimeZone.h:50
The date (year, month, day) and time (hour, minute, second) fields representing an instant in time...
Definition: ZonedDateTime.h:28
uint8_t second() const
Return the second.
void minute(uint8_t minute)
Set the minute.
static ZonedDateTime forDateString(const __FlashStringHelper *dateString)
Factory method.
acetime_t toUnixSeconds() const
Return the number of seconds from Unix epoch 1970-01-01 00:00:00Z.
friend bool operator==(const ZonedDateTime &a, const ZonedDateTime &b)
Return true if two ZonedDateTime objects are equal in all components.
uint8_t minute() const
Return the minute.
void timeZone(const TimeZone &timeZone)
Set the time zone.
bool isError() const
Return true if any component indicates an error condition.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
ZonedDateTime()
Default constructor.
static ZonedDateTime forUnixSeconds(acetime_t unixSeconds, const TimeZone &timeZone=TimeZone())
Factory method to create a ZonedDateTime using the number of seconds from Unix epoch.
Definition: ZonedDateTime.h:89
uint8_t month() const
Return the month with January=1, December=12.
void month(uint8_t month)
Set the month.
uint8_t second() const
Return the second.
static OffsetDateTime forError()
Factory method that returns an instance whose isError() is true.
uint8_t minute() const
Return the minute.
acetime_t toEpochDays() const
Return number of whole days since AceTime epoch (2000-01-01 00:00:00Z), taking into account the time ...
static OffsetDateTime forDateString(const char *dateString)
Factory method.