date_formatting.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef DATE_TIME_DATE_FORMATTING_HPP___
  2. #define DATE_TIME_DATE_FORMATTING_HPP___
  3. /* Copyright (c) 2002-2004 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: 2012-09-30 16:25:22 -0700 (Sun, 30 Sep 2012) $
  9. */
  10. #include "boost/date_time/iso_format.hpp"
  11. #include "boost/date_time/compiler_config.hpp"
  12. #include <string>
  13. #include <sstream>
  14. #include <iomanip>
  15. /* NOTE: "formatter" code for older compilers, ones that define
  16. * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
  17. * date_formatting_limited.hpp
  18. */
  19. namespace boost {
  20. namespace date_time {
  21. //! Formats a month as as string into an ostream
  22. template<class month_type, class format_type, class charT=char>
  23. class month_formatter
  24. {
  25. typedef std::basic_ostream<charT> ostream_type;
  26. public:
  27. //! Formats a month as as string into an ostream
  28. /*! This function demands that month_type provide
  29. * functions for converting to short and long strings
  30. * if that capability is used.
  31. */
  32. static ostream_type& format_month(const month_type& month,
  33. ostream_type &os)
  34. {
  35. switch (format_type::month_format())
  36. {
  37. case month_as_short_string:
  38. {
  39. os << month.as_short_string();
  40. break;
  41. }
  42. case month_as_long_string:
  43. {
  44. os << month.as_long_string();
  45. break;
  46. }
  47. case month_as_integer:
  48. {
  49. os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
  50. break;
  51. }
  52. default:
  53. break;
  54. }
  55. return os;
  56. } // format_month
  57. };
  58. //! Convert ymd to a standard string formatting policies
  59. template<class ymd_type, class format_type, class charT=char>
  60. class ymd_formatter
  61. {
  62. public:
  63. //! Convert ymd to a standard string formatting policies
  64. /*! This is standard code for handling date formatting with
  65. * year-month-day based date information. This function
  66. * uses the format_type to control whether the string will
  67. * contain separator characters, and if so what the character
  68. * will be. In addtion, it can format the month as either
  69. * an integer or a string as controled by the formatting
  70. * policy
  71. */
  72. static std::basic_string<charT> ymd_to_string(ymd_type ymd)
  73. {
  74. typedef typename ymd_type::month_type month_type;
  75. std::basic_ostringstream<charT> ss;
  76. // Temporarily switch to classic locale to prevent possible formatting
  77. // of year with comma or other character (for example 2,008).
  78. ss.imbue(std::locale::classic());
  79. ss << ymd.year;
  80. ss.imbue(std::locale());
  81. if (format_type::has_date_sep_chars()) {
  82. ss << format_type::month_sep_char();
  83. }
  84. //this name is a bit ugly, oh well....
  85. month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss);
  86. if (format_type::has_date_sep_chars()) {
  87. ss << format_type::day_sep_char();
  88. }
  89. ss << std::setw(2) << std::setfill(ss.widen('0'))
  90. << ymd.day;
  91. return ss.str();
  92. }
  93. };
  94. //! Convert a date to string using format policies
  95. template<class date_type, class format_type, class charT=char>
  96. class date_formatter
  97. {
  98. public:
  99. typedef std::basic_string<charT> string_type;
  100. //! Convert to a date to standard string using format policies
  101. static string_type date_to_string(date_type d)
  102. {
  103. typedef typename date_type::ymd_type ymd_type;
  104. if (d.is_not_a_date()) {
  105. return string_type(format_type::not_a_date());
  106. }
  107. if (d.is_neg_infinity()) {
  108. return string_type(format_type::neg_infinity());
  109. }
  110. if (d.is_pos_infinity()) {
  111. return string_type(format_type::pos_infinity());
  112. }
  113. ymd_type ymd = d.year_month_day();
  114. return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd);
  115. }
  116. };
  117. } } //namespace date_time
  118. #endif