Timezone.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Timezone.h - Time Zone support library
8  *
9  * Original code (c) Jack Christensen Mar 2012
10  * Arduino Timezone Library Copyright (C) 2018 by Jack Christensen and
11  * licensed under GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
12  *
13  * @author mikee47 <mike@sillyhouse.net>
14  * July 2018 Ported to Sming
15  *
16  ****/
17 
18 #pragma once
19 
20 #include <DateTime.h>
21 
22 // convenient constants for TimeChangeRules
23 enum __attribute__((packed)) week_t {
29 };
30 
31 enum __attribute__((packed)) dow_t {
32  Sun = 0,
33  Mon,
34  Tue,
35  Wed,
36  Thu,
37  Fri,
38  Sat,
39 };
40 
41 enum __attribute__((packed)) month_t {
42  Jan = 1,
43  Feb,
44  Mar,
45  Apr,
46  May,
47  Jun,
48  Jul,
49  Aug,
50  Sep,
51  Oct,
52  Nov,
53  Dec,
54 };
55 
56 /*
57  * Structure to describe rules for when daylight/summer time begins,
58  * and when standard time begins
59  */
61  char tag[6];
65  uint8_t hour;
66  int offset;
67 };
68 
72 class Timezone
73 {
74 public:
76  {
77  }
78 
79  Timezone(const TimeChangeRule& dstStart, const TimeChangeRule& stdStart)
80  {
81  init(dstStart, stdStart);
82  }
83 
84  void init(const TimeChangeRule& dstStart, const TimeChangeRule& stdStart);
85 
92  time_t toLocal(time_t utc, const TimeChangeRule** p_tcr = nullptr);
93 
122  time_t toUTC(time_t local);
123 
129  bool utcIsDST(time_t utc);
130 
136  bool locIsDST(time_t local);
137 
143  const char* timeTag(bool isDst) const
144  {
145  return isDst ? dstRule.tag : stdRule.tag;
146  }
147 
153  const char* utcTimeTag(time_t utc)
154  {
155  return timeTag(utcIsDST(utc));
156  }
157 
163  const char* localTimeTag(time_t local)
164  {
165  return timeTag(locIsDST(local));
166  }
167 
168 private:
169  /*
170  * Calculate the DST and standard time change points for the given
171  * given year as local and UTC time_t values.
172  */
173  void calcTimeChanges(unsigned yr);
174 
175  /*
176  * Convert the given time change rule to a time_t value for the given year.
177  */
178  time_t toTime_t(TimeChangeRule r, unsigned yr);
179 
180 private:
181  TimeChangeRule dstRule = {"DST", First, Sun, Jan, 1, 0};
182  TimeChangeRule stdRule = {"UTC", First, Sun, Jan, 1, 0};
183  time_t dstStartUTC = 0;
184  time_t stdStartUTC = 0;
185  time_t dstStartLoc = 0;
186  time_t stdStartLoc = 0;
187 };
week_t
Definition: Timezone.h:23
@ Third
Definition: Timezone.h:27
@ Second
Definition: Timezone.h:26
@ Fourth
Definition: Timezone.h:28
@ Last
Definition: Timezone.h:24
@ First
Definition: Timezone.h:25
dow_t
Definition: Timezone.h:31
@ Mon
Definition: Timezone.h:33
@ Sun
Definition: Timezone.h:32
@ Sat
Definition: Timezone.h:38
@ Wed
Definition: Timezone.h:35
@ Tue
Definition: Timezone.h:34
@ Thu
Definition: Timezone.h:36
@ Fri
Definition: Timezone.h:37
month_t
Definition: Timezone.h:41
@ Jul
Definition: Timezone.h:48
@ Jan
Definition: Timezone.h:42
@ Mar
Definition: Timezone.h:44
@ Nov
Definition: Timezone.h:52
@ Aug
Definition: Timezone.h:49
@ Oct
Definition: Timezone.h:51
@ Feb
Definition: Timezone.h:43
@ Jun
Definition: Timezone.h:47
@ Dec
Definition: Timezone.h:53
@ May
Definition: Timezone.h:46
@ Apr
Definition: Timezone.h:45
@ Sep
Definition: Timezone.h:50
Class to support local/UTC time conversions using rules.
Definition: Timezone.h:73
time_t toLocal(time_t utc, const TimeChangeRule **p_tcr=nullptr)
Convert the given UTC time to local time, standard or daylight time.
Timezone(const TimeChangeRule &dstStart, const TimeChangeRule &stdStart)
Definition: Timezone.h:79
bool locIsDST(time_t local)
Determine whether the given local time is within the DST interval or the Standard time interval.
const char * timeTag(bool isDst) const
Return the appropriate dalight-savings tag to append to displayed times.
Definition: Timezone.h:143
void init(const TimeChangeRule &dstStart, const TimeChangeRule &stdStart)
time_t toUTC(time_t local)
Convert the given local time to UTC time.
bool utcIsDST(time_t utc)
Determine whether the UTC time is within the DST interval or the Standard time interval.
const char * localTimeTag(time_t local)
Return the appropriate time tag for a local time.
Definition: Timezone.h:163
Timezone()
Definition: Timezone.h:75
const char * utcTimeTag(time_t utc)
Return the appropriate time tag for a UTC time.
Definition: Timezone.h:153
Definition: Timezone.h:60
uint8_t hour
0-23
Definition: Timezone.h:65
char tag[6]
e.g. DST, UTC, etc.
Definition: Timezone.h:61
month_t month
1=Jan
Definition: Timezone.h:64
week_t week
First, Second, Third, Fourth, or Last week of the month.
Definition: Timezone.h:62
dow_t dow
Day of week, 0=Sun.
Definition: Timezone.h:63
int offset
Offset from UTC in minutes.
Definition: Timezone.h:66