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
Diff: Time.cpp
- Revision:
- 4:c84afcfbac84
- Parent:
- 3:2ad51ec5ef6e
- Child:
- 5:fde01b92a384
--- a/Time.cpp Tue Jan 04 23:19:50 2011 +0000 +++ b/Time.cpp Fri Jan 21 23:05:05 2011 +0000 @@ -10,9 +10,10 @@ TimeStamp *_from; TimeStamp *_to; int _offset; + TimeZoneEntry* next; }; -list<TimeZoneEntry*> *Time::_timeZoneEntries=NULL; +TimeZoneEntry* Time::_timeZoneEntries=NULL; bool TimeStamp::isSame(TimeStamp* ts) { @@ -60,8 +61,9 @@ } Time::Time() { +// printf("init time\n"); if (NULL==Time::_timeZoneEntries) { - _timeZoneEntries=new list<TimeZoneEntry*>(); +// printf("reading time zones\n"); readTimeZones(); } } @@ -69,9 +71,10 @@ void Time::readTimeZones() { time_t rawtime; time ( &rawtime ); - TimeStamp ts(rawtime); + TimeStamp *ts=new TimeStamp(rawtime); - int currentYear=ts.getYear(); + int currentYear=ts->getYear(); + delete ts; FILE *fp = fopen("/local/timezone.csv", "r"); @@ -79,6 +82,8 @@ printf("error while reading timezone file [timezone.csv]\n"); return; } + + TimeZoneEntry *current=NULL; char tmp[128]; // enough for a single line @@ -95,16 +100,27 @@ continue; if (tyear<currentYear || fyear>currentYear+4) continue; - - TimeStamp *from=new TimeStamp(fyear, fmon, fday, fhour, fmin, fsec); - TimeStamp *to=new TimeStamp(tyear, tmon, tday, thour, tmin, tsec); + + TimeStamp *from=new TimeStamp(fyear, fmon, fday, fhour, fmin, fsec,0); + TimeStamp *to=new TimeStamp(tyear, tmon, tday, thour, tmin, tsec,0); TimeZoneEntry *tze=new TimeZoneEntry(); tze->_from=from; tze->_to=to; tze->_offset=offset; - _timeZoneEntries->push_back(tze); + tze->next=NULL; + + if (NULL==current) + { + current=tze; + _timeZoneEntries=tze; + } + else + { + current->next=tze; + current=tze; + } } - + printf("closing time zone file\n"); fclose(fp); } @@ -116,14 +132,17 @@ time ( &rawtime ); TimeStamp *ts=new TimeStamp(rawtime); - for (list<TimeZoneEntry*>::iterator it = _timeZoneEntries->begin(); it != _timeZoneEntries->end(); it++) { - TimeZoneEntry* tze=*it; - if (tze->_from->isBefore(ts) && tze->_to->isAfter(ts)) + TimeZoneEntry *current=_timeZoneEntries; + + while (current!=NULL) + { + if (current->_from->isBefore(ts) && current->_to->isAfter(ts)) { - rawtime+=tze->_offset; + rawtime+=current->_offset; ts->updateTime(rawtime); break; } + current=current->next; } return ts;