strings_from_facet.hpp 4.5 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: 2013-09-21 13:17:00 -0700 (Sat, 21 Sep 2013) $
  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::basic_ostringstream<charT> ostream_type;
  35. typedef std::ostreambuf_iterator<charT> ostream_iter_type;
  36. typedef std::basic_ostringstream<charT> stringstream_type;
  37. typedef std::time_put<charT> time_put_facet_type;
  38. charT short_fmt[3] = { '%', 'b' };
  39. charT long_fmt[3] = { '%', 'B' };
  40. collection_type months;
  41. string_type outfmt(short_fmt);
  42. if (!short_strings) {
  43. outfmt = long_fmt;
  44. }
  45. {
  46. //grab the needed strings by using the locale to
  47. //output each month
  48. const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
  49. tm tm_value;
  50. memset(&tm_value, 0, sizeof(tm_value));
  51. for (int m=0; m < 12; m++) {
  52. tm_value.tm_mon = m;
  53. stringstream_type ss;
  54. ostream_iter_type oitr(ss);
  55. std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
  56. &tm_value,
  57. p_outfmt,
  58. p_outfmt_end);
  59. months.push_back(ss.str());
  60. }
  61. }
  62. return months;
  63. }
  64. //! This function gathers up all the weekday strings from a std::locale
  65. /*! Using the time_put facet, this function creates a collection of
  66. * all the weekday strings from a locale starting with the string for
  67. * 'Sunday'. This is handy when building custom date parsers or
  68. * formatters that need to be localized.
  69. *
  70. *@param charT The type of char to use when gathering typically char
  71. * or wchar_t.
  72. *@param locale The locale to use when gathering the strings
  73. *@param short_strings True(default) to gather short strings,
  74. * false for long strings.
  75. *@return A vector of strings containing the weekdays in order. eg:
  76. * Sun, Mon, Tue, Wed, Thu, Fri, Sat
  77. */
  78. template<typename charT>
  79. std::vector<std::basic_string<charT> >
  80. gather_weekday_strings(const std::locale& locale, bool short_strings=true)
  81. {
  82. typedef std::basic_string<charT> string_type;
  83. typedef std::vector<string_type> collection_type;
  84. typedef std::basic_ostringstream<charT> ostream_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. 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