1 #ifndef GREG_MONTH_HPP___ 2 #define GREG_MONTH_HPP___ 3 4 /* Copyright (c) 2002,2003, 2020 CrystalClear Software, Inc. 5 * Use, modification and distribution is subject to the 6 * Boost Software License, Version 1.0. (See accompanying 7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 8 * Author: Jeff Garland, Bart Garst 9 * $Date$ 10 */ 11 12 #include <boost/date_time/constrained_value.hpp> 13 #include <boost/date_time/date_defs.hpp> 14 #include <boost/date_time/compiler_config.hpp> 15 #include <stdexcept> 16 #include <string> 17 18 namespace boost { 19 namespace gregorian { 20 21 typedef date_time::months_of_year months_of_year; 22 23 //bring enum values into the namespace 24 using date_time::Jan; 25 using date_time::Feb; 26 using date_time::Mar; 27 using date_time::Apr; 28 using date_time::May; 29 using date_time::Jun; 30 using date_time::Jul; 31 using date_time::Aug; 32 using date_time::Sep; 33 using date_time::Oct; 34 using date_time::Nov; 35 using date_time::Dec; 36 using date_time::NotAMonth; 37 using date_time::NumMonths; 38 39 //! Exception thrown if a greg_month is constructed with a value out of range 40 struct BOOST_SYMBOL_VISIBLE bad_month : public std::out_of_range 41 { bad_monthboost::gregorian::bad_month42 bad_month() : std::out_of_range(std::string("Month number is out of range 1..12")) {} 43 }; 44 //! Build a policy class for the greg_month_rep 45 typedef CV::simple_exception_policy<unsigned short, 1, 12, bad_month> greg_month_policies; 46 //! A constrained range that implements the gregorian_month rules 47 typedef CV::constrained_value<greg_month_policies> greg_month_rep; 48 49 50 //! Wrapper class to represent months in gregorian based calendar 51 class BOOST_SYMBOL_VISIBLE greg_month : public greg_month_rep { 52 public: 53 typedef date_time::months_of_year month_enum; 54 55 //! Construct a month from the months_of_year enumeration greg_month(month_enum theMonth)56 BOOST_CXX14_CONSTEXPR greg_month(month_enum theMonth) : 57 greg_month_rep(static_cast<greg_month_rep::value_type>(theMonth)) {} 58 //! Construct from a short value greg_month(value_type theMonth)59 BOOST_CXX14_CONSTEXPR greg_month(value_type theMonth) : greg_month_rep(theMonth) {} 60 //! Convert the value back to a short operator value_type() const61 BOOST_CXX14_CONSTEXPR operator value_type() const {return value_;} 62 //! Returns month as number from 1 to 12 as_number() const63 BOOST_CXX14_CONSTEXPR value_type as_number() const {return value_;} as_enum() const64 BOOST_CXX14_CONSTEXPR month_enum as_enum() const {return static_cast<month_enum>(value_);} 65 66 //! Returns 3 char english string for the month ex: Jan, Feb, Mar, Apr 67 const char* as_short_string() const68 as_short_string() const 69 { 70 static const char* const short_month_names[NumMonths] 71 = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec", "NAM"}; 72 return short_month_names[value_-1]; 73 } 74 75 //! Returns full name of month as string in english ex: January, February 76 const char* as_long_string() const77 as_long_string() const 78 { 79 static const char* const long_month_names[NumMonths] 80 = {"January","February","March","April","May","June","July","August", 81 "September","October","November","December","NotAMonth"}; 82 return long_month_names[value_-1]; 83 } 84 85 #ifndef BOOST_NO_STD_WSTRING 86 87 //! Returns 3 wchar_t english string for the month ex: Jan, Feb, Mar, Apr 88 const wchar_t* as_short_wstring() const89 as_short_wstring() const 90 { 91 static const wchar_t* const w_short_month_names[NumMonths] 92 = {L"Jan",L"Feb",L"Mar",L"Apr",L"May",L"Jun",L"Jul",L"Aug",L"Sep",L"Oct", 93 L"Nov",L"Dec",L"NAM"}; 94 return w_short_month_names[value_-1]; 95 } 96 97 //! Returns full name of month as wchar_t string in english ex: January, February 98 const wchar_t* as_long_wstring() const99 as_long_wstring() const 100 { 101 static const wchar_t* const w_long_month_names[NumMonths] 102 = {L"January",L"February",L"March",L"April",L"May",L"June",L"July",L"August", 103 L"September",L"October",L"November",L"December",L"NotAMonth"}; 104 return w_long_month_names[value_-1]; 105 } 106 107 #endif // BOOST_NO_STD_WSTRING 108 109 /* parameterized as_*_string functions are intended to be called 110 * from a template function: "... as_short_string(charT c='\0');" */ as_short_string(char) const111 const char* as_short_string(char) const 112 { 113 return as_short_string(); 114 } as_long_string(char) const115 const char* as_long_string(char) const 116 { 117 return as_long_string(); 118 } 119 #ifndef BOOST_NO_STD_WSTRING as_short_string(wchar_t) const120 const wchar_t* as_short_string(wchar_t) const 121 { 122 return as_short_wstring(); 123 } as_long_string(wchar_t) const124 const wchar_t* as_long_string(wchar_t) const 125 { 126 return as_long_wstring(); 127 } 128 #endif // BOOST_NO_STD_WSTRING 129 }; 130 131 } } //namespace gregorian 132 133 #endif 134