parsers.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef GREGORIAN_PARSERS_HPP___
  2. #define GREGORIAN_PARSERS_HPP___
  3. /* Copyright (c) 2002,2003,2005 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/gregorian/gregorian_types.hpp>
  11. #include <boost/date_time/date_parsing.hpp>
  12. #include <boost/date_time/compiler_config.hpp>
  13. #include <boost/date_time/parse_format_base.hpp>
  14. #include <boost/date_time/special_defs.hpp>
  15. #include <boost/date_time/find_match.hpp>
  16. #include <string>
  17. #include <iterator>
  18. namespace boost {
  19. namespace gregorian {
  20. //! Return special_value from string argument
  21. /*! Return special_value from string argument. If argument is
  22. * not one of the special value names (defined in names.hpp),
  23. * return 'not_special' */
  24. inline
  25. date_time::special_values
  26. special_value_from_string(const std::string& s) {
  27. static const char* const special_value_names[date_time::NumSpecialValues]
  28. = {"not-a-date-time","-infinity","+infinity","min_date_time",
  29. "max_date_time","not_special"};
  30. short i = date_time::find_match(special_value_names,
  31. special_value_names,
  32. date_time::NumSpecialValues,
  33. s);
  34. if(i >= date_time::NumSpecialValues) { // match not found
  35. return date_time::not_special;
  36. }
  37. else {
  38. return static_cast<date_time::special_values>(i);
  39. }
  40. }
  41. //! Deprecated: Use from_simple_string
  42. inline date from_string(const std::string& s) {
  43. return date_time::parse_date<date>(s);
  44. }
  45. //! From delimited date string where with order year-month-day eg: 2002-1-25 or 2003-Jan-25 (full month name is also accepted)
  46. inline date from_simple_string(const std::string& s) {
  47. return date_time::parse_date<date>(s, date_time::ymd_order_iso);
  48. }
  49. //! From delimited date string where with order year-month-day eg: 1-25-2003 or Jan-25-2003 (full month name is also accepted)
  50. inline date from_us_string(const std::string& s) {
  51. return date_time::parse_date<date>(s, date_time::ymd_order_us);
  52. }
  53. //! From delimited date string where with order day-month-year eg: 25-1-2002 or 25-Jan-2003 (full month name is also accepted)
  54. inline date from_uk_string(const std::string& s) {
  55. return date_time::parse_date<date>(s, date_time::ymd_order_dmy);
  56. }
  57. //! From ISO 8601 type date string where with order year-month-day eg: 20020125
  58. inline date from_undelimited_string(const std::string& s) {
  59. return date_time::parse_undelimited_date<date>(s);
  60. }
  61. //! From ISO 8601 type date string where with order year-month-day eg: 20020125
  62. inline date date_from_iso_string(const std::string& s) {
  63. return date_time::parse_undelimited_date<date>(s);
  64. }
  65. #if !(defined(BOOST_NO_STD_ITERATOR_TRAITS))
  66. //! Stream should hold a date in the form of: 2002-1-25. Month number, abbrev, or name are accepted
  67. /* Arguments passed in by-value for convertability of char[]
  68. * to iterator_type. Calls to from_stream_type are by-reference
  69. * since conversion is already done */
  70. template<class iterator_type>
  71. inline date from_stream(iterator_type beg, iterator_type end) {
  72. if(beg == end)
  73. {
  74. return date(not_a_date_time);
  75. }
  76. typedef typename std::iterator_traits<iterator_type>::value_type value_type;
  77. return date_time::from_stream_type<date>(beg, end, value_type());
  78. }
  79. #endif //BOOST_NO_STD_ITERATOR_TRAITS
  80. #if (defined(_MSC_VER) && (_MSC_VER < 1300))
  81. // This function cannot be compiled with MSVC 6.0 due to internal compiler shorcomings
  82. #else
  83. //! Function to parse a date_period from a string (eg: [2003-Oct-31/2003-Dec-25])
  84. inline date_period date_period_from_string(const std::string& s){
  85. return date_time::from_simple_string_type<date,char>(s);
  86. }
  87. # if !defined(BOOST_NO_STD_WSTRING)
  88. //! Function to parse a date_period from a wstring (eg: [2003-Oct-31/2003-Dec-25])
  89. inline date_period date_period_from_wstring(const std::wstring& s){
  90. return date_time::from_simple_string_type<date,wchar_t>(s);
  91. }
  92. # endif // BOOST_NO_STD_WSTRING
  93. #endif
  94. } } //namespace gregorian
  95. #endif