Geoff_Montee

DateTime.cpp

Oct 25th, 2012
2,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.16 KB | None | 0 0
  1.  
  2. #include <DateTime.h>
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. /**********************************************************************
  11. * DateTimeDuration
  12. ***********************************************************************/
  13.  
  14. DateTimeDuration::DateTimeDuration(unsigned int _days, unsigned int _hours, unsigned int _minutes,
  15.     unsigned int _seconds, unsigned int _milliseconds, bool _is_negative)
  16. {
  17.     setDateTimeDuration(_days, _hours, _minutes, _seconds, _milliseconds, _is_negative);
  18. }
  19.  
  20. DateTimeDuration::DateTimeDuration(const DateTimeDuration& dur)
  21. {
  22.     setDateTimeDuration(dur.days(), dur.hours(), dur.minutes(), dur.seconds(), dur.milliseconds(), dur.isNegative());
  23. }
  24.  
  25. int DateTimeDuration::compare(const DateTimeDuration& rhs) const
  26. {
  27.     if (isZero() && rhs.isZero())
  28.     {
  29.         return 0;
  30.     }
  31.    
  32.     if (days() == rhs.days())
  33.     {
  34.         if (hours() == rhs.hours())
  35.         {
  36.             if (minutes() == rhs.minutes())
  37.             {
  38.                 if (seconds() == rhs.seconds())
  39.                 {
  40.                     if (milliseconds() == rhs.milliseconds())
  41.                     {
  42.                         return 0;
  43.                     }
  44.                     else if (milliseconds() < rhs.milliseconds())
  45.                     {
  46.                         return -1;
  47.                     }
  48.                     else
  49.                     {
  50.                         return 1;
  51.                     }
  52.                 }
  53.                 else if (seconds() < rhs.seconds())
  54.                 {
  55.                     return -1;
  56.                 }
  57.                 else
  58.                 {
  59.                     return 1;
  60.                 }
  61.             }
  62.             else if (minutes() < rhs.minutes())
  63.             {
  64.                 return -1;
  65.             }
  66.             else
  67.             {
  68.                 return 1;
  69.             }
  70.         }
  71.         else if (hours() < rhs.hours())
  72.         {
  73.             return -1;
  74.         }
  75.         else
  76.         {
  77.             return 1;
  78.         }
  79.     }
  80.     else if (days() < rhs.days())
  81.     {
  82.         return -1;
  83.     }
  84.     else
  85.     {
  86.         return 1;
  87.     }
  88. }
  89.  
  90. DateTimeDuration& DateTimeDuration::operator=(const DateTimeDuration& rhs)
  91. {
  92.     setDateTimeDuration(rhs.days(), rhs.hours(), rhs.minutes(), rhs.seconds(), rhs.milliseconds(), rhs.isNegative());
  93.    
  94.     return *this;
  95. }
  96.  
  97. bool DateTimeDuration::operator==(const DateTimeDuration& rhs) const
  98. {
  99.     int compare_val = compare(rhs);
  100.    
  101.     if (compare_val == 0)
  102.     {
  103.         return true;
  104.     }
  105.    
  106.     return false;
  107. }
  108.  
  109. bool DateTimeDuration::operator!=(const DateTimeDuration& rhs) const
  110. {
  111.     int compare_val = compare(rhs);
  112.    
  113.     if (compare_val != 0)
  114.     {
  115.         return true;
  116.     }
  117.    
  118.     return false;
  119. }
  120.  
  121. bool DateTimeDuration::operator<(const DateTimeDuration& rhs) const
  122. {
  123.     int compare_val = compare(rhs);
  124.    
  125.     if (compare_val < 0)
  126.     {
  127.         return true;
  128.     }
  129.    
  130.     return false;
  131. }
  132.  
  133. bool DateTimeDuration::operator<=(const DateTimeDuration& rhs) const
  134. {
  135.     int compare_val = compare(rhs);
  136.    
  137.     if (compare_val <= 0)
  138.     {
  139.         return true;
  140.     }
  141.    
  142.     return false;
  143. }
  144.  
  145. bool DateTimeDuration::operator>(const DateTimeDuration& rhs) const
  146. {
  147.     int compare_val = compare(rhs);
  148.    
  149.     if (compare_val > 0)
  150.     {
  151.         return true;
  152.     }
  153.    
  154.     return false;
  155. }
  156.  
  157. bool DateTimeDuration::operator>=(const DateTimeDuration& rhs) const
  158. {
  159.     int compare_val = compare(rhs);
  160.    
  161.     if (compare_val >= 0)
  162.     {
  163.         return true;
  164.     }
  165.    
  166.     return false;
  167. }
  168.  
  169. void DateTimeDuration::setDateTimeDuration(unsigned int _days, unsigned int _hours, unsigned int _minutes,
  170.     unsigned int _seconds, unsigned int _milliseconds, bool _is_negative)
  171. {
  172.     isNegative(_is_negative);
  173.     days(_days);
  174.     hours(_hours);
  175.     minutes(_minutes);
  176.     seconds(_seconds);
  177.     milliseconds(_milliseconds);
  178. }
  179.  
  180. bool DateTimeDuration::isZero() const
  181. {
  182.     if (days() == 0
  183.         && hours() == 0
  184.         && minutes() == 0
  185.         && seconds() == 0
  186.         && milliseconds() == 0)
  187.     {
  188.         return true;
  189.     }
  190.    
  191.     return false;
  192. }
  193.  
  194. unsigned int DateTimeDuration::days() const
  195. {
  196.     return __days;
  197. }
  198.  
  199. unsigned int DateTimeDuration::days(unsigned int _days)
  200. {  
  201.     __days = _days;
  202.    
  203.     return __days;
  204. }      
  205.  
  206. unsigned int DateTimeDuration::hours() const
  207. {
  208.     return __hours;
  209. }
  210.  
  211. unsigned int DateTimeDuration::hours(unsigned int _hours)
  212. {
  213.     while (_hours >= 24)
  214.     {
  215.         unsigned int cur_days = days();
  216.         days(++cur_days);
  217.         _hours -= 24;
  218.     }
  219.    
  220.     __hours = _hours;
  221.    
  222.     return __hours;
  223. }
  224.  
  225. unsigned int DateTimeDuration::minutes() const
  226. {
  227.     return __minutes;
  228. }
  229.  
  230. unsigned int DateTimeDuration::minutes(unsigned int _minutes)
  231. {
  232.     while (_minutes >= 60)
  233.     {
  234.         unsigned int cur_hours = hours();
  235.         hours(++cur_hours);
  236.         _minutes -= 60;
  237.     }
  238.    
  239.     __minutes = _minutes;
  240.    
  241.     return __minutes;
  242. }
  243.  
  244. unsigned int DateTimeDuration::seconds() const
  245. {
  246.     return __seconds;
  247. }
  248.  
  249. unsigned int DateTimeDuration::seconds(unsigned int _seconds)
  250. {
  251.     while (_seconds >= 60)
  252.     {
  253.         unsigned int cur_mins = minutes();
  254.         minutes(++cur_mins);
  255.         _seconds -= 60;
  256.     }
  257.    
  258.     __seconds = _seconds;
  259.    
  260.     return __seconds;
  261. }
  262.  
  263. unsigned int DateTimeDuration::milliseconds() const
  264. {
  265.     return __milliseconds;
  266. }
  267.  
  268. unsigned int DateTimeDuration::milliseconds(unsigned int _milliseconds)
  269. {
  270.     while (_milliseconds >= 1000)
  271.     {
  272.         unsigned int cur_secs = seconds();
  273.         seconds(++cur_secs);
  274.         _milliseconds -= 1000;
  275.     }
  276.  
  277.     __milliseconds = _milliseconds;
  278.    
  279.     return __milliseconds;
  280. }
  281.  
  282. bool DateTimeDuration::isNegative() const
  283. {
  284.     return __is_negative;
  285. }
  286.  
  287. bool DateTimeDuration::isNegative(bool _is_negative)
  288. {
  289.     __is_negative = _is_negative;
  290.    
  291.     return __is_negative;
  292. }
  293.  
  294. ostream& operator<<(ostream& os, const DateTimeDuration& dtd)
  295. {
  296.     if (dtd.isNegative())
  297.     {
  298.         os << "negative ";
  299.     }
  300.    
  301.     os << dtd.days() << " days, " << dtd.hours() << " hours, " << dtd.minutes() << " minutes, "
  302.         << dtd.seconds() << " seconds, " << dtd.milliseconds() << " milliseconds";
  303.        
  304.     return os;
  305. }
  306.  
  307. /**********************************************************************
  308. * DateTime
  309. ***********************************************************************/
  310.  
  311. const unsigned short DateTime::MONTH_LENGTHS[] =
  312.     {31,28,31,30,31,30,31,31,30,31,30,31};
  313.  
  314. DateTime::DateTime(unsigned int _year, unsigned int _month, unsigned int _day, unsigned int _hour,
  315.     unsigned int _minute, unsigned int _second, unsigned int _millisecond)
  316. {
  317.     setDateTime(_year, _month, _day, _hour, _minute, _second, _millisecond);
  318. }
  319.  
  320. DateTime::DateTime(const DateTime& dt)
  321. {
  322.     setDateTime(dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute(), dt.second(), dt.millisecond());
  323. }
  324.  
  325. DateTime::DateTime(const tm& time_struct)
  326. {
  327.     setDateTime(time_struct.tm_year + 1900, time_struct.tm_mon + 1, time_struct.tm_mday, time_struct.tm_hour, time_struct.tm_min, time_struct.tm_sec, 0);
  328. }
  329.  
  330. DateTime::DateTime(const time_t& time)
  331. {
  332.     tm* time_struct = localtime(&time);
  333.    
  334.     setDateTime(time_struct->tm_year + 1900, time_struct->tm_mon + 1, time_struct->tm_mday, time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec, 0);
  335. }
  336.  
  337. int DateTime::compare(const DateTime& rhs) const
  338. {
  339.     if (year() == rhs.year())
  340.     {  
  341.         if (month() == rhs.month())
  342.         {
  343.             if (day() == rhs.day())
  344.             {
  345.                 if (hour() == rhs.hour())
  346.                 {
  347.                     if (minute() == rhs.minute())
  348.                     {
  349.                         if (second() == rhs.second())
  350.                         {
  351.                             if (millisecond() == rhs.millisecond())
  352.                             {
  353.                                 return 0;
  354.                             }
  355.                            
  356.                             else if (millisecond() < rhs.millisecond())
  357.                             {
  358.                                 return -1;
  359.                             }
  360.                            
  361.                             else
  362.                             {
  363.                                 return 1;
  364.                             }
  365.                         }
  366.                        
  367.                         else if (second() < rhs.second())
  368.                         {
  369.                             return -1;
  370.                         }
  371.                        
  372.                         else
  373.                         {
  374.                             return 1;
  375.                         }
  376.                     }
  377.                    
  378.                     else if (minute() < rhs.minute())
  379.                     {
  380.                         return -1;
  381.                     }
  382.                    
  383.                     else
  384.                     {
  385.                         return 1;
  386.                     }
  387.                 }
  388.                
  389.                 else if (hour() < rhs.hour())
  390.                 {
  391.                     return -1;
  392.                 }
  393.                
  394.                 else
  395.                 {
  396.                     return 1;
  397.                 }
  398.             }
  399.            
  400.             else if (day() < rhs.day())
  401.             {
  402.                 return -1;
  403.             }
  404.            
  405.             else
  406.             {
  407.                 return 1;
  408.             }
  409.         }
  410.        
  411.         else if (month() < rhs.month())
  412.         {
  413.             return -1;
  414.         }
  415.        
  416.         else
  417.         {
  418.             return 1;
  419.         }
  420.     }
  421.    
  422.     else if (year() < rhs.year())
  423.     {
  424.         return -1;
  425.     }
  426.    
  427.     else
  428.     {
  429.         return 1;
  430.     }
  431. }
  432.  
  433. DateTime& DateTime::operator=(const DateTime& rhs)
  434. {
  435.     setDateTime(rhs.year(), rhs.month(), rhs.day(), rhs.hour(), rhs.minute(), rhs.second(), rhs.millisecond());
  436.    
  437.     return *this;
  438. }
  439.  
  440. bool DateTime::operator==(const DateTime& rhs) const
  441. {
  442.     int compare_val = compare(rhs);
  443.    
  444.     if (compare_val == 0)
  445.     {
  446.         return true;
  447.     }
  448.    
  449.     return false;
  450. }
  451.  
  452. bool DateTime::operator!=(const DateTime& rhs) const
  453. {
  454.     int compare_val = compare(rhs);
  455.    
  456.     if (compare_val != 0)
  457.     {
  458.         return true;
  459.     }
  460.    
  461.     return false;
  462. }
  463.  
  464. bool DateTime::operator<(const DateTime& rhs) const
  465. {
  466.     int compare_val = compare(rhs);
  467.    
  468.     if (compare_val < 0)
  469.     {
  470.         return true;
  471.     }
  472.    
  473.     return false;
  474. }
  475.  
  476. bool DateTime::operator<=(const DateTime& rhs) const
  477. {
  478.     int compare_val = compare(rhs);
  479.    
  480.     if (compare_val <= 0)
  481.     {
  482.         return true;
  483.     }
  484.    
  485.     return false;
  486. }
  487.  
  488. bool DateTime::operator>(const DateTime& rhs) const
  489. {
  490.     int compare_val = compare(rhs);
  491.    
  492.     if (compare_val > 0)
  493.     {
  494.         return true;
  495.     }
  496.    
  497.     return false;
  498. }
  499.  
  500. bool DateTime::operator>=(const DateTime& rhs) const
  501. {
  502.     int compare_val = compare(rhs);
  503.    
  504.     if (compare_val >= 0)
  505.     {
  506.         return true;
  507.     }
  508.    
  509.     return false;
  510. }
  511.  
  512. void DateTime::setDate(unsigned int _year, unsigned int _month, unsigned int _day)
  513. {
  514.     if (!validDate(_year, _month, _day))
  515.     {
  516.         throw InvalidDateTimeException(_year, _month, _day, hour(), minute(), second(), millisecond());
  517.     }
  518.    
  519.     __year = _year;
  520.     __month = _month;
  521.     __day = _day;
  522. }
  523.  
  524. void DateTime::setTime(unsigned int _hour, unsigned int _minute, unsigned int _second, unsigned int _millisecond)
  525. {
  526.     if (_hour >= 24 || _minute >= 60 || _second >= 60 || _millisecond >= 1000)
  527.     {
  528.         throw InvalidDateTimeException(year(), month(), day(), _hour, _minute, _second, _millisecond);
  529.     }
  530.    
  531.     __hour = _hour;
  532.     __minute = _minute;
  533.     __second = _second;
  534.     __millisecond = _millisecond;
  535. }
  536.  
  537. void DateTime::setDateTime(unsigned int _year, unsigned int _month, unsigned int _day, unsigned int _hour, unsigned int _minute, unsigned int _second, unsigned int _millisecond)
  538. {
  539.     if (!validDate(_year, _month, _day) || _hour >= 24 || _minute >= 60 || _second >= 60 || _millisecond >= 1000)
  540.     {
  541.         throw InvalidDateTimeException(_year, _month, _day, _hour, _minute, _second, _millisecond);
  542.     }
  543.    
  544.     __year = _year;
  545.     __month = _month;
  546.     __day = _day;
  547.     __hour = _hour;
  548.     __minute = _minute;
  549.     __second = _second;
  550.     __millisecond = _millisecond;  
  551. }
  552.  
  553. unsigned int DateTime::year() const
  554. {
  555.     return __year;
  556. }
  557.  
  558. unsigned int DateTime::year(unsigned int _year)
  559. {
  560.     if (_year == 0)
  561.     {
  562.         throw InvalidDateTimeException(_year, month(), day(), hour(), minute(), second(), millisecond());  
  563.     }
  564.     __year = _year;
  565.    
  566.     if (!validDate(_year, month(), day()))
  567.     {
  568.         __month = DEFAULT_MONTH;
  569.         __day = DEFAULT_DAY;
  570.     }
  571.    
  572.     return __year;
  573. }
  574.  
  575. unsigned int DateTime::month() const
  576. {
  577.     return __month;
  578. }
  579.  
  580. unsigned int DateTime::month(unsigned int _month)
  581. {
  582.     if (_month == 0 || _month > 12)
  583.     {
  584.         throw InvalidDateTimeException(year(), _month, day(), hour(), minute(), second(), millisecond());  
  585.     }
  586.    
  587.     __month = _month;
  588.    
  589.     unsigned short days_in_month = daysInMonth(year(), _month);
  590.    
  591.     if (day() > days_in_month)
  592.     {
  593.         __day = DEFAULT_DAY;
  594.     }
  595.    
  596.     return __month;
  597. }
  598.  
  599. unsigned int DateTime::day() const
  600. {
  601.     return __day;
  602. }
  603.  
  604. unsigned int DateTime::day(unsigned int _day)
  605. {
  606.     unsigned short days_in_month = daysInMonth(year(), month());
  607.    
  608.     if (_day == 0 || _day > days_in_month)
  609.     {
  610.         throw InvalidDateTimeException(year(), month(), _day, hour(), minute(), second(), millisecond());  
  611.     }
  612.    
  613.     __day = _day;
  614.    
  615.     return __day;
  616. }      
  617.  
  618. unsigned int DateTime::dayOfYear() const
  619. {
  620.     return getDayOfYear(year(), month(), day());
  621. }
  622.  
  623. unsigned int DateTime::dayOfYear(unsigned int _dayOfYear)
  624. {
  625.     DateTime newDate = getReverseDayOfYear(year(), _dayOfYear);
  626.    
  627.     month(newDate.month());
  628.     day(newDate.day());
  629.    
  630.     return _dayOfYear;
  631. }
  632.  
  633. unsigned int DateTime::hour() const
  634. {
  635.     return __hour;
  636. }
  637.  
  638. unsigned int DateTime::hour(unsigned int _hour)
  639. {
  640.     if (_hour >= 24)
  641.     {
  642.         throw InvalidDateTimeException(year(), month(), day(), _hour, minute(), second(), millisecond());  
  643.     }
  644.    
  645.     __hour = _hour;
  646.    
  647.     return __hour;
  648. }
  649.  
  650. unsigned int DateTime::minute() const
  651. {
  652.     return __minute;
  653. }
  654.  
  655. unsigned int DateTime::minute(unsigned int _minute)
  656. {
  657.     if (_minute >= 60)
  658.     {
  659.         throw InvalidDateTimeException(year(), month(), day(), hour(), _minute, second(), millisecond());  
  660.     }
  661.    
  662.     __minute = _minute;
  663.    
  664.     return __minute;
  665. }
  666.  
  667. unsigned int DateTime::second() const
  668. {
  669.     return __second;
  670. }
  671.  
  672. unsigned int DateTime::second(unsigned int _second)
  673. {
  674.     if (_second >= 60)
  675.     {
  676.         throw InvalidDateTimeException(year(), month(), day(), hour(), minute(), _second, millisecond());  
  677.     }
  678.    
  679.     __second = _second;
  680.    
  681.     return __second;
  682. }
  683.  
  684. unsigned int DateTime::millisecond() const
  685. {
  686.     return __millisecond;
  687. }
  688.  
  689. unsigned int DateTime::millisecond(unsigned int _millisecond)
  690. {
  691.     if (_millisecond >= 1000)
  692.     {
  693.         throw InvalidDateTimeException(year(), month(), day(), hour(), minute(), second(), _millisecond);
  694.     }
  695.  
  696.     __millisecond = _millisecond;
  697.    
  698.     return __millisecond;
  699. }
  700.  
  701. DateTime DateTime::subtractDays(unsigned int _days) const
  702. {
  703.     DateTime tmp = *this;
  704.     unsigned int _year = year();
  705.     unsigned int _dayOfYear = dayOfYear();
  706.    
  707.     while (_days > 0)
  708.     {
  709.         if (_dayOfYear > _days)
  710.         {
  711.             _dayOfYear -= _days;
  712.             _days = 0;
  713.         }
  714.        
  715.         else
  716.         {
  717.             _days -= _dayOfYear;
  718.             _year--;
  719.             _dayOfYear = daysInYear(_year);
  720.         }
  721.     }
  722.    
  723.     tmp.year(_year);
  724.     tmp.dayOfYear(_dayOfYear);
  725.    
  726.     return tmp;
  727. }
  728.  
  729. DateTime DateTime::addDays(unsigned int _days) const
  730. {
  731.     DateTime tmp = *this;
  732.     unsigned int _year = year();
  733.     unsigned int _daysInYear = daysInYear(_year);
  734.     unsigned int _dayOfYear = dayOfYear() + _days;
  735.    
  736.     while (_dayOfYear > _daysInYear)
  737.     {
  738.         _dayOfYear -= _daysInYear;
  739.         _year++;
  740.         _daysInYear = daysInYear(_year);
  741.     }
  742.    
  743.     tmp.year(_year);
  744.     tmp.dayOfYear(_dayOfYear);
  745.    
  746.     return tmp;
  747. }
  748.  
  749. DateTime DateTime::subtractHours(unsigned int _hours) const
  750. {
  751.     DateTime tmp = *this;
  752.     unsigned int currentH = hour();
  753.    
  754.     unsigned int subD = _hours / 24;
  755.     unsigned int subH = _hours % 24;
  756.     unsigned int newH;
  757.    
  758.     if (subH > currentH)
  759.     {
  760.         subD++;
  761.         newH = 24 - (subH - currentH);
  762.     }
  763.    
  764.     else
  765.     {
  766.         newH = currentH - subH;
  767.     }
  768.    
  769.     if (subD > 0)
  770.     {
  771.         tmp = tmp.subtractDays(subD);
  772.     }
  773.    
  774.     tmp.hour(newH);
  775.    
  776.     return tmp;
  777. }
  778.  
  779. DateTime DateTime::addHours(unsigned int _hours) const
  780. {
  781.     DateTime tmp = *this;
  782.     unsigned int currentH = hour();
  783.    
  784.     unsigned int addD = _hours / 24;
  785.     unsigned int addH = _hours % 24;
  786.     unsigned int newH;
  787.    
  788.     if ((addH + currentH) >= 24)
  789.     {
  790.         addD++;
  791.         newH = (addH + currentH) % 24;
  792.     }
  793.    
  794.     else
  795.     {
  796.         newH = addH + currentH;
  797.     }
  798.    
  799.     if (addD > 0)
  800.     {
  801.         tmp = tmp.addDays(addD);
  802.     }
  803.    
  804.     tmp.hour(newH);
  805.    
  806.     return tmp;
  807. }
  808.  
  809. DateTime DateTime::subtractMinutes(unsigned int _minutes) const
  810. {
  811.     DateTime tmp = *this;
  812.     unsigned int currentM = minute();
  813.    
  814.     unsigned int subH = _minutes / 60;
  815.     unsigned int subM = _minutes % 60;
  816.     unsigned int newM;
  817.    
  818.     if (subM > currentM)
  819.     {
  820.         subH++;
  821.         newM = 60 - (subM - currentM);
  822.     }
  823.    
  824.     else
  825.     {
  826.         newM = currentM - subM;
  827.     }
  828.    
  829.     if (subH > 0)
  830.     {
  831.         tmp = tmp.subtractHours(subH);
  832.     }
  833.    
  834.     tmp.minute(newM);
  835.    
  836.     return tmp;
  837. }
  838.  
  839. DateTime DateTime::addMinutes(unsigned int _minutes) const
  840. {
  841.     DateTime tmp = *this;
  842.     unsigned int currentM = minute();
  843.    
  844.     unsigned int addH = _minutes / 60;
  845.     unsigned int addM = _minutes % 60;
  846.     unsigned int newM;
  847.    
  848.     if ((addM + currentM) >= 60)
  849.     {
  850.         addH++;
  851.         newM = (addM + currentM) % 60;
  852.     }
  853.    
  854.     else
  855.     {
  856.         newM = addM + currentM;
  857.     }
  858.    
  859.     if (addH > 0)
  860.     {
  861.         tmp = tmp.addHours(addH);
  862.     }
  863.    
  864.     tmp.minute(newM);
  865.    
  866.     return tmp;
  867. }
  868.  
  869. DateTime DateTime::subtractSeconds(unsigned int _seconds) const
  870. {
  871.     DateTime tmp = *this;
  872.     unsigned int currentS = second();
  873.    
  874.     unsigned int subM = _seconds / 60;
  875.     unsigned int subS = _seconds % 60;
  876.     unsigned int newS;
  877.    
  878.     if (subS > currentS)
  879.     {
  880.         subM++;
  881.         newS = 60 - (subS - currentS);
  882.     }
  883.    
  884.     else
  885.     {
  886.         newS = currentS - subS;
  887.     }
  888.    
  889.     if (subM > 0)
  890.     {
  891.         tmp = tmp.subtractMinutes(subM);
  892.     }
  893.    
  894.     tmp.second(newS);
  895.    
  896.     return tmp;
  897. }
  898.  
  899. DateTime DateTime::addSeconds(unsigned int _seconds) const
  900. {
  901.     DateTime tmp = *this;
  902.     unsigned int currentS = second();
  903.    
  904.     unsigned int addM = _seconds / 60;
  905.     unsigned int addS = _seconds % 60;
  906.     unsigned int newS;
  907.    
  908.     if ((addS + currentS) >= 60)
  909.     {
  910.         addM++;
  911.         newS = (addS + currentS) % 60;
  912.     }
  913.    
  914.     else
  915.     {
  916.         newS = addS + currentS;
  917.     }
  918.    
  919.     if (addM > 0)
  920.     {
  921.         tmp = tmp.addMinutes(addM);
  922.     }
  923.    
  924.     tmp.second(newS);
  925.    
  926.     return tmp;
  927. }
  928.  
  929. DateTime DateTime::subtractMilliseconds(unsigned int _milliseconds) const
  930. {
  931.     DateTime tmp = *this;
  932.     unsigned int currentMs = millisecond();
  933.    
  934.     unsigned int subS = _milliseconds / 1000;
  935.     unsigned int subMs = _milliseconds % 1000;
  936.     unsigned int newMs;
  937.    
  938.     if (subMs > currentMs)
  939.     {
  940.         subS++;
  941.         newMs = 1000 - (subMs - currentMs);
  942.     }
  943.    
  944.     else
  945.     {
  946.         newMs = currentMs - subMs;
  947.     }
  948.    
  949.     if (subS > 0)
  950.     {
  951.         tmp = tmp.subtractSeconds(subS);
  952.     }
  953.    
  954.     tmp.millisecond(newMs);
  955.    
  956.     return tmp;
  957. }
  958.  
  959. DateTime DateTime::addMilliseconds(unsigned int _milliseconds) const
  960. {
  961.     DateTime tmp = *this;
  962.     unsigned int currentMs = millisecond();
  963.    
  964.     unsigned int addS = _milliseconds / 1000;
  965.     unsigned int addMs = _milliseconds % 1000;
  966.     unsigned int newMs;
  967.    
  968.     if ((addMs + currentMs) >= 1000)
  969.     {
  970.         addS++;
  971.         newMs = (addMs + currentMs) % 1000;
  972.     }
  973.    
  974.     else
  975.     {
  976.         newMs = addMs + currentMs;
  977.     }
  978.    
  979.     if (addS > 0)
  980.     {
  981.         tmp = tmp.addSeconds(addS);
  982.     }
  983.    
  984.     tmp.millisecond(newMs);
  985.    
  986.     return tmp;
  987. }
  988.  
  989. DateTime DateTime::subtract(unsigned int _days, unsigned int _hours, unsigned int _minutes, unsigned int _seconds, unsigned int _milliseconds) const
  990. {
  991.     DateTime tmp = *this;
  992.    
  993.     tmp = tmp.subtractMilliseconds(_milliseconds);
  994.     tmp = tmp.subtractSeconds(_seconds);
  995.     tmp = tmp.subtractMinutes(_minutes);
  996.     tmp = tmp.subtractHours(_hours);
  997.     tmp = tmp.subtractDays(_days);
  998.    
  999.     return tmp;
  1000. }
  1001.  
  1002. DateTime DateTime::add(unsigned int _days, unsigned int _hours, unsigned int _minutes, unsigned int _seconds, unsigned int _milliseconds) const
  1003. {
  1004.     DateTime tmp = *this;
  1005.    
  1006.     tmp = tmp.addMilliseconds(_milliseconds);
  1007.     tmp = tmp.addSeconds(_seconds);
  1008.     tmp = tmp.addMinutes(_minutes);
  1009.     tmp = tmp.addHours(_hours);
  1010.     tmp = tmp.addDays(_days);
  1011.    
  1012.     return tmp;
  1013. }
  1014.  
  1015. DateTime DateTime::subtract(const DateTimeDuration& dur) const
  1016. {
  1017.     bool is_negative = !dur.isNegative();
  1018.    
  1019.     DateTimeDuration tmp = dur;
  1020.     tmp.isNegative(is_negative);
  1021.    
  1022.     return add(tmp);
  1023. }
  1024.  
  1025. DateTime DateTime::add(const DateTimeDuration& dur) const
  1026. {
  1027.     if (dur.isNegative())
  1028.     {
  1029.         return subtract(dur.days(), dur.hours(), dur.minutes(), dur.seconds(), dur.milliseconds());
  1030.     }
  1031.     else
  1032.     {
  1033.         return add(dur.days(), dur.hours(), dur.minutes(), dur.seconds(), dur.milliseconds());
  1034.     }
  1035. }
  1036.  
  1037. DateTimeDuration DateTime::timeBetween(const DateTime& rhs) const
  1038. {
  1039.     if (rhs == *this)
  1040.     {
  1041.         DateTimeDuration dur(0, 0, 0, 0, 0);
  1042.        
  1043.         return dur;
  1044.     }
  1045.     else if (rhs > *this)
  1046.     {
  1047.         DateTimeDuration dur = rhs.timeBetween(*this);
  1048.         dur.isNegative(true);
  1049.        
  1050.         return dur;
  1051.     }
  1052.     else
  1053.     {
  1054.         bool is_negative = false;
  1055.        
  1056.         static const unsigned long long int MS_PER_SEC = 1000;
  1057.         static const unsigned long long int MS_PER_MINUTE = MS_PER_SEC * 60;
  1058.         static const unsigned long long int MS_PER_HOUR = MS_PER_MINUTE * 60;
  1059.         static const unsigned long long int MS_PER_DAY = MS_PER_HOUR * 24;
  1060.        
  1061.         unsigned long long int ms = (hour() * MS_PER_HOUR) + (minute() * MS_PER_MINUTE) + (second() * MS_PER_SEC) + millisecond();
  1062.         unsigned long long int rhs_ms = (rhs.hour() * MS_PER_HOUR) + (rhs.minute() * MS_PER_MINUTE) + (rhs.second() * MS_PER_SEC) + rhs.millisecond();     
  1063.        
  1064.         DateTime stop_datetime;
  1065.        
  1066.         unsigned long long int total_ms = 0;
  1067.        
  1068.         //need to stop one day after in this case
  1069.         if (rhs_ms > ms)
  1070.         {
  1071.             stop_datetime = rhs.add(1, 0, 0, 0, 0);
  1072.            
  1073.             total_ms = MS_PER_DAY - (rhs_ms - ms);
  1074.         }
  1075.         else
  1076.         {
  1077.             stop_datetime = rhs;
  1078.            
  1079.             total_ms = ms - rhs_ms;
  1080.         }
  1081.        
  1082.         unsigned int hours = total_ms / MS_PER_HOUR;
  1083.        
  1084.         total_ms = total_ms % MS_PER_HOUR;
  1085.        
  1086.         unsigned int minutes = total_ms / MS_PER_MINUTE;
  1087.        
  1088.         total_ms = total_ms % MS_PER_MINUTE;
  1089.        
  1090.         unsigned int seconds = total_ms / MS_PER_SEC;
  1091.        
  1092.         total_ms = total_ms % MS_PER_SEC;
  1093.        
  1094.         unsigned int milliseconds = total_ms;
  1095.        
  1096.         unsigned int total_days = 0;
  1097.        
  1098.         unsigned int current_year = year();
  1099.        
  1100.         unsigned int current_day = dayOfYear();
  1101.        
  1102.         while (current_year > stop_datetime.year())
  1103.         {
  1104.             if (current_year == year())
  1105.             {
  1106.                 total_days += dayOfYear();
  1107.             }
  1108.             else
  1109.             {
  1110.                 total_days += daysInYear(current_year);
  1111.             }
  1112.  
  1113.             current_year--;
  1114.             current_day = dayOfYear();
  1115.         }
  1116.        
  1117.         while (current_day > stop_datetime.dayOfYear())
  1118.         {
  1119.             total_days++;
  1120.             current_day--;
  1121.         }
  1122.        
  1123.         DateTimeDuration dur(total_days, hours, minutes, seconds, milliseconds, is_negative);
  1124.        
  1125.         return dur;
  1126.     }
  1127. }
  1128.  
  1129. bool DateTime::isLeapYear(unsigned int _year)
  1130. {
  1131.     return (!(_year % 4) && (_year % 100) || !(_year % 400));
  1132. }
  1133.  
  1134. bool DateTime::validDate(unsigned int _year, unsigned int _month, unsigned int _day)
  1135. {
  1136.     if (_year == 0 || _month == 0 || _day == 0)
  1137.         return false;
  1138.        
  1139.     if (_month > 12)
  1140.         return false;
  1141.        
  1142.     unsigned short days_in_month = daysInMonth(_year, _month);
  1143.    
  1144.     if (_day > days_in_month)
  1145.         return false;
  1146.        
  1147.     return true;
  1148. }
  1149.  
  1150. unsigned short DateTime::daysInYear(unsigned int _year)
  1151. {      
  1152.     if (isLeapYear(_year))
  1153.     {
  1154.         return 366;
  1155.     }
  1156.    
  1157.     return 365;
  1158. }
  1159.  
  1160. unsigned short DateTime::daysInMonth(unsigned int _year, unsigned int _month)
  1161. {
  1162.     unsigned short days_in_month = MONTH_LENGTHS[_month - 1];
  1163.        
  1164.     if (_month == 2 && isLeapYear(_year))
  1165.     {
  1166.         days_in_month++;
  1167.     }
  1168.    
  1169.     return days_in_month;
  1170. }
  1171.  
  1172. unsigned short DateTime::getDayOfYear(unsigned int _year, unsigned int _month, unsigned int _day)
  1173. {
  1174.     unsigned int day_of_year = 0;
  1175.    
  1176.     for (unsigned int month = 1; month < _month; month++)
  1177.     {
  1178.         unsigned int num_days = DateTime::daysInMonth(_year, month);
  1179.        
  1180.         day_of_year += num_days;
  1181.     }
  1182.    
  1183.     day_of_year += _day;
  1184.    
  1185.     return day_of_year;
  1186. }
  1187.  
  1188. DateTime DateTime::getReverseDayOfYear(unsigned int _year, unsigned int _dayOfYear)
  1189. {
  1190.     unsigned int month;
  1191.    
  1192.     for (month = 1; month < 12; month++)
  1193.     {
  1194.         unsigned int num_days = DateTime::daysInMonth(_year, month);
  1195.        
  1196.         if (num_days >= _dayOfYear)
  1197.         {
  1198.             break;
  1199.         }
  1200.        
  1201.         _dayOfYear -= num_days;    
  1202.     }
  1203.    
  1204.     return DateTime(_year, month, _dayOfYear);
  1205. }
  1206.  
  1207. time_t DateTime::mktime() const
  1208. {
  1209.     tm time_struct;
  1210.    
  1211.     time_struct.tm_year = year() - 1900;
  1212.     time_struct.tm_mon = month() - 1;
  1213.     time_struct.tm_mday = day();
  1214.     time_struct.tm_hour = hour();
  1215.     time_struct.tm_min = minute();
  1216.     time_struct.tm_sec = second();
  1217.    
  1218.     time_t time = ::mktime(&time_struct);
  1219.    
  1220.     return time;
  1221. }
  1222.  
  1223. double DateTime::difftime(const DateTime& rhs) const
  1224. {
  1225.     time_t time_val = mktime();
  1226.     time_t rhs_time_val = rhs.mktime();
  1227.    
  1228.     double diff = ::difftime(time_val, rhs_time_val);
  1229.    
  1230.     return diff;
  1231. }
  1232.  
  1233. DateTime DateTime::now()
  1234. {
  1235.     time_t local_time = time(NULL);
  1236.    
  1237.     tm* time_struct = localtime(&local_time);
  1238.    
  1239.     return DateTime(*time_struct);
  1240. }
  1241.  
  1242. DateTime DateTime::now_utc()
  1243. {
  1244.     time_t utc_time = time(NULL);
  1245.    
  1246.     tm* time_struct = gmtime(&utc_time);
  1247.    
  1248.     return DateTime(*time_struct);
  1249. }
  1250.  
  1251. ostream& operator<<(ostream& os, const DateTime& ddt)
  1252. {
  1253.     os << setfill('0') << setw(2) << ddt.month() << "/" << setw(2) << ddt.day() << "/" << ddt.year()
  1254.         << " " << setw(2) << ddt.hour() << ":" << setw(2) << ddt.minute() << ":"
  1255.         <<  setw(6) << fixed << setprecision(3) << static_cast<double>(static_cast<double>(ddt.second()) + (static_cast<double>(ddt.millisecond()) / 1000));
  1256.        
  1257.     return os;
  1258. }
Advertisement
Add Comment
Please, Sign In to add comment