Brandon Fictorie / Mbed 2 deprecated BF_Websocket

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Time.cpp Source File

Time.cpp

00001 #include "Time.h"
00002 
00003 #include "stdio.h"
00004 // #include "time.h"
00005 
00006 using namespace std;
00007 
00008 class TimeZoneEntry {
00009     public:
00010     TimeStamp *_from;
00011     TimeStamp *_to;
00012     int _offset;
00013 };
00014 
00015 list<TimeZoneEntry*> *Time::_timeZoneEntries=NULL;
00016 
00017 bool TimeStamp::isSame (TimeStamp* ts)
00018 {
00019     if (ts->getYear ()!=getYear())
00020         return false;
00021     if (ts->getMonth ()!=getMonth())
00022         return false;
00023     if (ts->getDay ()!=getDay())
00024         return false;
00025     if (ts->getHour ()!=getHour())
00026         return false;
00027     if (ts->getSecond ()!=getSecond())
00028         return false;
00029     return true;
00030 }
00031 bool TimeStamp::isBefore (TimeStamp* ts)
00032 {
00033     if (getYear()<ts->getYear ())
00034         return true;
00035     if (getYear()>ts->getYear ())
00036         return false;
00037 
00038     if (getMonth()<ts->getMonth ())
00039         return true;
00040     if (getMonth()>ts->getMonth ())
00041         return false;
00042 
00043     if (getDay()<ts->getDay ())
00044         return true;
00045     if (getDay()>ts->getDay ())
00046         return false;
00047 
00048     if (getHour()<ts->getHour ())
00049         return true;
00050     if (getHour()>ts->getHour ())
00051         return false;
00052 
00053     if (getSecond()<ts->getSecond ())
00054         return true;
00055     return false;        
00056 }
00057 bool TimeStamp::isAfter (TimeStamp* ts)
00058 {
00059     return ts->isBefore (this);        
00060 }
00061 
00062 Time::Time() {
00063     if (NULL==Time::_timeZoneEntries) {
00064         _timeZoneEntries=new list<TimeZoneEntry*>();
00065         readTimeZones();
00066     }
00067 }
00068 
00069 void Time::readTimeZones() {
00070     time_t rawtime;
00071     time ( &rawtime );
00072     TimeStamp ts(rawtime);
00073 
00074     int currentYear=ts.getYear();
00075 
00076     FILE *fp = fopen("/local/Edmonton.csv", "r");
00077 
00078     if (fp==NULL) {
00079         printf("error while reading timezone file\n");
00080         return;
00081     }
00082 
00083 
00084     char tmp[128]; // enough for a single line
00085     while (fgets(tmp,sizeof(tmp),fp)!=0) {
00086         int fyear, fmon, fday, fhour, fmin, fsec;
00087         int tyear, tmon, tday, thour, tmin, tsec;
00088         int offset;
00089         int r=sscanf(tmp,"%4d-%2d-%2dT%2d:%2d:%2dZ,%4d-%2d-%2dT%2d:%2d:%2dZ,%d",
00090                      &fyear, &fmon, &fday, &fhour, &fmin, &fsec,
00091                      &tyear, &tmon, &tday, &thour, &tmin, &tsec,
00092                      &offset
00093                     );
00094         if (13!=r)
00095             continue;
00096         if (fyear<currentYear || tyear>currentYear+5)
00097             continue;
00098         
00099         TimeStamp *from=new TimeStamp(fyear, fmon, fday, fhour, fmin, fsec);
00100         TimeStamp *to=new TimeStamp(tyear, tmon, tday, thour, tmin, tsec);
00101         TimeZoneEntry *tze=new TimeZoneEntry();
00102         tze->_from=from;
00103         tze->_to=to;
00104         tze->_offset=offset;
00105         _timeZoneEntries->push_back(tze);
00106     }
00107 
00108     fclose(fp);
00109 }
00110 
00111 Time::~Time() {
00112 }
00113 
00114 TimeStamp* Time::getTime() {
00115     time_t rawtime;
00116     time ( &rawtime );
00117     TimeStamp *ts=new TimeStamp(rawtime);
00118 
00119     for (list<TimeZoneEntry*>::iterator it = _timeZoneEntries->begin(); it != _timeZoneEntries->end(); it++) {
00120         TimeZoneEntry* tze=*it;
00121         if (tze->_from->isBefore(ts) && tze->_to->isAfter(ts))
00122         {
00123             rawtime+=tze->_offset;
00124             ts->updateTime(rawtime);
00125         }
00126     }
00127     
00128     return ts;
00129 }