24 string Date::getMon(
string mmm)
26 StringUtil::toUpper(mmm);
27 if(mmm==
"JAN")
return "01";
28 else if(mmm==
"FEB")
return "02";
29 else if(mmm==
"MAR")
return "03";
30 else if(mmm==
"APR")
return "04";
31 else if(mmm==
"MAY")
return "05";
32 else if(mmm==
"JUN")
return "06";
33 else if(mmm==
"JUL")
return "07";
34 else if(mmm==
"AUG")
return "08";
35 else if(mmm==
"SEP")
return "09";
36 else if(mmm==
"OCT")
return "10";
37 else if(mmm==
"NOV")
return "11";
38 else if(mmm==
"DEC")
return "12";
42 string Date::getMonFd(
string mmm)
44 StringUtil::toUpper(mmm);
45 if(mmm==
"01" || mmm==
"1")
return "JAN";
46 else if(mmm==
"02" || mmm==
"2")
return "FEB";
47 else if(mmm==
"03" || mmm==
"3")
return "MAR";
48 else if(mmm==
"04" || mmm==
"4")
return "APR";
49 else if(mmm==
"05" || mmm==
"5")
return "MAY";
50 else if(mmm==
"06" || mmm==
"6")
return "JUN";
51 else if(mmm==
"07" || mmm==
"7")
return "JUL";
52 else if(mmm==
"08" || mmm==
"8")
return "AUG";
53 else if(mmm==
"09" || mmm==
"9")
return "SEP";
54 else if(mmm==
"10")
return "OCT";
55 else if(mmm==
"11")
return "NOV";
56 else if(mmm==
"12")
return "DEC";
60 int Date::getWeekDayVal(
string dayName)
62 StringUtil::toUpper(dayName);
63 if(dayName==
"SUN" || dayName==
"SUNDAY")
65 else if(dayName==
"MON" || dayName==
"MONDAY")
67 else if(dayName==
"TUE" || dayName==
"TUESDAY")
69 else if(dayName==
"WED" || dayName==
"WEDNESDAY")
71 else if(dayName==
"THU" || dayName==
"THURSDAY")
73 else if(dayName==
"FRI" || dayName==
"FRIDAY")
75 else if(dayName==
"SAT" || dayName==
"SATURDAY")
80 string getHalfDayName(
string dayName)
82 if(dayName.length()>3)
83 return dayName.substr(0,3);
93 timeinfo = localtime(&rawtime);
95 clock_gettime(CLOCK_REALTIME, &en);
96 this->nanoseconds = en.tv_nsec;
98 tem.append(asctime(timeinfo));
99 StringUtil::replaceAll(tem,
"\n",
"");
100 StringUtil::replaceAll(tem,
" ",
" ");
101 vector<string> temp,vemp;
102 StringUtil::split(temp,tem,(
" "));
103 this->dayw = StringUtil::toUpperCopy(temp.at(0));
104 this->monthw = StringUtil::toUpperCopy(temp.at(1));
105 this->month = getMon(monthw);
106 this->day = temp.at(2);
107 StringUtil::split(vemp,temp.at(3),(
":"));
108 this->hh = vemp.at(0);
109 this->mm = vemp.at(1);
110 this->ss = vemp.at(2);
111 this->year = temp.at(4);
112 this->weekday = getWeekDayVal(this->dayw);
113 this->timeZoneOffset = 0;
123 return CastUtil::lexical_cast<
int>(this->month);
125 return CastUtil::lexical_cast<
int>(this->getMon(this->monthw));
128 void Date::setMonth(
int month)
130 string t = (month<=9?
"0":
"");
131 this->month = t + CastUtil::lexical_cast<
string>(month);
132 this->monthw = getMonFd(this->month);
135 string Date::getMonthw()
const
140 void Date::setMonthw(
string monthw)
142 this->monthw = monthw;
145 int Date::getYear()
const
147 return CastUtil::lexical_cast<
int>(year);
150 int Date::getWeekDay()
const
155 void Date::setYear(
int year)
157 string t = (year<=9?
"0":
"");
158 this->year = t + CastUtil::lexical_cast<
string>(year);
161 int Date::getDay()
const
163 return CastUtil::lexical_cast<
int>(day);
166 void Date::setDay(
int day)
168 string t = (day<=9?
"0":
"");
169 this->day = t + CastUtil::lexical_cast<
string>(day);
172 int Date::getHh()
const
174 return CastUtil::lexical_cast<
int>(hh);
177 void Date::setHh(
int hh)
179 string t = (hh<=9?
"0":
"");
180 this->hh = t + CastUtil::lexical_cast<
string>(hh);
183 int Date::getMm()
const
185 return CastUtil::lexical_cast<
int>(mm);
188 void Date::setMm(
int mm)
190 string t = (mm<=9?
"0":
"");
191 this->mm = t + CastUtil::lexical_cast<
string>(mm);
194 int Date::getSs()
const
196 return CastUtil::lexical_cast<
int>(ss);
199 long long Date::getNanoSeconds()
const
204 void Date::setSs(
int ss)
206 string t = (ss<=9?
"0":
"");
207 this->ss = t + CastUtil::lexical_cast<
string>(ss);
210 string Date::getDayw()
const
215 void Date::setDayw(
string dayw)
220 string Date::getDayStr()
const
225 string Date::getMonthStr()
const
230 string Date::getHhStr()
const
235 string Date::getSsStr()
const
240 string Date::getNsStr()
const
242 return CastUtil::lexical_cast<
string>(nanoseconds);
245 string Date::getMmStr()
const
250 string Date::getYearStr()
const
255 void Date::setTimeZoneOffset(
float tzVal)
257 timeZoneOffset = tzVal;
260 float Date::getTimeZoneOffset()
262 return timeZoneOffset;
265 string Date::toString()
267 return dayw+
" "+year+
" "+monthw+
" "+day+
" "+hh+
":"+mm+
":"+ss+
"."+CastUtil::lexical_cast<
string>(nanoseconds)+CastUtil::lexical_cast<string>(timeZoneOffset);
270 Date Date::addSeconds(
long seconds)
272 long minutes = (getSs()+seconds)/60;
273 seconds = (getSs()+seconds)%60;
274 Date d = addMinutes(minutes);
279 Date Date::addMinutes(
long minutes)
281 long hours = (getMm()+minutes)/60;
282 minutes = (getMm()+minutes)%60;
283 Date d = addHours(hours);
288 Date Date::addHours(
long hours)
290 long days = (getHh()+hours)/24;
291 hours = (getHh()+hours)%24;
292 Date d = addDays(days);
297 Date Date::addDays(
long days)
299 long months = getMonth();
300 long year = getYear() + months/12;
302 long g = getDays(year,months,getDay()+days);
303 return getDateFromDays(g);
306 Date Date::addMonths(
long months)
308 months = getMonth() + months;
309 long year = getYear() + months/12;
311 long g = getDays(year,months,getDay());
312 return getDateFromDays(g);
315 Date Date::addYears(
long years)
317 long g = getDays(getYear()+years,getMonth(),getDay());
318 return getDateFromDays(g);
321 long Date::getDaysInt()
329 return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 );
331 long Date::getDays(
long y,
long m,
long d)
336 return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 );
339 long Date::getHours(
long y,
long m,
long d,
long hh)
341 long g = getDays(y,m,d);
344 return (g+hfd)*24 + hh;
347 long Date::getMinutes(
long y,
long m,
long d,
long hh,
long mi)
349 long g = getHours(y,m,d,hh);
352 return (g+mfh)*60 + mi;
355 unsigned long long Date::getSeconds(
long y,
long m,
long d,
long hh,
long mi,
long ss)
357 long g = getMinutes(y,m,d,hh,mi);
360 return (g+sfm)*60 + ss;
363 Date Date::getDateFromDays(
long days)
365 long y = (10000*days + 14780)/3652425;
366 long ddd = days - (365*y + y/4 - y/100 + y/400);
370 ddd = days - (365*y + y/4 - y/100 + y/400);
372 long mi = (100*ddd + 52)/3060;
373 long mm = (mi + 2)%12 + 1;
374 y = y + (mi + 2)/12 + 1900;
375 long dd = ddd - (mi*306 + 5)/10 + 1;
381 d.setDayw(getHalfDayName(getDayName(dd,mm,y)));
385 void Date::getDateFromHours(
long hours)
403 void Date::getDateFromMinutes(
long long minutes)
423 void Date::getDateFromSeconds(
long long seconds)
445 bool Date::validateDate(
int dd,
int mm,
int yyyy)
448 if(dd<1 || dd>31 || yyyy<0)
451 if (mm < 1 || mm > 12)
453 string mw31days =
",1,3,5,7,8,10,12,";
454 string mw30oldays =
",4,6,9,11,";
455 string mtc =
"," + CastUtil::lexical_cast<
string>(mm) +
",";
458 if (!(yyyy % 4) && ((yyyy % 100) || !(yyyy % 400)))
460 if (dd < 1 || dd > 29)
463 else if (dd < 1 || dd >28)
466 else if (mw31days.find(mtc)!=string::npos)
468 if (dd < 1 || dd > 31)
471 else if (mw30oldays.find(mtc)!=string::npos)
473 if (dd < 1 || dd > 30)
480 string Date::getDayName(
int dd,
int mm,
int yyyy)
483 if(!validateDate(dd,mm,yyyy))
return dayName;
484 int days = ((yyyy-1)*365 + (yyyy-1)/4 - (yyyy-1)/100 + (yyyy-1)/400) % 7;
500 if ((!(yyyy % 4) && ((yyyy % 100) || !(yyyy % 400)))&& mm > 2)
505 case 0:dayName =
"SUNDAY";
break;
506 case 1:dayName =
"MONDAY";
break;
507 case 2:dayName =
"TUESDAY";
break;
508 case 3:dayName =
"WEDNESDAY";
break;
509 case 4:dayName =
"THURSDAY";
break;
510 case 5:dayName =
"FRIDAY";
break;
511 case 6:dayName =
"SATURDAY";
break;
516 Date::Date(
int yyyy,
string mmm,
int dd)
518 string mm = getMon(mmm);
519 if(mm==
"-1")
throw "Invalid month";
520 if(!validateDate(dd,CastUtil::lexical_cast<int>(mm),yyyy))
throw "Invalid date";
521 long g = getDays(yyyy,CastUtil::lexical_cast<long>(mm),dd);
522 *
this = getDateFromDays(g);
523 this->nanoseconds = 0;
524 this->timeZoneOffset = 0;
527 Date::Date(
int yyyy,
int mm,
int dd)
529 if(!validateDate(dd,mm,yyyy))
throw "Invalid date";
530 long g = getDays(yyyy,mm,dd);
531 *
this = getDateFromDays(g);
532 this->nanoseconds = 0;
533 this->timeZoneOffset = 0;
536 Date::Date(
int yy,
string mmm,
int dd,
bool te)
539 string syyyy = d.year.substr(0,2) + CastUtil::lexical_cast<
string>(yy);
540 int yyyy = CastUtil::lexical_cast<
int>(syyyy);
541 string mm = getMon(mmm);
542 if(mm==
"-1")
throw "Invalid month";
543 if(!validateDate(dd,CastUtil::lexical_cast<int>(mm),yyyy))
throw "Invalid date";
544 long g = getDays(yyyy,CastUtil::lexical_cast<long>(mm),dd);
545 *
this = getDateFromDays(g);
546 this->nanoseconds = 0;
547 this->timeZoneOffset = 0;
550 Date::Date(
int yy,
int mm,
int dd,
bool te)
553 string syyyy = d.year.substr(0,2) + CastUtil::lexical_cast<
string>(yy);
554 int yyyy = CastUtil::lexical_cast<
int>(syyyy);
555 if(!validateDate(dd,mm,yyyy))
throw "Invalid date";
556 long g = getDays(yyyy,mm,dd);
557 *
this = getDateFromDays(g);
558 this->nanoseconds = 0;
559 this->timeZoneOffset = 0;
562 void Date::setTime(
int hh,
int mi,
int ss)
564 if(hh>24 || hh<0 || mi<0 || mi>60 || ss<0 || ss>60)
565 throw "Invalid Time";
569 this->nanoseconds = 0;
574 unsigned long long gg = getDays(2008,12,31);
576 gg = getHours(2008,12,31,12);
577 getDateFromHours(gg);
578 gg = getMinutes(2008,12,31,12,56);
579 getDateFromMinutes(gg);
580 gg = getSeconds(2008,12,31,12,56,56);
581 getDateFromSeconds(gg);
584 Date d1 = d.addYears(1);
586 Date d2 = d.addMonths(23);
588 Date d3 = d.addDays(17);
590 Date d4 = d.addHours(25);
592 Date d5 = d.addMinutes(61);
594 Date d6 = d.addSeconds(61);