Guest User

Reply to https://r3dux.org/2017/08/how-to-convert-day-number

a guest
Aug 28th, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. // --------------------------------------------------------------------------------
  2. // Reply to https://r3dux.org/2017/08/how-to-convert-day-number-to-month-and-day/
  3. // No error checking, this is just an example implementation of the date algorithms
  4. // from: https://alcor.concordia.ca/~gpkatch/gdate-method.html.
  5. // --------------------------------------------------------------------------------
  6.  
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. typedef std::vector<std::string> stringvec;
  11. typedef unsigned long ul;
  12. typedef signed long sl;
  13.  
  14. // --------------------------------------------------------------------------------
  15. // Given the year, month and day, return the day number.
  16. // --------------------------------------------------------------------------------
  17. ul CalcDayNumFromDate(ul y, ul m, ul d)
  18. {
  19.   m = (m + 9) % 12;
  20.   y -= m / 10;
  21.   ul dn = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d - 1);
  22.   return dn;
  23. }
  24.  
  25.  
  26. // --------------------------------------------------------------------------------
  27. // Given a day, calculate and return the date (in a string vector).
  28. // --------------------------------------------------------------------------------
  29. template<typename OUT>
  30. void CalcDateFromDayNum(ul day, OUT &retval)
  31. {
  32.   ul y = (10000*day + 14780) / 3652425;
  33.   sl ddd = day - (365*y + y/4 - y/100 + y/400);
  34.   if (ddd < 0) {
  35.     y--;
  36.     ddd = day - (365*y + y/4 + y/100 + y/400);
  37.   }
  38.  
  39.   ul mi = (100*ddd + 52) / 3060;
  40.   ul mm = (mi + 2) % 12 + 1;
  41.   y += (mi + 2) / 12;
  42.   ul dd = ddd - (mi*306 + 5) / 10 + 1;
  43.  
  44.   retval.push_back(std::to_string(y));
  45.   retval.push_back(std::to_string(mm));
  46.   retval.push_back(std::to_string(dd));
  47. }
  48.  
  49. // --------------------------------------------------------------------------------
  50. // Given the year and a day number, calculate and return the month and day.
  51. // --------------------------------------------------------------------------------
  52. stringvec CalcDayAndMonth(ul year, ul day)
  53. {
  54.   stringvec date;
  55.  
  56.   ul start_day = CalcDayNumFromDate(year, 1, 1);
  57.   CalcDateFromDayNum(start_day + day - 1, date);
  58.  
  59.   return date;
  60. }
  61.  
  62. // --------------------------------------------------------------------------------
  63. // Program entry point.
  64. // --------------------------------------------------------------------------------
  65. int main(int argc, char **argv)
  66. {
  67.   std::string year, day;
  68.  
  69.   std::string month[] =
  70.       {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  71.        "Aug", "Sep", "Oct", "Nov", "Dec"};
  72.  
  73.   std::cout << "Enter the year (YYYY): ";
  74.   getline(std::cin, year);
  75.   std::cout << "Enter the day (between 0 and 365): ";
  76.   getline(std::cin, day);
  77.  
  78.   stringvec date = CalcDayAndMonth(std::atoi(year.c_str()), std::atoi(day.c_str()));
  79.  
  80.   std::cout << "Month: " << month[std::atoi(date[1].c_str()) - 1]
  81.             << ", Day: " << date[2] << std::endl;
  82.  
  83.   return 0;
  84. }
Add Comment
Please, Sign In to add comment