Advertisement
iwishiknew

chrono_date.cpp

Jul 28th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8. #include "t_extra.h"
  9. #include <sstream>
  10.  
  11. using namespace std;
  12.  
  13. //Wed Mar 13 15:34:46 2013
  14.  
  15. /**
  16. 0-2: day of week (name)
  17. 4-6: name of month
  18. 8-9: day of month
  19. 11-12: hour
  20. 14-15: minute
  21. 17-18: second
  22. 20-23: year
  23. */
  24.  
  25. static int def_int_dates = int();
  26.  
  27. ///a static so that the default of is_past args can be the current asc_time
  28. static string def_is_past_asc_time = chrono_date().gasc_time();
  29. static int def_isleap = chrono_date().year_v;
  30.  
  31. //handy dandy functions...
  32.  
  33. /**All function members of class chrono_date take asc_time and extract
  34. data from it.  asc_time is defaulted to the current time as returned
  35. by the system.   This allows for only 1 piece of data, to represent many
  36.  different pieces of data, as well as makes it easier to retrieve the
  37.  individual pieces.*/
  38.  
  39. template<class type1, class type2>
  40. type2 conv(const type1& t1)
  41. {
  42.     stringstream ss;
  43.     type2 t2 = type2();
  44.     ss<< t1;
  45.     ss>> t2;
  46.     ss.str("");
  47.     return t2;
  48. }
  49.  
  50.  
  51. /*--------------------------------------
  52.  * Constructors                        |
  53.  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  54.  
  55. chrono_date::chrono_date()
  56. {
  57.     chrono_date::asc_time = conv<string, chronoT>(chrono_date::t());
  58. }
  59.  
  60. chrono_date::chrono_date(const string& s)
  61. {
  62.     chrono_date::asc_time = s;
  63. }
  64.  
  65. chrono_date::chrono_date(const char* ch)
  66. {
  67.     chrono_date::asc_time = ch;
  68. }
  69.  
  70. chrono_date::~chrono_date()
  71. {
  72. }
  73.  
  74. /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  75.  * Constructors                        |
  76.  *------------------------------------*/
  77.  
  78.  
  79. /*****************************************
  80.  * Member functions of chrono_date():    |
  81.  vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
  82.  
  83. const string chrono_date::gasc_time()
  84. {
  85.     return chrono_date::asc_time;
  86. }
  87.  
  88. /* Returns the military hour as an integer.  This is
  89. usful for calculations.*/
  90. int chrono_date::hour()
  91. {
  92.     stringstream ss;
  93.     int hourtime = 0;
  94.     ss.str("");
  95.  
  96.     string temps = "";
  97.     if(asc_time.size() > 11)
  98.     {
  99.         for(unsigned int x = 11; x < 13; x++)
  100.         {
  101.             temps = (temps + asc_time[x]);
  102.         }
  103.     }
  104.  
  105.     //convert the (suppposed) hour into an integer value now
  106.     ss<< temps;
  107.     ss>> hourtime;
  108.     ss.str("");
  109.     return hourtime;
  110. }
  111.  
  112. /*Returns raw asctime as a string.  All time information
  113. is gathered through this.*/
  114. string chrono_date::t()
  115. {
  116.     string tme;
  117.     time_t rawtime;
  118.     struct tm * timeinfo;
  119.     time(&rawtime);
  120.     timeinfo = localtime(&rawtime);
  121.     tme = asctime(timeinfo);
  122.     tme.resize((tme.size() - 1));
  123.     return tme;
  124. }
  125.  
  126. /* Returns the minute value as an integer.  Usflul
  127. for calculations.*/
  128. int chrono_date::minute()
  129. {
  130.     stringstream ss;
  131.     ss.str("");
  132.     int minutetime = 0;
  133.     string temps = "";
  134.  
  135.     if(asc_time.size() > 14)
  136.     {
  137.         for(unsigned int x = 14; x < 16; x++)
  138.         {
  139.             temps = (temps + asc_time[x]);
  140.         }
  141.     }
  142.  
  143.     ss<< temps;
  144.     ss>> minutetime;
  145.     ss.str("");
  146.     return minutetime;
  147. }
  148.  
  149. /* Returns the second value as an integer.  This
  150. is useful for calculations.*/
  151. int chrono_date::second()
  152. {
  153.     stringstream ss;
  154.     ss.str("");
  155.  
  156.     int secondtime = 0;
  157.     string temps = "";
  158.  
  159.     if(asc_time.size() > 17)
  160.     {
  161.         for(unsigned int x = 17; x < 19; x++)
  162.         {
  163.             temps = (temps + asc_time[x]);
  164.         }
  165.     }
  166.  
  167.     ss<< temps;
  168.     ss>> secondtime;
  169.     ss.str("");
  170.     return secondtime;
  171. }
  172.  
  173. /* This returns the name of the day of the week as
  174. a string.*/
  175. string chrono_date::day_of_week()
  176. {
  177.     string temps = "";
  178.     if(asc_time.size() > 1)
  179.     {
  180.         for(unsigned int x = 0; x < 3; x++)
  181.         {
  182.             temps = (temps + asc_time[x]);
  183.         }
  184.     }
  185.     return temps;
  186. }
  187.  
  188. /* Returns the numeric value which represents the day of the week.
  189. 1 = sunday, and 7 = saturday*/
  190. int chrono_date::num_day_of_week()
  191. {
  192.     vector<string> week_names = {
  193.         "Sun",
  194.         "Mon",
  195.         "Tue",
  196.         "Wed",
  197.         "Thu",
  198.         "Fri",
  199.         "Sat"
  200.     };
  201.     string day = "";
  202.     day = chrono_date::day_of_week();
  203.     for(unsigned int x = 0; x < week_names.size(); x++)
  204.     {
  205.         if(week_names[x] == day)
  206.         {
  207.             return (x + 1);
  208.         }
  209.     }
  210.     return 0;
  211. }
  212.  
  213. /* Returns the integer which represents the day of the month.*/
  214. int chrono_date::day_of_month()
  215. {
  216.     stringstream ss;
  217.     ss.str("");
  218.  
  219.     //0 cant be a month day... :|
  220.     int monthday = 1;
  221.     string temps = "";
  222.     if(asc_time.size() > 8)
  223.     {
  224.         for(unsigned int x = 8; x < 10; x++)
  225.         {
  226.             temps = (temps + asc_time[x]);
  227.         }
  228.     }
  229.  
  230.     ss<< temps;
  231.     ss>> monthday;
  232.     ss.str("");
  233.     return monthday;
  234. }
  235.  
  236. /* Returns a string which represents the month name.*/
  237. string chrono_date::month_name()
  238. {
  239.     string temps = "";
  240.     if(asc_time.size() > 5)
  241.     {
  242.         for(unsigned int x = 4; x < 7; x++)
  243.         {
  244.             temps = (temps + asc_time[x]);
  245.         }
  246.     }
  247.     return temps;
  248. }
  249.  
  250. /* Returns the integer value of the month.  1 = january, 12 = december.*/
  251. int chrono_date::month_number()
  252. {
  253.     string mname = chrono_date::month_name();
  254.     vector<string> months = {
  255.         "Jan",
  256.         "Feb",
  257.         "Mar",
  258.         "Apr",
  259.         "May",
  260.         "Jun",
  261.         "Jul",
  262.         "Aug",
  263.         "Sep",
  264.         "Oct",
  265.         "Nov",
  266.         "Dec"
  267.     };
  268.  
  269.     for(unsigned int x = 0; x < months.size(); x++)
  270.     {
  271.         if(mname == months[x])
  272.         {
  273.             return (x + 1);
  274.         }
  275.     }
  276.     return 0;
  277. }
  278.  
  279. /* Returns the integer value of the year.*/
  280. int chrono_date::year()
  281. {
  282.     string temps = "";
  283.     int yeari = 0;
  284.     stringstream ss;
  285.     ss.str("");
  286.     if(asc_time.size() > 22)
  287.     {
  288.         for(unsigned int x = 20; x < 24; x++)
  289.         {
  290.             temps = (temps + asc_time[x]);
  291.         }
  292.     }
  293.     ss<< temps;
  294.     ss>> yeari;
  295.     ss.str("");
  296.     return yeari;
  297. }
  298.  
  299. /* Returns an integer value which represents
  300. the day of the week.  This can be used for calculations.*/
  301. string chrono_date::week_day_id_name(const int& id = def_int_dates)
  302. {
  303.     vector<string> week_names = {
  304.         "Sun",
  305.         "Mon",
  306.         "Tue",
  307.         "Wed",
  308.         "Thu",
  309.         "Fri",
  310.         "Sat"
  311.     };
  312.     string temps = chrono_date::day_of_week();
  313.     int i = id;
  314.     i--;
  315.  
  316.     //so that we do not "step out-of-bounds", lets modulate if its too big
  317.     while(i > 6)
  318.     {
  319.         i = (i - 7);
  320.     }
  321.  
  322.     for(unsigned int x = 0; x < week_names.size(); x++)
  323.     {
  324.         if(unsigned(i) == x)
  325.         {
  326.             return week_names[x];
  327.         }
  328.     }
  329.     return "Mon";
  330. }
  331.  
  332. /* Returns true if asc_time represents a date which
  333. is earlier than the present time, aka: it's the past (relative to the current time)
  334.  
  335.  NOTE: May help to think of this is chrono_date::is_earlier_than()*/
  336. bool chrono_date::is_past(const string& curt = def_is_past_asc_time)
  337. {
  338.     chrono_date pres_time = chrono_date();
  339.     pres_time.asc_time = curt;
  340.     chrono_date temp;
  341.  
  342.     //using a temporary class so that we can also change this to
  343.     //somthing other than the current time if we want to.
  344.     temp.asc_time = asc_time;
  345.     /*
  346.     from the biggest value, to the smallest:
  347.  
  348.     chrono_date::time < pres_time to be true
  349.  
  350.     year
  351.     month
  352.     day of month
  353.     hour
  354.     minute
  355.     second
  356.     */
  357.  
  358.     if(temp.year() < pres_time.year())
  359.     {
  360.         return true;
  361.     }
  362.     if(temp.year() == pres_time.year())
  363.     {
  364.         if(temp.month_number() < pres_time.month_number())
  365.         {
  366.             return true;
  367.         }
  368.         if(temp.month_number() == pres_time.month_number())
  369.         {
  370.             if(temp.day_of_month() < pres_time.day_of_month())
  371.             {
  372.                 return true;
  373.             }
  374.             if(temp.day_of_month() == pres_time.day_of_month())
  375.             {
  376.                 if(temp.hour() < pres_time.hour())
  377.                 {
  378.                     return true;
  379.                 }
  380.                 if(temp.hour() == pres_time.hour())
  381.                 {
  382.                     if(temp.minute() < pres_time.minute())
  383.                     {
  384.                         return true;
  385.                     }
  386.                     if(temp.minute() == pres_time.minute())
  387.                     {
  388.                         if(temp.second() < pres_time.second())
  389.                         {
  390.                             return true;
  391.                         }
  392.                         if(temp.second() == pres_time.second())
  393.                         {
  394.                             return false;
  395.                         }
  396.                     }
  397.                 }
  398.             }
  399.         }
  400.     }
  401.     return false;
  402. }
  403.  
  404. /* Returns a string: "AM" or "PM", depending
  405. on the value of asc_time.*/
  406. string chrono_date::std_ampm()
  407. {
  408.     string ampm = "AM";
  409.     int h = 1;
  410.     h = chrono_date::hour();
  411.     if(h > 11)
  412.     {
  413.         ampm = "PM";
  414.     }
  415.     return ampm;
  416. }
  417.  
  418. /* Returns hour as standard hour, instead of
  419. military hour.*/
  420. int chrono_date::std_hour()
  421. {
  422.     int h = 1;
  423.     h = chrono_date::hour();
  424.  
  425.     //this is because when h = 0, h = 12 AM
  426.     if(h == 0)
  427.     {
  428.         h = 24;
  429.     }
  430.  
  431.     while(h > 12)
  432.     {
  433.         h = (h - 12);
  434.     }
  435.     return h;
  436. }
  437.  
  438. /* This is just for context.  Thought it would be nice.*/
  439. int chrono_date::std_minute()
  440. {
  441.     return chrono_date::minute();
  442. }
  443.  
  444. /* "mm/dd/yyyy HH:MM AMP/PM" */
  445. string chrono_date::std_time_disp()
  446. {
  447.     string temps = "";
  448.     stringstream ss;
  449.  
  450.     ss.str("");
  451.  
  452.     //format: mm/dd/yyyy  hh:mm AM/PM
  453.     if(chrono_date::month_number() < 10)
  454.     {
  455.         temps = (temps + "0");
  456.     }
  457.     ss<< chrono_date::month_number();
  458.     temps = (temps + ss.str() + "/");
  459.  
  460.     ss.str("");
  461.  
  462.     if(chrono_date::day_of_month() < 10)
  463.     {
  464.         temps = (temps + "0");
  465.     }
  466.     ss<< chrono_date::day_of_month();
  467.     temps = (temps + ss.str() + "/");
  468.  
  469.     ss.str("");
  470.  
  471.     ss<< chrono_date::year();
  472.     temps = (temps + ss.str() + "  ");
  473.  
  474.     ss.str("");
  475.  
  476.     if(chrono_date::hour() < 10)
  477.     {
  478.         temps = (temps + "0");
  479.     }
  480.     ss<< chrono_date::std_hour();
  481.     temps = (temps + ss.str() + ":");
  482.  
  483.     ss.str("");
  484.  
  485.     if(chrono_date::std_minute() < 10)
  486.     {
  487.         temps = (temps + "0");
  488.     }
  489.     ss<< chrono_date::minute();
  490.     temps = (temps + ss.str() + " " + chrono_date::std_ampm());
  491.  
  492.     ss.str("");
  493.  
  494.     return temps;
  495. }
  496.  
  497. bool chrono_date::is_leap_year(const int& setty = def_isleap)
  498. {
  499.     int y = setty;
  500.     while(y > 4)
  501.     {
  502.         y = (y - 4);
  503.     }
  504.     if(y == 4)
  505.     {
  506.         return true;
  507.     }
  508.     return false;
  509. }
  510.  
  511. /* Returns the day number relative to the first day this year.*/
  512. int chrono_date::day_number()
  513. {
  514.     vector<int> month_days = {
  515.         31,
  516.         28,
  517.         31,
  518.         30,
  519.         31,
  520.         30,
  521.         31,
  522.         31,
  523.         30,
  524.         31,
  525.         30,
  526.         31
  527.     };
  528.     if(chrono_date::is_leap_year() == true)
  529.     {
  530.         if(month_days[1] == 28)
  531.         {
  532.             month_days[1]++;
  533.         }
  534.     }
  535.  
  536.     int dom = chrono_date::day_of_month(), dayid = 0;
  537.     dayid = (dayid + dom);
  538.     for(int x = 0; x < (chrono_date::month_number() - 1); x++)
  539.     {
  540.         dayid = (dayid + month_days[x]);
  541.     }
  542.     return dayid;
  543. }
  544.  
  545. /* Sets chrono_date::asc_time to the date of the current
  546.  date's id + arg id*/
  547. void chrono_date::set_day_id(const int& newnum)
  548. {  
  549.     int year = chrono_date::year();
  550.     int day_id_of_week = chrono_date::num_day_of_week();
  551.     int day_of_month = chrono_date::day_of_month();
  552.     vector<int> month_days = {
  553.         31,
  554.         28,
  555.         31,
  556.         30,
  557.         31,
  558.         30,
  559.         31,
  560.         31,
  561.         30,
  562.         31,
  563.         30,
  564.         31
  565.     };
  566.     vector<string> month_names = {
  567.         "Jan",
  568.         "Feb",
  569.         "Mar",
  570.         "Apr",
  571.         "May",
  572.         "Jun",
  573.         "Jul",
  574.         "Aug",
  575.         "Sep",
  576.         "Oct",
  577.         "Nov",
  578.         "Dec"
  579.     };
  580.     int month = chrono_date::month_number();
  581.     month--;
  582.     while(month < 0)
  583.     {
  584.         month += 12;
  585.     }
  586.     chrono_date::is_leap_year(year) ? (month_days[1] = 29) : (month_days[1] = 28);
  587.    
  588.     /*Calculate the new day of the week by
  589.      modulating the new id by 7.*/
  590.     day_id_of_week += newnum;
  591.     while(day_id_of_week > 7)
  592.     {
  593.         day_id_of_week -= 7;
  594.     }
  595.     while(day_id_of_week < 1)
  596.     {
  597.         day_id_of_week += 7;
  598.     }
  599.    
  600.     //so, now we have the new id for the day of the week
  601.     //now for the day of the month
  602.    
  603.     day_of_month += newnum;
  604.    
  605.     //so, we are setting the date to one of previous months
  606.     while(day_of_month < 1)
  607.     {
  608.         month--;
  609.         while(month < 0)
  610.         {
  611.             month += 12;
  612.             year--;
  613.         }
  614.         chrono_date::is_leap_year(year) ? (month_days[1] = 29) : (month_days[1] = 28);
  615.         day_of_month += month_days[month];
  616.     }
  617.     while(day_of_month > month_days[month])
  618.     {
  619.         chrono_date::is_leap_year(year) ? (month_days[1] = 29) : (month_days[1] = 28);
  620.         day_of_month -= month_days[month];
  621.         month++;
  622.         while(month > 11)
  623.         {
  624.             month -= 12;
  625.             year++;
  626.         }
  627.     }
  628.    
  629.     /*Now to construct the asc string*/
  630.     chrono_date::asc_time = (chrono_date::week_day_id_name(day_id_of_week) + " ");
  631.     chrono_date::asc_time += (month_names[month] + " ");
  632.     if(day_of_month < 10)
  633.     {
  634.         chrono_date::asc_time += "0";
  635.     }
  636.     chrono_date::asc_time += (conv<int, string>(day_of_month) + " 12:00:00 ");
  637.     chrono_date::asc_time += conv<int, string>(year);
  638. }
  639.  
  640. /* Resets the class using the no-arg constructor*/
  641. void chrono_date::reset()
  642. {
  643.     chrono_date::asc_time = chrono_date::t();
  644. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement