Arduino-RTC
Real-Time Clock (RTC) library for Arduino
usa_dst.h
Go to the documentation of this file.
1 /*
2  * (c)2012 Michael Duane Rice All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer. Redistributions in binary
10  * form must reproduce the above copyright notice, this list of conditions
11  * and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution. Neither the name of the copyright holders
13  * nor the names of contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
50 #ifndef USA_DST_H
51 #define USA_DST_H
52 
53 #include <time.h>
54 #include <inttypes.h>
55 
56 #ifndef DST_START_MONTH
57 #define DST_START_MONTH MARCH
58 #endif
59 
60 #ifndef DST_END_MONTH
61 #define DST_END_MONTH NOVEMBER
62 #endif
63 
64 #ifndef DST_START_WEEK
65 #define DST_START_WEEK 2
66 #endif
67 
68 #ifndef DST_END_WEEK
69 #define DST_END_WEEK 1
70 #endif
71 
72 int usa_dst(const time_t * timer, int32_t * z)
73 {
74  time_t t;
75  struct tm tmptr;
76  uint8_t month, week, hour, day_of_week, d;
77  int n;
78 
79  // Obtain the variables
80  t = *timer + *z;
81  gmtime_r(&t, &tmptr);
82  month = tmptr.tm_mon;
83  day_of_week = tmptr.tm_wday;
84  week = week_of_month(&tmptr, 0);
85  hour = tmptr.tm_hour;
86 
87  if ((month > DST_START_MONTH) && (month < DST_END_MONTH))
88  return ONE_HOUR;
89 
90  if (month < DST_START_MONTH)
91  return 0;
92  if (month > DST_END_MONTH)
93  return 0;
94 
95  if (month == DST_START_MONTH) {
96  if (week < DST_START_WEEK)
97  return 0;
98  if (week > DST_START_WEEK)
99  return ONE_HOUR;
100 
101  if (day_of_week > SUNDAY)
102  return ONE_HOUR;
103  if (hour >= 2)
104  return ONE_HOUR;
105  return 0;
106  }
107 
108  if (week > DST_END_WEEK)
109  return 0;
110  if (week < DST_END_WEEK)
111  return ONE_HOUR;
112  if (day_of_week > SUNDAY)
113  return 0;
114  if (hour >= 1)
115  return 0;
116  return ONE_HOUR;
117 }
118 #endif
#define DST_START_WEEK
Definition: usa_dst.h:65
Definition: time.h:68
#define DST_START_MONTH
Definition: usa_dst.h:57
#define ONE_HOUR
Definition: time.h:148
struct tm * gmtime_r(const time_t *timer, struct tm *timeptr)
Definition: gmtime_r.cpp:33
uint32_t time_t
Definition: time.h:30
int usa_dst(const time_t *timer, int32_t *z)
Definition: usa_dst.h:72
int8_t tm_hour
Hours [0-23].
Definition: time.h:39
int8_t tm_wday
Days since Sunday [0-6].
Definition: time.h:41
Definition: time.h:36
#define DST_END_WEEK
Definition: usa_dst.h:69
int8_t tm_mon
0-11 Months since January [0-11].
Definition: time.h:42
#define DST_END_MONTH
Definition: usa_dst.h:61