AceTime  1.11.6
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 
186  protected:
194  uint16_t zoneRegistrySize,
195  const ZI* const* zoneRegistry,
196  ZoneProcessorCacheBaseTemplate<ZP>& zoneProcessorCache
197  ):
198  mZoneRegistrar(zoneRegistrySize, zoneRegistry),
199  mZoneProcessorCache(zoneProcessorCache)
200  {}
201 
202  // disable copy constructor and assignment operator
203  ZoneManagerTemplate(const ZoneManagerTemplate&) = delete;
204  ZoneManagerTemplate& operator=(const ZoneManagerTemplate&) = delete;
205 
206  protected:
207  const ZRR mZoneRegistrar;
208  ZoneProcessorCacheBaseTemplate<ZP>& mZoneProcessorCache;
209 };
210 
211 #if 1
217  basic::ZoneInfo,
218  basic::ZoneRegistrar,
219  BasicZoneProcessor,
220  BasicZone
221 > {
222 
223  public:
225  uint16_t zoneRegistrySize,
226  const basic::ZoneInfo* const* zoneRegistry,
227  BasicZoneProcessorCacheBase& zoneProcessorCache
228  ):
230  basic::ZoneInfo,
233  BasicZone
234  >(
236  zoneRegistry,
237  zoneProcessorCache
238  )
239  {}
240 };
241 
247  extended::ZoneInfo,
248  extended::ZoneRegistrar,
249  ExtendedZoneProcessor,
250  ExtendedZone
251 > {
252 
253  public:
255  uint16_t zoneRegistrySize,
256  const extended::ZoneInfo* const* zoneRegistry,
257  ExtendedZoneProcessorCacheBase& zoneProcessorCache
258  ):
260  extended::ZoneInfo,
264  >(
266  zoneRegistry,
267  zoneProcessorCache
268  )
269  {}
270 };
271 
272 #else
273 
274 // NOTE: The following typedefs seem shorter and easier to maintain. The
275 // problem is that they make error messages basically impossible to decipher
276 // because the template class names are far too long for human comprehension.
277 // Fortunately, there seems to be no difference in code size between the above
278 // solution using subclasses and this solution using typedefs. The compiler
279 // seems to optimize away the vtables of the parent and child classes. So we'll
280 // use the above subclassing solution to get better error messages.
281 
283  basic::ZoneInfo,
286  BasicZone
287 >;
288 
290  extended::ZoneInfo,
294 >;
295 
296 #endif
297 
298 }
299 
300 #endif
An implementation of the ZoneManager which uses a registry of basic::ZoneInfo records.
Definition: ZoneManager.h:221
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:251
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:85
static TimeZone forError()
Return a TimeZone representing an error condition.
Definition: TimeZone.h:216
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: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
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:193
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