greg_month.hpp 4.4 KB

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