date_formatting.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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: 2008-02-27 15:00:24 -0500 (Wed, 27 Feb 2008) $
  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. }
  53. return os;
  54. } // format_month
  55. };
  56. //! Convert ymd to a standard string formatting policies
  57. template<class ymd_type, class format_type, class charT=char>
  58. class ymd_formatter
  59. {
  60. public:
  61. //! Convert ymd to a standard string formatting policies
  62. /*! This is standard code for handling date formatting with
  63. * year-month-day based date information. This function
  64. * uses the format_type to control whether the string will
  65. * contain separator characters, and if so what the character
  66. * will be. In addtion, it can format the month as either
  67. * an integer or a string as controled by the formatting
  68. * policy
  69. */
  70. static std::basic_string<charT> ymd_to_string(ymd_type ymd)
  71. {
  72. typedef typename ymd_type::month_type month_type;
  73. std::basic_ostringstream<charT> ss;
  74. ss << ymd.year;
  75. if (format_type::has_date_sep_chars()) {
  76. ss << format_type::month_sep_char();
  77. }
  78. //this name is a bit ugly, oh well....
  79. month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss);
  80. if (format_type::has_date_sep_chars()) {
  81. ss << format_type::day_sep_char();
  82. }
  83. ss << std::setw(2) << std::setfill(ss.widen('0'))
  84. << ymd.day;
  85. return ss.str();
  86. }
  87. };
  88. //! Convert a date to string using format policies
  89. template<class date_type, class format_type, class charT=char>
  90. class date_formatter
  91. {
  92. public:
  93. typedef std::basic_string<charT> string_type;
  94. //! Convert to a date to standard string using format policies
  95. static string_type date_to_string(date_type d)
  96. {
  97. typedef typename date_type::ymd_type ymd_type;
  98. if (d.is_not_a_date()) {
  99. return string_type(format_type::not_a_date());
  100. }
  101. if (d.is_neg_infinity()) {
  102. return string_type(format_type::neg_infinity());
  103. }
  104. if (d.is_pos_infinity()) {
  105. return string_type(format_type::pos_infinity());
  106. }
  107. ymd_type ymd = d.year_month_day();
  108. return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd);
  109. }
  110. };
  111. } } //namespace date_time
  112. #endif