greg_month.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef GREG_MONTH_HPP___
  2. #define GREG_MONTH_HPP___
  3. /* Copyright (c) 2002,2003 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/shared_ptr.hpp>
  13. #include <boost/date_time/compiler_config.hpp>
  14. #include <stdexcept>
  15. #include <string>
  16. #include <map>
  17. #include <algorithm>
  18. #include <cctype>
  19. namespace boost {
  20. namespace gregorian {
  21. typedef date_time::months_of_year months_of_year;
  22. //bring enum values into the namespace
  23. using date_time::Jan;
  24. using date_time::Feb;
  25. using date_time::Mar;
  26. using date_time::Apr;
  27. using date_time::May;
  28. using date_time::Jun;
  29. using date_time::Jul;
  30. using date_time::Aug;
  31. using date_time::Sep;
  32. using date_time::Oct;
  33. using date_time::Nov;
  34. using date_time::Dec;
  35. using date_time::NotAMonth;
  36. using date_time::NumMonths;
  37. //! Exception thrown if a greg_month is constructed with a value out of range
  38. struct BOOST_SYMBOL_VISIBLE bad_month : public std::out_of_range
  39. {
  40. bad_month() : std::out_of_range(std::string("Month number is out of range 1..12")) {}
  41. };
  42. //! Build a policy class for the greg_month_rep
  43. typedef CV::simple_exception_policy<unsigned short, 1, 12, bad_month> greg_month_policies;
  44. //! A constrained range that implements the gregorian_month rules
  45. typedef CV::constrained_value<greg_month_policies> greg_month_rep;
  46. //! Wrapper class to represent months in gregorian based calendar
  47. class BOOST_DATE_TIME_DECL greg_month : public greg_month_rep {
  48. public:
  49. typedef date_time::months_of_year month_enum;
  50. typedef std::map<std::string, unsigned short> month_map_type;
  51. typedef boost::shared_ptr<month_map_type> month_map_ptr_type;
  52. //! Construct a month from the months_of_year enumeration
  53. greg_month(month_enum theMonth) :
  54. greg_month_rep(static_cast<greg_month_rep::value_type>(theMonth)) {}
  55. //! Construct from a short value
  56. greg_month(value_type theMonth) : greg_month_rep(theMonth) {}
  57. //! Convert the value back to a short
  58. operator value_type() const {return value_;}
  59. //! Returns month as number from 1 to 12
  60. value_type as_number() const {return value_;}
  61. month_enum as_enum() const {return static_cast<month_enum>(value_);}
  62. const char* as_short_string() const;
  63. const char* as_long_string() const;
  64. #ifndef BOOST_NO_STD_WSTRING
  65. const wchar_t* as_short_wstring() const;
  66. const wchar_t* as_long_wstring() const;
  67. #endif // BOOST_NO_STD_WSTRING
  68. //! Shared pointer to a map of Month strings (Names & Abbrev) & numbers
  69. static month_map_ptr_type get_month_map_ptr();
  70. /* parameterized as_*_string functions are intended to be called
  71. * from a template function: "... as_short_string(charT c='\0');" */
  72. const char* as_short_string(char) const
  73. {
  74. return as_short_string();
  75. }
  76. const char* as_long_string(char) const
  77. {
  78. return as_long_string();
  79. }
  80. #ifndef BOOST_NO_STD_WSTRING
  81. const wchar_t* as_short_string(wchar_t) const
  82. {
  83. return as_short_wstring();
  84. }
  85. const wchar_t* as_long_string(wchar_t) const
  86. {
  87. return as_long_wstring();
  88. }
  89. #endif // BOOST_NO_STD_WSTRING
  90. };
  91. } } //namespace gregorian
  92. #endif