This library takes the current time (which must be set to UTC, e.g. via an NTP call), and applies a timezone definition (loaded at startup) to calculate the local time. This includes the handling of daylight saving. See http://mbed.org/users/hlipka/notebook/time-zone-handling/ for more information (esp. how to get a time zone definition file).

Dependents:   CubiScan 000-FIN_youcef 005_ESSAI_youcef

Committer:
hlipka
Date:
Fri Feb 18 13:15:35 2011 +0000
Revision:
7:0c7207d674d3
Parent:
6:f4693f2d03e6
added license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 7:0c7207d674d3 1 /*
hlipka 7:0c7207d674d3 2 * TimeZone library
hlipka 7:0c7207d674d3 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 7:0c7207d674d3 4 *
hlipka 7:0c7207d674d3 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 7:0c7207d674d3 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 7:0c7207d674d3 7 * in the Software without restriction, including without limitation the rights
hlipka 7:0c7207d674d3 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 7:0c7207d674d3 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 7:0c7207d674d3 10 * furnished to do so, subject to the following conditions:
hlipka 7:0c7207d674d3 11 *
hlipka 7:0c7207d674d3 12 * The above copyright notice and this permission notice shall be included in
hlipka 7:0c7207d674d3 13 * all copies or substantial portions of the Software.
hlipka 7:0c7207d674d3 14 *
hlipka 7:0c7207d674d3 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 7:0c7207d674d3 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 7:0c7207d674d3 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 7:0c7207d674d3 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 7:0c7207d674d3 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 7:0c7207d674d3 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 7:0c7207d674d3 21 * THE SOFTWARE.
hlipka 7:0c7207d674d3 22 */
hlipka 7:0c7207d674d3 23
hlipka 4:c84afcfbac84 24 #ifndef __TIME_H_
hlipka 4:c84afcfbac84 25 #define __TIME_H_
hlipka 4:c84afcfbac84 26
hlipka 4:c84afcfbac84 27 #include <time.h>
hlipka 4:c84afcfbac84 28 #include "stdio.h"
hlipka 4:c84afcfbac84 29 #include "string.h"
hlipka 4:c84afcfbac84 30 #include <list>
hlipka 4:c84afcfbac84 31
hlipka 4:c84afcfbac84 32 class Time;
hlipka 4:c84afcfbac84 33
hlipka 4:c84afcfbac84 34 /**
hlipka 4:c84afcfbac84 35 This class encapsulates a point in time - which means it will not change after it has been created.
hlipka 4:c84afcfbac84 36 */
hlipka 4:c84afcfbac84 37 class TimeStamp {
hlipka 4:c84afcfbac84 38 friend class Time;
hlipka 4:c84afcfbac84 39 public:
hlipka 4:c84afcfbac84 40 /**
hlipka 4:c84afcfbac84 41 @returns the year of the time stamp
hlipka 4:c84afcfbac84 42 */
hlipka 4:c84afcfbac84 43 int getYear() {
hlipka 4:c84afcfbac84 44 return _year+1900;
hlipka 4:c84afcfbac84 45 };
hlipka 4:c84afcfbac84 46
hlipka 4:c84afcfbac84 47 /**
hlipka 4:c84afcfbac84 48 @returns the month of the time stamp (January is 1)
hlipka 4:c84afcfbac84 49 */
hlipka 4:c84afcfbac84 50 int getMonth() {
hlipka 4:c84afcfbac84 51 return _mon+1;
hlipka 4:c84afcfbac84 52 };
hlipka 4:c84afcfbac84 53
hlipka 4:c84afcfbac84 54 int getDay() {
hlipka 4:c84afcfbac84 55 /**
hlipka 4:c84afcfbac84 56 @returns the day-of-the-month of the time stamp (starting with 1)
hlipka 4:c84afcfbac84 57 */
hlipka 4:c84afcfbac84 58 return _mday;
hlipka 4:c84afcfbac84 59 };
hlipka 4:c84afcfbac84 60
hlipka 4:c84afcfbac84 61 /**
hlipka 4:c84afcfbac84 62 @returns the hour of the time stamp (0-23)
hlipka 4:c84afcfbac84 63 */
hlipka 4:c84afcfbac84 64 int getHour() {
hlipka 4:c84afcfbac84 65 return _hour;
hlipka 4:c84afcfbac84 66 };
hlipka 4:c84afcfbac84 67
hlipka 4:c84afcfbac84 68 /**
hlipka 4:c84afcfbac84 69 @returns the minute of the time stamp (0-59)
hlipka 4:c84afcfbac84 70 */
hlipka 4:c84afcfbac84 71 int getMinute() {
hlipka 4:c84afcfbac84 72 return _min;
hlipka 4:c84afcfbac84 73 };
hlipka 4:c84afcfbac84 74
hlipka 4:c84afcfbac84 75 /**
hlipka 4:c84afcfbac84 76 @returns the second of the time stamp (0-59)
hlipka 4:c84afcfbac84 77 */
hlipka 4:c84afcfbac84 78 int getSecond() {
hlipka 4:c84afcfbac84 79 return _sec;
hlipka 4:c84afcfbac84 80 };
hlipka 4:c84afcfbac84 81
hlipka 4:c84afcfbac84 82 /**
hlipka 4:c84afcfbac84 83 get the day of the week - monday is 0
hlipka 4:c84afcfbac84 84 @returns the day-of-the-week of the time stamp
hlipka 4:c84afcfbac84 85 */
hlipka 4:c84afcfbac84 86 int getDayOfWeek() {
hlipka 4:c84afcfbac84 87 int dow=_wday;
hlipka 4:c84afcfbac84 88 dow--;
hlipka 4:c84afcfbac84 89 if (dow==-1)
hlipka 4:c84afcfbac84 90 dow=6;
hlipka 4:c84afcfbac84 91 return dow;
hlipka 4:c84afcfbac84 92 };
hlipka 4:c84afcfbac84 93
hlipka 4:c84afcfbac84 94 /**
hlipka 4:c84afcfbac84 95 @returns the day-of-the-year of the time stamp
hlipka 4:c84afcfbac84 96 */
hlipka 4:c84afcfbac84 97 /*
hlipka 4:c84afcfbac84 98 int getDayOfYear() {
hlipka 4:c84afcfbac84 99 return _yday;
hlipka 4:c84afcfbac84 100 };
hlipka 4:c84afcfbac84 101 */
hlipka 4:c84afcfbac84 102 /**
hlipka 4:c84afcfbac84 103 creates a new time stamp based on the UNIX time stamp (seconds since 1970)
hlipka 4:c84afcfbac84 104 */
hlipka 4:c84afcfbac84 105 TimeStamp(time_t unixTime) {
hlipka 4:c84afcfbac84 106 updateTime(unixTime);
hlipka 4:c84afcfbac84 107 };
hlipka 4:c84afcfbac84 108
hlipka 6:f4693f2d03e6 109 TimeStamp(int year, int mon, int day, int hour, int min, int sec, int wday)
hlipka 6:f4693f2d03e6 110 {
hlipka 6:f4693f2d03e6 111 _year=(char)(year-1900);
hlipka 6:f4693f2d03e6 112 _mon=(char)(mon-1);
hlipka 6:f4693f2d03e6 113 _mday=(char)day;
hlipka 6:f4693f2d03e6 114 _hour=(char)hour;
hlipka 6:f4693f2d03e6 115 _min=(char)min;
hlipka 6:f4693f2d03e6 116 _sec=(char)sec;
hlipka 6:f4693f2d03e6 117 _wday=(char)wday;
hlipka 6:f4693f2d03e6 118 }
hlipka 4:c84afcfbac84 119 ~TimeStamp() {
hlipka 4:c84afcfbac84 120 };
hlipka 4:c84afcfbac84 121
hlipka 4:c84afcfbac84 122 /**
hlipka 4:c84afcfbac84 123 @param ts the time stamp to compare to
hlipka 4:c84afcfbac84 124 @returns true when both timestamp are the same point in time
hlipka 4:c84afcfbac84 125 */
hlipka 4:c84afcfbac84 126 bool isSame(TimeStamp* ts);
hlipka 4:c84afcfbac84 127
hlipka 4:c84afcfbac84 128 /**
hlipka 4:c84afcfbac84 129 @param ts the time stamp to compare to
hlipka 4:c84afcfbac84 130 @return true when the current time stamp is before the given time stamp
hlipka 4:c84afcfbac84 131 */
hlipka 4:c84afcfbac84 132 bool isBefore(TimeStamp* ts);
hlipka 4:c84afcfbac84 133
hlipka 4:c84afcfbac84 134 /**
hlipka 4:c84afcfbac84 135 @returns time stamp transformed to ASCII (done by asctime() - but transformed to local time)
hlipka 4:c84afcfbac84 136 */
hlipka 4:c84afcfbac84 137 char *asChar() { struct tm * time=getTm();char *s=asctime(time); delete time;return s;};
hlipka 4:c84afcfbac84 138
hlipka 4:c84afcfbac84 139 /**
hlipka 4:c84afcfbac84 140 @param ts the time stamp to compare to
hlipka 4:c84afcfbac84 141 @return true when the current time stamp is after the given time stamp
hlipka 4:c84afcfbac84 142 */
hlipka 4:c84afcfbac84 143 bool isAfter(TimeStamp* ts);
hlipka 6:f4693f2d03e6 144 int getStartOfDay()
hlipka 6:f4693f2d03e6 145 {
hlipka 6:f4693f2d03e6 146 tm* t=getTm();
hlipka 6:f4693f2d03e6 147 t->tm_hour=0;
hlipka 6:f4693f2d03e6 148 t->tm_min=0;
hlipka 6:f4693f2d03e6 149 t->tm_sec=0;
hlipka 6:f4693f2d03e6 150 return mktime(t);
hlipka 6:f4693f2d03e6 151 }
hlipka 4:c84afcfbac84 152 private:
hlipka 4:c84afcfbac84 153 tm *getTm()
hlipka 4:c84afcfbac84 154 {
hlipka 4:c84afcfbac84 155 tm *time=new tm();
hlipka 4:c84afcfbac84 156
hlipka 4:c84afcfbac84 157 time->tm_year=_year;
hlipka 4:c84afcfbac84 158 time->tm_mon=_mon;
hlipka 4:c84afcfbac84 159 time->tm_mday=_mday;
hlipka 4:c84afcfbac84 160 time->tm_hour=_hour;
hlipka 4:c84afcfbac84 161 time->tm_min=_min;
hlipka 4:c84afcfbac84 162 time->tm_sec=_sec;
hlipka 4:c84afcfbac84 163 time->tm_wday=_wday;
hlipka 4:c84afcfbac84 164
hlipka 4:c84afcfbac84 165 return time;
hlipka 4:c84afcfbac84 166 }
hlipka 4:c84afcfbac84 167 void updateTime(time_t unixTime) {
hlipka 4:c84afcfbac84 168 struct tm *time;
hlipka 4:c84afcfbac84 169 time=localtime(&unixTime);
hlipka 4:c84afcfbac84 170 _year=time->tm_year;
hlipka 4:c84afcfbac84 171 _mon=time->tm_mon;
hlipka 4:c84afcfbac84 172 _mday=time->tm_mday;
hlipka 4:c84afcfbac84 173 _hour=time->tm_hour;
hlipka 4:c84afcfbac84 174 _min=time->tm_min;
hlipka 4:c84afcfbac84 175 _sec=time->tm_sec;
hlipka 4:c84afcfbac84 176 _wday=time->tm_wday;
hlipka 4:c84afcfbac84 177 }
hlipka 4:c84afcfbac84 178 void print()
hlipka 4:c84afcfbac84 179 {
hlipka 4:c84afcfbac84 180 printf("ts=%i.%i.%i %i:%i.%i\n",_mday,_mon+1,_year+1900,_hour,_min,_sec);
hlipka 4:c84afcfbac84 181 }
hlipka 4:c84afcfbac84 182 // make sure this structure needs only 7 bytes - otherwise it's 48 bytes...
hlipka 4:c84afcfbac84 183 char __packed _year;
hlipka 4:c84afcfbac84 184 char __packed _mon;
hlipka 4:c84afcfbac84 185 char __packed _mday;
hlipka 4:c84afcfbac84 186 char __packed _hour;
hlipka 4:c84afcfbac84 187 char __packed _min;
hlipka 4:c84afcfbac84 188 char __packed _sec;
hlipka 4:c84afcfbac84 189 char __packed _wday;
hlipka 4:c84afcfbac84 190 };
hlipka 4:c84afcfbac84 191
hlipka 4:c84afcfbac84 192 class TimeZoneEntry;
hlipka 4:c84afcfbac84 193
hlipka 4:c84afcfbac84 194 /**
hlipka 4:c84afcfbac84 195 This class handles the time zone calculation, and is used for creating time stamp objects.
hlipka 4:c84afcfbac84 196 */
hlipka 4:c84afcfbac84 197 class Time {
hlipka 4:c84afcfbac84 198 public:
hlipka 4:c84afcfbac84 199 /**
hlipka 4:c84afcfbac84 200 creates a new Time instance. On the first call, it reads the file 'timezone.csv' from the USB disk (local file system).
hlipka 4:c84afcfbac84 201 */
hlipka 4:c84afcfbac84 202 Time();
hlipka 4:c84afcfbac84 203 ~Time();
hlipka 4:c84afcfbac84 204 /**
hlipka 4:c84afcfbac84 205 creates a new TimeStamp instance. The caller is responsible for deleting it afterwards!
hlipka 4:c84afcfbac84 206 @returns the time stamp
hlipka 4:c84afcfbac84 207 */
hlipka 4:c84afcfbac84 208 TimeStamp* getTime();
hlipka 4:c84afcfbac84 209 /**
hlipka 4:c84afcfbac84 210 @returns the current time stamp (UTC) in UNIX format, without time zone correction.
hlipka 4:c84afcfbac84 211 */
hlipka 4:c84afcfbac84 212 long getUnixTime();
hlipka 4:c84afcfbac84 213 private:
hlipka 4:c84afcfbac84 214 static TimeZoneEntry* _timeZoneEntries;
hlipka 5:fde01b92a384 215 static void readTimeZones();
hlipka 5:fde01b92a384 216 static int getTimeOffset(TimeStamp* ts);
hlipka 4:c84afcfbac84 217 };
hlipka 4:c84afcfbac84 218
hlipka 0:ab93db24fcc8 219 #endif