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