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.cpp@4:c84afcfbac84, 2011-01-21 (annotated)
- Committer:
- hlipka
- Date:
- Fri Jan 21 23:05:05 2011 +0000
- Revision:
- 4:c84afcfbac84
- Parent:
- 3:2ad51ec5ef6e
- Child:
- 5:fde01b92a384
reduced memory consumption (using packed data structures and a linked list instead of std::list), and added an asChar() method for easier printf()ing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:ab93db24fcc8 | 1 | #include "Time.h" |
hlipka | 0:ab93db24fcc8 | 2 | |
hlipka | 0:ab93db24fcc8 | 3 | #include "stdio.h" |
hlipka | 0:ab93db24fcc8 | 4 | #include "time.h" |
hlipka | 0:ab93db24fcc8 | 5 | |
hlipka | 0:ab93db24fcc8 | 6 | using namespace std; |
hlipka | 0:ab93db24fcc8 | 7 | |
hlipka | 0:ab93db24fcc8 | 8 | class TimeZoneEntry { |
hlipka | 0:ab93db24fcc8 | 9 | public: |
hlipka | 0:ab93db24fcc8 | 10 | TimeStamp *_from; |
hlipka | 0:ab93db24fcc8 | 11 | TimeStamp *_to; |
hlipka | 0:ab93db24fcc8 | 12 | int _offset; |
hlipka | 4:c84afcfbac84 | 13 | TimeZoneEntry* next; |
hlipka | 0:ab93db24fcc8 | 14 | }; |
hlipka | 0:ab93db24fcc8 | 15 | |
hlipka | 4:c84afcfbac84 | 16 | TimeZoneEntry* Time::_timeZoneEntries=NULL; |
hlipka | 0:ab93db24fcc8 | 17 | |
hlipka | 0:ab93db24fcc8 | 18 | bool TimeStamp::isSame(TimeStamp* ts) |
hlipka | 0:ab93db24fcc8 | 19 | { |
hlipka | 0:ab93db24fcc8 | 20 | if (ts->getYear()!=getYear()) |
hlipka | 0:ab93db24fcc8 | 21 | return false; |
hlipka | 0:ab93db24fcc8 | 22 | if (ts->getMonth()!=getMonth()) |
hlipka | 0:ab93db24fcc8 | 23 | return false; |
hlipka | 0:ab93db24fcc8 | 24 | if (ts->getDay()!=getDay()) |
hlipka | 0:ab93db24fcc8 | 25 | return false; |
hlipka | 0:ab93db24fcc8 | 26 | if (ts->getHour()!=getHour()) |
hlipka | 0:ab93db24fcc8 | 27 | return false; |
hlipka | 0:ab93db24fcc8 | 28 | if (ts->getSecond()!=getSecond()) |
hlipka | 0:ab93db24fcc8 | 29 | return false; |
hlipka | 0:ab93db24fcc8 | 30 | return true; |
hlipka | 0:ab93db24fcc8 | 31 | } |
hlipka | 0:ab93db24fcc8 | 32 | bool TimeStamp::isBefore(TimeStamp* ts) |
hlipka | 0:ab93db24fcc8 | 33 | { |
hlipka | 0:ab93db24fcc8 | 34 | if (getYear()<ts->getYear()) |
hlipka | 0:ab93db24fcc8 | 35 | return true; |
hlipka | 0:ab93db24fcc8 | 36 | if (getYear()>ts->getYear()) |
hlipka | 0:ab93db24fcc8 | 37 | return false; |
hlipka | 0:ab93db24fcc8 | 38 | |
hlipka | 0:ab93db24fcc8 | 39 | if (getMonth()<ts->getMonth()) |
hlipka | 0:ab93db24fcc8 | 40 | return true; |
hlipka | 0:ab93db24fcc8 | 41 | if (getMonth()>ts->getMonth()) |
hlipka | 0:ab93db24fcc8 | 42 | return false; |
hlipka | 0:ab93db24fcc8 | 43 | |
hlipka | 0:ab93db24fcc8 | 44 | if (getDay()<ts->getDay()) |
hlipka | 0:ab93db24fcc8 | 45 | return true; |
hlipka | 0:ab93db24fcc8 | 46 | if (getDay()>ts->getDay()) |
hlipka | 0:ab93db24fcc8 | 47 | return false; |
hlipka | 0:ab93db24fcc8 | 48 | |
hlipka | 0:ab93db24fcc8 | 49 | if (getHour()<ts->getHour()) |
hlipka | 0:ab93db24fcc8 | 50 | return true; |
hlipka | 0:ab93db24fcc8 | 51 | if (getHour()>ts->getHour()) |
hlipka | 0:ab93db24fcc8 | 52 | return false; |
hlipka | 0:ab93db24fcc8 | 53 | |
hlipka | 0:ab93db24fcc8 | 54 | if (getSecond()<ts->getSecond()) |
hlipka | 0:ab93db24fcc8 | 55 | return true; |
hlipka | 0:ab93db24fcc8 | 56 | return false; |
hlipka | 0:ab93db24fcc8 | 57 | } |
hlipka | 0:ab93db24fcc8 | 58 | bool TimeStamp::isAfter(TimeStamp* ts) |
hlipka | 0:ab93db24fcc8 | 59 | { |
hlipka | 0:ab93db24fcc8 | 60 | return ts->isBefore(this); |
hlipka | 0:ab93db24fcc8 | 61 | } |
hlipka | 0:ab93db24fcc8 | 62 | |
hlipka | 0:ab93db24fcc8 | 63 | Time::Time() { |
hlipka | 4:c84afcfbac84 | 64 | // printf("init time\n"); |
hlipka | 0:ab93db24fcc8 | 65 | if (NULL==Time::_timeZoneEntries) { |
hlipka | 4:c84afcfbac84 | 66 | // printf("reading time zones\n"); |
hlipka | 0:ab93db24fcc8 | 67 | readTimeZones(); |
hlipka | 0:ab93db24fcc8 | 68 | } |
hlipka | 0:ab93db24fcc8 | 69 | } |
hlipka | 0:ab93db24fcc8 | 70 | |
hlipka | 0:ab93db24fcc8 | 71 | void Time::readTimeZones() { |
hlipka | 0:ab93db24fcc8 | 72 | time_t rawtime; |
hlipka | 0:ab93db24fcc8 | 73 | time ( &rawtime ); |
hlipka | 4:c84afcfbac84 | 74 | TimeStamp *ts=new TimeStamp(rawtime); |
hlipka | 0:ab93db24fcc8 | 75 | |
hlipka | 4:c84afcfbac84 | 76 | int currentYear=ts->getYear(); |
hlipka | 4:c84afcfbac84 | 77 | delete ts; |
hlipka | 0:ab93db24fcc8 | 78 | |
hlipka | 0:ab93db24fcc8 | 79 | FILE *fp = fopen("/local/timezone.csv", "r"); |
hlipka | 0:ab93db24fcc8 | 80 | |
hlipka | 0:ab93db24fcc8 | 81 | if (fp==NULL) { |
hlipka | 0:ab93db24fcc8 | 82 | printf("error while reading timezone file [timezone.csv]\n"); |
hlipka | 0:ab93db24fcc8 | 83 | return; |
hlipka | 0:ab93db24fcc8 | 84 | } |
hlipka | 4:c84afcfbac84 | 85 | |
hlipka | 4:c84afcfbac84 | 86 | TimeZoneEntry *current=NULL; |
hlipka | 0:ab93db24fcc8 | 87 | |
hlipka | 0:ab93db24fcc8 | 88 | |
hlipka | 0:ab93db24fcc8 | 89 | char tmp[128]; // enough for a single line |
hlipka | 0:ab93db24fcc8 | 90 | while (fgets(tmp,sizeof(tmp),fp)!=0) { |
hlipka | 0:ab93db24fcc8 | 91 | int fyear, fmon, fday, fhour, fmin, fsec; |
hlipka | 0:ab93db24fcc8 | 92 | int tyear, tmon, tday, thour, tmin, tsec; |
hlipka | 0:ab93db24fcc8 | 93 | int offset; |
hlipka | 0:ab93db24fcc8 | 94 | int r=sscanf(tmp,"%4d-%2d-%2dT%2d:%2d:%2dZ,%4d-%2d-%2dT%2d:%2d:%2dZ,%d", |
hlipka | 0:ab93db24fcc8 | 95 | &fyear, &fmon, &fday, &fhour, &fmin, &fsec, |
hlipka | 0:ab93db24fcc8 | 96 | &tyear, &tmon, &tday, &thour, &tmin, &tsec, |
hlipka | 0:ab93db24fcc8 | 97 | &offset |
hlipka | 0:ab93db24fcc8 | 98 | ); |
hlipka | 0:ab93db24fcc8 | 99 | if (13!=r) |
hlipka | 0:ab93db24fcc8 | 100 | continue; |
hlipka | 3:2ad51ec5ef6e | 101 | if (tyear<currentYear || fyear>currentYear+4) |
hlipka | 0:ab93db24fcc8 | 102 | continue; |
hlipka | 4:c84afcfbac84 | 103 | |
hlipka | 4:c84afcfbac84 | 104 | TimeStamp *from=new TimeStamp(fyear, fmon, fday, fhour, fmin, fsec,0); |
hlipka | 4:c84afcfbac84 | 105 | TimeStamp *to=new TimeStamp(tyear, tmon, tday, thour, tmin, tsec,0); |
hlipka | 0:ab93db24fcc8 | 106 | TimeZoneEntry *tze=new TimeZoneEntry(); |
hlipka | 0:ab93db24fcc8 | 107 | tze->_from=from; |
hlipka | 0:ab93db24fcc8 | 108 | tze->_to=to; |
hlipka | 0:ab93db24fcc8 | 109 | tze->_offset=offset; |
hlipka | 4:c84afcfbac84 | 110 | tze->next=NULL; |
hlipka | 4:c84afcfbac84 | 111 | |
hlipka | 4:c84afcfbac84 | 112 | if (NULL==current) |
hlipka | 4:c84afcfbac84 | 113 | { |
hlipka | 4:c84afcfbac84 | 114 | current=tze; |
hlipka | 4:c84afcfbac84 | 115 | _timeZoneEntries=tze; |
hlipka | 4:c84afcfbac84 | 116 | } |
hlipka | 4:c84afcfbac84 | 117 | else |
hlipka | 4:c84afcfbac84 | 118 | { |
hlipka | 4:c84afcfbac84 | 119 | current->next=tze; |
hlipka | 4:c84afcfbac84 | 120 | current=tze; |
hlipka | 4:c84afcfbac84 | 121 | } |
hlipka | 0:ab93db24fcc8 | 122 | } |
hlipka | 4:c84afcfbac84 | 123 | printf("closing time zone file\n"); |
hlipka | 0:ab93db24fcc8 | 124 | fclose(fp); |
hlipka | 0:ab93db24fcc8 | 125 | } |
hlipka | 0:ab93db24fcc8 | 126 | |
hlipka | 0:ab93db24fcc8 | 127 | Time::~Time() { |
hlipka | 0:ab93db24fcc8 | 128 | } |
hlipka | 0:ab93db24fcc8 | 129 | |
hlipka | 0:ab93db24fcc8 | 130 | TimeStamp* Time::getTime() { |
hlipka | 0:ab93db24fcc8 | 131 | time_t rawtime; |
hlipka | 0:ab93db24fcc8 | 132 | time ( &rawtime ); |
hlipka | 0:ab93db24fcc8 | 133 | TimeStamp *ts=new TimeStamp(rawtime); |
hlipka | 3:2ad51ec5ef6e | 134 | |
hlipka | 4:c84afcfbac84 | 135 | TimeZoneEntry *current=_timeZoneEntries; |
hlipka | 4:c84afcfbac84 | 136 | |
hlipka | 4:c84afcfbac84 | 137 | while (current!=NULL) |
hlipka | 4:c84afcfbac84 | 138 | { |
hlipka | 4:c84afcfbac84 | 139 | if (current->_from->isBefore(ts) && current->_to->isAfter(ts)) |
hlipka | 0:ab93db24fcc8 | 140 | { |
hlipka | 4:c84afcfbac84 | 141 | rawtime+=current->_offset; |
hlipka | 0:ab93db24fcc8 | 142 | ts->updateTime(rawtime); |
hlipka | 3:2ad51ec5ef6e | 143 | break; |
hlipka | 0:ab93db24fcc8 | 144 | } |
hlipka | 4:c84afcfbac84 | 145 | current=current->next; |
hlipka | 0:ab93db24fcc8 | 146 | } |
hlipka | 0:ab93db24fcc8 | 147 | |
hlipka | 0:ab93db24fcc8 | 148 | return ts; |
hlipka | 0:ab93db24fcc8 | 149 | } |