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
Time.h@3:2ad51ec5ef6e, 2011-01-04 (annotated)
- Committer:
- hlipka
- Date:
- Tue Jan 04 23:19:50 2011 +0000
- Revision:
- 3:2ad51ec5ef6e
- Parent:
- 2:7946f902f2d4
- Child:
- 4:c84afcfbac84
fixed reading of time zone entries (was missing the beginning of the year), months are starting with 1 now, updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:ab93db24fcc8 | 1 | #ifndef __TIME_H_ |
hlipka | 0:ab93db24fcc8 | 2 | #define __TIME_H_ |
hlipka | 0:ab93db24fcc8 | 3 | |
hlipka | 0:ab93db24fcc8 | 4 | #include <time.h> |
hlipka | 0:ab93db24fcc8 | 5 | #include "stdio.h" |
hlipka | 0:ab93db24fcc8 | 6 | #include "string.h" |
hlipka | 0:ab93db24fcc8 | 7 | #include <list> |
hlipka | 0:ab93db24fcc8 | 8 | |
hlipka | 0:ab93db24fcc8 | 9 | class Time; |
hlipka | 0:ab93db24fcc8 | 10 | |
hlipka | 1:81f7dd124217 | 11 | /** |
hlipka | 1:81f7dd124217 | 12 | This class encapsulates a point in time - which means it will not change after it has been created. |
hlipka | 1:81f7dd124217 | 13 | */ |
hlipka | 0:ab93db24fcc8 | 14 | class TimeStamp { |
hlipka | 0:ab93db24fcc8 | 15 | friend class Time; |
hlipka | 0:ab93db24fcc8 | 16 | public: |
hlipka | 1:81f7dd124217 | 17 | /** |
hlipka | 1:81f7dd124217 | 18 | @returns the year of the time stamp |
hlipka | 1:81f7dd124217 | 19 | */ |
hlipka | 0:ab93db24fcc8 | 20 | int getYear() { |
hlipka | 0:ab93db24fcc8 | 21 | return _tm.tm_year+1900; |
hlipka | 0:ab93db24fcc8 | 22 | }; |
hlipka | 0:ab93db24fcc8 | 23 | |
hlipka | 1:81f7dd124217 | 24 | /** |
hlipka | 3:2ad51ec5ef6e | 25 | @returns the month of the time stamp (January is 1) |
hlipka | 1:81f7dd124217 | 26 | */ |
hlipka | 0:ab93db24fcc8 | 27 | int getMonth() { |
hlipka | 3:2ad51ec5ef6e | 28 | return _tm.tm_mon+1; |
hlipka | 0:ab93db24fcc8 | 29 | }; |
hlipka | 0:ab93db24fcc8 | 30 | |
hlipka | 0:ab93db24fcc8 | 31 | int getDay() { |
hlipka | 1:81f7dd124217 | 32 | /** |
hlipka | 3:2ad51ec5ef6e | 33 | @returns the day-of-the-month of the time stamp (starting with 1) |
hlipka | 1:81f7dd124217 | 34 | */ |
hlipka | 0:ab93db24fcc8 | 35 | return _tm.tm_mday; |
hlipka | 0:ab93db24fcc8 | 36 | }; |
hlipka | 0:ab93db24fcc8 | 37 | |
hlipka | 1:81f7dd124217 | 38 | /** |
hlipka | 3:2ad51ec5ef6e | 39 | @returns the hour of the time stamp (0-23) |
hlipka | 1:81f7dd124217 | 40 | */ |
hlipka | 0:ab93db24fcc8 | 41 | int getHour() { |
hlipka | 0:ab93db24fcc8 | 42 | return _tm.tm_hour; |
hlipka | 0:ab93db24fcc8 | 43 | }; |
hlipka | 0:ab93db24fcc8 | 44 | |
hlipka | 1:81f7dd124217 | 45 | /** |
hlipka | 3:2ad51ec5ef6e | 46 | @returns the minute of the time stamp (0-59) |
hlipka | 1:81f7dd124217 | 47 | */ |
hlipka | 0:ab93db24fcc8 | 48 | int getMinute() { |
hlipka | 0:ab93db24fcc8 | 49 | return _tm.tm_min; |
hlipka | 0:ab93db24fcc8 | 50 | }; |
hlipka | 0:ab93db24fcc8 | 51 | |
hlipka | 1:81f7dd124217 | 52 | /** |
hlipka | 3:2ad51ec5ef6e | 53 | @returns the second of the time stamp (0-59) |
hlipka | 1:81f7dd124217 | 54 | */ |
hlipka | 0:ab93db24fcc8 | 55 | int getSecond() { |
hlipka | 0:ab93db24fcc8 | 56 | return _tm.tm_sec; |
hlipka | 0:ab93db24fcc8 | 57 | }; |
hlipka | 0:ab93db24fcc8 | 58 | |
hlipka | 1:81f7dd124217 | 59 | /** |
hlipka | 1:81f7dd124217 | 60 | get the day of the week - monday is 0 |
hlipka | 1:81f7dd124217 | 61 | @returns the day-of-the-week of the time stamp |
hlipka | 1:81f7dd124217 | 62 | */ |
hlipka | 0:ab93db24fcc8 | 63 | int getDayOfWeek() { |
hlipka | 0:ab93db24fcc8 | 64 | int dow=_tm.tm_wday; |
hlipka | 0:ab93db24fcc8 | 65 | dow--; |
hlipka | 0:ab93db24fcc8 | 66 | if (dow==-1) |
hlipka | 0:ab93db24fcc8 | 67 | dow=6; |
hlipka | 0:ab93db24fcc8 | 68 | return dow; |
hlipka | 0:ab93db24fcc8 | 69 | }; |
hlipka | 0:ab93db24fcc8 | 70 | |
hlipka | 1:81f7dd124217 | 71 | /** |
hlipka | 1:81f7dd124217 | 72 | @returns the day-of-the-year of the time stamp |
hlipka | 1:81f7dd124217 | 73 | */ |
hlipka | 0:ab93db24fcc8 | 74 | int getDayOfYear() { |
hlipka | 0:ab93db24fcc8 | 75 | return _tm.tm_yday; |
hlipka | 0:ab93db24fcc8 | 76 | }; |
hlipka | 0:ab93db24fcc8 | 77 | |
hlipka | 1:81f7dd124217 | 78 | /** |
hlipka | 1:81f7dd124217 | 79 | creates a new time stamp based on the UNIX time stamp (seconds since 1970) |
hlipka | 1:81f7dd124217 | 80 | */ |
hlipka | 0:ab93db24fcc8 | 81 | TimeStamp(time_t unixTime) { |
hlipka | 0:ab93db24fcc8 | 82 | updateTime(unixTime); |
hlipka | 0:ab93db24fcc8 | 83 | }; |
hlipka | 0:ab93db24fcc8 | 84 | |
hlipka | 0:ab93db24fcc8 | 85 | ~TimeStamp() { |
hlipka | 0:ab93db24fcc8 | 86 | }; |
hlipka | 1:81f7dd124217 | 87 | |
hlipka | 1:81f7dd124217 | 88 | /** |
hlipka | 1:81f7dd124217 | 89 | @param ts the time stamp to compare to |
hlipka | 1:81f7dd124217 | 90 | @returns true when both timestamp are the same point in time |
hlipka | 1:81f7dd124217 | 91 | */ |
hlipka | 0:ab93db24fcc8 | 92 | bool isSame(TimeStamp* ts); |
hlipka | 1:81f7dd124217 | 93 | |
hlipka | 1:81f7dd124217 | 94 | /** |
hlipka | 1:81f7dd124217 | 95 | @param ts the time stamp to compare to |
hlipka | 1:81f7dd124217 | 96 | @return true when the current time stamp is before the given time stamp |
hlipka | 1:81f7dd124217 | 97 | */ |
hlipka | 0:ab93db24fcc8 | 98 | bool isBefore(TimeStamp* ts); |
hlipka | 1:81f7dd124217 | 99 | /** |
hlipka | 1:81f7dd124217 | 100 | @param ts the time stamp to compare to |
hlipka | 1:81f7dd124217 | 101 | @return true when the current time stamp is after the given time stamp |
hlipka | 1:81f7dd124217 | 102 | */ |
hlipka | 0:ab93db24fcc8 | 103 | bool isAfter(TimeStamp* ts); |
hlipka | 0:ab93db24fcc8 | 104 | private: |
hlipka | 0:ab93db24fcc8 | 105 | void updateTime(time_t unixTime) { |
hlipka | 0:ab93db24fcc8 | 106 | struct tm *time; |
hlipka | 0:ab93db24fcc8 | 107 | time=localtime(&unixTime); |
hlipka | 0:ab93db24fcc8 | 108 | memcpy(&_tm, time, sizeof(_tm)); |
hlipka | 0:ab93db24fcc8 | 109 | } |
hlipka | 0:ab93db24fcc8 | 110 | TimeStamp(int year, int mon, int day, int hour, int min, int sec) |
hlipka | 0:ab93db24fcc8 | 111 | { |
hlipka | 0:ab93db24fcc8 | 112 | _tm.tm_year=year-1900; |
hlipka | 3:2ad51ec5ef6e | 113 | _tm.tm_mon=mon-1; |
hlipka | 0:ab93db24fcc8 | 114 | _tm.tm_mday=day; |
hlipka | 0:ab93db24fcc8 | 115 | _tm.tm_hour=hour; |
hlipka | 0:ab93db24fcc8 | 116 | _tm.tm_min=min; |
hlipka | 0:ab93db24fcc8 | 117 | _tm.tm_sec=sec; |
hlipka | 0:ab93db24fcc8 | 118 | } |
hlipka | 3:2ad51ec5ef6e | 119 | |
hlipka | 3:2ad51ec5ef6e | 120 | void print() |
hlipka | 3:2ad51ec5ef6e | 121 | { |
hlipka | 3:2ad51ec5ef6e | 122 | printf("ts=%i.%i.%i %i:%i.%i\n",_tm.tm_mday,_tm.tm_mon+1,_tm.tm_year+1900,_tm.tm_hour,_tm.tm_min,_tm.tm_sec); |
hlipka | 3:2ad51ec5ef6e | 123 | } |
hlipka | 0:ab93db24fcc8 | 124 | |
hlipka | 0:ab93db24fcc8 | 125 | struct tm _tm; |
hlipka | 0:ab93db24fcc8 | 126 | }; |
hlipka | 0:ab93db24fcc8 | 127 | |
hlipka | 0:ab93db24fcc8 | 128 | class TimeZoneEntry; |
hlipka | 0:ab93db24fcc8 | 129 | |
hlipka | 2:7946f902f2d4 | 130 | /** |
hlipka | 2:7946f902f2d4 | 131 | This class handles the time zone calculation, and is used for creating time stamp objects. |
hlipka | 2:7946f902f2d4 | 132 | */ |
hlipka | 0:ab93db24fcc8 | 133 | class Time { |
hlipka | 0:ab93db24fcc8 | 134 | public: |
hlipka | 2:7946f902f2d4 | 135 | /** |
hlipka | 2:7946f902f2d4 | 136 | creates a new Time instance. On the first call, it reads the file 'timezone.csv' from the USB disk (local file system). |
hlipka | 2:7946f902f2d4 | 137 | */ |
hlipka | 0:ab93db24fcc8 | 138 | Time(); |
hlipka | 0:ab93db24fcc8 | 139 | ~Time(); |
hlipka | 2:7946f902f2d4 | 140 | /** |
hlipka | 2:7946f902f2d4 | 141 | creates a new TimeStamp instance. The caller is responsible for deleting it afterwards! |
hlipka | 2:7946f902f2d4 | 142 | @returns the time stamp |
hlipka | 2:7946f902f2d4 | 143 | */ |
hlipka | 0:ab93db24fcc8 | 144 | TimeStamp* getTime(); |
hlipka | 2:7946f902f2d4 | 145 | /** |
hlipka | 2:7946f902f2d4 | 146 | @returns the current time stamp (UTC) in UNIX format, without time zone correction. |
hlipka | 2:7946f902f2d4 | 147 | */ |
hlipka | 0:ab93db24fcc8 | 148 | long getUnixTime(); |
hlipka | 0:ab93db24fcc8 | 149 | private: |
hlipka | 0:ab93db24fcc8 | 150 | static std::list<TimeZoneEntry*> *_timeZoneEntries; |
hlipka | 0:ab93db24fcc8 | 151 | void readTimeZones(); |
hlipka | 0:ab93db24fcc8 | 152 | }; |
hlipka | 0:ab93db24fcc8 | 153 | |
hlipka | 0:ab93db24fcc8 | 154 | #endif |