AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized from an NTP server or an RTC chip.
TimeZone.h
1 #ifndef ACE_TIME_TIME_ZONE_H
2 #define ACE_TIME_TIME_ZONE_H
3 
4 #include <stdint.h>
5 #include "TimeOffset.h"
6 #include "ZoneSpecifier.h"
7 #include "ManualZoneSpecifier.h"
8 
9 class Print;
10 
11 namespace ace_time {
12 
50 class TimeZone {
51  public:
52  static const uint8_t kTypeFixed = 0;
53  static const uint8_t kTypeZoneSpecifier = 1;
54 
61  return TimeZone(offset);
62  }
63 
70  static TimeZone forZoneSpecifier(const ZoneSpecifier* zoneSpecifier) {
71  return TimeZone(zoneSpecifier);
72  }
73 
76  mType(kTypeFixed),
77  mOffset() {}
78 
80  uint8_t getType() const { return mType; }
81 
83  TimeOffset getUtcOffset(acetime_t epochSeconds) const {
84  return (mType == kTypeFixed)
85  ? mOffset
86  : mZoneSpecifier->getUtcOffset(epochSeconds);
87  }
88 
94  TimeOffset getDeltaOffset(acetime_t epochSeconds) const {
95  return (mType == kTypeFixed)
96  ? TimeOffset()
97  : mZoneSpecifier->getDeltaOffset(epochSeconds);
98  }
99 
106  return (mType == kTypeFixed)
107  ? mOffset
109  }
110 
112  void printTo(Print& printer) const;
113 
115  void printAbbrevTo(Print& printer, acetime_t epochSeconds) const;
116 
125  bool isDst() const {
126  if (mType != kTypeZoneSpecifier) return false;
127  if (mZoneSpecifier->getType() != ZoneSpecifier::kTypeManual) return false;
129  }
130 
137  void isDst(bool dst) {
138  if (mType != kTypeZoneSpecifier) return;
140  ((ManualZoneSpecifier*) mZoneSpecifier)->isDst(dst);
141  }
142 
143  // Use default copy constructor and assignment operator.
144  TimeZone(const TimeZone&) = default;
145  TimeZone& operator=(const TimeZone&) = default;
146 
147  private:
148  friend bool operator==(const TimeZone& a, const TimeZone& b);
149 
154  explicit TimeZone(TimeOffset offset):
155  mType(kTypeFixed),
156  mOffset(offset) {}
157 
170  explicit TimeZone(const ZoneSpecifier* zoneSpecifier):
171  mType(kTypeZoneSpecifier),
172  mZoneSpecifier(zoneSpecifier) {}
173 
174  uint8_t mType;
175 
176  union {
179 
182  };
183 };
184 
185 inline bool operator==(const TimeZone& a, const TimeZone& b) {
186  if (a.getType() != b.getType()) return false;
187  if (a.getType() == TimeZone::kTypeFixed) {
188  return a.mOffset == b.mOffset;
189  } else {
190  if (a.mZoneSpecifier == b.mZoneSpecifier) return true;
191  return *a.mZoneSpecifier == *b.mZoneSpecifier;
192  }
193 }
194 
195 inline bool operator!=(const TimeZone& a, const TimeZone& b) {
196  return ! (a == b);
197 }
198 
199 }
200 
201 #endif
bool isDst() const
Return the isDst() value of the underlying ManualZoneSpecifier.
Definition: TimeZone.h:125
const ZoneSpecifier * mZoneSpecifier
Used if mType == mTypeZoneSpecifier.
Definition: TimeZone.h:181
TimeOffset getUtcOffset(acetime_t epochSeconds) const
Return the total UTC offset at epochSeconds, including DST offset.
Definition: TimeZone.h:83
uint8_t getType() const
Return the type of TimeZone.
Definition: TimeZone.h:80
static TimeZone forTimeOffset(TimeOffset offset=TimeOffset())
Factory method to create from a fixed UTC offset.
Definition: TimeZone.h:60
void printAbbrevTo(Print &printer, acetime_t epochSeconds) const
Print the time zone abbreviation for the given epochSeconds.
Definition: TimeZone.cpp:21
virtual TimeOffset getUtcOffsetForDateTime(const LocalDateTime &ldt) const =0
Return the UTC offset matching the given the date/time components.
TimeOffset getUtcOffsetForDateTime(const LocalDateTime &ldt) const
Return the best guess of the UTC offset at the given LocalDateTime for the current TimeZone...
Definition: TimeZone.h:105
virtual TimeOffset getDeltaOffset(acetime_t epochSeconds) const =0
Return the DST delta offset at epochSeconds.
An implementation of ZoneSpecifier which allows the user to manually adjust the UTC offset and the DS...
Base interface for ZoneSpecifier classes.
Definition: ZoneSpecifier.h:39
TimeZone()
Default constructor.
Definition: TimeZone.h:75
static TimeZone forZoneSpecifier(const ZoneSpecifier *zoneSpecifier)
Factory method to create from a ZoneSpecifier.
Definition: TimeZone.h:70
uint8_t getType() const
Return the kTypeXxx of the current instance.
Definition: ZoneSpecifier.h:51
void printTo(Print &printer) const
Print the human readable representation of the time zone.
Definition: TimeZone.cpp:7
TimeOffset getDeltaOffset(acetime_t epochSeconds) const
Return the DST offset from standard UTC offset at epochSeconds.
Definition: TimeZone.h:94
static const uint8_t kTypeManual
Indicate ManualZoneSpecifier.
Definition: ZoneSpecifier.h:42
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
TimeOffset mOffset
Used if mType == mTypeFixed.
Definition: TimeZone.h:178
Class that describes a time zone.
Definition: TimeZone.h:50
virtual TimeOffset getUtcOffset(acetime_t epochSeconds) const =0
Return the total UTC offset at epochSeconds, including DST offset.
void isDst(bool dst)
Sets the isDst() flag of the underlying ManualZoneSpecifier.
Definition: TimeZone.h:137