date_formatting.hpp 4.1 KB

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