greg_weekday.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef GREG_WEEKDAY_HPP___
  2. #define GREG_WEEKDAY_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. //bring enum values into the namespace
  18. using date_time::Sunday;
  19. using date_time::Monday;
  20. using date_time::Tuesday;
  21. using date_time::Wednesday;
  22. using date_time::Thursday;
  23. using date_time::Friday;
  24. using date_time::Saturday;
  25. //! Exception that flags that a weekday number is incorrect
  26. struct BOOST_SYMBOL_VISIBLE bad_weekday : public std::out_of_range
  27. {
  28. bad_weekday() : std::out_of_range(std::string("Weekday is out of range 0..6")) {}
  29. };
  30. typedef CV::simple_exception_policy<unsigned short, 0, 6, bad_weekday> greg_weekday_policies;
  31. typedef CV::constrained_value<greg_weekday_policies> greg_weekday_rep;
  32. //! Represent a day within a week (range 0==Sun to 6==Sat)
  33. class BOOST_SYMBOL_VISIBLE greg_weekday : public greg_weekday_rep {
  34. public:
  35. typedef boost::date_time::weekdays weekday_enum;
  36. BOOST_CXX14_CONSTEXPR greg_weekday(value_type day_of_week_num) :
  37. greg_weekday_rep(day_of_week_num)
  38. {}
  39. BOOST_CXX14_CONSTEXPR value_type as_number() const {return value_;}
  40. BOOST_CXX14_CONSTEXPR weekday_enum as_enum() const {return static_cast<weekday_enum>(value_);}
  41. //! Return a 3 digit english string of the day of week (eg: Sun)
  42. const char* as_short_string() const
  43. {
  44. static const char* const short_weekday_names[]
  45. = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  46. return short_weekday_names[value_];
  47. }
  48. //! Return a point to a long english string representing day of week
  49. const char* as_long_string() const
  50. {
  51. static const char* const long_weekday_names[]
  52. = {"Sunday","Monday","Tuesday","Wednesday", "Thursday", "Friday", "Saturday"};
  53. return long_weekday_names[value_];
  54. }
  55. #ifndef BOOST_NO_STD_WSTRING
  56. //! Return a 3 digit english wchar_t string of the day of week (eg: Sun)
  57. const wchar_t* as_short_wstring() const
  58. {
  59. static const wchar_t* const w_short_weekday_names[]={L"Sun", L"Mon", L"Tue",
  60. L"Wed", L"Thu", L"Fri", L"Sat"};
  61. return w_short_weekday_names[value_];
  62. }
  63. //! Return a point to a long english wchar_t string representing day of week
  64. const wchar_t* as_long_wstring() const
  65. {
  66. static const wchar_t* const w_long_weekday_names[]= {L"Sunday",L"Monday",L"Tuesday",
  67. L"Wednesday", L"Thursday",
  68. L"Friday", L"Saturday"};
  69. return w_long_weekday_names[value_];
  70. }
  71. #endif // BOOST_NO_STD_WSTRING
  72. };
  73. } } //namespace gregorian
  74. #endif