1 | #ifndef _DATE_TIME_DATE_GENERATOR_FORMATTER__HPP___
|
---|
2 | #define _DATE_TIME_DATE_GENERATOR_FORMATTER__HPP___
|
---|
3 |
|
---|
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 or http://www.boost.org/LICENSE-1.0)
|
---|
8 | * Author: Jeff Garland, Bart Garst
|
---|
9 | * $Date: 2005/03/28 14:27:09 $
|
---|
10 | */
|
---|
11 |
|
---|
12 | #include <iostream>
|
---|
13 | #include <string>
|
---|
14 | #include <vector>
|
---|
15 | #include <algorithm>
|
---|
16 | #include "boost/date_time/date_generators.hpp"
|
---|
17 |
|
---|
18 | namespace boost {
|
---|
19 | namespace date_time {
|
---|
20 |
|
---|
21 | //! Formats date_generators for output
|
---|
22 | /*! Formatting of date_generators follows specific orders for the
|
---|
23 | * various types of date_generators.
|
---|
24 | * - partial_date => "dd Month"
|
---|
25 | * - nth_day_of_the_week_in_month => "nth weekday of month"
|
---|
26 | * - first_day_of_the_week_in_month => "first weekday of month"
|
---|
27 | * - last_day_of_the_week_in_month => "last weekday of month"
|
---|
28 | * - first_day_of_the_week_after => "weekday after"
|
---|
29 | * - first_day_of_the_week_before => "weekday before"
|
---|
30 | * While the order of the elements in these phrases cannot be changed,
|
---|
31 | * the elements themselves can be. Weekday and Month get their formats
|
---|
32 | * and names from the date_facet. The remaining elements are stored in
|
---|
33 | * the date_generator_formatter and can be customized upon construction
|
---|
34 | * or via a member function. The default elements are those shown in the
|
---|
35 | * examples above.
|
---|
36 | */
|
---|
37 | template <class date_type, class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
|
---|
38 | class date_generator_formatter {
|
---|
39 | public:
|
---|
40 | typedef partial_date<date_type> partial_date_type;
|
---|
41 | typedef nth_kday_of_month<date_type> nth_kday_type;
|
---|
42 | typedef first_kday_of_month<date_type> first_kday_type;
|
---|
43 | typedef last_kday_of_month<date_type> last_kday_type;
|
---|
44 | typedef first_kday_after<date_type> kday_after_type;
|
---|
45 | typedef first_kday_before<date_type> kday_before_type;
|
---|
46 |
|
---|
47 | typedef CharT char_type;
|
---|
48 | typedef std::basic_string<char_type> string_type;
|
---|
49 | typedef std::vector<string_type> collection_type;
|
---|
50 | static const char_type first_string[6];
|
---|
51 | static const char_type second_string[7];
|
---|
52 | static const char_type third_string[6];
|
---|
53 | static const char_type fourth_string[7];
|
---|
54 | static const char_type fifth_string[6];
|
---|
55 | static const char_type last_string[5];
|
---|
56 | static const char_type before_string[8];
|
---|
57 | static const char_type after_string[6];
|
---|
58 | static const char_type of_string[3];
|
---|
59 |
|
---|
60 | enum phrase_elements {first=0, second, third, fourth, fifth, last,
|
---|
61 | before, after, of, number_of_phrase_elements};
|
---|
62 |
|
---|
63 | //! Default format elements used
|
---|
64 | date_generator_formatter()
|
---|
65 | {
|
---|
66 | phrase_strings.push_back(string_type(first_string));
|
---|
67 | phrase_strings.push_back(string_type(second_string));
|
---|
68 | phrase_strings.push_back(string_type(third_string));
|
---|
69 | phrase_strings.push_back(string_type(fourth_string));
|
---|
70 | phrase_strings.push_back(string_type(fifth_string));
|
---|
71 | phrase_strings.push_back(string_type(last_string));
|
---|
72 | phrase_strings.push_back(string_type(before_string));
|
---|
73 | phrase_strings.push_back(string_type(after_string));
|
---|
74 | phrase_strings.push_back(string_type(of_string));
|
---|
75 | }
|
---|
76 |
|
---|
77 | //! Constructor that allows for a custom set of phrase elements
|
---|
78 | date_generator_formatter(const string_type& first,
|
---|
79 | const string_type& second,
|
---|
80 | const string_type& third,
|
---|
81 | const string_type& fourth,
|
---|
82 | const string_type& fifth,
|
---|
83 | const string_type& last,
|
---|
84 | const string_type& before,
|
---|
85 | const string_type& after,
|
---|
86 | const string_type& of)
|
---|
87 | {
|
---|
88 | phrase_strings.push_back(string_type(first_string));
|
---|
89 | phrase_strings.push_back(string_type(second_string));
|
---|
90 | phrase_strings.push_back(string_type(third_string));
|
---|
91 | phrase_strings.push_back(string_type(fourth_string));
|
---|
92 | phrase_strings.push_back(string_type(fifth_string));
|
---|
93 | phrase_strings.push_back(string_type(last_string));
|
---|
94 | phrase_strings.push_back(string_type(before_string));
|
---|
95 | phrase_strings.push_back(string_type(after_string));
|
---|
96 | phrase_strings.push_back(string_type(of_string));
|
---|
97 | }
|
---|
98 |
|
---|
99 | //! Replace the set of phrase elements with those contained in new_strings
|
---|
100 | /*! The order of the strings in the given collection is important.
|
---|
101 | * They must follow:
|
---|
102 | * - first, second, third, fourth, fifth, last, before, after, of.
|
---|
103 | *
|
---|
104 | * It is not necessary to send in a complete set if only a few
|
---|
105 | * elements are to be replaced as long as the correct beg_pos is used.
|
---|
106 | *
|
---|
107 | * Ex: To keep the default first through fifth elements, but replace
|
---|
108 | * the rest with a collection of:
|
---|
109 | * - "final", "prior", "following", "in".
|
---|
110 | * The beg_pos of date_generator_formatter::last would be used.
|
---|
111 | */
|
---|
112 | void elements(const collection_type& new_strings,
|
---|
113 | phrase_elements beg_pos=first)
|
---|
114 | {
|
---|
115 | if(beg_pos < number_of_phrase_elements) {
|
---|
116 | typename collection_type::iterator itr = phrase_strings.begin();
|
---|
117 | itr += beg_pos;
|
---|
118 | std::copy(new_strings.begin(), new_strings.end(),
|
---|
119 | itr);
|
---|
120 | //phrase_strings.begin());
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | //!Put a partial_date => "dd Month"
|
---|
125 | template<class facet_type>
|
---|
126 | OutItrT put_partial_date(OutItrT next, std::ios_base& a_ios,
|
---|
127 | CharT a_fill, const partial_date_type& pd,
|
---|
128 | const facet_type& facet) const
|
---|
129 | {
|
---|
130 | facet.put(next, a_ios, a_fill, pd.day());
|
---|
131 | next = a_fill; //TODO change this ???
|
---|
132 | facet.put(next, a_ios, a_fill, pd.month());
|
---|
133 | return next;
|
---|
134 | }
|
---|
135 |
|
---|
136 | //! Put an nth_day_of_the_week_in_month => "nth weekday of month"
|
---|
137 | template<class facet_type>
|
---|
138 | OutItrT put_nth_kday(OutItrT next, std::ios_base& a_ios,
|
---|
139 | CharT a_fill, const nth_kday_type& nkd,
|
---|
140 | const facet_type& facet) const
|
---|
141 | {
|
---|
142 | put_string(next, phrase_strings[nkd.nth_week() -1]);
|
---|
143 | next = a_fill; //TODO change this ???
|
---|
144 | facet.put(next, a_ios, a_fill, nkd.day_of_week());
|
---|
145 | next = a_fill; //TODO change this ???
|
---|
146 | put_string(next, string_type(of_string));
|
---|
147 | next = a_fill; //TODO change this ???
|
---|
148 | facet.put(next, a_ios, a_fill, nkd.month());
|
---|
149 | return next;
|
---|
150 | }
|
---|
151 |
|
---|
152 | //! Put a first_day_of_the_week_in_month => "first weekday of month"
|
---|
153 | template<class facet_type>
|
---|
154 | OutItrT put_first_kday(OutItrT next, std::ios_base& a_ios,
|
---|
155 | CharT a_fill, const first_kday_type& fkd,
|
---|
156 | const facet_type& facet) const
|
---|
157 | {
|
---|
158 | put_string(next, phrase_strings[first]);
|
---|
159 | next = a_fill; //TODO change this ???
|
---|
160 | facet.put(next, a_ios, a_fill, fkd.day_of_week());
|
---|
161 | next = a_fill; //TODO change this ???
|
---|
162 | put_string(next, string_type(of_string));
|
---|
163 | next = a_fill; //TODO change this ???
|
---|
164 | facet.put(next, a_ios, a_fill, fkd.month());
|
---|
165 | return next;
|
---|
166 | }
|
---|
167 |
|
---|
168 | //! Put a last_day_of_the_week_in_month => "last weekday of month"
|
---|
169 | template<class facet_type>
|
---|
170 | OutItrT put_last_kday(OutItrT next, std::ios_base& a_ios,
|
---|
171 | CharT a_fill, const last_kday_type& lkd,
|
---|
172 | const facet_type& facet) const
|
---|
173 | {
|
---|
174 | put_string(next, phrase_strings[last]);
|
---|
175 | next = a_fill; //TODO change this ???
|
---|
176 | facet.put(next, a_ios, a_fill, lkd.day_of_week());
|
---|
177 | next = a_fill; //TODO change this ???
|
---|
178 | put_string(next, string_type(of_string));
|
---|
179 | next = a_fill; //TODO change this ???
|
---|
180 | facet.put(next, a_ios, a_fill, lkd.month());
|
---|
181 | return next;
|
---|
182 | }
|
---|
183 |
|
---|
184 | //! Put a first_day_of_the_week_before => "weekday before"
|
---|
185 | template<class facet_type>
|
---|
186 | OutItrT put_kday_before(OutItrT next, std::ios_base& a_ios,
|
---|
187 | CharT a_fill, const kday_before_type& fkb,
|
---|
188 | const facet_type& facet) const
|
---|
189 | {
|
---|
190 | facet.put(next, a_ios, a_fill, fkb.day_of_week());
|
---|
191 | next = a_fill; //TODO change this ???
|
---|
192 | put_string(next, phrase_strings[before]);
|
---|
193 | return next;
|
---|
194 | }
|
---|
195 |
|
---|
196 | //! Put a first_day_of_the_week_after => "weekday after"
|
---|
197 | template<class facet_type>
|
---|
198 | OutItrT put_kday_after(OutItrT next, std::ios_base& a_ios,
|
---|
199 | CharT a_fill, const kday_after_type& fka,
|
---|
200 | const facet_type& facet) const
|
---|
201 | {
|
---|
202 | facet.put(next, a_ios, a_fill, fka.day_of_week());
|
---|
203 | next = a_fill; //TODO change this ???
|
---|
204 | put_string(next, phrase_strings[after]);
|
---|
205 | return next;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | private:
|
---|
210 | collection_type phrase_strings;
|
---|
211 |
|
---|
212 | //! helper function to put the various member string into stream
|
---|
213 | OutItrT put_string(OutItrT next, const string_type& str) const
|
---|
214 | {
|
---|
215 | typename string_type::const_iterator itr = str.begin();
|
---|
216 | while(itr != str.end()) {
|
---|
217 | *next = *itr;
|
---|
218 | ++itr;
|
---|
219 | ++next;
|
---|
220 | }
|
---|
221 | return next;
|
---|
222 | }
|
---|
223 | };
|
---|
224 |
|
---|
225 | template<class date_type, class CharT, class OutItrT>
|
---|
226 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
227 | date_generator_formatter<date_type, CharT, OutItrT>::first_string[6] =
|
---|
228 | {'f','i','r','s','t'};
|
---|
229 | template<class date_type, class CharT, class OutItrT>
|
---|
230 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
231 | date_generator_formatter<date_type, CharT, OutItrT>::second_string[7] =
|
---|
232 | {'s','e','c','o','n','d'};
|
---|
233 | template<class date_type, class CharT, class OutItrT>
|
---|
234 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
235 | date_generator_formatter<date_type, CharT, OutItrT>::third_string[6] =
|
---|
236 | {'t','h','i','r','d'};
|
---|
237 | template<class date_type, class CharT, class OutItrT>
|
---|
238 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
239 | date_generator_formatter<date_type, CharT, OutItrT>::fourth_string[7] =
|
---|
240 | {'f','o','u','r','t','h'};
|
---|
241 | template<class date_type, class CharT, class OutItrT>
|
---|
242 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
243 | date_generator_formatter<date_type, CharT, OutItrT>::fifth_string[6] =
|
---|
244 | {'f','i','f','t','h'};
|
---|
245 | template<class date_type, class CharT, class OutItrT>
|
---|
246 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
247 | date_generator_formatter<date_type, CharT, OutItrT>::last_string[5] =
|
---|
248 | {'l','a','s','t'};
|
---|
249 | template<class date_type, class CharT, class OutItrT>
|
---|
250 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
251 | date_generator_formatter<date_type, CharT, OutItrT>::before_string[8] =
|
---|
252 | {'b','e','f','o','r','e'};
|
---|
253 | template<class date_type, class CharT, class OutItrT>
|
---|
254 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
255 | date_generator_formatter<date_type, CharT, OutItrT>::after_string[6] =
|
---|
256 | {'a','f','t','e','r'};
|
---|
257 | template<class date_type, class CharT, class OutItrT>
|
---|
258 | const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
|
---|
259 | date_generator_formatter<date_type, CharT, OutItrT>::of_string[3] =
|
---|
260 | {'o','f'};
|
---|
261 | } } // namespaces
|
---|
262 |
|
---|
263 | #endif // _DATE_TIME_DATE_GENERATOR_FORMATTER__HPP___
|
---|