ffead.server.doc
Date.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 /*
17  * Date.cpp
18  *
19  * Created on: Jun 4, 2010
20  * Author: sumeet
21  */
22 #include "Date.h"
23 
24 string Date::getMon(string mmm)
25 {
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";
39  else return "-1";
40 }
41 
42 string Date::getMonFd(string mmm)
43 {
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";
57  else return "-1";
58 }
59 
60 int Date::getWeekDayVal(string dayName)
61 {
62  StringUtil::toUpper(dayName);
63  if(dayName=="SUN" || dayName=="SUNDAY")
64  return 7;
65  else if(dayName=="MON" || dayName=="MONDAY")
66  return 1;
67  else if(dayName=="TUE" || dayName=="TUESDAY")
68  return 2;
69  else if(dayName=="WED" || dayName=="WEDNESDAY")
70  return 3;
71  else if(dayName=="THU" || dayName=="THURSDAY")
72  return 4;
73  else if(dayName=="FRI" || dayName=="FRIDAY")
74  return 5;
75  else if(dayName=="SAT" || dayName=="SATURDAY")
76  return 6;
77  else return -1;
78 }
79 
80 string getHalfDayName(string dayName)
81 {
82  if(dayName.length()>3)
83  return dayName.substr(0,3);
84  return dayName;
85 }
86 
87 Date::Date()
88 {
89  //logger = //logger::get//logger("Date");
90  time_t rawtime;
91  struct tm * timeinfo;
92  time (&rawtime);
93  timeinfo = localtime(&rawtime);
94  timespec en;
95  clock_gettime(CLOCK_REALTIME, &en);
96  this->nanoseconds = en.tv_nsec;
97  string tem;
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;
114 }
115 
116 Date::~Date() {
117  // TODO Auto-generated destructor stub
118 }
119 
120 int Date::getMonth()
121 {
122  if(this->month!="")
123  return CastUtil::lexical_cast<int>(this->month);
124  else
125  return CastUtil::lexical_cast<int>(this->getMon(this->monthw));
126 }
127 
128 void Date::setMonth(int month)
129 {
130  string t = (month<=9?"0":"");
131  this->month = t + CastUtil::lexical_cast<string>(month);
132  this->monthw = getMonFd(this->month);
133 }
134 
135 string Date::getMonthw() const
136 {
137  return monthw;
138 }
139 
140 void Date::setMonthw(string monthw)
141 {
142  this->monthw = monthw;
143 }
144 
145 int Date::getYear() const
146 {
147  return CastUtil::lexical_cast<int>(year);
148 }
149 
150 int Date::getWeekDay() const
151 {
152  return weekday;
153 }
154 
155 void Date::setYear(int year)
156 {
157  string t = (year<=9?"0":"");
158  this->year = t + CastUtil::lexical_cast<string>(year);
159 }
160 
161 int Date::getDay() const
162 {
163  return CastUtil::lexical_cast<int>(day);
164 }
165 
166 void Date::setDay(int day)
167 {
168  string t = (day<=9?"0":"");
169  this->day = t + CastUtil::lexical_cast<string>(day);
170 }
171 
172 int Date::getHh() const
173 {
174  return CastUtil::lexical_cast<int>(hh);
175 }
176 
177 void Date::setHh(int hh)
178 {
179  string t = (hh<=9?"0":"");
180  this->hh = t + CastUtil::lexical_cast<string>(hh);
181 }
182 
183 int Date::getMm() const
184 {
185  return CastUtil::lexical_cast<int>(mm);
186 }
187 
188 void Date::setMm(int mm)
189 {
190  string t = (mm<=9?"0":"");
191  this->mm = t + CastUtil::lexical_cast<string>(mm);
192 }
193 
194 int Date::getSs() const
195 {
196  return CastUtil::lexical_cast<int>(ss);
197 }
198 
199 long long Date::getNanoSeconds() const
200 {
201  return nanoseconds;
202 }
203 
204 void Date::setSs(int ss)
205 {
206  string t = (ss<=9?"0":"");
207  this->ss = t + CastUtil::lexical_cast<string>(ss);
208 }
209 
210 string Date::getDayw() const
211 {
212  return dayw;
213 }
214 
215 void Date::setDayw(string dayw)
216 {
217  this->dayw = dayw;
218 }
219 
220 string Date::getDayStr() const
221 {
222  return day;
223 }
224 
225 string Date::getMonthStr() const
226 {
227  return month;
228 }
229 
230 string Date::getHhStr() const
231 {
232  return hh;
233 }
234 
235 string Date::getSsStr() const
236 {
237  return ss;
238 }
239 
240 string Date::getNsStr() const
241 {
242  return CastUtil::lexical_cast<string>(nanoseconds);
243 }
244 
245 string Date::getMmStr() const
246 {
247  return mm;
248 }
249 
250 string Date::getYearStr() const
251 {
252  return year;
253 }
254 
255 void Date::setTimeZoneOffset(float tzVal)
256 {
257  timeZoneOffset = tzVal;
258 }
259 
260 float Date::getTimeZoneOffset()
261 {
262  return timeZoneOffset;
263 }
264 
265 string Date::toString()
266 {
267  return dayw+" "+year+" "+monthw+" "+day+" "+hh+":"+mm+":"+ss+"."+CastUtil::lexical_cast<string>(nanoseconds)+CastUtil::lexical_cast<string>(timeZoneOffset);
268 }
269 
270 Date Date::addSeconds(long seconds)
271 {
272  long minutes = (getSs()+seconds)/60;
273  seconds = (getSs()+seconds)%60;
274  Date d = addMinutes(minutes);
275  d.setSs(seconds);
276  return d;
277 }
278 
279 Date Date::addMinutes(long minutes)
280 {
281  long hours = (getMm()+minutes)/60;
282  minutes = (getMm()+minutes)%60;
283  Date d = addHours(hours);
284  d.setMm(minutes);
285  return d;
286 }
287 
288 Date Date::addHours(long hours)
289 {
290  long days = (getHh()+hours)/24;
291  hours = (getHh()+hours)%24;
292  Date d = addDays(days);
293  d.setHh(hours);
294  return d;
295 }
296 
297 Date Date::addDays(long days)
298 {
299  long months = getMonth();
300  long year = getYear() + months/12;
301  months = months%12;
302  long g = getDays(year,months,getDay()+days);
303  return getDateFromDays(g);
304 }
305 
306 Date Date::addMonths(long months)
307 {
308  months = getMonth() + months;
309  long year = getYear() + months/12;
310  months = months%12;
311  long g = getDays(year,months,getDay());
312  return getDateFromDays(g);
313 }
314 
315 Date Date::addYears(long years)
316 {
317  long g = getDays(getYear()+years,getMonth(),getDay());
318  return getDateFromDays(g);
319 }
320 
321 long Date::getDaysInt()
322 {
323  long y = getYear();
324  long m = getMonth();
325  long d = getDay();
326  y = y -1900;
327  m = (m + 9) % 12;
328  y = y - m/10;
329  return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 );
330 }
331 long Date::getDays(long y,long m,long d)
332 {
333  y = y -1900;
334  m = (m + 9) % 12;
335  y = y - m/10;
336  return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 );
337 }
338 
339 long Date::getHours(long y,long m,long d, long hh)
340 {
341  long g = getDays(y,m,d);
342  int hfd = hh/24;
343  hh = hh%24;
344  return (g+hfd)*24 + hh;
345 }
346 
347 long Date::getMinutes(long y,long m,long d, long hh, long mi)
348 {
349  long g = getHours(y,m,d,hh);
350  int mfh = mi/60;
351  mi = mi%60;
352  return (g+mfh)*60 + mi;
353 }
354 
355 unsigned long long Date::getSeconds(long y,long m,long d, long hh, long mi, long ss)
356 {
357  long g = getMinutes(y,m,d,hh,mi);
358  int sfm = ss/60;
359  ss = ss%60;
360  return (g+sfm)*60 + ss;
361 }
362 
363 Date Date::getDateFromDays(long days)
364 {
365  long y = (10000*days + 14780)/3652425;
366  long ddd = days - (365*y + y/4 - y/100 + y/400);
367  if (ddd < 0)
368  {
369  y = y - 1;
370  ddd = days - (365*y + y/4 - y/100 + y/400);
371  }
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;
377  Date d;
378  d.setDay(dd);
379  d.setMonth(mm);
380  d.setYear(y);
381  d.setDayw(getHalfDayName(getDayName(dd,mm,y)));
382  return d;
383 }
384 
385 void Date::getDateFromHours(long hours)
386 {
387  /*long days = hours/24;
388  hours = hours%24;
389  long y = (10000*days + 14780)/3652425;
390  long ddd = days - (365*y + y/4 - y/100 + y/400);
391  if (ddd < 0)
392  {
393  y = y - 1;
394  ddd = days - (365*y + y/4 - y/100 + y/400);
395  }
396  long mi = (100*ddd + 52)/3060;
397  long mm = (mi + 2)%12 + 1;
398  y = y + (mi + 2)/12 + 1900;
399  long dd = ddd - (mi*306 + 5)/10 + 1;*/
401 }
402 
403 void Date::getDateFromMinutes(long long minutes)
404 {
405  /*long hours = minutes/60;
406  minutes = minutes%60;
407  long days = hours/24;
408  hours = hours%24;
409  long y = (10000*days + 14780)/3652425;
410  long ddd = days - (365*y + y/4 - y/100 + y/400);
411  if (ddd < 0)
412  {
413  y = y - 1;
414  ddd = days - (365*y + y/4 - y/100 + y/400);
415  }
416  long mi = (100*ddd + 52)/3060;
417  long mm = (mi + 2)%12 + 1;
418  y = y + (mi + 2)/12 + 1900;
419  long dd = ddd - (mi*306 + 5)/10 + 1;*/
421 }
422 
423 void Date::getDateFromSeconds(long long seconds)
424 {
425  /*long long minutes = seconds/60;
426  seconds = seconds%60;
427  long hours = minutes/60;
428  minutes = minutes%60;
429  long days = hours/24;
430  hours = hours%24;
431  long y = (10000*days + 14780)/3652425;
432  long ddd = days - (365*y + y/4 - y/100 + y/400);
433  if (ddd < 0)
434  {
435  y = y - 1;
436  ddd = days - (365*y + y/4 - y/100 + y/400);
437  }
438  long mi = (100*ddd + 52)/3060;
439  long mm = (mi + 2)%12 + 1;
440  y = y + (mi + 2)/12 + 1900;
441  long dd = ddd - (mi*306 + 5)/10 + 1;*/
443 }
444 
445 bool Date::validateDate(int dd, int mm, int yyyy)
446 {
447  bool error = true;
448  if(dd<1 || dd>31 || yyyy<0)
449  error = false;
450 
451  if (mm < 1 || mm > 12)
452  error = false;
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) + ",";
456  if (mm==2)
457  {
458  if (!(yyyy % 4) && ((yyyy % 100) || !(yyyy % 400)))
459  {
460  if (dd < 1 || dd > 29)
461  error = false;
462  }
463  else if (dd < 1 || dd >28)
464  error = false;
465  }
466  else if (mw31days.find(mtc)!=string::npos)
467  {
468  if (dd < 1 || dd > 31)
469  error = false;
470  }
471  else if (mw30oldays.find(mtc)!=string::npos)
472  {
473  if (dd < 1 || dd > 30)
474  error = false;
475  }
476  return error;
477 }
478 
479 //Based on program by Stanley Wong
480 string Date::getDayName(int dd, int mm, int yyyy)
481 {
482  string dayName;
483  if(!validateDate(dd,mm,yyyy))return dayName;
484  int days = ((yyyy-1)*365 + (yyyy-1)/4 - (yyyy-1)/100 + (yyyy-1)/400) % 7;//Daycode for prev year 31st Dec
485  switch(mm)
486  {
487  case 12:dd += 30;
488  case 11:dd += 31;
489  case 10:dd += 30;
490  case 9:dd += 31;
491  case 8:dd += 31;
492  case 7:dd += 30;
493  case 6:dd += 31;
494  case 5:dd += 30;
495  case 4:dd += 31;
496  case 3:dd += 28;
497  case 2:dd += 31;
498  }
499  days += dd;
500  if ((!(yyyy % 4) && ((yyyy % 100) || !(yyyy % 400)))&& mm > 2)
501  days++;
502  days = days%7;
503  switch(days)
504  {
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;
512  }
513  return dayName;
514 }
515 
516 Date::Date(int yyyy,string mmm,int dd)
517 {
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;
525 }
526 
527 Date::Date(int yyyy,int mm,int dd)
528 {
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;
534 }
535 
536 Date::Date(int yy,string mmm,int dd,bool te)
537 {
538  Date d;
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;
548 }
549 
550 Date::Date(int yy,int mm,int dd,bool te)
551 {
552  Date d;
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;
560 }
561 
562 void Date::setTime(int hh,int mi,int ss)
563 {
564  if(hh>24 || hh<0 || mi<0 || mi>60 || ss<0 || ss>60)
565  throw "Invalid Time";
566  setHh(hh);
567  setMm(mi);
568  setSs(ss);
569  this->nanoseconds = 0;
570 }
571 
572 int Date::test()
573 {
574  unsigned long long gg = getDays(2008,12,31);
575  getDateFromDays(gg);
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);
582  Date d;
583  //logger << d.toString() << endl;
584  Date d1 = d.addYears(1);
585  //logger << d1.toString() << endl;
586  Date d2 = d.addMonths(23);
587  //logger << d2.toString() << endl;
588  Date d3 = d.addDays(17);
589  //logger << d3.toString() << endl;
590  Date d4 = d.addHours(25);
591  //logger << d4.toString() << endl;
592  Date d5 = d.addMinutes(61);
593  //logger << d5.toString() << endl;
594  Date d6 = d.addSeconds(61);
595  //logger << d6.toString() << endl;
596  return 0;
597 }