SHARE
TWEET

Untitled

a guest Oct 13th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. /*
  3. khronos\gregorian_calendar.hpp
  4. (c) Garth Santor
  5. Updated by Patrick Crowley
  6. Created: 2015-09-22
  7. Last Updated: 2015-09-22
  8.  
  9. Khronos library 'Gregorian calendar' declarations.
  10. */
  11.  
  12.  
  13. #include <khronos/def.hpp>
  14. #include <khronos/calendar.hpp>
  15. #include <khronos/julian_day.hpp>
  16. #include "timeofday.hpp"
  17. #include <string>
  18.  
  19.  
  20. namespace khronos {
  21.  
  22.     // LITERALS
  23.     /* UDL - converts a Gregorian year CE to an astronomical Gregorian year. */
  24.     constexpr year_t operator ""_CE(unsigned long long gergorianYearBCE) { return -static_cast<year_t>(gergorianYearBCE) * -1; }
  25.     constexpr year_t operator ""_ce(unsigned long long gergorianYearBCE) { return -static_cast<year_t>(gergorianYearBCE) * -1; }
  26.     /* UDL - converts a Gregorian year BCE to an astronomical Gregorian year. */
  27.     constexpr year_t operator ""_BCE(unsigned long long gergorianYearBCE) { return -static_cast<year_t>(gergorianYearBCE) + 1; }
  28.     constexpr year_t operator ""_bce(unsigned long long gergorianYearBCE) { return -static_cast<year_t>(gergorianYearBCE) + 1; }
  29.  
  30.  
  31.  
  32.  
  33.     // VALUES
  34.     /*! JD of the start of the Gregorian epoch. */
  35.     jd_t constexpr GREGORIAN_EPOCH = 1'721'425.5;
  36.  
  37.     //FUNCTIONS
  38.     /*! Leap year test for Proleptic Gregorian Calendar.
  39.     \return 'true' if leap year, 'false' if not.
  40.     \param year [in] Astronomical year (1 CE = 1, 1 BCE = 0, 2 BCE = -1, etc.)
  41.     */
  42.     constexpr bool is_gregorian_leapyear(year_t year) {
  43.         return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
  44.     }
  45.  
  46.     jd_t gregorian_to_jd(year_t year, month_t month, day_t day);
  47.     jd_t gregorian_to_jd(year_t year, month_t month, day_t day, hour_t hour, minute_t minute, second_t second);
  48.  
  49.  
  50.     // OPERATIONS
  51.    
  52.  
  53.    
  54.  
  55.     /** Provide the number of days in the month of the Gregorian Calendar. */
  56.     constexpr day_t gregorian_days_in_month(month_t month, bool isLeapYear) {
  57.         return civil::days_in_month(month, isLeapYear);
  58.     }
  59.  
  60.  
  61.     /** Provide the name of the given month in the Gregorian calendar. */
  62.     constexpr char const * gregorian_month_name(month_t month) {
  63.         return civil::month_name_long(month);
  64.     }
  65.  
  66.  
  67.     /** Provide the short name of the given month in the Gregorian calendar. */
  68.     constexpr char const * gregorian_short_month_name(month_t month) {
  69.         return civil::month_name_short(month);
  70.     }
  71.  
  72.  
  73.  
  74.  
  75.     // CLASSES
  76.     // --------------------------------------------------------------------------------------
  77.  
  78.     /** Proleptic Gregorian Calendar Date class. */
  79.     class Gregorian {
  80.         year_t      year_ = 1;
  81.         month_t     month_ = 1;
  82.         day_t       day_ = 1;
  83.  
  84.         void from_jd(jd_t jd) { jd_to_gregorian(jd, year_, month_, day_); }
  85.         jd_t to_jd() const { return gregorian_to_jd(year_, month_, day_); }
  86.     public:
  87.         Gregorian();
  88.  
  89.         /*! Get the year.
  90.             \return Astronomical year. */
  91.         constexpr year_t  year() const { return year_; }
  92.  
  93.  
  94.         /*! Get the month.
  95.             \return Month number [1..12] */
  96.         constexpr month_t month() const { return month_; }
  97.  
  98.  
  99.         /*! Get the day of the month.
  100.             \return Day of month number [1..31]. */
  101.         constexpr day_t   day() const { return day_; }
  102.  
  103.         /*! Construct a Gregorian calendar date from year,month,day,[hour,minute,second]
  104.             \param year [in] Astronomical year.
  105.             \param month [in] Month number [1..12]
  106.             \param day [in] Day of month [1..31]
  107.             \param hour [in] 24-hour of the day [0..23]
  108.             \param minute [in] minute of the hour [0..59]
  109.             \param second [in] second of the minute [0..59]
  110.             */
  111.         constexpr Gregorian(year_t year, month_t month, day_t day) : year_(year), month_(month), day_(day) {}
  112.  
  113.  
  114.         /** Construct a Gregorian date from Julian Day Number object.
  115.             @param jd [in] Jd object.
  116.             */
  117.         Gregorian(Jd const& jd) { from_jd(jd.jd()); }
  118.  
  119.  
  120.         std::string to_string() const;
  121.  
  122.  
  123.         /** Implicit cast to Jd class. */
  124.         operator Jd () const { return Jd(to_jd()); }
  125.  
  126.  
  127.         /** Assign and convert from Jd type to Gregorian type. */
  128.         Gregorian& operator = (Jd const& jd) {
  129.             from_jd(jd.jd());
  130.             return *this;
  131.         }
  132.  
  133.  
  134.         // block some operators
  135.     private:
  136.         Gregorian operator + (detail::packaged_year_real const&);
  137.         Gregorian operator - (detail::packaged_year_real const&);
  138.         Gregorian operator + (detail::packaged_month_real const&);
  139.         Gregorian operator - (detail::packaged_month_real const&);
  140.     };
  141.  
  142.  
  143.  
  144.     // OPERATORS
  145.     // ====================
  146.  
  147.     /** Gregorian + (integer month) */
  148.     Gregorian operator + (Gregorian const& dt, detail::packaged_month_integer const& month);
  149.  
  150.     /** Gregorian - (integer month) */
  151.     inline Gregorian operator - (Gregorian const& dt, detail::packaged_month_integer const& month) { return dt + detail::packaged_month_integer(-month.nMonths_); }
  152.  
  153.     /** Gregorian + (integer year) */
  154.     Gregorian operator + (Gregorian const& dt, detail::packaged_year_integer const& year);
  155.  
  156.     /** Gregorian - (integer year) */
  157.     inline Gregorian operator - (Gregorian const& dt, detail::packaged_year_integer const& year) { return dt + detail::packaged_year_integer(-year.nYears_); }
  158.  
  159.     /** Stream insertion operator. */
  160.     inline std::ostream& operator << (std::ostream& os, Gregorian const& g) { return os << g.to_string(); }
  161.  
  162.  
  163. } // end-of-namespace khronos
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top