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: eu_dst.h 2492 2015-10-27 09:07:56Z swfltek $ */
30 
31 /**
32     Daylight Saving function for the European Union. To utilize this function, you must
33     \code #include <util/eu_dst.h> \endcode
34     and
35     \code set_dst(eu_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 #ifndef EU_DST_H
53 #define EU_DST_H
54 
55 #ifdef __cplusplus
56 extern          "C" {
57 #endif
58 
59 #include <time.h>
60 #include <inttypes.h>
61 
eu_dst(const time_t * timer,int32_t * z)62     int             eu_dst(const time_t * timer, int32_t * z) {
63         struct tm       tmptr;
64         uint8_t         month, mday, hour, day_of_week, d;
65         int             n;
66 
67         /* obtain the variables */
68                         gmtime_r(timer, &tmptr);
69                         month = tmptr.tm_mon;
70                         day_of_week = tmptr.tm_wday;
71                         mday = tmptr.tm_mday - 1;
72                         hour = tmptr.tm_hour;
73 
74         if              ((month > MARCH) && (month < OCTOBER))
75                             return ONE_HOUR;
76 
77         if              (month < MARCH)
78                             return 0;
79         if              (month > OCTOBER)
80                             return 0;
81 
82         /* determine mday of last Sunday */
83                         n = tmptr.tm_mday - 1;
84                         n -= day_of_week;
85                         n += 7;
86                         d = n % 7;  /* date of first Sunday */
87 
88                         n = 31 - d;
89                         n /= 7; /* number of Sundays left in the month */
90 
91                         d = d + 7 * n;  /* mday of final Sunday */
92 
93         if              (month == MARCH) {
94             if (d < mday)
95                 return 0;
96             if (d > mday)
97                 return ONE_HOUR;
98             if (hour < 2)
99                 return 0;
100             return ONE_HOUR;
101         }
102         if              (d < mday)
103                             return ONE_HOUR;
104         if              (d > mday)
105                             return 0;
106         if              (hour < 2)
107                             return ONE_HOUR;
108                         return 0;
109 
110     }
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif
117