AceTime 2.4.0 Date and time classes for Arduino that support timezones from the TZ Database.

ZoneManager.h
1 /*
2 * MIT License
3 * Copyright (c) 2019 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_ZONE_MANAGER_H
7 #define ACE_TIME_ZONE_MANAGER_H
8
9 #include <AceCommon.h>
10 #include <AceSorting.h>
11 #include "../zoneinfo/infos.h"
12 #include "ZoneRegistrar.h"
13 #include "TimeOffset.h"
14 #include "ZoneProcessorCache.h"
15 #include "TimeZoneData.h"
16 #include "TimeZone.h"
17 #include "BasicZone.h"
18 #include "ExtendedZone.h"
19 #include "CompleteZone.h"
20
21 namespace ace_time {
22
29 class ZoneManager {
30 public:
31
33 static const uint16_t kInvalidIndex = 0xffff;
34 };
35
40 class ManualZoneManager {
41 public:
47 TimeZone createForTimeZoneData(const TimeZoneData& d) {
48 switch (d.type) {
49 case TimeZoneData::kTypeError:
50 return TimeZone::forError();
51 case TimeZoneData::kTypeManual:
52 return TimeZone::forTimeOffset(
53 TimeOffset::forMinutes(d.stdOffsetMinutes),
54 TimeOffset::forMinutes(d.dstOffsetMinutes));
55 default:
56 return TimeZone::forError();
57 }
58 }
59
60 uint16_t zoneRegistrySize() const { return 0; }
61 };
62
86 template <typename ZI, typename ZRR, typename ZP, typename Z>
87 class ZoneManagerTemplate : public ZoneManager {
88 public:
95 ZoneManagerTemplate(
96 uint16_t zoneRegistrySize,
97 const ZI* const* zoneRegistry,
98 ZoneProcessorCacheBaseTemplate<ZP>& zoneProcessorCache
99 ):
100 mZoneRegistrar(zoneRegistrySize, zoneRegistry),
101 mZoneProcessorCache(zoneProcessorCache)
102 {}
103
107 TimeZone createForZoneName(const char* name) {
108 const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForName(name);
109 return createForZoneInfo(zoneInfo);
110 }
111
113 TimeZone createForZoneId(uint32_t id) {
114 const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForId(id);
115 return createForZoneInfo(zoneInfo);
116 }
117
122 TimeZone createForZoneIndex(uint16_t index) {
123 const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForIndex(index);
124 return createForZoneInfo(zoneInfo);
125 }
126
131 TimeZone createForTimeZoneData(const TimeZoneData& d) {
132 switch (d.type) {
133 case TimeZoneData::kTypeError:
134 return TimeZone::forError();
135 case TimeZoneData::kTypeManual:
136 return TimeZone::forTimeOffset(
137 TimeOffset::forMinutes(d.stdOffsetMinutes),
138 TimeOffset::forMinutes(d.dstOffsetMinutes));
139 case TimeZoneData::kTypeZoneId:
140 return createForZoneId(d.zoneId);
141 default:
142 // Maybe this should return TimeZone::forError()?
143 return TimeZone();
144 }
145 }
146
151 uint16_t indexForZoneName(const char* name) const {
152 return mZoneRegistrar.findIndexForName(name);
153 }
154
159 uint16_t indexForZoneId(uint32_t id) const {
160 return mZoneRegistrar.findIndexForId(id);
161 }
162
167 uint16_t zoneRegistrySize() const {
168 return mZoneRegistrar.zoneRegistrySize();
169 }
170
178 TimeZone createForZoneInfo(const ZI* zoneInfo) {
179 if (! zoneInfo) return TimeZone::forError();
180 ZP* processor = mZoneProcessorCache.getZoneProcessor(
181 (uintptr_t) zoneInfo);
182 return TimeZone::forZoneInfo(zoneInfo, processor);
183 }
184
189 ZP* getZoneProcessor(const char* name) {
190 const ZI* zoneInfo = this->mZoneRegistrar.getZoneInfoForName(name);
191 if (! zoneInfo) return nullptr;
192 return this->mZoneProcessorCache.getZoneProcessor((uintptr_t) zoneInfo);
193 }
194
196 Z getZoneForIndex(uint16_t index) const {
197 const ZI* zoneInfo = this->mZoneRegistrar.getZoneInfoForIndex(index);
198 return Z(zoneInfo);
199 }
200
201 private:
202 // disable copy constructor and assignment operator
203 ZoneManagerTemplate(const ZoneManagerTemplate&) = delete;
204 ZoneManagerTemplate& operator=(const ZoneManagerTemplate&) = delete;
205
206 private:
207 const ZRR mZoneRegistrar;
208 ZoneProcessorCacheBaseTemplate<ZP>& mZoneProcessorCache;
209 };
210
215 using BasicZoneManager = ZoneManagerTemplate<
216 basic::ZoneInfo,
217 basic::ZoneRegistrar,
218 BasicZoneProcessor,
219 BasicZone
220 >;
221
226 using ExtendedZoneManager = ZoneManagerTemplate<
227 extended::ZoneInfo,
228 extended::ZoneRegistrar,
229 ExtendedZoneProcessor,
230 ExtendedZone
231 >;
232
237 using CompleteZoneManager = ZoneManagerTemplate<
238 complete::ZoneInfo,
239 complete::ZoneRegistrar,
240 CompleteZoneProcessor,
241 CompleteZone
242 >;
243
244 }
245
246 #endif
ace_time::ManualZoneManager
A simple version of ZoneManager that converts a manual TimeZoneData with fixed STD and DST offsets in...
Definition: ZoneManager.h:40
ace_time::ManualZoneManager::createForTimeZoneData
TimeZone createForTimeZoneData(const TimeZoneData &d)
Create a TimeZone with fixed STD and DST offsets stored in the TimeZoneData which was created by Time...
Definition: ZoneManager.h:47
ace_time::TimeOffset::forMinutes
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:91
ace_time::TimeZone
Class that describes a time zone.
Definition: TimeZone.h:86
ace_time::TimeZone::forError
static TimeZone forError()
Return a TimeZone representing an error condition.
Definition: TimeZone.h:241
ace_time::TimeZone::forZoneInfo
static TimeZone forZoneInfo(const basic::ZoneInfo *zoneInfo, BasicZoneProcessor *zoneProcessor)
Convenience factory method to create from a zoneInfo and an associated BasicZoneProcessor.
Definition: TimeZone.h:174
ace_time::TimeZone::forTimeOffset
static TimeZone forTimeOffset(TimeOffset stdOffset, TimeOffset dstOffset=TimeOffset())
Factory method to create from a UTC offset and an optional DST offset.
Definition: TimeZone.h:115
ace_time::ZoneManagerTemplate
A templatized implementation of ZoneManager that binds the ZoneRegistrar with the corresponding (Basi...
Definition: ZoneManager.h:87
ace_time::ZoneManagerTemplate::createForTimeZoneData
TimeZone createForTimeZoneData(const TimeZoneData &d)
Create a TimeZone from the TimeZoneData created by TimeZone::toTimeZoneData().
Definition: ZoneManager.h:131
ace_time::ZoneManagerTemplate::createForZoneId
TimeZone createForZoneId(uint32_t id)
Create a TimeZone for the given 32-bit zoneId.
Definition: ZoneManager.h:113
ace_time::ZoneManagerTemplate::getZoneForIndex
Z getZoneForIndex(uint16_t index) const
Return the Zone wrapper object for the given index.
Definition: ZoneManager.h:196
ace_time::ZoneManagerTemplate::createForZoneName
TimeZone createForZoneName(const char *name)
Create a TimeZone for the given zone name (e.g.
Definition: ZoneManager.h:107
ace_time::ZoneManagerTemplate::indexForZoneName
uint16_t indexForZoneName(const char *name) const
Find the registry index for the given time zone name.
Definition: ZoneManager.h:151
ace_time::ZoneManagerTemplate::createForZoneInfo
TimeZone createForZoneInfo(const ZI *zoneInfo)
Create a TimeZone from an explicit ZoneInfo reference.
Definition: ZoneManager.h:178
ace_time::ZoneManagerTemplate::indexForZoneId
uint16_t indexForZoneId(uint32_t id) const
Find the registry index for the given time zone id.
Definition: ZoneManager.h:159
ace_time::ZoneManagerTemplate::getZoneProcessor
ZP * getZoneProcessor(const char *name)
Return the ZoneProcessor for given zone name.
Definition: ZoneManager.h:189
ace_time::ZoneManagerTemplate::zoneRegistrySize
uint16_t zoneRegistrySize() const
Return the number of elements in the Zone and Fat Link registry.
Definition: ZoneManager.h:167
ace_time::ZoneManagerTemplate::ZoneManagerTemplate
ZoneManagerTemplate(uint16_t zoneRegistrySize, const ZI *const *zoneRegistry, ZoneProcessorCacheBaseTemplate< ZP > &zoneProcessorCache)
Constructor.
Definition: ZoneManager.h:95
ace_time::ZoneManagerTemplate::createForZoneIndex
TimeZone createForZoneIndex(uint16_t index)
Create a TimeZone for the given index in the ZoneInfo registry that was used to create this ZoneManag...
Definition: ZoneManager.h:122
ace_time::ZoneManager
Base class for ManualZoneManager, BasicZoneManager, and ExtendedZoneManager to keep ZoneManager::kInv...
Definition: ZoneManager.h:29
ace_time::ZoneManager::kInvalidIndex
static const uint16_t kInvalidIndex
Registry index which is not valid.
Definition: ZoneManager.h:33
ace_time::ZoneProcessorCacheBaseTemplate
The template class of BasicZoneProcessorCacheBase or ExtendedZoneProcessorCacheBase.
Definition: ZoneProcessorCache.h:26
ace_time::TimeZoneData
Data structure that captures the internal state of a TimeZone object with enough information so that ...
Definition: TimeZoneData.h:38
ace_time::TimeZoneData::zoneId
uint32_t zoneId
Both TimeZone::kTypeBasic and TimeZone::kTypeExtended are mapped to a TimeZoneData::kTypeZoneId.
Definition: TimeZoneData.h:85