strings_from_facet.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef DATE_TIME_STRINGS_FROM_FACET__HPP___
  2. #define DATE_TIME_STRINGS_FROM_FACET__HPP___
  3. /* Copyright (c) 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
  8. * $Date$
  9. */
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. #include <locale>
  14. namespace boost { namespace date_time {
  15. //! This function gathers up all the month strings from a std::locale
  16. /*! Using the time_put facet, this function creates a collection of
  17. * all the month strings from a locale. This is handy when building
  18. * custom date parsers or formatters that need to be localized.
  19. *
  20. *@param charT The type of char to use when gathering typically char
  21. * or wchar_t.
  22. *@param locale The locale to use when gathering the strings
  23. *@param short_strings True(default) to gather short strings,
  24. * false for long strings.
  25. *@return A vector of strings containing the strings in order. eg:
  26. * Jan, Feb, Mar, etc.
  27. */
  28. template<typename charT>
  29. std::vector<std::basic_string<charT> >
  30. gather_month_strings(const std::locale& locale, bool short_strings=true)
  31. {
  32. typedef std::basic_string<charT> string_type;
  33. typedef std::vector<string_type> collection_type;
  34. typedef std::ostreambuf_iterator<charT> ostream_iter_type;
  35. typedef std::basic_ostringstream<charT> stringstream_type;
  36. typedef std::time_put<charT> time_put_facet_type;
  37. charT short_fmt[3] = { '%', 'b' };
  38. charT long_fmt[3] = { '%', 'B' };
  39. collection_type months;
  40. string_type outfmt(short_fmt);
  41. if (!short_strings) {
  42. outfmt = long_fmt;
  43. }
  44. {
  45. //grab the needed strings by using the locale to
  46. //output each month
  47. const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
  48. tm tm_value;
  49. memset(&tm_value, 0, sizeof(tm_value));
  50. for (int m=0; m < 12; m++) {
  51. tm_value.tm_mon = m;
  52. stringstream_type ss;
  53. ostream_iter_type oitr(ss);
  54. std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
  55. &tm_value,
  56. p_outfmt,
  57. p_outfmt_end);
  58. months.push_back(ss.str());
  59. }
  60. }
  61. return months;
  62. }
  63. //! This function gathers up all the weekday strings from a std::locale
  64. /*! Using the time_put facet, this function creates a collection of
  65. * all the weekday strings from a locale starting with the string for
  66. * 'Sunday'. This is handy when building custom date parsers or
  67. * formatters that need to be localized.
  68. *
  69. *@param charT The type of char to use when gathering typically char
  70. * or wchar_t.
  71. *@param locale The locale to use when gathering the strings
  72. *@param short_strings True(default) to gather short strings,
  73. * false for long strings.
  74. *@return A vector of strings containing the weekdays in order. eg:
  75. * Sun, Mon, Tue, Wed, Thu, Fri, Sat
  76. */
  77. template<typename charT>
  78. std::vector<std::basic_string<charT> >
  79. gather_weekday_strings(const std::locale& locale, bool short_strings=true)
  80. {
  81. typedef std::basic_string<charT> string_type;
  82. typedef std::vector<string_type> collection_type;
  83. typedef std::ostreambuf_iterator<charT> ostream_iter_type;
  84. typedef std::basic_ostringstream<charT> stringstream_type;
  85. typedef std::time_put<charT> time_put_facet_type;
  86. charT short_fmt[3] = { '%', 'a' };
  87. charT long_fmt[3] = { '%', 'A' };
  88. collection_type weekdays;
  89. string_type outfmt(short_fmt);
  90. if (!short_strings) {
  91. outfmt = long_fmt;
  92. }
  93. {
  94. //grab the needed strings by using the locale to
  95. //output each month / weekday
  96. const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
  97. tm tm_value;
  98. memset(&tm_value, 0, sizeof(tm_value));
  99. for (int i=0; i < 7; i++) {
  100. tm_value.tm_wday = i;
  101. stringstream_type ss;
  102. ostream_iter_type oitr(ss);
  103. std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
  104. &tm_value,
  105. p_outfmt,
  106. p_outfmt_end);
  107. weekdays.push_back(ss.str());
  108. }
  109. }
  110. return weekdays;
  111. }
  112. } } //namespace
  113. #endif