1 #ifndef DATE_TIME_STRINGS_FROM_FACET__HPP___
2 #define DATE_TIME_STRINGS_FROM_FACET__HPP___
4 /* Copyright (c) 2004 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
17 namespace boost { namespace date_time {
19 //! This function gathers up all the month strings from a std::locale
20 /*! Using the time_put facet, this function creates a collection of
21 * all the month strings from a locale. This is handy when building
22 * custom date parsers or formatters that need to be localized.
24 *@param charT The type of char to use when gathering typically char
26 *@param locale The locale to use when gathering the strings
27 *@param short_strings True(default) to gather short strings,
28 * false for long strings.
29 *@return A vector of strings containing the strings in order. eg:
32 template<typename charT>
33 std::vector<std::basic_string<charT> >
34 gather_month_strings(const std::locale& locale, bool short_strings=true)
36 typedef std::basic_string<charT> string_type;
37 typedef std::vector<string_type> collection_type;
38 typedef std::ostreambuf_iterator<charT> ostream_iter_type;
39 typedef std::basic_ostringstream<charT> stringstream_type;
40 typedef std::time_put<charT> time_put_facet_type;
41 charT short_fmt[3] = { '%', 'b' };
42 charT long_fmt[3] = { '%', 'B' };
43 collection_type months;
44 string_type outfmt(short_fmt);
49 //grab the needed strings by using the locale to
51 const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
53 memset(&tm_value, 0, sizeof(tm_value));
54 for (int m=0; m < 12; m++) {
57 ostream_iter_type oitr(ss);
58 std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
62 months.push_back(ss.str());
68 //! This function gathers up all the weekday strings from a std::locale
69 /*! Using the time_put facet, this function creates a collection of
70 * all the weekday strings from a locale starting with the string for
71 * 'Sunday'. This is handy when building custom date parsers or
72 * formatters that need to be localized.
74 *@param charT The type of char to use when gathering typically char
76 *@param locale The locale to use when gathering the strings
77 *@param short_strings True(default) to gather short strings,
78 * false for long strings.
79 *@return A vector of strings containing the weekdays in order. eg:
80 * Sun, Mon, Tue, Wed, Thu, Fri, Sat
82 template<typename charT>
83 std::vector<std::basic_string<charT> >
84 gather_weekday_strings(const std::locale& locale, bool short_strings=true)
86 typedef std::basic_string<charT> string_type;
87 typedef std::vector<string_type> collection_type;
88 typedef std::ostreambuf_iterator<charT> ostream_iter_type;
89 typedef std::basic_ostringstream<charT> stringstream_type;
90 typedef std::time_put<charT> time_put_facet_type;
91 charT short_fmt[3] = { '%', 'a' };
92 charT long_fmt[3] = { '%', 'A' };
94 collection_type weekdays;
97 string_type outfmt(short_fmt);
102 //grab the needed strings by using the locale to
103 //output each month / weekday
104 const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
106 memset(&tm_value, 0, sizeof(tm_value));
107 for (int i=0; i < 7; i++) {
108 tm_value.tm_wday = i;
109 stringstream_type ss;
110 ostream_iter_type oitr(ss);
111 std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
116 weekdays.push_back(ss.str());