AceTime  3.0.0
Date and time classes for Arduino that support timezones from the TZ Database.
OffsetDateTime.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_OFFSET_DATE_TIME_H
7 #define ACE_TIME_OFFSET_DATE_TIME_H
8 
9 #include <stdint.h>
10 #include "TimeOffset.h"
11 #include "LocalDateTime.h"
12 
13 class Print;
14 
15 namespace ace_time {
16 
38  public:
39 
44  }
45 
61  static OffsetDateTime forComponents(int16_t year, uint8_t month,
62  uint8_t day, uint8_t hour, uint8_t minute, uint8_t second,
63  TimeOffset timeOffset, uint8_t fold = 0) {
66  return OffsetDateTime(ldt, timeOffset);
67  }
68 
80  TimeOffset timeOffset, uint8_t fold = 0) {
81  if (epochSeconds != LocalDate::kInvalidEpochSeconds) {
82  epochSeconds += timeOffset.toSeconds();
83  }
84  auto ldt = LocalDateTime::forEpochSeconds(epochSeconds, fold);
85  return OffsetDateTime(ldt, timeOffset);
86  }
87 
99  int64_t unixSeconds, TimeOffset timeOffset, int8_t fold = 0) {
100  if (unixSeconds != LocalDate::kInvalidUnixSeconds64) {
101  unixSeconds += timeOffset.toSeconds();
102  }
103  auto ldt = LocalDateTime::forUnixSeconds64(unixSeconds, fold);
104  return OffsetDateTime(ldt, timeOffset);
105  }
106 
123  static OffsetDateTime forDateString(const char* dateString);
124 
130  static OffsetDateTime forDateString(const __FlashStringHelper* dateString);
131 
139  static OffsetDateTime forDateStringChainable(const char*& dateString);
140 
144  }
145 
147  explicit OffsetDateTime() {}
148 
150  bool isError() const {
151  // Check mTimeOffset first because it's expected to be invalid more often.
152  return mTimeOffset.isError() || mLocalDateTime.isError();
153  }
154 
156  int16_t year() const { return mLocalDateTime.year(); }
157 
159  void year(int16_t year) { mLocalDateTime.year(year); }
160 
162  uint8_t month() const { return mLocalDateTime.month(); }
163 
165  void month(uint8_t month) { mLocalDateTime.month(month); }
166 
168  uint8_t day() const { return mLocalDateTime.day(); }
169 
171  void day(uint8_t day) { mLocalDateTime.day(day); }
172 
174  uint8_t hour() const { return mLocalDateTime.hour(); }
175 
177  void hour(uint8_t hour) { mLocalDateTime.hour(hour); }
178 
180  uint8_t minute() const { return mLocalDateTime.minute(); }
181 
183  void minute(uint8_t minute) { mLocalDateTime.minute(minute); }
184 
186  uint8_t second() const { return mLocalDateTime.second(); }
187 
189  void second(uint8_t second) { mLocalDateTime.second(second); }
190 
192  uint8_t fold() const { return mLocalDateTime.fold(); }
193 
195  void fold(uint8_t fold) { mLocalDateTime.fold(fold); }
196 
198  uint8_t dayOfWeek() const { return mLocalDateTime.dayOfWeek(); }
199 
201  TimeOffset timeOffset() const { return mTimeOffset; }
202 
204  void timeOffset(TimeOffset timeOffset) { mTimeOffset = timeOffset; }
205 
207  const LocalDateTime& localDateTime() const { return mLocalDateTime; }
208 
210  const LocalDate& localDate() const { return mLocalDateTime.localDate(); }
211 
213  const LocalTime& localTime() const { return mLocalDateTime.localTime(); }
214 
222  acetime_t epochSeconds = toEpochSeconds();
223  return OffsetDateTime::forEpochSeconds(epochSeconds, timeOffset);
224  }
225 
231  int32_t toEpochDays() const {
233 
234  int32_t epochDays = mLocalDateTime.localDate().toEpochDays();
235 
236  // Increment or decrement the day count depending on the time offset.
237  acetime_t timeOffset = mLocalDateTime.localTime().toSeconds()
238  - mTimeOffset.toSeconds();
239  if (timeOffset >= 86400) {
240  epochDays++;
241  } else if (timeOffset < 0) {
242  epochDays--;
243  }
244 
245  return epochDays;
246  }
247 
249  int32_t toUnixDays() const {
252  }
253 
261  acetime_t epochSeconds = mLocalDateTime.toEpochSeconds();
262  if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
263  return epochSeconds;
264  }
265  return epochSeconds - mTimeOffset.toSeconds();
266  }
267 
275  int64_t toUnixSeconds64() const {
277  return mLocalDateTime.toUnixSeconds64() - mTimeOffset.toSeconds();
278  }
279 
296  int8_t compareTo(const OffsetDateTime& that) const {
297  acetime_t thisSeconds = toEpochSeconds();
298  acetime_t thatSeconds = that.toEpochSeconds();
299  if (thisSeconds < thatSeconds) return -1;
300  if (thisSeconds > thatSeconds) return 1;
301  return 0;
302  }
303 
309  void printTo(Print& printer) const;
310 
311  // Use default copy constructor and assignment operator.
312  OffsetDateTime(const OffsetDateTime&) = default;
313  OffsetDateTime& operator=(const OffsetDateTime&) = default;
314 
315  private:
316  friend bool operator==(const OffsetDateTime& a, const OffsetDateTime& b);
317 
319  static const uint8_t kDateStringLength = 25;
320 
322  explicit OffsetDateTime(const LocalDateTime& ldt, TimeOffset timeOffset):
323  mLocalDateTime(ldt),
324  mTimeOffset(timeOffset) {}
325 
326  LocalDateTime mLocalDateTime;
327  TimeOffset mTimeOffset;
328 };
329 
335 inline bool operator==(const OffsetDateTime& a, const OffsetDateTime& b) {
336  return a.mLocalDateTime == b.mLocalDateTime
337  && a.mTimeOffset == b.mTimeOffset;
338 }
339 
341 inline bool operator!=(const OffsetDateTime& a, const OffsetDateTime& b) {
342  return ! (a == b);
343 }
344 
345 }
346 
347 #endif
static int32_t daysToCurrentEpochFromUnixEpoch()
Return the number of days from the Unix epoch (1970-01-01T00:00:00) to the current epoch.
Definition: Epoch.h:58
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
int64_t toUnixSeconds64() const
Return 64-bit seconds from Unix epoch 1970-01-01 00:00:00 UTC, after assuming that the date and time ...
static LocalDateTime forEpochSeconds(acetime_t epochSeconds, uint8_t fold=0)
Factory method.
Definition: LocalDateTime.h:63
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
uint8_t day() const
Return the day of the month.
const LocalDate & localDate() const
Return the LocalDate.
bool isError() const
Return true if any component indicates an error condition.
static LocalDateTime forUnixSeconds64(int64_t unixSeconds, uint8_t fold=0)
Factory method that takes the 64-bit number of seconds since Unix Epoch of 1970-01-01.
Definition: LocalDateTime.h:90
static LocalDateTime forError()
Factory method that returns an instance where isError() returns true.
uint8_t month() const
Return the month with January=1, December=12.
uint8_t fold() const
Return the fold.
uint8_t second() const
Return the second.
uint8_t minute() const
Return the minute.
const LocalTime & localTime() const
Return the LocalTime.
uint8_t hour() const
Return the hour.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
acetime_t toEpochSeconds() const
Return seconds since the current AceTime epoch defined by Epoch::currentEpochYear().
int16_t year() const
Return the year.
The date (year, month, day) representing the date without regards to time zone.
Definition: LocalDate.h:46
static const int32_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:81
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
int32_t toEpochDays() const
Return number of days since the current epoch year sCurrentEpochYear.
Definition: LocalDate.h:352
The time (hour, minute, second) fields representing the time without regards to the day or the time z...
Definition: LocalTime.h:27
acetime_t toSeconds() const
Return the number of seconds since midnight.
Definition: LocalTime.h:145
The date (year, month, day), time (hour, minute, second) and fixed offset from UTC (timeOffset).
static OffsetDateTime forUnixSeconds64(int64_t unixSeconds, TimeOffset timeOffset, int8_t fold=0)
Factory method that takes the number of seconds (64-bit) since Unix Epoch of 1970-01-01.
const LocalTime & localTime() const
Return the LocalTime.
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.
static OffsetDateTime forDateStringChainable(const char *&dateString)
Variant of forDateString() that updates the pointer to the next unprocessed character.
void month(uint8_t month)
Set the month.
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.
void timeOffset(TimeOffset timeOffset)
Set the UTC offset.
OffsetDateTime()
Constructor.
uint8_t month() const
Return the month with January=1, December=12.
void printTo(Print &printer) const
Print OffsetDateTime to 'printer' in ISO 8601 format.
void minute(uint8_t minute)
Set the minute.
static OffsetDateTime forLocalDateTimeAndOffset(const LocalDateTime &localDateTime, TimeOffset timeOffset)
Factory method from LocalDateTime and TimeOffset.
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...
void hour(uint8_t hour)
Set the hour.
uint8_t minute() const
Return the minute.
OffsetDateTime convertToTimeOffset(TimeOffset timeOffset) const
Create a OffsetDateTime in a different UTC offset code (with the same epochSeconds).
int16_t year() const
Return the year.
void day(uint8_t day)
Set the day of the month.
const LocalDate & localDate() const
Return the LocalDate.
friend bool operator==(const OffsetDateTime &a, const OffsetDateTime &b)
Return true if two OffsetDateTime objects are equal in all components.
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).
static OffsetDateTime forEpochSeconds(acetime_t epochSeconds, TimeOffset timeOffset, uint8_t fold=0)
Factory method.
void year(int16_t year)
Set the year.
void second(uint8_t second)
Set the second.
void fold(uint8_t fold)
Set the fold.
uint8_t second() const
Return the second.
static OffsetDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, TimeOffset timeOffset, uint8_t fold=0)
Factory method using separated date, time, and UTC offset fields.
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:122
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:131
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:165
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24