AceTime  2.2.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/ZoneInfo.h"
12 #include "../zoneinfo/ZoneRegistrar.h"
13 #include "ZoneProcessorCache.h"
14 #include "TimeZoneData.h"
15 #include "TimeZone.h"
16 #include "BasicZone.h"
17 #include "ExtendedZone.h"
18 
19 namespace ace_time {
20 
27 class ZoneManager {
28  public:
29 
31  static const uint16_t kInvalidIndex = 0xffff;
32 };
33 
39  public:
46  switch (d.type) {
47  case TimeZoneData::kTypeError:
48  return TimeZone::forError();
49  case TimeZoneData::kTypeManual:
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 
84 template <
85  typename ZI, typename ZRR,
86  typename ZP, typename Z
87 >
89  public:
93  TimeZone createForZoneName(const char* name) {
94  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForName(name);
95  return createForZoneInfo(zoneInfo);
96  }
97 
99  TimeZone createForZoneId(uint32_t id) {
100  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForId(id);
101  return createForZoneInfo(zoneInfo);
102  }
103 
108  TimeZone createForZoneIndex(uint16_t index) {
109  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForIndex(index);
110  return createForZoneInfo(zoneInfo);
111  }
112 
118  switch (d.type) {
119  case TimeZoneData::kTypeError:
120  return TimeZone::forError();
121  case TimeZoneData::kTypeManual:
123  TimeOffset::forMinutes(d.stdOffsetMinutes),
124  TimeOffset::forMinutes(d.dstOffsetMinutes));
125  case TimeZoneData::kTypeZoneId:
126  return createForZoneId(d.zoneId);
127  default:
128  // Maybe this should return TimeZone::forError()?
129  return TimeZone();
130  }
131  }
132 
137  uint16_t indexForZoneName(const char* name) const {
138  return mZoneRegistrar.findIndexForName(name);
139  }
140 
145  uint16_t indexForZoneId(uint32_t id) const {
146  return mZoneRegistrar.findIndexForId(id);
147  }
148 
153  uint16_t zoneRegistrySize() const {
154  return mZoneRegistrar.zoneRegistrySize();
155  }
156 
164  TimeZone createForZoneInfo(const ZI* zoneInfo) {
165  if (! zoneInfo) return TimeZone::forError();
166  ZP* processor = mZoneProcessorCache.getZoneProcessor(
167  (uintptr_t) zoneInfo);
168  return TimeZone::forZoneInfo(zoneInfo, processor);
169  }
170 
175  ZP* getZoneProcessor(const char* name) {
176  const ZI* zoneInfo = this->mZoneRegistrar.getZoneInfoForName(name);
177  if (! zoneInfo) return nullptr;
178  return this->mZoneProcessorCache.getZoneProcessor((uintptr_t) zoneInfo);
179  }
180 
182  Z getZoneForIndex(uint16_t index) const {
183  const ZI* zoneInfo = this->mZoneRegistrar.getZoneInfoForIndex(index);
184  return Z(zoneInfo);
185  }
186 
192  mZoneProcessorCache.resetZoneProcessors();
193  }
194 
195  protected:
203  uint16_t zoneRegistrySize,
204  const ZI* const* zoneRegistry,
205  ZoneProcessorCacheBaseTemplate<ZP>& zoneProcessorCache
206  ):
207  mZoneRegistrar(zoneRegistrySize, zoneRegistry),
208  mZoneProcessorCache(zoneProcessorCache)
209  {}
210 
211  // disable copy constructor and assignment operator
212  ZoneManagerTemplate(const ZoneManagerTemplate&) = delete;
213  ZoneManagerTemplate& operator=(const ZoneManagerTemplate&) = delete;
214 
215  protected:
216  const ZRR mZoneRegistrar;
217  ZoneProcessorCacheBaseTemplate<ZP>& mZoneProcessorCache;
218 };
219 
220 #if 1
226  basic::ZoneInfo,
227  basic::ZoneRegistrar,
228  BasicZoneProcessor,
229  BasicZone
230 > {
231 
232  public:
234  uint16_t zoneRegistrySize,
235  const basic::ZoneInfo* const* zoneRegistry,
236  BasicZoneProcessorCacheBase& zoneProcessorCache
237  ):
239  basic::ZoneInfo,
242  BasicZone
243  >(
245  zoneRegistry,
246  zoneProcessorCache
247  )
248  {}
249 };
250 
256  extended::ZoneInfo,
257  extended::ZoneRegistrar,
258  ExtendedZoneProcessor,
259  ExtendedZone
260 > {
261 
262  public:
264  uint16_t zoneRegistrySize,
265  const extended::ZoneInfo* const* zoneRegistry,
266  ExtendedZoneProcessorCacheBase& zoneProcessorCache
267  ):
269  extended::ZoneInfo,
273  >(
275  zoneRegistry,
276  zoneProcessorCache
277  )
278  {}
279 };
280 
281 #else
282 
283 // NOTE: The following typedefs seem shorter and easier to maintain. The
284 // problem is that they make error messages basically impossible to decipher
285 // because the template class names are far too long for human comprehension.
286 // Fortunately, there seems to be no difference in code size between the above
287 // solution using subclasses and this solution using typedefs. The compiler
288 // seems to optimize away the vtables of the parent and child classes. So we'll
289 // use the above subclassing solution to get better error messages.
290 
292  basic::ZoneInfo,
295  BasicZone
296 >;
297 
299  extended::ZoneInfo,
303 >;
304 
305 #endif
306 
307 }
308 
309 #endif
An implementation of the ZoneManager which uses a registry of basic::ZoneInfo records.
Definition: ZoneManager.h:230
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:260
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:38
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
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:85
static TimeZone forError()
Return a TimeZone representing an error condition.
Definition: TimeZone.h:221
static TimeZone forZoneInfo(const basic::ZoneInfo *zoneInfo, BasicZoneProcessor *zoneProcessor)
Convenience factory method to create from a zoneInfo and an associated BasicZoneProcessor.
Definition: TimeZone.h:173
static TimeZone forTimeOffset(TimeOffset stdOffset, TimeOffset dstOffset=TimeOffset())
Factory method to create from a UTC offset and an optional DST offset.
Definition: TimeZone.h:114
A templatized implementation of ZoneManager that binds the ZoneRegistrar with the corresponding (Basi...
Definition: ZoneManager.h:88
TimeZone createForTimeZoneData(const TimeZoneData &d)
Create a TimeZone from the TimeZoneData created by TimeZone::toTimeZoneData().
Definition: ZoneManager.h:117
TimeZone createForZoneId(uint32_t id)
Create a TimeZone for the given 32-bit zoneId.
Definition: ZoneManager.h:99
void resetZoneProcessors()
Reset the transition cache of the zone processors in the cache.
Definition: ZoneManager.h:191
Z getZoneForIndex(uint16_t index) const
Return the Zone wrapper object for the given index.
Definition: ZoneManager.h:182
TimeZone createForZoneName(const char *name)
Create a TimeZone for the given zone name (e.g.
Definition: ZoneManager.h:93
uint16_t indexForZoneName(const char *name) const
Find the registry index for the given time zone name.
Definition: ZoneManager.h:137
TimeZone createForZoneInfo(const ZI *zoneInfo)
Create a TimeZone from an explicit ZoneInfo reference.
Definition: ZoneManager.h:164
uint16_t indexForZoneId(uint32_t id) const
Find the registry index for the given time zone id.
Definition: ZoneManager.h:145
ZP * getZoneProcessor(const char *name)
Return the ZoneProcessor for given zone name.
Definition: ZoneManager.h:175
uint16_t zoneRegistrySize() const
Return the number of elements in the Zone and Fat Link registry.
Definition: ZoneManager.h:153
ZoneManagerTemplate(uint16_t zoneRegistrySize, const ZI *const *zoneRegistry, ZoneProcessorCacheBaseTemplate< ZP > &zoneProcessorCache)
Constructor.
Definition: ZoneManager.h:202
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:108
Base class for ManualZoneManager, BasicZoneManager, and ExtendedZoneManager to keep ZoneManager::kInv...
Definition: ZoneManager.h:27
static const uint16_t kInvalidIndex
Registry index which is not valid.
Definition: ZoneManager.h:31
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