Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. /** @file gregorian.cpp
  2. @author Garth Santor
  3. @date 2013-09-19
  4.  
  5. Gregorian calendar class implementation.
  6. */
  7.  
  8. #include <khronos/gregorian_calendar.hpp>
  9. #include <cassert>
  10. #include <sstream>
  11. #include <ctime>
  12. #include <iomanip>
  13. #include <algorithm>
  14.  
  15. namespace khronos {
  16.  
  17.  
  18. /** Gregorian default constructor. Initialize to the current local time. */
  19. Gregorian::Gregorian() {
  20. time_t nowTime = time(NULL);
  21. struct tm tmNow;
  22. localtime_s(&tmNow, &nowTime);
  23. year_ = year_t(tmNow.tm_year) + 1900;
  24. month_ = month_t(tmNow.tm_mon) + 1;
  25. day_ = day_t(tmNow.tm_mday);
  26. }
  27.  
  28.  
  29.  
  30. /** Print the Gregorian Date as a string. */
  31. std::string Gregorian::to_string() const {
  32. std::ostringstream oss;
  33.  
  34. // day of week
  35. oss << civil::day_name(khronos::civil::day_of_week(to_jd())) << ", ";
  36.  
  37. oss << gregorian_month_name(month_) << ' ' << (unsigned)day_ << ' ';
  38. if (year_ <= 0)
  39. oss << (-year_ + 1) << " BCE";
  40. else
  41. oss << year_ << " CE";
  42.  
  43. return oss.str();
  44. }
  45.  
  46.  
  47.  
  48. /** Gregorian + (integer year) operator. */
  49. Gregorian operator + (Gregorian const& dt, detail::packaged_year_integer const& year) {
  50. year_t y = dt.year() + year.nYears_;
  51. month_t m = dt.month();
  52. day_t d = dt.day();
  53.  
  54. if (m == February && d == 29 && !is_gregorian_leapyear(y))
  55. d = 28;
  56.  
  57. return Gregorian(y, m, d);
  58. }
  59.  
  60.  
  61.  
  62. /** Gregorian + (integer month) operator. */
  63. Gregorian operator + (Gregorian const& dt, detail::packaged_month_integer const& month) {
  64. year_t yearsToAdd = month.nMonths_ / 12;
  65. month_t monthsToAdd = month.nMonths_ % 12;
  66. year_t y = dt.year() + yearsToAdd;
  67. month_t m = dt.month() + monthsToAdd;
  68.  
  69. int adjustment = (m - 1) / 12 + (m - 12) / 12;
  70. y += adjustment;
  71. m -= month_t(adjustment * 12);
  72.  
  73. day_t d = std::min(dt.day(), gregorian_days_in_month(m, is_gregorian_leapyear(y)));
  74.  
  75. return Gregorian(y, m, d);
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement