1 #include "../include/datetime.h"
\r
4 #include "../include/stringfunctions.h"
\r
10 DateTime::DateTime()
\r
15 DateTime::DateTime(const time_t &timet)
\r
20 DateTime::DateTime(const struct tm *stm)
\r
25 void DateTime::Add(const int seconds, const int minutes, const int hours, const int days, const int months, const int years)
\r
27 m_tm.tm_sec+=seconds;
\r
28 m_tm.tm_min+=minutes;
\r
29 m_tm.tm_hour+=hours;
\r
31 m_tm.tm_mon+=months;
\r
32 m_tm.tm_year+=years;
\r
37 std::string DateTime::Format(const std::string &formatstring) const
\r
39 std::vector<char> str(256,0);
\r
41 size_t len=strftime(&str[0],str.size()-1,formatstring.c_str(),&m_tm);
\r
43 return std::string(str.begin(),str.begin()+len);
\r
46 void DateTime::Normalize()
\r
48 // check for tm_isdst in local time and take appropriate action when normalizing
\r
49 // thanks to http://www.erack.de/download/timetest.c for example
\r
53 temptimet=time(NULL);
\r
54 temptm=*localtime(&temptimet);
\r
55 isdst=temptm.tm_isdst;
\r
57 temptm.tm_year=m_tm.tm_year;
\r
58 temptm.tm_mon=m_tm.tm_mon;
\r
59 temptm.tm_mday=m_tm.tm_mday;
\r
60 temptm.tm_hour=m_tm.tm_hour;
\r
61 temptm.tm_min=m_tm.tm_min;
\r
62 temptm.tm_sec=m_tm.tm_sec;
\r
63 temptm.tm_isdst=isdst;
\r
64 temptimet=mktime(&temptm);
\r
66 if(temptm.tm_isdst!=isdst)
\r
68 // keep tm_isdst to whatever mktime returned and try again
\r
69 temptm.tm_year=m_tm.tm_year;
\r
70 temptm.tm_mon=m_tm.tm_mon;
\r
71 temptm.tm_mday=m_tm.tm_mday;
\r
72 temptm.tm_hour=m_tm.tm_hour;
\r
73 temptm.tm_min=m_tm.tm_min;
\r
74 temptm.tm_sec=m_tm.tm_sec;
\r
75 temptimet=mktime(&temptm);
\r
77 else if(isdst && temptimet==-1)
\r
79 // isdst set, but TZ has no offset (e.g. GMT), try with isdst=0
\r
80 temptm.tm_year=m_tm.tm_year;
\r
81 temptm.tm_mon=m_tm.tm_mon;
\r
82 temptm.tm_mday=m_tm.tm_mday;
\r
83 temptm.tm_hour=m_tm.tm_hour;
\r
84 temptm.tm_min=m_tm.tm_min;
\r
85 temptm.tm_sec=m_tm.tm_sec;
\r
87 temptimet=mktime(&temptm);
\r
93 // date is erroneous - set to default date
\r
94 if(m_timet==-1 && (m_tm.tm_mon<0 || m_tm.tm_mon>11 || m_tm.tm_mday <1 || m_tm.tm_mday>31 || m_tm.tm_hour<0 || m_tm.tm_hour>23 || m_tm.tm_min<0 || m_tm.tm_min>59 || m_tm.tm_sec<0 || m_tm.tm_sec>59))
\r
101 DateTime DateTime::operator+(const double &rhs)
\r
103 DateTime temp=*this;
\r
109 int hours=(int)val;
\r
112 int minutes=(int)val;
\r
115 int seconds=(int)val;
\r
117 temp.Add(seconds,minutes,hours,days);
\r
121 DateTime DateTime::operator+(const DateTime &rhs)
\r
123 DateTime temp=*this;
\r
125 temp.Add(rhs.m_tm.tm_sec,rhs.m_tm.tm_min,rhs.m_tm.tm_hour,rhs.m_tm.tm_mday,rhs.m_tm.tm_mon+1,rhs.m_tm.tm_year+1900);
\r
129 DateTime &DateTime::operator+=(const double &rhs)
\r
136 DateTime &DateTime::operator+=(const DateTime &rhs)
\r
143 DateTime DateTime::operator-(const double &rhs)
\r
145 DateTime temp=*this;
\r
151 int hours=(int)val;
\r
154 int minutes=(int)val;
\r
157 int seconds=(int)val;
\r
159 temp.Add(-seconds,-minutes,-hours,-days);
\r
163 DateTime DateTime::operator-(const DateTime &rhs)
\r
165 DateTime temp=*this;
\r
167 temp.Add(-rhs.m_tm.tm_sec,-rhs.m_tm.tm_min,-rhs.m_tm.tm_hour,-rhs.m_tm.tm_mday,-(rhs.m_tm.tm_mon+1),-(rhs.m_tm.tm_year+1900));
\r
171 DateTime &DateTime::operator-=(const double &rhs)
\r
178 DateTime &DateTime::operator-=(const DateTime &rhs)
\r
185 const bool DateTime::operator==(const struct tm &rhs) const
\r
187 return (m_tm.tm_year==rhs.tm_year && m_tm.tm_mon==rhs.tm_mon && m_tm.tm_mday==rhs.tm_mday && m_tm.tm_hour==rhs.tm_hour && m_tm.tm_min==rhs.tm_min && m_tm.tm_sec==rhs.tm_sec) ? true : false;
\r
190 void DateTime::Set(const int year, const int month, const int day, const int hour, const int minute, const int second)
\r
192 m_tm.tm_year=year-1900;
\r
193 m_tm.tm_mon=month-1;
\r
196 m_tm.tm_min=minute;
\r
197 m_tm.tm_sec=second;
\r
202 void DateTime::Set(const time_t &timet)
\r
206 m_tm=*gmtime(&m_timet);
\r
210 void DateTime::Set(const struct tm *stm)
\r
213 m_timet=mktime(&m_tm);
\r
217 void DateTime::Set(const std::string &datestring)
\r
219 int year,month,day,hour,minute,second;
\r
220 std::vector<std::string> tokens;
\r
224 year=month=day=hour=minute=second=-1;
\r
226 // reset to 1900-01-01 00:00:00
\r
229 StringFunctions::SplitMultiple(datestring,"-/\\., :",tokens);
\r
231 // loop through 1st time to try to find 4 digit year and month (if it is a text month)
\r
233 for(std::vector<std::string>::iterator i=tokens.begin(); i!=tokens.end(); i++,vecpos++)
\r
235 StringFunctions::UpperCase((*i),(*i));
\r
237 if((*i).find("JAN")==0)
\r
242 if((*i).find("FEB")==0)
\r
247 if((*i).find("MAR")==0)
\r
252 if((*i).find("APR")==0)
\r
257 if((*i).find("MAY")==0)
\r
262 if((*i).find("JUN")==0)
\r
267 if((*i).find("JUL")==0)
\r
272 if((*i).find("AUG")==0)
\r
277 if((*i).find("SEP")==0)
\r
282 if((*i).find("OCT")==0)
\r
287 if((*i).find("NOV")==0)
\r
292 if((*i).find("DEC")==0)
\r
298 // if we just got month - day is probaby in the next position
\r
299 if(month==vecpos && vecpos+1<tokens.size() && tokens[vecpos+1].size()>0)
\r
302 StringFunctions::Convert(tokens[vecpos+1],tempint);
\r
303 if(tempint>0 && tempint<32)
\r
310 // if this is not month or day, and the size is 4 then it is probably the year
\r
311 if(month!=vecpos && day!=vecpos && (*i).size()==4)
\r
314 StringFunctions::Convert((*i),tempint);
\r
321 // month is probably right after year
\r
322 if(year!=-1 && month==-1 && year+1<tokens.size())
\r
325 StringFunctions::Convert(tokens[year+1],tempint);
\r
326 if(tempint>=1 && tempint<=12)
\r
333 // otherwise it is probably 2 steps back (m/d/y)
\r
334 if(year!=-1 && month==-1 && year-2>=0)
\r
337 StringFunctions::Convert(tokens[year-2],tempint);
\r
338 if(tempint>=1 && tempint<=12)
\r
345 // day is probably right after month
\r
346 if(month!=-1 && month+1<tokens.size())
\r
349 StringFunctions::Convert(tokens[month+1],tempint);
\r
350 if(tempint>=1 && tempint<32)
\r
357 // loop through another time to find hour
\r
359 for(std::vector<std::string>::iterator i=tokens.begin(); i!=tokens.end(); i++,vecpos++)
\r
361 if(vecpos!=year && vecpos!=month && vecpos!=day && hour==-1)
\r
364 StringFunctions::Convert((*i),tempint);
\r
365 if(tempint>=0 && tempint<24)
\r
373 // minute right after hour
\r
374 if(hour!=-1 && hour+1<tokens.size())
\r
377 StringFunctions::Convert(tokens[hour+1],tempint);
\r
378 if(tempint>=0 && tempint<60)
\r
380 SetMinute(tempint);
\r
385 //second right after minute
\r
386 if(minute!=-1 && minute+1<tokens.size())
\r
389 StringFunctions::Convert(tokens[minute+1],tempint);
\r
390 if(tempint>=0 && tempint<60)
\r
392 SetSecond(tempint);
\r
399 void DateTime::SetToGMTime()
\r
401 m_timet=time(NULL);
\r
403 m_tm=*gmtime(&m_timet);
\r
407 void DateTime::SetToLocalTime()
\r
409 m_timet=time(NULL);
\r
410 m_tm=*localtime(&m_timet);
\r