1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#ifndef DATETIME_PERIOD_FORMATTER_HPP___
#define DATETIME_PERIOD_FORMATTER_HPP___
/* Copyright (c) 2002-2004 CrystalClear Software, Inc.
* Use, modification and distribution is subject to the
* Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
* Author: Jeff Garland, Bart Garst
* $Date$
*/
namespace boost { namespace date_time {
//! Not a facet, but a class used to specify and control period formats
/*! Provides settings for the following:
* - period_separator -- default '/'
* - period_open_start_delimeter -- default '['
* - period_open_range_end_delimeter -- default ')'
* - period_closed_range_end_delimeter -- default ']'
* - display_as_open_range, display_as_closed_range -- default closed_range
*
* Thus the default formatting for a period is as follows:
*@code
* [period.start()/period.last()]
*@endcode
* So for a typical date_period this would be
*@code
* [2004-Jan-04/2004-Feb-01]
*@endcode
* where the date formatting is controlled by the date facet
*/
template <class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
class period_formatter {
public:
typedef std::basic_string<CharT> string_type;
typedef CharT char_type;
typedef typename std::basic_string<char_type>::const_iterator const_itr_type;
typedef std::vector<std::basic_string<CharT> > collection_type;
static const char_type default_period_separator[2];
static const char_type default_period_start_delimeter[2];
static const char_type default_period_open_range_end_delimeter[2];
static const char_type default_period_closed_range_end_delimeter[2];
enum range_display_options { AS_OPEN_RANGE, AS_CLOSED_RANGE };
//! Constructor that sets up period formatter options -- default should suffice most cases.
period_formatter(range_display_options range_option_in = AS_CLOSED_RANGE,
const char_type* const period_separator = default_period_separator,
const char_type* const period_start_delimeter = default_period_start_delimeter,
const char_type* const period_open_range_end_delimeter = default_period_open_range_end_delimeter,
const char_type* const period_closed_range_end_delimeter = default_period_closed_range_end_delimeter) :
m_range_option(range_option_in),
m_period_separator(period_separator),
m_period_start_delimeter(period_start_delimeter),
m_open_range_end_delimeter(period_open_range_end_delimeter),
m_closed_range_end_delimeter(period_closed_range_end_delimeter)
{}
//! Puts the characters between period elements into stream -- default is /
OutItrT put_period_separator(OutItrT& oitr) const
{
const_itr_type ci = m_period_separator.begin();
while (ci != m_period_separator.end()) {
*oitr = *ci;
ci++;
}
return oitr;
}
//! Puts the period start characters into stream -- default is [
OutItrT put_period_start_delimeter(OutItrT& oitr) const
{
const_itr_type ci = m_period_start_delimeter.begin();
while (ci != m_period_start_delimeter.end()) {
*oitr = *ci;
ci++;
}
return oitr;
}
//! Puts the period end characters into stream as controled by open/closed range setting.
OutItrT put_period_end_delimeter(OutItrT& oitr) const
{
const_itr_type ci, end;
if (m_range_option == AS_OPEN_RANGE) {
ci = m_open_range_end_delimeter.begin();
end = m_open_range_end_delimeter.end();
}
else {
ci = m_closed_range_end_delimeter.begin();
end = m_closed_range_end_delimeter.end();
}
while (ci != end) {
*oitr = *ci;
ci++;
}
return oitr;
}
range_display_options range_option() const
{
return m_range_option;
}
//! Reset the range_option control
void
range_option(range_display_options option) const
{
m_range_option = option;
}
void delimiter_strings(const string_type& ,
const string_type& ,
const string_type& ,
const string_type& )
{
m_period_separator;
m_period_start_delimeter;
m_open_range_end_delimeter;
m_closed_range_end_delimeter;
}
//! Generic code to output a period -- no matter the period type.
/*! This generic code will output any period using a facet to
* to output the 'elements'. For example, in the case of a date_period
* the elements will be instances of a date which will be formatted
* according the to setup in the passed facet parameter.
*
* The steps for formatting a period are always the same:
* - put the start delimiter
* - put start element
* - put the separator
* - put either last or end element depending on range settings
* - put end delimeter depending on range settings
*
* Thus for a typical date period the result might look like this:
*@code
*
* [March 01, 2004/June 07, 2004] <-- closed range
* [March 01, 2004/June 08, 2004) <-- open range
*
*@endcode
*/
template<class period_type, class facet_type>
OutItrT put_period(OutItrT next,
std::ios_base& a_ios,
char_type a_fill,
const period_type& p,
const facet_type& facet) const {
put_period_start_delimeter(next);
next = facet.put(next, a_ios, a_fill, p.begin());
put_period_separator(next);
if (m_range_option == AS_CLOSED_RANGE) {
facet.put(next, a_ios, a_fill, p.last());
}
else {
facet.put(next, a_ios, a_fill, p.end());
}
put_period_end_delimeter(next);
return next;
}
private:
range_display_options m_range_option;
string_type m_period_separator;
string_type m_period_start_delimeter;
string_type m_open_range_end_delimeter;
string_type m_closed_range_end_delimeter;
};
template <class CharT, class OutItrT>
const typename period_formatter<CharT, OutItrT>::char_type
period_formatter<CharT, OutItrT>::default_period_separator[2] = {'/'};
template <class CharT, class OutItrT>
const typename period_formatter<CharT, OutItrT>::char_type
period_formatter<CharT, OutItrT>::default_period_start_delimeter[2] = {'['};
template <class CharT, class OutItrT>
const typename period_formatter<CharT, OutItrT>::char_type
period_formatter<CharT, OutItrT>::default_period_open_range_end_delimeter[2] = {')'};
template <class CharT, class OutItrT>
const typename period_formatter<CharT, OutItrT>::char_type
period_formatter<CharT, OutItrT>::default_period_closed_range_end_delimeter[2] = {']'};
} } //namespace boost::date_time
#endif
|