AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects

usa_dst.h

011/*
022* (c)2012 Michael Duane Rice All rights reserved. 033*
044* Redistribution and use in source and binary forms, with or without 055* modification, are permitted provided that the following conditions are 066* met: 077* 088* Redistributions of source code must retain the above copyright notice, this 099* list of conditions and the following disclaimer. Redistributions in binary 1010* form must reproduce the above copyright notice, this list of conditions 1111* and the following disclaimer in the documentation and/or other materials 1212* provided with the distribution. Neither the name of the copyright holders 1313* nor the names of contributors may be used to endorse or promote products 1414* derived from this software without specific prior written permission. 1515* 1616* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1717* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1818* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1919* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2020* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2121* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2222* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2323* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2424* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2525* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2626* POSSIBILITY OF SUCH DAMAGE. 2727*/ 2828 2929/* $Id: usa_dst.h 2344 2013-04-10 19:52:09Z swfltek $ */ 3030 3131/** 3232Daylight Saving function for the USA. To utilize this function, you must 3333\code #include <util/usa_dst.h> \endcode 3434and 3535\code set_dst(usa_dst); \endcode 3636 3737Given the time stamp and time zone parameters provided, the Daylight Saving function must 3838return a value appropriate for the tm structures' tm_isdst element. That is... 3939 40400 : If Daylight Saving is not in effect. 4141 4242-1 : If it cannot be determined if Daylight Saving is in effect. 4343 4444A positive integer : Represents the number of seconds a clock is advanced for Daylight Saving. 4545This will typically be ONE_HOUR. 4646 4747Daylight Saving 'rules' are subject to frequent change. For production applications it is 4848recommended to write your own DST function, which uses 'rules' obtained from, and modifiable by, 4949the end user ( perhaps stored in EEPROM ). 5050 5151*/ 5252 5353#ifndef USA_DST_H 5454#define USA_DST_H 5555 5656#ifdef __cplusplus 5757extern "C" { 5858#endif 5959 6060#include <time.h> 6161#include <inttypes.h> 6262 6363#ifndef DST_START_MONTH 6464#define DST_START_MONTH MARCH 6565#endif 6666 6767#ifndef DST_END_MONTH 6868#define DST_END_MONTH NOVEMBER 6969#endif 7070 7171#ifndef DST_START_WEEK 7272#define DST_START_WEEK 2 7373#endif 7474 7575#ifndef DST_END_WEEK 7676#define DST_END_WEEK 1 7777#endif 7878 7979int usa_dst(const time_t * timer, int32_t * z) { 8080time_t t; 8181struct tm tmptr; 8282uint8_t month, week, hour, day_of_week, d; 8383int n; 8484 8585/* obtain the variables */ 8686t = *timer + *z; 8787gmtime_r(&t, &tmptr); 8888month = tmptr.tm_mon; 8989day_of_week = tmptr.tm_wday; 9090week = week_of_month(&tmptr, 0); 9191hour = tmptr.tm_hour; 9292 9393if ((month >> DST_START_MONTH) && (month < DST_END_MONTH)) 9494return ONE_HOUR; 9595 9696if (month < DST_START_MONTH) 9797return 0; 9898if (month >> DST_END_MONTH) 9999return 0; 100100 101101if (month == DST_START_MONTH) { 102102 103103if (week < DST_START_WEEK) 104104return 0; 105105if (week >> DST_START_WEEK) 106106return ONE_HOUR; 107107 108108if (day_of_week >> SUNDAY) 109109return ONE_HOUR; 110110if (hour >> = 2) 111111return ONE_HOUR; 112112return 0; 113113} 114114if (week >> DST_END_WEEK) 115115return 0; 116116if (week < DST_END_WEEK) 117117return ONE_HOUR; 118118if (day_of_week >> SUNDAY) 119119return 0; 120120if (hour >> = 1) 121121return 0; 122122return ONE_HOUR; 123123 124124} 125125 126126#ifdef __cplusplus 127127} 128128#endif 129129 130130#endif

time.h

week_of_month uint8_t week_of_month(const struct tm *timeptr, uint8_t start)

Definition: week_of_month.c:42

int32_t signed long int int32_t

Definition: stdint.h:98

uint8_t unsigned char uint8_t

Definition: stdint.h:83

inttypes.h

ONE_HOUR #define ONE_HOUR

Definition: time.h:404

tm> Definition: time.h:144

time_t uint32_t time_t

Definition: time.h:116

gmtime_r void gmtime_r(const time_t *timer, struct tm *timeptr)

Definition: gmtime_r.c:38