Advertisement
Guest User

date class

a guest
Nov 15th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.54 KB | None | 0 0
  1. #include "Date.h"
  2. Date::Date()
  3. {
  4. dateDelimeter='/';
  5. timeDelimeter=':';
  6. resDate="";
  7. resTime="";
  8. resDay=0;
  9. resMonth=0;
  10. resYear=0;
  11. resMinute=0;
  12. resHour=0;
  13. }
  14. Date::Date(unsigned int day, unsigned int month, unsigned int year, unsigned int minute, unsigned int hour)
  15. {
  16. setResDate(day, month, year);
  17. setResTime(minute, hour, day);// 45 22 20
  18. }
  19. Date::Date(Date &obj)
  20. {
  21. dateDelimeter='/';
  22. timeDelimeter=':';
  23. resDate=obj.getResDate();
  24. resTime=obj.getResTime();
  25. resDay=obj.getResDay();
  26. resMonth=obj.getResMonth();
  27. resYear=obj.getResYear();
  28. resMinute=obj.getResMinute();
  29. resHour=obj.getResHour();
  30. }
  31.  
  32. bool Date::setResDate(unsigned int day, unsigned int month, unsigned int year)
  33. {
  34. if(verifyResDate(day, month, year)==true)
  35. {
  36. resDay=day;
  37. resMonth=month;
  38. resYear=year;
  39. if (day<10)
  40. {
  41. resDate="0"+to_string(resDay);
  42. }
  43. else
  44. {
  45. resDate=to_string(resDay);
  46. }
  47. if (month<10)
  48. {
  49. resDate+="/0"+to_string(resMonth);
  50. }
  51. else
  52. {
  53. resDate+="/"+to_string(resMonth);
  54. }
  55. resDate+="/"+to_string(resYear);
  56. return true;
  57. }
  58. else
  59. {
  60. cout<<"\nDate could be assigned since it is not withing valid range.\n";
  61. return false;
  62. }
  63. }
  64.  
  65. bool Date::setResTime(unsigned int minute,unsigned int hour, unsigned int day)// 45 22 20
  66. {
  67. if(verifyResTime(minute, hour, day)==true)
  68. {
  69. resMinute=minute;
  70. resHour=hour;
  71. resTime=to_string(resHour)+":"+to_string(resMinute);
  72. return true;
  73. }
  74. else
  75. {
  76. cout<<"\nTime could be assigned since it is not withing valid range.\n";
  77. return false;
  78. }
  79. }
  80. bool Date::setResTime(string val)
  81. {
  82. //checkTimeFormat(val);
  83. if(checkTimeFormat(val)!=false && verifyResTime(val)!=false)
  84. {
  85. cout<<"did i make it"<<endl;
  86. resTime=val;
  87. system("PAUSE");
  88. return true;
  89. }
  90. else
  91. {
  92. cout<<"\nTime could be assigned since it is not withing valid range.\n";
  93. return false;
  94. }
  95. }
  96. void Date::setResDirect(string val)
  97. {
  98. resTime=val;
  99. parseResTime();
  100. }
  101. bool Date::setResDate(string val)
  102. {
  103. if(checkDateFormat(val)!=false && verifyResDate(val)!=false)
  104. {
  105. resDate=val;
  106. parseResDate();
  107. return true;
  108. }
  109. else
  110. {
  111. cout<<"\nDate could not be assigned, as it is not within valid range\n";
  112. return false;
  113. }
  114. }
  115.  
  116. bool Date::verifyResDate()
  117. {
  118. time_t curtime=time(NULL);//uses the time_t standard data type to crate the object curt time
  119. struct tm *tmdata=localtime(&curtime);
  120. unsigned int y=tmdata->tm_year+1900;//sets y to the years since the year 1900 + 1900, yielding current year.
  121. unsigned int m=tmdata->tm_mon+1;//sets month to current month, however tm_mon is from 0 to 11, so a one is added to get the proper 12
  122. unsigned int d=tmdata->tm_mday;//returns day of the month
  123. return repetativeComparison(resDay, resMonth, resYear, d, m , y);
  124. }
  125. bool Date::verifyResDate(unsigned int day,unsigned int month,unsigned int year)
  126. {
  127. time_t curtime=time(NULL);//uses the time_t standard data type to crate the object curt time
  128. struct tm *tmdata=localtime(&curtime);
  129. int y=tmdata->tm_year+1900;//sets y to the years since the year 1900 + 1900, yielding current year.
  130. int m=tmdata->tm_mon+1;//sets month to current month, however tm_mon is from 0 to 11, so a one is added to get the proper 12
  131. int d=tmdata->tm_mday;
  132. return repetativeComparison(day, month, year, d, m , y);
  133. }
  134.  
  135. bool Date::verifyResDate(string val)
  136. {
  137. string sday=val.substr(0, val.find(dateDelimeter));
  138. val=val.erase(0, val.find(dateDelimeter)+1);
  139. string smonth=val.substr(0, val.find(dateDelimeter));
  140. val=val.erase(0, val.find(dateDelimeter)+1);
  141. string syear=val;
  142. unsigned int month=atoi(smonth.c_str());
  143. unsigned int day=atoi(sday.c_str());
  144. unsigned int year=atoi(syear.c_str());
  145. time_t curtime=time(NULL);//uses the time_t standard data type to crate the object curt time
  146. struct tm *tmdata=localtime(&curtime);
  147. int y=tmdata->tm_year+1900;//sets y to the years since the year 1900 + 1900, yielding current year.
  148. int m=tmdata->tm_mon+1;//sets month to current month, however tm_mon is from 0 to 11, so a one is added to get the proper 12
  149. int d=tmdata->tm_mday;
  150. return repetativeComparison( day, month, year, d, m , y);
  151. }
  152. //verify res date
  153. //fix verify res time here
  154. bool Date::verifyResTime()
  155. {
  156. time_t curtime=time(NULL);//uses the time_t standard data type to crate the object curt time
  157. struct tm *tmdata=localtime(&curtime);
  158. unsigned short m=tmdata->tm_min;
  159. unsigned short h=tmdata->tm_hour;
  160. unsigned short d=tmdata->tm_mday;
  161. if(dateGreater==true)
  162. {
  163. if(resHour>24 || resHour<0 || resMinute>59 || resMinute<0)
  164. {
  165. return false;
  166. }
  167. else{return true;}
  168. }
  169. else if(resDay==d)
  170. {
  171. if(resHour>24 || resHour<0 || resMinute>59 || resMinute<0)
  172. {
  173. return false;
  174. }
  175. if(resHour>h)
  176. {
  177. return true;
  178. }
  179. else if(resHour==h && resMinute>=m)
  180. {
  181. return true;
  182. }
  183. else
  184. {
  185. return false;
  186. }
  187. }
  188. }
  189. bool Date::verifyResTime(unsigned int minute, unsigned int hour, unsigned int day) //45 22 20
  190. {
  191. time_t curtime=time(NULL);//uses the time_t standard data type to crate the object curt time
  192. struct tm *tmdata=localtime(&curtime);
  193. unsigned short m=tmdata->tm_min;
  194. unsigned short h=tmdata->tm_hour;
  195. unsigned short d=tmdata->tm_mday; //8
  196. if(dateGreater==true||yearGreater==true)
  197. {
  198. if(hour>23 || hour<0 || minute>59 || minute<0)
  199. {
  200. return false;
  201. }
  202. else{return true;}
  203. }
  204. else
  205. {
  206. if(day==d)
  207. {
  208. if(hour>23 || hour<0 || minute>59 || minute<0)
  209. {
  210. return false;
  211. }
  212.  
  213. if(hour>h)
  214. {
  215. return true;
  216. }
  217. else if(hour==h && minute>=m)
  218. {
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. else{
  227. return false;
  228. }
  229. }
  230. }
  231. bool Date::verifyResTime(string val)
  232. {
  233. time_t curtime=time(NULL);//uses the time_t standard data type to crate the object curt time
  234. struct tm *tmdata=localtime(&curtime);
  235. unsigned short mm=tmdata->tm_min;
  236. unsigned short hh=tmdata->tm_hour;
  237. unsigned short dd=tmdata->tm_mday;
  238. unsigned short month=tmdata->tm_mon+1;
  239. unsigned short year=tmdata->tm_year+1900;
  240. cout<<val.at(0)<<endl;
  241. cout<<val.find(timeDelimeter)+1;
  242. string sun=val.substr(0,val.find(timeDelimeter));
  243. unsigned short hour=atoi(sun.c_str());
  244. sun=val.erase(0,(val.find(timeDelimeter))+1);
  245. unsigned short minute = atoi(sun.c_str());
  246. cout<<'\n'<<hour<<" : "<<minute<<endl;
  247. system("PAUSE");
  248. if(hour >23 || hour < 0 || minute > 59 || minute < 0)
  249. {
  250. return false;
  251. system("PAUSE");
  252. }
  253. else if(resYear>year || (resYear==year && resMonth>month) || (resYear==year && resMonth==month && resDay>dd))
  254. {
  255. return true;
  256. system("PAUSE");
  257. }
  258. else if(resDay==dd)
  259. {
  260. if(hour>hh)
  261. {
  262. return true;
  263. }
  264. else if(hour==hh && minute>=mm)
  265. {
  266. return true;
  267. }
  268. else
  269. {
  270. return false;
  271. }
  272. system("PAUSE");
  273. }
  274. else
  275. {
  276. return false;
  277. }
  278. system("PAUSE");
  279. }
  280.  
  281. void Date::parseResDate()
  282. {
  283. unsigned short count=0;
  284. unsigned short pos=0;
  285. string token;
  286. string rd=resDate;
  287. while((pos=rd.find(dateDelimeter)!=string::npos))
  288. {
  289. token=rd.substr(0, pos+1);
  290. rd=rd.erase(0,pos+2);
  291. if(count==0)
  292. {
  293. resDay=atoi(token.c_str());
  294. }
  295. else if(count==1)
  296. {
  297. resMonth=atoi(token.c_str());
  298. }
  299. if(count==1)
  300. {
  301. resYear=atoi(rd.c_str());
  302. }
  303. count++;
  304. }
  305. }
  306. void Date::parseResTime()
  307. {
  308. string rt;
  309. string token;
  310. unsigned short pos=0;
  311. unsigned short count=0;
  312. rt=resTime;
  313. while((pos=rt.find(timeDelimeter))!=string::npos)
  314. {
  315. token=rt.substr(0, pos+1);
  316. rt=rt.erase(0, pos+1);
  317. if(count==0)
  318. {
  319. resHour=atoi(token.c_str());
  320. }
  321. else
  322. {
  323. resMinute=atoi(token.c_str());
  324. }
  325. verifyResTime();
  326. count++;
  327. }
  328. }
  329.  
  330. bool Date::checkDateFormat()
  331. {
  332. if(resDate.length()>=8 && resDate.length()>=10)
  333. {
  334. string sub=resDate.substr(0, resDate.find(dateDelimeter)+1);
  335. if(resDate.find(dateDelimeter)!=2 && resDate.find(dateDelimeter)!=3 && sub.find(dateDelimeter)!=2 && sub.find(dateDelimeter)!=3)
  336. {
  337. return true;
  338. }
  339. else
  340. {
  341. return false;
  342. }
  343. }
  344. else{return false;}
  345. }
  346. bool Date::checkTimeFormat()
  347. {
  348. if(resTime.length()<=5 && resTime.length()>=4 && (resTime.find(timeDelimeter)==2||resTime.find(timeDelimeter)==3))
  349. {
  350. return true;
  351. }
  352. else{return false;}
  353. }
  354.  
  355. bool Date::checkDateFormat(string val)
  356. {
  357. if(val.length()>=8 && val.length()<=10)
  358. {
  359. string sub=val.substr(val.find(dateDelimeter)+1, (val.length()-(val.find(dateDelimeter)+1)));
  360. if((val.find(dateDelimeter)==2 || val.find(dateDelimeter)==3) && (sub.find(dateDelimeter)==1 || sub.find(dateDelimeter)==2))
  361. {
  362. return true;
  363. }
  364. else
  365. {
  366. return false;
  367. }
  368. }
  369. else{return false;}
  370. }
  371. bool Date::checkTimeFormat(string val)
  372. {
  373. if(val.length()<=5 && val.length()>=4 && val.find(timeDelimeter))
  374. {
  375. return true;
  376. }
  377. else
  378. {
  379. return false;
  380. }
  381. }
  382. unsigned int Date::getResDay() { return resDay; }
  383. unsigned int Date::getResMonth() { return resMonth; }
  384. unsigned int Date::getResYear() { return resYear; }
  385. unsigned int Date::getResHour() { return resHour; }
  386. unsigned int Date::getResMinute() { return resMinute; }
  387. string Date::getResDate() { return resDate; }
  388. string Date::getResTime() { return resTime; }
  389. string Date::getResTime12()
  390. {
  391. if(resHour>12)
  392. {
  393. string resHour12=to_string((resHour-12));
  394. string newTime=resHour12+":"+to_string(resMinute)+" PM";
  395. return newTime;
  396. }
  397. else
  398. {
  399. return resTime+" AM";
  400. }
  401. }
  402.  
  403. bool Date::repetativeComparison(int day, int month, int year, int d, int m , int y)
  404. {
  405. if(year>y && year<=y+1)//RESERVATIONS CAN BE MADE ATMOST ONE YEAR IN ADVANCE
  406. {
  407. yearGreater=true;
  408. dateGreater=true;
  409. return true;
  410. }
  411. else if(y>year)
  412. {
  413. yearGreater=false;
  414. return false;
  415. }
  416. else
  417. {
  418. yearGreater=false;
  419. }
  420. if(year==y && month<m)
  421. {
  422. return false;
  423. }
  424. else if(year==y && month==m && day<d)
  425. {
  426. return false;
  427. }
  428. if(month>12 || month<1)
  429. {
  430. return false;
  431. }
  432. if (yearGreater==true)//THIS IS TO ENSURE THAT IF THE DESIRED YEAR IS GREATER THAN THE SELECTED YEAR, THEY CAN PICK ANY DATE
  433. {
  434. if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&& (day<=31&&day>=1) )//These conditions are based on the amount of days in a specific month
  435. {
  436. return true;
  437. }
  438. else if((month==2)&&(day<=28&&day>=1))
  439. {
  440. return true;
  441. }
  442. else if((year%4==0)&&(month==2)&&(day<=29 && day>=1))
  443. {
  444. return true;
  445. }//fix the thing here
  446. else if((month==4||month==6||month==9||month==11)&&(day<=30&&day>=1))
  447. {
  448. return true;
  449. }
  450. else{return false;}
  451. }
  452. else{
  453. if(year==y && month>=m)
  454. {
  455. if(month>m || (month==m && day>d) || (month==m && day==d))
  456. {
  457. dateGreater=true;
  458. return true;
  459. }
  460. else
  461. {
  462. dateGreater=false;
  463. return true;
  464. }
  465. if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&& (day<=31&&day>=1) )//These conditions are based on the amount of days in a specific month
  466. {
  467. return true;
  468. }
  469. else if((month==2)&&(day<=28&&day>=1))
  470. {
  471. return true;
  472. }
  473. else if((year%4==0)&&(month==2)&&(day<=29 && day>=1))
  474. {
  475. return true;
  476. }
  477. else if((month==4||month==6||month==9||month==11)&&(day<=30&&day>=1))
  478. {
  479. return true;
  480. }
  481. else{return false;}
  482. }
  483. else
  484. {
  485. return false;
  486. }
  487. }
  488. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement