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::string returnval="";
\r
40 char *str=new char[512];
\r
43 strftime(str,511,formatstring.c_str(),&m_tm);
\r
54 void DateTime::Normalize()
\r
56 // check for tm_isdst in local time and take appropriate action when normalizing
\r
57 // thanks to http://www.erack.de/download/timetest.c for example
\r
61 temptimet=time(NULL);
\r
62 temptm=*localtime(&temptimet);
\r
63 isdst=temptm.tm_isdst;
\r
65 temptm.tm_year=m_tm.tm_year;
\r
66 temptm.tm_mon=m_tm.tm_mon;
\r
67 temptm.tm_mday=m_tm.tm_mday;
\r
68 temptm.tm_hour=m_tm.tm_hour;
\r
69 temptm.tm_min=m_tm.tm_min;
\r
70 temptm.tm_sec=m_tm.tm_sec;
\r
71 temptm.tm_isdst=isdst;
\r
72 temptimet=mktime(&temptm);
\r
74 if(temptm.tm_isdst!=isdst)
\r
76 // keep tm_isdst to whatever mktime returned and try again
\r
77 temptm.tm_year=m_tm.tm_year;
\r
78 temptm.tm_mon=m_tm.tm_mon;
\r
79 temptm.tm_mday=m_tm.tm_mday;
\r
80 temptm.tm_hour=m_tm.tm_hour;
\r
81 temptm.tm_min=m_tm.tm_min;
\r
82 temptm.tm_sec=m_tm.tm_sec;
\r
83 temptimet=mktime(&temptm);
\r
85 else if(isdst && temptimet==-1)
\r
87 // isdst set, but TZ has no offset (e.g. GMT), try with isdst=0
\r
88 temptm.tm_year=m_tm.tm_year;
\r
89 temptm.tm_mon=m_tm.tm_mon;
\r
90 temptm.tm_mday=m_tm.tm_mday;
\r
91 temptm.tm_hour=m_tm.tm_hour;
\r
92 temptm.tm_min=m_tm.tm_min;
\r
93 temptm.tm_sec=m_tm.tm_sec;
\r
95 temptimet=mktime(&temptm);
\r
101 // date is erroneous - set to default date
\r
102 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
109 DateTime DateTime::operator+(const double &rhs)
\r
111 DateTime temp=*this;
\r
117 int hours=(int)val;
\r
120 int minutes=(int)val;
\r
123 int seconds=(int)val;
\r
125 temp.Add(seconds,minutes,hours,days);
\r
129 DateTime DateTime::operator+(const DateTime &rhs)
\r
131 DateTime temp=*this;
\r
133 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
137 DateTime &DateTime::operator+=(const double &rhs)
\r
144 DateTime &DateTime::operator+=(const DateTime &rhs)
\r
151 DateTime DateTime::operator-(const double &rhs)
\r
153 DateTime temp=*this;
\r
159 int hours=(int)val;
\r
162 int minutes=(int)val;
\r
165 int seconds=(int)val;
\r
167 temp.Add(-seconds,-minutes,-hours,-days);
\r
171 DateTime DateTime::operator-(const DateTime &rhs)
\r
173 DateTime temp=*this;
\r
175 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
179 DateTime &DateTime::operator-=(const double &rhs)
\r
186 DateTime &DateTime::operator-=(const DateTime &rhs)
\r
193 const bool DateTime::operator==(const struct tm &rhs) const
\r
195 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
198 void DateTime::Set(const int year, const int month, const int day, const int hour, const int minute, const int second)
\r
200 m_tm.tm_year=year-1900;
\r
201 m_tm.tm_mon=month-1;
\r
204 m_tm.tm_min=minute;
\r
205 m_tm.tm_sec=second;
\r
210 void DateTime::Set(const time_t &timet)
\r
214 m_tm=*gmtime(&m_timet);
\r
218 void DateTime::Set(const struct tm *stm)
\r
221 m_timet=mktime(&m_tm);
\r
225 void DateTime::Set(const std::string &datestring)
\r
227 int year,month,day,hour,minute,second;
\r
228 std::vector<std::string> tokens;
\r
232 year=month=day=hour=minute=second=-1;
\r
234 // reset to 1900-01-01 00:00:00
\r
237 StringFunctions::SplitMultiple(datestring,"-/\\., :",tokens);
\r
239 // loop through 1st time to try to find 4 digit year and month (if it is a text month)
\r
241 for(std::vector<std::string>::iterator i=tokens.begin(); i!=tokens.end(); i++,vecpos++)
\r
243 StringFunctions::UpperCase((*i),(*i));
\r
245 if((*i).find("JAN")==0)
\r
250 if((*i).find("FEB")==0)
\r
255 if((*i).find("MAR")==0)
\r
260 if((*i).find("APR")==0)
\r
265 if((*i).find("MAY")==0)
\r
270 if((*i).find("JUN")==0)
\r
275 if((*i).find("JUL")==0)
\r
280 if((*i).find("AUG")==0)
\r
285 if((*i).find("SEP")==0)
\r
290 if((*i).find("OCT")==0)
\r
295 if((*i).find("NOV")==0)
\r
300 if((*i).find("DEC")==0)
\r
306 // if we just got month - day is probaby in the next position
\r
307 if(month==vecpos && vecpos+1<tokens.size() && tokens[vecpos+1].size()>0)
\r
310 StringFunctions::Convert(tokens[vecpos+1],tempint);
\r
311 if(tempint>0 && tempint<32)
\r
318 // if this is not month or day, and the size is 4 then it is probably the year
\r
319 if(month!=vecpos && day!=vecpos && (*i).size()==4)
\r
322 StringFunctions::Convert((*i),tempint);
\r
329 // month is probably right after year
\r
330 if(year!=-1 && month==-1 && year+1<tokens.size())
\r
333 StringFunctions::Convert(tokens[year+1],tempint);
\r
334 if(tempint>=1 && tempint<=12)
\r
341 // otherwise it is probably 2 steps back (m/d/y)
\r
342 if(year!=-1 && month==-1 && year-2>=0)
\r
345 StringFunctions::Convert(tokens[year-2],tempint);
\r
346 if(tempint>=1 && tempint<=12)
\r
353 // day is probably right after month
\r
354 if(month!=-1 && month+1<tokens.size())
\r
357 StringFunctions::Convert(tokens[month+1],tempint);
\r
358 if(tempint>=1 && tempint<32)
\r
365 // loop through another time to find hour
\r
367 for(std::vector<std::string>::iterator i=tokens.begin(); i!=tokens.end(); i++,vecpos++)
\r
369 if(vecpos!=year && vecpos!=month && vecpos!=day && hour==-1)
\r
372 StringFunctions::Convert((*i),tempint);
\r
373 if(tempint>=0 && tempint<24)
\r
381 // minute right after hour
\r
382 if(hour!=-1 && hour+1<tokens.size())
\r
385 StringFunctions::Convert(tokens[hour+1],tempint);
\r
386 if(tempint>=0 && tempint<60)
\r
388 SetMinute(tempint);
\r
393 //second right after minute
\r
394 if(minute!=-1 && minute+1<tokens.size())
\r
397 StringFunctions::Convert(tokens[minute+1],tempint);
\r
398 if(tempint>=0 && tempint<60)
\r
400 SetSecond(tempint);
\r
407 void DateTime::SetToGMTime()
\r
409 m_timet=time(NULL);
\r
411 m_tm=*gmtime(&m_timet);
\r
415 void DateTime::SetToLocalTime()
\r
417 m_timet=time(NULL);
\r
418 m_tm=*localtime(&m_timet);
\r