formatters.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef GREGORIAN_FORMATTERS_HPP___
  2. #define GREGORIAN_FORMATTERS_HPP___
  3. /* Copyright (c) 2002,2003 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/compiler_config.hpp"
  11. #include "boost/date_time/gregorian/gregorian_types.hpp"
  12. #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
  13. #include "boost/date_time/date_formatting_limited.hpp"
  14. #else
  15. #include "boost/date_time/date_formatting.hpp"
  16. #endif
  17. #include "boost/date_time/iso_format.hpp"
  18. #include "boost/date_time/date_format_simple.hpp"
  19. /* NOTE: "to_*_string" code for older compilers, ones that define
  20. * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
  21. * formatters_limited.hpp
  22. */
  23. namespace boost {
  24. namespace gregorian {
  25. // wrapper function for to_simple_(w)string(date)
  26. template<class charT>
  27. inline
  28. std::basic_string<charT> to_simple_string_type(const date& d) {
  29. return date_time::date_formatter<date,date_time::simple_format<charT>,charT>::date_to_string(d);
  30. }
  31. //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01
  32. /*!\ingroup date_format
  33. */
  34. inline std::string to_simple_string(const date& d) {
  35. return to_simple_string_type<char>(d);
  36. }
  37. // wrapper function for to_simple_(w)string(date_period)
  38. template<class charT>
  39. inline std::basic_string<charT> to_simple_string_type(const date_period& d) {
  40. typedef std::basic_string<charT> string_type;
  41. charT b = '[', m = '/', e=']';
  42. string_type d1(date_time::date_formatter<date,date_time::simple_format<charT>,charT>::date_to_string(d.begin()));
  43. string_type d2(date_time::date_formatter<date,date_time::simple_format<charT>,charT>::date_to_string(d.last()));
  44. return string_type(b + d1 + m + d2 + e);
  45. }
  46. //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02]
  47. /*!\ingroup date_format
  48. */
  49. inline std::string to_simple_string(const date_period& d) {
  50. return to_simple_string_type<char>(d);
  51. }
  52. // wrapper function for to_iso_(w)string(date_period)
  53. template<class charT>
  54. inline std::basic_string<charT> to_iso_string_type(const date_period& d) {
  55. charT sep = '/';
  56. std::basic_string<charT> s(date_time::date_formatter<date,date_time::iso_format<charT>,charT>::date_to_string(d.begin()));
  57. return s + sep + date_time::date_formatter<date,date_time::iso_format<charT>,charT>::date_to_string(d.last());
  58. }
  59. //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231
  60. /*!\ingroup date_format
  61. */
  62. inline std::string to_iso_string(const date_period& d) {
  63. return to_iso_string_type<char>(d);
  64. }
  65. // wrapper function for to_iso_extended_(w)string(date)
  66. template<class charT>
  67. inline std::basic_string<charT> to_iso_extended_string_type(const date& d) {
  68. return date_time::date_formatter<date,date_time::iso_extended_format<charT>,charT>::date_to_string(d);
  69. }
  70. //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31
  71. /*!\ingroup date_format
  72. */
  73. inline std::string to_iso_extended_string(const date& d) {
  74. return to_iso_extended_string_type<char>(d);
  75. }
  76. // wrapper function for to_iso_(w)string(date)
  77. template<class charT>
  78. inline std::basic_string<charT> to_iso_string_type(const date& d) {
  79. return date_time::date_formatter<date,date_time::iso_format<charT>,charT>::date_to_string(d);
  80. }
  81. //! Convert to iso standard string YYYYMMDD. Example: 20021231
  82. /*!\ingroup date_format
  83. */
  84. inline std::string to_iso_string(const date& d) {
  85. return to_iso_string_type<char>(d);
  86. }
  87. // wrapper function for to_sql_(w)string(date)
  88. template<class charT>
  89. inline std::basic_string<charT> to_sql_string_type(const date& d)
  90. {
  91. date::ymd_type ymd = d.year_month_day();
  92. std::basic_ostringstream<charT> ss;
  93. ss << ymd.year << "-"
  94. << std::setw(2) << std::setfill(ss.widen('0'))
  95. << ymd.month.as_number() //solves problem with gcc 3.1 hanging
  96. << "-"
  97. << std::setw(2) << std::setfill(ss.widen('0'))
  98. << ymd.day;
  99. return ss.str();
  100. }
  101. inline std::string to_sql_string(const date& d) {
  102. return to_sql_string_type<char>(d);
  103. }
  104. #if !defined(BOOST_NO_STD_WSTRING)
  105. //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02]
  106. /*!\ingroup date_format
  107. */
  108. inline std::wstring to_simple_wstring(const date_period& d) {
  109. return to_simple_string_type<wchar_t>(d);
  110. }
  111. //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01
  112. /*!\ingroup date_format
  113. */
  114. inline std::wstring to_simple_wstring(const date& d) {
  115. return to_simple_string_type<wchar_t>(d);
  116. }
  117. //! Date period to iso standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231
  118. /*!\ingroup date_format
  119. */
  120. inline std::wstring to_iso_wstring(const date_period& d) {
  121. return to_iso_string_type<wchar_t>(d);
  122. }
  123. //! Convert to iso extended format string CCYY-MM-DD. Example 2002-12-31
  124. /*!\ingroup date_format
  125. */
  126. inline std::wstring to_iso_extended_wstring(const date& d) {
  127. return to_iso_extended_string_type<wchar_t>(d);
  128. }
  129. //! Convert to iso standard string YYYYMMDD. Example: 20021231
  130. /*!\ingroup date_format
  131. */
  132. inline std::wstring to_iso_wstring(const date& d) {
  133. return to_iso_string_type<wchar_t>(d);
  134. }
  135. inline std::wstring to_sql_wstring(const date& d) {
  136. return to_sql_string_type<wchar_t>(d);
  137. }
  138. #endif // BOOST_NO_STD_WSTRING
  139. } } //namespace gregorian
  140. #endif