AceTime 3.0.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 "Zone.h"
18
19 namespace ace_time {
20
27 class ZoneManager {
28 public:
29
31 static const uint16_t kInvalidIndex = 0xffff;
32 };
33
38 class ManualZoneManager {
39 public:
45 TimeZone createForTimeZoneData(const TimeZoneData& d) {
46 switch (d.type) {
47 case TimeZoneData::kTypeError:
48 return TimeZone::forError();
49 case TimeZoneData::kTypeManual:
50 return TimeZone::forTimeOffset(
51 TimeOffset::forMinutes(d.stdOffsetMinutes),
52 TimeOffset::forMinutes(d.dstOffsetMinutes));
53 default:
54 return TimeZone::forError();
55 }
56 }
57
58 uint16_t zoneRegistrySize() const { return 0; }
59 };
60
80 template <typename D, typename ZP, typename Z>
81 class ZoneManagerTemplate : public ZoneManager {
82 public:
89 ZoneManagerTemplate(
90 uint16_t zoneRegistrySize,
91 const typename D::ZoneInfo* const* zoneRegistry,
92 ZoneProcessorCacheBaseTemplate<ZP>& zoneProcessorCache
93 ):
94 mZoneRegistrar(zoneRegistrySize, zoneRegistry),
95 mZoneProcessorCache(zoneProcessorCache)
96 {}
97
101 TimeZone createForZoneName(const char* name) {
102 const typename D::ZoneInfo* zoneInfo =
103 mZoneRegistrar.getZoneInfoForName(name);
104 return createForZoneInfo(zoneInfo);
105 }
106
108 TimeZone createForZoneId(uint32_t id) {
109 const typename D::ZoneInfo* zoneInfo =
110 mZoneRegistrar.getZoneInfoForId(id);
111 return createForZoneInfo(zoneInfo);
112 }
113
118 TimeZone createForZoneIndex(uint16_t index) {
119 const typename D::ZoneInfo* zoneInfo =
120 mZoneRegistrar.getZoneInfoForIndex(index);
121 return createForZoneInfo(zoneInfo);
122 }
123
128 TimeZone createForTimeZoneData(const TimeZoneData& d) {
129 switch (d.type) {
130 case TimeZoneData::kTypeError:
131 return TimeZone::forError();
132 case TimeZoneData::kTypeManual:
133 return TimeZone::forTimeOffset(
134 TimeOffset::forMinutes(d.stdOffsetMinutes),
135 TimeOffset::forMinutes(d.dstOffsetMinutes));
136 case TimeZoneData::kTypeZoneId:
137 return createForZoneId(d.zoneId);
138 default:
139 // Maybe this should return TimeZone::forError()?
140 return TimeZone();
141 }
142 }
143
148 uint16_t indexForZoneName(const char* name) const {
149 return mZoneRegistrar.findIndexForName(name);
150 }
151
156 uint16_t indexForZoneId(uint32_t id) const {
157 return mZoneRegistrar.findIndexForId(id);
158 }
159
164 uint16_t zoneRegistrySize() const {
165 return mZoneRegistrar.zoneRegistrySize();
166 }
167
175 TimeZone createForZoneInfo(const typename D::ZoneInfo* zoneInfo) {
176 if (! zoneInfo) return TimeZone::forError();
177 ZP* processor = mZoneProcessorCache.getZoneProcessor(
178 (uintptr_t) zoneInfo);
179 return TimeZone::forZoneInfo(zoneInfo, processor);
180 }
181
186 ZP* getZoneProcessor(const char* name) {
187 const typename D::ZoneInfo* zoneInfo =
188 this->mZoneRegistrar.getZoneInfoForName(name);
189 if (! zoneInfo) return nullptr;
190 return this->mZoneProcessorCache.getZoneProcessor((uintptr_t) zoneInfo);
191 }
192
194 Z getZoneForIndex(uint16_t index) const {
195 const typename D::ZoneInfo* zoneInfo =
196 this->mZoneRegistrar.getZoneInfoForIndex(index);
197 return Z(zoneInfo);
198 }
199
200 private:
201 // disable copy constructor and assignment operator
202 ZoneManagerTemplate(const ZoneManagerTemplate&) = delete;
203 ZoneManagerTemplate& operator=(const ZoneManagerTemplate&) = delete;
204
205 private:
206 const ZoneRegistrarTemplate<D> mZoneRegistrar;
207 ZoneProcessorCacheBaseTemplate<ZP>& mZoneProcessorCache;
208 };
209
214 using BasicZoneManager = ZoneManagerTemplate<
215 basic::Info, BasicZoneProcessor, BasicZone>;
216
221 using ExtendedZoneManager = ZoneManagerTemplate<
222 extended::Info, ExtendedZoneProcessor, ExtendedZone>;
223
228 using CompleteZoneManager = ZoneManagerTemplate<
229 complete::Info, CompleteZoneProcessor, CompleteZone>;
230
231 }
232
233 #endif
ace_time::ManualZoneManager
A simple version of ZoneManager that converts a manual TimeZoneData with fixed STD and DST offsets in...
Definition: ZoneManager.h:38
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:45
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::forZoneInfo
static TimeZone forZoneInfo(const basic::Info::ZoneInfo *zoneInfo, BasicZoneProcessor *zoneProcessor)
Convenience factory method to create from a zoneInfo and an associated BasicZoneProcessor.
Definition: TimeZone.h:174
ace_time::TimeZone::forError
static TimeZone forError()
Return a TimeZone representing an error condition.
Definition: TimeZone.h:241
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:81
ace_time::ZoneManagerTemplate::indexForZoneName
uint16_t indexForZoneName(const char *name) const
Find the registry index for the given time zone name.
Definition: ZoneManager.h:148
ace_time::ZoneManagerTemplate::ZoneManagerTemplate
ZoneManagerTemplate(uint16_t zoneRegistrySize, const typename D::ZoneInfo *const *zoneRegistry, ZoneProcessorCacheBaseTemplate< ZP > &zoneProcessorCache)
Constructor.
Definition: ZoneManager.h:89
ace_time::ZoneManagerTemplate::createForTimeZoneData
TimeZone createForTimeZoneData(const TimeZoneData &d)
Create a TimeZone from the TimeZoneData created by TimeZone::toTimeZoneData().
Definition: ZoneManager.h:128
ace_time::ZoneManagerTemplate::getZoneProcessor
ZP * getZoneProcessor(const char *name)
Return the ZoneProcessor for given zone name.
Definition: ZoneManager.h:186
ace_time::ZoneManagerTemplate::indexForZoneId
uint16_t indexForZoneId(uint32_t id) const
Find the registry index for the given time zone id.
Definition: ZoneManager.h:156
ace_time::ZoneManagerTemplate::getZoneForIndex
Z getZoneForIndex(uint16_t index) const
Return the Zone wrapper object for the given index.
Definition: ZoneManager.h:194
ace_time::ZoneManagerTemplate::createForZoneId
TimeZone createForZoneId(uint32_t id)
Create a TimeZone for the given 32-bit zoneId.
Definition: ZoneManager.h:108
ace_time::ZoneManagerTemplate::createForZoneInfo
TimeZone createForZoneInfo(const typename D::ZoneInfo *zoneInfo)
Create a TimeZone from an explicit ZoneInfo reference.
Definition: ZoneManager.h:175
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:118
ace_time::ZoneManagerTemplate::createForZoneName
TimeZone createForZoneName(const char *name)
Create a TimeZone for the given zone name (e.g.
Definition: ZoneManager.h:101
ace_time::ZoneManagerTemplate::zoneRegistrySize
uint16_t zoneRegistrySize() const
Return the number of elements in the Zone and Fat Link registry.
Definition: ZoneManager.h:164
ace_time::ZoneManager
Base class for ManualZoneManager, BasicZoneManager, and ExtendedZoneManager to keep ZoneManager::kInv...
Definition: ZoneManager.h:27
ace_time::ZoneManager::kInvalidIndex
static const uint16_t kInvalidIndex
Registry index which is not valid.
Definition: ZoneManager.h:31
ace_time::ZoneProcessorCacheBaseTemplate
The template class of BasicZoneProcessorCacheBase or ExtendedZoneProcessorCacheBase.
Definition: ZoneProcessorCache.h:26
ace_time::ZoneRegistrarTemplate
Class that allows looking up the ZoneInfo from its TZDB identifier (e.g.
Definition: ZoneRegistrar.h:46
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