AceTime  2.1.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 "internal/ZoneRegistrar.h"
12 #include "ZoneProcessorCache.h"
13 #include "TimeZoneData.h"
14 #include "TimeZone.h"
15 #include "BasicZone.h"
16 #include "ExtendedZone.h"
17 
18 namespace ace_time {
19 
26 class ZoneManager {
27  public:
28 
30  static const uint16_t kInvalidIndex = 0xffff;
31 };
32 
38  public:
45  switch (d.type) {
46  case TimeZoneData::kTypeError:
47  return TimeZone::forError();
48  case TimeZoneData::kTypeManual:
50  TimeOffset::forMinutes(d.stdOffsetMinutes),
51  TimeOffset::forMinutes(d.dstOffsetMinutes));
52  default:
53  return TimeZone::forError();
54  }
55  }
56 
57  uint16_t zoneRegistrySize() const { return 0; }
58 };
59 
83 template <
84  typename ZI, typename ZRR,
85  typename ZP, typename Z
86 >
88  public:
92  TimeZone createForZoneName(const char* name) {
93  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForName(name);
94  return createForZoneInfo(zoneInfo);
95  }
96 
98  TimeZone createForZoneId(uint32_t id) {
99  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForId(id);
100  return createForZoneInfo(zoneInfo);
101  }
102 
107  TimeZone createForZoneIndex(uint16_t index) {
108  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForIndex(index);
109  return createForZoneInfo(zoneInfo);
110  }
111 
117  switch (d.type) {
118  case TimeZoneData::kTypeError:
119  return TimeZone::forError();
120  case TimeZoneData::kTypeManual:
122  TimeOffset::forMinutes(d.stdOffsetMinutes),
123  TimeOffset::forMinutes(d.dstOffsetMinutes));
124  case TimeZoneData::kTypeZoneId:
125  return createForZoneId(d.zoneId);
126  default:
127  // Maybe this should return TimeZone::forError()?
128  return TimeZone();
129  }
130  }
131 
136  uint16_t indexForZoneName(const char* name) const {
137  return mZoneRegistrar.findIndexForName(name);
138  }
139 
144  uint16_t indexForZoneId(uint32_t id) const {
145  return mZoneRegistrar.findIndexForId(id);
146  }
147 
152  uint16_t zoneRegistrySize() const {
153  return mZoneRegistrar.zoneRegistrySize();
154  }
155 
163  TimeZone createForZoneInfo(const ZI* zoneInfo) {
164  if (! zoneInfo) return TimeZone::forError();
165  ZP* processor = mZoneProcessorCache.getZoneProcessor(
166  (uintptr_t) zoneInfo);
167  return TimeZone::forZoneInfo(zoneInfo, processor);
168  }
169 
174  ZP* getZoneProcessor(const char* name) {
175  const ZI* zoneInfo = this->mZoneRegistrar.getZoneInfoForName(name);
176  if (! zoneInfo) return nullptr;
177  return this->mZoneProcessorCache.getZoneProcessor((uintptr_t) zoneInfo);
178  }
179 
181  Z getZoneForIndex(uint16_t index) const {
182  const ZI* zoneInfo = this->mZoneRegistrar.getZoneInfoForIndex(index);
183  return Z(zoneInfo);
184  }
185 
191  mZoneProcessorCache.resetZoneProcessors();
192  }
193 
194  protected:
202  uint16_t zoneRegistrySize,
203  const ZI* const* zoneRegistry,
204  ZoneProcessorCacheBaseTemplate<ZP>& zoneProcessorCache
205  ):
206  mZoneRegistrar(zoneRegistrySize, zoneRegistry),
207  mZoneProcessorCache(zoneProcessorCache)
208  {}
209 
210  // disable copy constructor and assignment operator
211  ZoneManagerTemplate(const ZoneManagerTemplate&) = delete;
212  ZoneManagerTemplate& operator=(const ZoneManagerTemplate&) = delete;
213 
214  protected:
215  const ZRR mZoneRegistrar;
216  ZoneProcessorCacheBaseTemplate<ZP>& mZoneProcessorCache;
217 };
218 
219 #if 1
225  basic::ZoneInfo,
226  basic::ZoneRegistrar,
227  BasicZoneProcessor,
228  BasicZone
229 > {
230 
231  public:
233  uint16_t zoneRegistrySize,
234  const basic::ZoneInfo* const* zoneRegistry,
235  BasicZoneProcessorCacheBase& zoneProcessorCache
236  ):
238  basic::ZoneInfo,
241  BasicZone
242  >(
244  zoneRegistry,
245  zoneProcessorCache
246  )
247  {}
248 };
249 
255  extended::ZoneInfo,
256  extended::ZoneRegistrar,
257  ExtendedZoneProcessor,
258  ExtendedZone
259 > {
260 
261  public:
263  uint16_t zoneRegistrySize,
264  const extended::ZoneInfo* const* zoneRegistry,
265  ExtendedZoneProcessorCacheBase& zoneProcessorCache
266  ):
268  extended::ZoneInfo,
272  >(
274  zoneRegistry,
275  zoneProcessorCache
276  )
277  {}
278 };
279 
280 #else
281 
282 // NOTE: The following typedefs seem shorter and easier to maintain. The
283 // problem is that they make error messages basically impossible to decipher
284 // because the template class names are far too long for human comprehension.
285 // Fortunately, there seems to be no difference in code size between the above
286 // solution using subclasses and this solution using typedefs. The compiler
287 // seems to optimize away the vtables of the parent and child classes. So we'll
288 // use the above subclassing solution to get better error messages.
289 
291  basic::ZoneInfo,
294  BasicZone
295 >;
296 
298  extended::ZoneInfo,
302 >;
303 
304 #endif
305 
306 }
307 
308 #endif
An implementation of the ZoneManager which uses a registry of basic::ZoneInfo records.
Definition: ZoneManager.h:229
A specific implementation of BasicZoneProcessorTemplate that uses ZoneXxxBrokers which read from zone...
A thin wrapper around a basic::ZoneInfo data structure to provide a stable API access to some useful ...
Definition: BasicZone.h:22
An implementation of the ZoneManager which uses a registry of extended::ZoneInfo records.
Definition: ZoneManager.h:259
A specific implementation of ExtendedZoneProcessorTemplate that uses ZoneXxxBrokers which read from z...
A thin wrapper around an extended::ZoneInfo data structure to provide a stable API access to some use...
Definition: ExtendedZone.h:23
A simple version of ZoneManager that converts a manual TimeZoneData with fixed STD and DST offsets in...
Definition: ZoneManager.h:37
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:44
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
static TimeZone forError()
Return a TimeZone representing an error condition.
Definition: TimeZone.h:224
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
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
A templatized implementation of ZoneManager that binds the ZoneRegistrar with the corresponding (Basi...
Definition: ZoneManager.h:87
TimeZone createForTimeZoneData(const TimeZoneData &d)
Create a TimeZone from the TimeZoneData created by TimeZone::toTimeZoneData().
Definition: ZoneManager.h:116
TimeZone createForZoneId(uint32_t id)
Create a TimeZone for the given 32-bit zoneId.
Definition: ZoneManager.h:98
void resetZoneProcessors()
Reset the transition cache of the zone processors in the cache.
Definition: ZoneManager.h:190
Z getZoneForIndex(uint16_t index) const
Return the Zone wrapper object for the given index.
Definition: ZoneManager.h:181
TimeZone createForZoneName(const char *name)
Create a TimeZone for the given zone name (e.g.
Definition: ZoneManager.h:92
uint16_t indexForZoneName(const char *name) const
Find the registry index for the given time zone name.
Definition: ZoneManager.h:136
TimeZone createForZoneInfo(const ZI *zoneInfo)
Create a TimeZone from an explicit ZoneInfo reference.
Definition: ZoneManager.h:163
uint16_t indexForZoneId(uint32_t id) const
Find the registry index for the given time zone id.
Definition: ZoneManager.h:144
ZP * getZoneProcessor(const char *name)
Return the ZoneProcessor for given zone name.
Definition: ZoneManager.h:174
uint16_t zoneRegistrySize() const
Return the number of elements in the Zone and Fat Link registry.
Definition: ZoneManager.h:152
ZoneManagerTemplate(uint16_t zoneRegistrySize, const ZI *const *zoneRegistry, ZoneProcessorCacheBaseTemplate< ZP > &zoneProcessorCache)
Constructor.
Definition: ZoneManager.h:201
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:107
Base class for ManualZoneManager, BasicZoneManager, and ExtendedZoneManager to keep ZoneManager::kInv...
Definition: ZoneManager.h:26
static const uint16_t kInvalidIndex
Registry index which is not valid.
Definition: ZoneManager.h:30
The template class of BasicZoneProcessorCacheBase or ExtendedZoneProcessorCacheBase.
Concrete template instantiation of ZoneRegistrarTemplate for basic::ZoneInfo, which can be used with ...
Concrete template instantiation of ZoneRegistrarTemplate for extended::ZoneInfo, which can be used wi...
Data structure that captures the internal state of a TimeZone object with enough information so that ...
Definition: TimeZoneData.h:38
uint32_t zoneId
Both TimeZone::kTypeBasic and TimeZone::kTypeExtended are mapped to a TimeZoneData::kTypeZoneId.
Definition: TimeZoneData.h:85