$extrastylesheet
avr-libc  2.0.0
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
00001 /*
00002  * (c)2012 Michael Duane Rice All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are
00006  * met:
00007  *
00008  * Redistributions of source code must retain the above copyright notice, this
00009  * list of conditions and the following disclaimer. Redistributions in binary
00010  * form must reproduce the above copyright notice, this list of conditions
00011  * and the following disclaimer in the documentation and/or other materials
00012  * provided with the distribution. Neither the name of the copyright holders
00013  * nor the names of contributors may be used to endorse or promote products
00014  * derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 /* $Id: usa_dst.h 2344 2013-04-10 19:52:09Z swfltek $ */
00030 
00031 /**
00032     Daylight Saving function for the USA. To utilize this function, you must
00033     \code #include <util/usa_dst.h> \endcode
00034     and
00035     \code set_dst(usa_dst); \endcode
00036 
00037     Given the time stamp and time zone parameters provided, the Daylight Saving function must
00038     return a value appropriate for the tm structures' tm_isdst element. That is...
00039 
00040     0 : If Daylight Saving is not in effect.
00041 
00042     -1 : If it cannot be determined if Daylight Saving is in effect.
00043 
00044     A positive integer : Represents the number of seconds a clock is advanced for Daylight Saving.
00045     This will typically be ONE_HOUR.
00046 
00047     Daylight Saving 'rules' are subject to frequent change. For production applications it is
00048     recommended to write your own DST function, which uses 'rules' obtained from, and modifiable by,
00049     the end user ( perhaps stored in EEPROM ).
00050 
00051 */
00052 
00053 #ifndef USA_DST_H
00054 #define USA_DST_H
00055 
00056 #ifdef __cplusplus
00057 extern          "C" {
00058 #endif
00059 
00060 #include <time.h>
00061 #include <inttypes.h>
00062 
00063 #ifndef DST_START_MONTH
00064 #define DST_START_MONTH MARCH
00065 #endif
00066 
00067 #ifndef DST_END_MONTH
00068 #define DST_END_MONTH NOVEMBER
00069 #endif
00070 
00071 #ifndef DST_START_WEEK
00072 #define DST_START_WEEK 2
00073 #endif
00074 
00075 #ifndef DST_END_WEEK
00076 #define DST_END_WEEK 1
00077 #endif
00078 
00079     int             usa_dst(const time_t * timer, int32_t * z) {
00080         time_t          t;
00081         struct tm       tmptr;
00082         uint8_t         month, week, hour, day_of_week, d;
00083         int             n;
00084 
00085         /* obtain the variables */
00086                         t = *timer + *z;
00087                         gmtime_r(&t, &tmptr);
00088                         month = tmptr.tm_mon;
00089                         day_of_week = tmptr.tm_wday;
00090                         week = week_of_month(&tmptr, 0);
00091                         hour = tmptr.tm_hour;
00092 
00093         if              ((month > DST_START_MONTH) && (month < DST_END_MONTH))
00094                             return ONE_HOUR;
00095 
00096         if              (month < DST_START_MONTH)
00097                             return 0;
00098         if              (month > DST_END_MONTH)
00099                             return 0;
00100 
00101         if              (month == DST_START_MONTH) {
00102 
00103             if (week < DST_START_WEEK)
00104                 return 0;
00105             if (week > DST_START_WEEK)
00106                 return ONE_HOUR;
00107 
00108             if (day_of_week > SUNDAY)
00109                 return ONE_HOUR;
00110             if (hour >= 2)
00111                 return ONE_HOUR;
00112             return 0;
00113         }
00114         if              (week > DST_END_WEEK)
00115                             return 0;
00116         if              (week < DST_END_WEEK)
00117                             return ONE_HOUR;
00118         if              (day_of_week > SUNDAY)
00119                             return 0;
00120         if              (hour >= 1)
00121                             return 0;
00122                         return ONE_HOUR;
00123 
00124     }
00125 
00126 #ifdef __cplusplus
00127 }
00128 #endif
00129 
00130 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Defines