AceTime  2.1.0
Date and time classes for Arduino that support timezones from the TZ Database.
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> // memcpy()
10 #include <stdint.h>
11 #include "common/common.h" // acetime_t
12 #include "internal/common.h" // kAbbrevSize
13 #include "TimeOffset.h"
14 
15 namespace ace_time {
16 
17 class TimeZone;
18 class LocalDateTime;
19 
20 class ZonedExtra {
21  public:
22  static const int16_t kInvalidMinutes = INT16_MIN;
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 
61  acetime_t epochSeconds,
62  const TimeZone& tz);
63 
70  const LocalDateTime& ldt,
71  const TimeZone& tz);
72 
74  explicit ZonedExtra() {}
75 
77  explicit ZonedExtra(
78  uint8_t type,
79  int16_t stdOffsetMinutes,
80  int16_t dstOffsetMinutes,
81  int16_t reqStdOffsetMinutes,
82  int16_t reqDstOffsetMinutes,
83  const char* abbrev)
84  : mStdOffsetMinutes(stdOffsetMinutes)
85  , mDstOffsetMinutes(dstOffsetMinutes)
86  , mReqStdOffsetMinutes(reqStdOffsetMinutes)
87  , mReqDstOffsetMinutes(reqDstOffsetMinutes)
88  , mType(type)
89  {
90  memcpy(mAbbrev, abbrev, internal::kAbbrevSize);
91  mAbbrev[internal::kAbbrevSize - 1] = '\0';
92  }
93 
95  bool isError() const {
96  return mStdOffsetMinutes == kInvalidMinutes;
97  }
98 
99  uint8_t type() const { return mType; }
100 
103  return TimeOffset::forMinutes(mStdOffsetMinutes);
104  }
105 
108  return TimeOffset::forMinutes(mDstOffsetMinutes);
109  }
110 
118  return TimeOffset::forMinutes(mStdOffsetMinutes + mDstOffsetMinutes);
119  }
120 
126  return TimeOffset::forMinutes(mReqStdOffsetMinutes);
127  }
128 
134  return TimeOffset::forMinutes(mReqDstOffsetMinutes);
135  }
136 
146  return TimeOffset::forMinutes(
147  mReqStdOffsetMinutes + mReqDstOffsetMinutes);
148  }
149 
156  const char* abbrev() const { return mAbbrev; }
157 
158  private:
159  int16_t mStdOffsetMinutes = kInvalidMinutes;
160  int16_t mDstOffsetMinutes = kInvalidMinutes;
161  int16_t mReqStdOffsetMinutes = kInvalidMinutes;
162  int16_t mReqDstOffsetMinutes = kInvalidMinutes;
163  uint8_t mType = kTypeNotFound;
164  char mAbbrev[internal::kAbbrevSize] = "";
165 };
166 
167 }
168 
169 #endif
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:31
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:83
Class that describes a time zone.
Definition: TimeZone.h:86
TimeOffset reqDstOffset() const
DST offset of the requested epochSeconds or LocalDateTime.
Definition: ZonedExtra.h:133
TimeOffset reqStdOffset() const
STD offset of the requested epochSeconds or LocalDateTime.
Definition: ZonedExtra.h:125
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:14
TimeOffset stdOffset() const
STD offset of the resulting OffsetDateTime.
Definition: ZonedExtra.h:102
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:107
ZonedExtra()
Consructor.
Definition: ZonedExtra.h:74
TimeOffset reqTimeOffset() const
The total time offset of the requested epochSeconds of LocalDateTime, (reqStdOffset + reqDstOffset).
Definition: ZonedExtra.h:145
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:156
static const uint8_t kTypeGap
The given LocalDateTime occurs in a gap and does not match any epochSeconds.
Definition: ZonedExtra.h:43
static ZonedExtra forEpochSeconds(acetime_t epochSeconds, const TimeZone &tz)
Return an instance for the given epochSeconds and TimeZone.
Definition: ZonedExtra.cpp:7
bool isError() const
Indicates that the LocalDateTime or epochSeconds was not found.
Definition: ZonedExtra.h:95
TimeOffset timeOffset() const
The total time offset (stdOffset + dstOffset).
Definition: ZonedExtra.h:117
ZonedExtra(uint8_t type, int16_t stdOffsetMinutes, int16_t dstOffsetMinutes, int16_t reqStdOffsetMinutes, int16_t reqDstOffsetMinutes, const char *abbrev)
Consructor.
Definition: ZonedExtra.h:77
Identifiers used by implementation code which need to be publically exported.
int32_t acetime_t
Type for the number of seconds from epoch.
Definition: common.h:24
Internal identifiers used by implementation code, not intended to be publically exported.