strings_from_facet.hpp 4.4 KB

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