AceTime  2.2.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 
124  static OffsetDateTime forDateString(const char* dateString);
125 
133  static OffsetDateTime forDateStringChainable(const char*& dateString);
134 
140  static OffsetDateTime forDateString(const __FlashStringHelper* dateString) {
141  // Copy the F() string into a buffer. Use strncpy_P() because ESP32 and
142  // ESP8266 do not have strlcpy_P(). We need +1 for the '\0' character and
143  // another +1 to determine if the dateString is too long to fit.
144  char buffer[kDateStringLength + 2];
145  strncpy_P(buffer, (const char*) dateString, sizeof(buffer));
146  buffer[kDateStringLength + 1] = 0;
147 
148  // check if the original F() was too long
149  size_t len = strlen(buffer);
150  if (len > kDateStringLength) {
151  return forError();
152  }
153 
154  return forDateString(buffer);
155  }
156 
160  }
161 
163  explicit OffsetDateTime() {}
164 
166  bool isError() const {
167  // Check mTimeOffset first because it's expected to be invalid more often.
168  return mTimeOffset.isError() || mLocalDateTime.isError();
169  }
170 
172  int16_t year() const { return mLocalDateTime.year(); }
173 
175  void year(int16_t year) { mLocalDateTime.year(year); }
176 
178  uint8_t month() const { return mLocalDateTime.month(); }
179 
181  void month(uint8_t month) { mLocalDateTime.month(month); }
182 
184  uint8_t day() const { return mLocalDateTime.day(); }
185 
187  void day(uint8_t day) { mLocalDateTime.day(day); }
188 
190  uint8_t hour() const { return mLocalDateTime.hour(); }
191 
193  void hour(uint8_t hour) { mLocalDateTime.hour(hour); }
194 
196  uint8_t minute() const { return mLocalDateTime.minute(); }
197 
199  void minute(uint8_t minute) { mLocalDateTime.minute(minute); }
200 
202  uint8_t second() const { return mLocalDateTime.second(); }
203 
205  void second(uint8_t second) { mLocalDateTime.second(second); }
206 
208  uint8_t fold() const { return mLocalDateTime.fold(); }
209 
211  void fold(uint8_t fold) { mLocalDateTime.fold(fold); }
212 
214  uint8_t dayOfWeek() const { return mLocalDateTime.dayOfWeek(); }
215 
217  TimeOffset timeOffset() const { return mTimeOffset; }
218 
220  void timeOffset(TimeOffset timeOffset) { mTimeOffset = timeOffset; }
221 
223  const LocalDateTime& localDateTime() const { return mLocalDateTime; }
224 
226  const LocalDate& localDate() const { return mLocalDateTime.localDate(); }
227 
229  const LocalTime& localTime() const { return mLocalDateTime.localTime(); }
230 
238  acetime_t epochSeconds = toEpochSeconds();
239  return OffsetDateTime::forEpochSeconds(epochSeconds, timeOffset);
240  }
241 
247  int32_t toEpochDays() const {
249 
250  int32_t epochDays = mLocalDateTime.localDate().toEpochDays();
251 
252  // Increment or decrement the day count depending on the time offset.
253  acetime_t timeOffset = mLocalDateTime.localTime().toSeconds()
254  - mTimeOffset.toSeconds();
255  if (timeOffset >= 86400) {
256  epochDays++;
257  } else if (timeOffset < 0) {
258  epochDays--;
259  }
260 
261  return epochDays;
262  }
263 
265  int32_t toUnixDays() const {
268  }
269 
277  acetime_t epochSeconds = mLocalDateTime.toEpochSeconds();
278  if (epochSeconds == LocalDate::kInvalidEpochSeconds) {
279  return epochSeconds;
280  }
281  return epochSeconds - mTimeOffset.toSeconds();
282  }
283 
291  int64_t toUnixSeconds64() const {
293  return mLocalDateTime.toUnixSeconds64() - mTimeOffset.toSeconds();
294  }
295 
312  int8_t compareTo(const OffsetDateTime& that) const {
313  acetime_t thisSeconds = toEpochSeconds();
314  acetime_t thatSeconds = that.toEpochSeconds();
315  if (thisSeconds < thatSeconds) return -1;
316  if (thisSeconds > thatSeconds) return 1;
317  return 0;
318  }
319 
325  void printTo(Print& printer) const;
326 
327  // Use default copy constructor and assignment operator.
328  OffsetDateTime(const OffsetDateTime&) = default;
329  OffsetDateTime& operator=(const OffsetDateTime&) = default;
330 
331  private:
332  friend bool operator==(const OffsetDateTime& a, const OffsetDateTime& b);
333 
335  static const uint8_t kDateStringLength = 25;
336 
338  explicit OffsetDateTime(const LocalDateTime& ldt, TimeOffset timeOffset):
339  mLocalDateTime(ldt),
340  mTimeOffset(timeOffset) {}
341 
342  LocalDateTime mLocalDateTime;
343  TimeOffset mTimeOffset;
344 };
345 
351 inline bool operator==(const OffsetDateTime& a, const OffsetDateTime& b) {
352  return a.mLocalDateTime == b.mLocalDateTime
353  && a.mTimeOffset == b.mTimeOffset;
354 }
355 
357 inline bool operator!=(const OffsetDateTime& a, const OffsetDateTime& b) {
358  return ! (a == b);
359 }
360 
361 }
362 
363 #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:57
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:31
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:64
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
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:91
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:49
static const int32_t kInvalidEpochDays
Sentinel epochDays which indicates an error.
Definition: LocalDate.h:84
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
int32_t toEpochDays() const
Return number of days since the current epoch year sCurrentEpochYear.
Definition: LocalDate.h:355
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.
static OffsetDateTime forDateString(const __FlashStringHelper *dateString)
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:105
int32_t toSeconds() const
Return the time offset as seconds.
Definition: TimeOffset.h:116
bool isError() const
Return true if this TimeOffset represents an error.
Definition: TimeOffset.h:138
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24