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

Zone.h
1 /*
2 * MIT License
3 * Copyright (c) 2019 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_ZONE_DATA_H
7 #define ACE_TIME_ZONE_DATA_H
8
9 #include <AceCommon.h> // KString
10 #include "../zoneinfo/infos.h"
11 #include "TimeOffset.h"
12
13 class Print;
14
15 namespace ace_time {
16
24 template <typename D>
25 class ZoneTemplate {
26 public:
31 ZoneTemplate(const typename D::ZoneInfo* zoneInfo):
32 mZoneInfoBroker(zoneInfo) {}
33
39 ZoneTemplate(const typename D::ZoneInfoBroker& zoneInfo):
40 mZoneInfoBroker(zoneInfo) {}
41
42 // Use default copy constructor and assignment operator
43 ZoneTemplate(const ZoneTemplate&) = default;
44 ZoneTemplate& operator=(const ZoneTemplate&) = default;
45
47 bool isNull() const { return mZoneInfoBroker.isNull(); }
48
50 void printNameTo(Print& printer) const {
51 const __FlashStringHelper* name = mZoneInfoBroker.name();
52 typename D::ZoneContextBroker zoneContext =
53 mZoneInfoBroker.zoneContext();
54 ace_common::KString kname(
55 name, zoneContext.fragments(), zoneContext.numFragments());
56 kname.printTo(printer);
57 }
58
64 void printShortNameTo(Print& printer) const {
65 const __FlashStringHelper* name = mZoneInfoBroker.name();
66 const __FlashStringHelper* shortName = zoneinfo::findShortName(name);
67 ace_common::printReplaceCharTo(printer, shortName, '_', ' ');
68 }
69
71 uint32_t zoneId() const {
72 return mZoneInfoBroker.zoneId();
73 }
74
76 TimeOffset stdOffset() const {
77 uint8_t numEras = mZoneInfoBroker.numEras();
78 typename D::ZoneEraBroker zeb = mZoneInfoBroker.era(numEras - 1);
79 return TimeOffset::forSeconds(zeb.offsetSeconds());
80 }
81
83 ace_common::KString kname() const {
84 const auto* name = isNull() ? nullptr : mZoneInfoBroker.name();
85 typename D::ZoneContextBroker zoneContext =
86 mZoneInfoBroker.zoneContext();
87 return ace_common::KString(
88 name, zoneContext.fragments(), zoneContext.numFragments());
89 }
90
91 private:
92 typename D::ZoneInfoBroker mZoneInfoBroker;
93 };
94
95 using BasicZone = ZoneTemplate<basic::Info>;
96 using ExtendedZone = ZoneTemplate<extended::Info>;
97 using CompleteZone = ZoneTemplate<complete::Info>;
98
99 }
100
101 #endif
ace_time::TimeOffset
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
ace_time::TimeOffset::forSeconds
static TimeOffset forSeconds(int32_t seconds)
Create TimeOffset from seconds from 00:00.
Definition: TimeOffset.h:96
ace_time::ZoneTemplate
A thin wrapper around a ZoneInfo data structure to provide a stable API access to some useful ZoneInf...
Definition: Zone.h:25
ace_time::ZoneTemplate::ZoneTemplate
ZoneTemplate(const typename D::ZoneInfoBroker &zoneInfo)
Constructor from a Info:ZoneInfoBroker, used by various ZoneProcessor.
Definition: Zone.h:39
ace_time::ZoneTemplate::kname
ace_common::KString kname() const
Return the name as a KString.
Definition: Zone.h:83
ace_time::ZoneTemplate::ZoneTemplate
ZoneTemplate(const typename D::ZoneInfo *zoneInfo)
Constructor from a raw Info::ZoneInfo* pointer, intended for manual inspection of a ZoneInfo record.
Definition: Zone.h:31
ace_time::ZoneTemplate::isNull
bool isNull() const
Return true if zoneInfo is null.
Definition: Zone.h:47
ace_time::ZoneTemplate::zoneId
uint32_t zoneId() const
Return the zoneId of the current zoneInfo.
Definition: Zone.h:71
ace_time::ZoneTemplate::printShortNameTo
void printShortNameTo(Print &printer) const
Print the short pretty zone name to the printer.
Definition: Zone.h:64
ace_time::ZoneTemplate::stdOffset
TimeOffset stdOffset() const
Return the STDOFF of the last ZoneEra.
Definition: Zone.h:76
ace_time::ZoneTemplate::printNameTo
void printNameTo(Print &printer) const
Print the full zone name to printer.
Definition: Zone.h:50