date_formatting_limited.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef DATE_TIME_DATE_FORMATTING_LIMITED_HPP___
  2. #define DATE_TIME_DATE_FORMATTING_LIMITED_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 <string>
  13. #include <sstream>
  14. #include <iomanip>
  15. namespace boost {
  16. namespace date_time {
  17. //! Formats a month as as string into an ostream
  18. template<class month_type, class format_type>
  19. class month_formatter
  20. {
  21. public:
  22. //! Formats a month as as string into an ostream
  23. /*! This function demands that month_type provide
  24. * functions for converting to short and long strings
  25. * if that capability is used.
  26. */
  27. static std::ostream& format_month(const month_type& month,
  28. std::ostream& os)
  29. {
  30. switch (format_type::month_format())
  31. {
  32. case month_as_short_string:
  33. {
  34. os << month.as_short_string();
  35. break;
  36. }
  37. case month_as_long_string:
  38. {
  39. os << month.as_long_string();
  40. break;
  41. }
  42. case month_as_integer:
  43. {
  44. os << std::setw(2) << std::setfill('0') << month.as_number();
  45. break;
  46. }
  47. }
  48. return os;
  49. } // format_month
  50. };
  51. //! Convert ymd to a standard string formatting policies
  52. template<class ymd_type, class format_type>
  53. class ymd_formatter
  54. {
  55. public:
  56. //! Convert ymd to a standard string formatting policies
  57. /*! This is standard code for handling date formatting with
  58. * year-month-day based date information. This function
  59. * uses the format_type to control whether the string will
  60. * contain separator characters, and if so what the character
  61. * will be. In addtion, it can format the month as either
  62. * an integer or a string as controled by the formatting
  63. * policy
  64. */
  65. static std::string ymd_to_string(ymd_type ymd)
  66. {
  67. typedef typename ymd_type::month_type month_type;
  68. std::ostringstream ss;
  69. ss << ymd.year;
  70. if (format_type::has_date_sep_chars()) {
  71. ss << format_type::month_sep_char();
  72. }
  73. //this name is a bit ugly, oh well....
  74. month_formatter<month_type,format_type>::format_month(ymd.month, ss);
  75. if (format_type::has_date_sep_chars()) {
  76. ss << format_type::day_sep_char();
  77. }
  78. ss << std::setw(2) << std::setfill('0')
  79. << ymd.day;
  80. return ss.str();
  81. }
  82. };
  83. //! Convert a date to string using format policies
  84. template<class date_type, class format_type>
  85. class date_formatter
  86. {
  87. public:
  88. //! Convert to a date to standard string using format policies
  89. static std::string date_to_string(date_type d)
  90. {
  91. typedef typename date_type::ymd_type ymd_type;
  92. if (d.is_not_a_date()) {
  93. return format_type::not_a_date();
  94. }
  95. if (d.is_neg_infinity()) {
  96. return format_type::neg_infinity();
  97. }
  98. if (d.is_pos_infinity()) {
  99. return format_type::pos_infinity();
  100. }
  101. ymd_type ymd = d.year_month_day();
  102. return ymd_formatter<ymd_type, format_type>::ymd_to_string(ymd);
  103. }
  104. };
  105. } } //namespace date_time
  106. #endif