AceTime  3.0.0
Date and time classes for Arduino that support timezones from the TZ Database.
All Classes Files Functions Variables Typedefs Friends Macros Pages
ZonedExtra.h
1 /*
2  * MIT License
3  * Copyright (c) 2023 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_ZONED_EXTRA_H
7 #define ACE_TIME_ZONED_EXTRA_H
8 
9 #include <string.h> // strncpy()
10 #include <stdint.h>
11 #include "common/common.h" // acetime_t, kAbbrevSize
12 #include "TimeOffset.h"
13 
14 namespace ace_time {
15 
16 class TimeZone;
17 class LocalDateTime;
18 
19 class ZonedExtra {
20  public:
22  static const uint8_t kAbbrevSize = ace_time::kAbbrevSize;
23 
29  static const uint8_t kTypeNotFound = 0;
30 
35  static const uint8_t kTypeExact = 1;
36 
43  static const uint8_t kTypeGap = 2;
44 
52  static const uint8_t kTypeOverlap = 3;
53 
55  static ZonedExtra forError() {
56  return ZonedExtra();
57  }
58 
65  int16_t year, uint8_t month, uint8_t day,
66  uint8_t hour, uint8_t minute, uint8_t second,
67  const TimeZone& tz, uint8_t fold = 0);
68 
71  acetime_t epochSeconds,
72  const TimeZone& tz);
73 
80  const LocalDateTime& ldt,
81  const TimeZone& tz);
82 
84  explicit ZonedExtra() {}
85 
87  explicit ZonedExtra(
88  uint8_t type,
89  int32_t stdOffsetSeconds,
90  int32_t dstOffsetSeconds,
91  int32_t reqStdOffsetSeconds,
92  int32_t reqDstOffsetSeconds,
93  const char* abbrev)
94  : mStdOffsetSeconds(stdOffsetSeconds)
95  , mDstOffsetSeconds(dstOffsetSeconds)
96  , mReqStdOffsetSeconds(reqStdOffsetSeconds)
97  , mReqDstOffsetSeconds(reqDstOffsetSeconds)
98  , mType(type)
99  {
100  strncpy(mAbbrev, abbrev, kAbbrevSize - 1);
101  mAbbrev[kAbbrevSize - 1] = '\0';
102  }
103 
105  bool isError() const {
106  return mStdOffsetSeconds == kInvalidSeconds;
107  }
108 
109  uint8_t type() const { return mType; }
110 
113  return TimeOffset::forSeconds(mStdOffsetSeconds);
114  }
115 
118  return TimeOffset::forSeconds(mDstOffsetSeconds);
119  }
120 
128  return TimeOffset::forSeconds(mStdOffsetSeconds + mDstOffsetSeconds);
129  }
130 
136  return TimeOffset::forSeconds(mReqStdOffsetSeconds);
137  }
138 
144  return TimeOffset::forSeconds(mReqDstOffsetSeconds);
145  }
146 
156  return TimeOffset::forSeconds(
157  mReqStdOffsetSeconds + mReqDstOffsetSeconds);
158  }
159 
166  const char* abbrev() const { return mAbbrev; }
167 
168  private:
169  static const int32_t kInvalidSeconds = INT32_MIN;
170 
171  int32_t mStdOffsetSeconds = kInvalidSeconds;
172  int32_t mDstOffsetSeconds = kInvalidSeconds;
173  int32_t mReqStdOffsetSeconds = kInvalidSeconds;
174  int32_t mReqDstOffsetSeconds = kInvalidSeconds;
175  uint8_t mType = kTypeNotFound;
176  char mAbbrev[kAbbrevSize] = "";
177 };
178 
179 }
180 
181 #endif
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
static TimeOffset forSeconds(int32_t seconds)
Create TimeOffset from seconds from 00:00.
Definition: TimeOffset.h:96
Class that describes a time zone.
Definition: TimeZone.h:86
TimeOffset reqDstOffset() const
DST offset of the requested epochSeconds or LocalDateTime.
Definition: ZonedExtra.h:143
TimeOffset reqStdOffset() const
STD offset of the requested epochSeconds or LocalDateTime.
Definition: ZonedExtra.h:135
static ZonedExtra forError()
Return an instance that indicates an error.
Definition: ZonedExtra.h:55
static ZonedExtra forLocalDateTime(const LocalDateTime &ldt, const TimeZone &tz)
Return an instance for the given LocalDateTime and TimeZone.
Definition: ZonedExtra.cpp:23
TimeOffset stdOffset() const
STD offset of the resulting OffsetDateTime.
Definition: ZonedExtra.h:112
static const uint8_t kAbbrevSize
Size of char buffer needed to hold the largest abbreviation.
Definition: ZonedExtra.h:22
static ZonedExtra forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, const TimeZone &tz, uint8_t fold=0)
Return an instance for the given LocalDateTime and TimeZone.
Definition: ZonedExtra.cpp:6
static const uint8_t kTypeNotFound
The epochSeconds or LocalDateTime was not found because it was outside the range of the zoneinfo data...
Definition: ZonedExtra.h:29
static const uint8_t kTypeOverlap
The given LocalDateTime matches 2 possible epochSeconds, which is disambguiated by the LocalDateTime:...
Definition: ZonedExtra.h:52
TimeOffset dstOffset() const
DST offset of the resulting OffsetDateTime.
Definition: ZonedExtra.h:117
ZonedExtra()
Consructor.
Definition: ZonedExtra.h:84
TimeOffset reqTimeOffset() const
The total time offset of the requested epochSeconds of LocalDateTime, (reqStdOffset + reqDstOffset).
Definition: ZonedExtra.h:155
static const uint8_t kTypeExact
The given LocalDateTime matches a single epochSeconds.
Definition: ZonedExtra.h:35
const char * abbrev() const
Returns the pointer to the local string buffer containing the timezone abbreviation (e....
Definition: ZonedExtra.h:166
static const uint8_t kTypeGap
The given LocalDateTime occurs in a gap and does not match any epochSeconds.
Definition: ZonedExtra.h:43
ZonedExtra(uint8_t type, int32_t stdOffsetSeconds, int32_t dstOffsetSeconds, int32_t reqStdOffsetSeconds, int32_t reqDstOffsetSeconds, const char *abbrev)
Consructor.
Definition: ZonedExtra.h:87
static ZonedExtra forEpochSeconds(acetime_t epochSeconds, const TimeZone &tz)
Return an instance for the given epochSeconds and TimeZone.
Definition: ZonedExtra.cpp:16
bool isError() const
Indicates that the LocalDateTime or epochSeconds was not found.
Definition: ZonedExtra.h:105
TimeOffset timeOffset() const
The total time offset (stdOffset + dstOffset).
Definition: ZonedExtra.h:127
Identifiers used by implementation code which need to be publically exported.
const uint8_t kAbbrevSize
Size of the c-string buffer needed to hold a time zone abbreviation.
Definition: common.h:44
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24