avr-libc  2.2.0git
Standard C library for AVR-GCC

AVR Libc Home Page

AVRs

AVR Libc Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

usa_dst.h
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
29/* $Id$ */
30
31/**
32 Daylight Saving function for the USA. To utilize this function, you must
33 \code #include <util/usa_dst.h> \endcode
34 and
35 \code set_dst(usa_dst); \endcode
36
37 Given the time stamp and time zone parameters provided, the Daylight Saving function must
38 return a value appropriate for the tm structures' tm_isdst element. That is...
39
40 0 : If Daylight Saving is not in effect.
41
42 -1 : If it cannot be determined if Daylight Saving is in effect.
43
44 A positive integer : Represents the number of seconds a clock is advanced for Daylight Saving.
45 This will typically be ONE_HOUR.
46
47 Daylight Saving 'rules' are subject to frequent change. For production applications it is
48 recommended to write your own DST function, which uses 'rules' obtained from, and modifiable by,
49 the end user ( perhaps stored in EEPROM ).
50
51*/
52
53#ifndef USA_DST_H
54#define USA_DST_H
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60#include <time.h>
61#include <inttypes.h>
62
63#ifndef DST_START_MONTH
64#define DST_START_MONTH MARCH
65#endif
66
67#ifndef DST_END_MONTH
68#define DST_END_MONTH NOVEMBER
69#endif
70
71#ifndef DST_START_WEEK
72#define DST_START_WEEK 2
73#endif
74
75#ifndef DST_END_WEEK
76#define DST_END_WEEK 1
77#endif
78
79 int usa_dst(const time_t * timer, int32_t * z) {
80 time_t t;
81 struct tm tmptr;
82 uint8_t month, week, hour, day_of_week, d;
83 int n;
84
85 /* obtain the variables */
86 t = *timer + *z;
87 gmtime_r(&t, &tmptr);
88 month = tmptr.tm_mon;
89 day_of_week = tmptr.tm_wday;
90 week = week_of_month(&tmptr, 0);
91 hour = tmptr.tm_hour;
92
93 if ((month > DST_START_MONTH) && (month < DST_END_MONTH))
94 return ONE_HOUR;
95
96 if (month < DST_START_MONTH)
97 return 0;
98 if (month > DST_END_MONTH)
99 return 0;
100
101 if (month == DST_START_MONTH) {
102
103 if (week < DST_START_WEEK)
104 return 0;
105 if (week > DST_START_WEEK)
106 return ONE_HOUR;
107
108 if (day_of_week > SUNDAY)
109 return ONE_HOUR;
110 if (hour >= 2)
111 return ONE_HOUR;
112 return 0;
113 }
114 if (week > DST_END_WEEK)
115 return 0;
116 if (week < DST_END_WEEK)
117 return ONE_HOUR;
118 if (day_of_week > SUNDAY)
119 return 0;
120 if (hour >= 1)
121 return 0;
122 return ONE_HOUR;
123
124 }
125
126#ifdef __cplusplus
127}
128#endif
129
130#endif
unsigned char uint8_t
Definition: stdint.h:83
signed long int int32_t
Definition: stdint.h:98
uint32_t time_t
Definition: time.h:116
Definition: time.h:144
uint8_t week_of_month(const struct tm *timeptr, uint8_t start)
Definition: week_of_month.c:42
void gmtime_r(const time_t *timer, struct tm *timeptr)
Definition: gmtime_r.c:38
#define ONE_HOUR
Definition: time.h:404