formatters_limited.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef GREGORIAN_FORMATTERS_LIMITED_HPP___
  2. #define GREGORIAN_FORMATTERS_LIMITED_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/gregorian/gregorian_types.hpp"
  11. #include "boost/date_time/date_formatting_limited.hpp"
  12. #include "boost/date_time/iso_format.hpp"
  13. #include "boost/date_time/date_format_simple.hpp"
  14. #include "boost/date_time/compiler_config.hpp"
  15. namespace boost {
  16. namespace gregorian {
  17. //! To YYYY-mmm-DD string where mmm 3 char month name. Example: 2002-Jan-01
  18. /*!\ingroup date_format
  19. */
  20. inline std::string to_simple_string(const date& d) {
  21. return date_time::date_formatter<date,date_time::simple_format<char> >::date_to_string(d);
  22. }
  23. //! Convert date period to simple string. Example: [2002-Jan-01/2002-Jan-02]
  24. /*!\ingroup date_format
  25. */
  26. inline std::string to_simple_string(const date_period& d) {
  27. std::string s("[");
  28. std::string d1(date_time::date_formatter<date,date_time::simple_format<char> >::date_to_string(d.begin()));
  29. std::string d2(date_time::date_formatter<date,date_time::simple_format<char> >::date_to_string(d.last()));
  30. return std::string("[" + d1 + "/" + d2 + "]");
  31. }
  32. //! Date period to ISO 8601 standard format CCYYMMDD/CCYYMMDD. Example: 20021225/20021231
  33. /*!\ingroup date_format
  34. */
  35. inline std::string to_iso_string(const date_period& d) {
  36. std::string s(date_time::date_formatter<date,date_time::iso_format<char> >::date_to_string(d.begin()));
  37. return s + "/" + date_time::date_formatter<date,date_time::iso_format<char> >::date_to_string(d.last());
  38. }
  39. //! Convert to ISO 8601 extended format string CCYY-MM-DD. Example 2002-12-31
  40. /*!\ingroup date_format
  41. */
  42. inline std::string to_iso_extended_string(const date& d) {
  43. return date_time::date_formatter<date,date_time::iso_extended_format<char> >::date_to_string(d);
  44. }
  45. //! Convert to ISO 8601 standard string YYYYMMDD. Example: 20021231
  46. /*!\ingroup date_format
  47. */
  48. inline std::string to_iso_string(const date& d) {
  49. return date_time::date_formatter<date,date_time::iso_format<char> >::date_to_string(d);
  50. }
  51. inline std::string to_sql_string(const date& d)
  52. {
  53. date::ymd_type ymd = d.year_month_day();
  54. std::ostringstream ss;
  55. ss << ymd.year << "-"
  56. << std::setw(2) << std::setfill('0')
  57. << ymd.month.as_number() //solves problem with gcc 3.1 hanging
  58. << "-"
  59. << std::setw(2) << std::setfill('0')
  60. << ymd.day;
  61. return ss.str();
  62. }
  63. } } //namespace gregorian
  64. #endif