Chris@16
|
1 //
|
Chris@16
|
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 #ifndef BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_LOCALE_DATE_TIME_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/locale/config.hpp>
|
Chris@16
|
12 #ifdef BOOST_MSVC
|
Chris@16
|
13 # pragma warning(push)
|
Chris@16
|
14 # pragma warning(disable : 4275 4251 4231 4660)
|
Chris@16
|
15 #endif
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/locale/hold_ptr.hpp>
|
Chris@16
|
18 #include <boost/locale/date_time_facet.hpp>
|
Chris@16
|
19 #include <boost/locale/formatting.hpp>
|
Chris@16
|
20 #include <boost/locale/time_zone.hpp>
|
Chris@16
|
21 #include <locale>
|
Chris@16
|
22 #include <vector>
|
Chris@16
|
23 #include <stdexcept>
|
Chris@16
|
24
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost {
|
Chris@16
|
27 namespace locale {
|
Chris@16
|
28 ///
|
Chris@16
|
29 /// \defgroup date_time Date, Time, Timezone and Calendar manipulations
|
Chris@16
|
30 ///
|
Chris@16
|
31 /// This module provides various calendar, timezone and date time services
|
Chris@16
|
32 ///
|
Chris@16
|
33 /// @{
|
Chris@16
|
34
|
Chris@16
|
35
|
Chris@16
|
36 ///
|
Chris@16
|
37 /// \brief This error is thrown in case of invalid state that occurred
|
Chris@16
|
38 ///
|
Chris@16
|
39 class BOOST_SYMBOL_VISIBLE date_time_error : public std::runtime_error {
|
Chris@16
|
40 public:
|
Chris@16
|
41 ///
|
Chris@16
|
42 /// Constructor of date_time_error class
|
Chris@16
|
43 ///
|
Chris@16
|
44 date_time_error(std::string const &e) : std::runtime_error(e) {}
|
Chris@16
|
45 };
|
Chris@16
|
46
|
Chris@16
|
47
|
Chris@16
|
48 ///
|
Chris@16
|
49 /// \brief This class represents a pair of period_type and the integer
|
Chris@16
|
50 /// values that describes its amount. For example 3 days or 4 years.
|
Chris@16
|
51 ///
|
Chris@16
|
52 /// Usually obtained as product of period_type and integer or
|
Chris@16
|
53 /// my calling a representative functions
|
Chris@16
|
54 /// For example day()*3 == date_time_period(day(),3) == day(3)
|
Chris@16
|
55 ///
|
Chris@16
|
56 struct date_time_period
|
Chris@16
|
57 {
|
Chris@16
|
58 period::period_type type; ///< The type of period, i.e. era, year, day etc.
|
Chris@16
|
59 int value; ///< The value the actual number of \a periods
|
Chris@16
|
60 ///
|
Chris@16
|
61 /// Operator + returns copy of itself
|
Chris@16
|
62 ///
|
Chris@16
|
63 date_time_period operator+() const { return *this; }
|
Chris@16
|
64 ///
|
Chris@16
|
65 /// Operator -, switches the sign of period
|
Chris@16
|
66 ///
|
Chris@16
|
67 date_time_period operator-() const { return date_time_period(type,-value); }
|
Chris@16
|
68
|
Chris@16
|
69 ///
|
Chris@16
|
70 /// Constructor that creates date_time_period from period_type \a f and a value \a v -- default 1.
|
Chris@16
|
71 ///
|
Chris@16
|
72 date_time_period(period::period_type f=period::period_type(),int v=1) : type(f), value(v) {}
|
Chris@16
|
73 };
|
Chris@16
|
74
|
Chris@16
|
75 namespace period {
|
Chris@16
|
76 ///
|
Chris@16
|
77 /// Get period_type for: special invalid value, should not be used directly
|
Chris@16
|
78 ///
|
Chris@16
|
79 inline period_type invalid(){ return period_type(marks::invalid); }
|
Chris@16
|
80 ///
|
Chris@16
|
81 /// Get period_type for: Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
|
Chris@16
|
82 ///
|
Chris@16
|
83 inline period_type era(){ return period_type(marks::era); }
|
Chris@16
|
84 ///
|
Chris@16
|
85 /// Get period_type for: Year, it is calendar specific, for example 2011 in Gregorian calendar.
|
Chris@16
|
86 ///
|
Chris@16
|
87 inline period_type year(){ return period_type(marks::year); }
|
Chris@16
|
88 ///
|
Chris@16
|
89 /// Get period_type for: Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
|
Chris@16
|
90 ///
|
Chris@16
|
91 inline period_type extended_year(){ return period_type(marks::extended_year); }
|
Chris@16
|
92 ///
|
Chris@16
|
93 /// Get period_type for: The month of year, calendar specific, in Gregorian [0..11]
|
Chris@16
|
94 ///
|
Chris@16
|
95 inline period_type month(){ return period_type(marks::month); }
|
Chris@16
|
96 ///
|
Chris@16
|
97 /// Get period_type for: The day of month, calendar specific, in Gregorian [1..31]
|
Chris@16
|
98 ///
|
Chris@16
|
99 inline period_type day(){ return period_type(marks::day); }
|
Chris@16
|
100 ///
|
Chris@16
|
101 /// Get period_type for: The number of day in year, starting from 1, in Gregorian [1..366]
|
Chris@16
|
102 ///
|
Chris@16
|
103 inline period_type day_of_year(){ return period_type(marks::day_of_year); }
|
Chris@16
|
104 ///
|
Chris@16
|
105 /// Get period_type for: Day of week, Sunday=1, Monday=2,..., Saturday=7.
|
Chris@16
|
106 ///
|
Chris@16
|
107 /// Note that updating this value respects local day of week, so for example,
|
Chris@16
|
108 /// If first day of week is Monday and the current day is Tuesday then setting
|
Chris@16
|
109 /// the value to Sunday (1) would forward the date by 5 days forward and not backward
|
Chris@16
|
110 /// by two days as it could be expected if the numbers were taken as is.
|
Chris@16
|
111 ///
|
Chris@16
|
112 inline period_type day_of_week(){ return period_type(marks::day_of_week); }
|
Chris@16
|
113 ///
|
Chris@16
|
114 /// Get period_type for: Original number of the day of the week in month. For example 1st Sunday,
|
Chris@16
|
115 /// 2nd Sunday, etc. in Gregorian [1..5]
|
Chris@16
|
116 ///
|
Chris@16
|
117 inline period_type day_of_week_in_month(){ return period_type(marks::day_of_week_in_month); }
|
Chris@16
|
118 ///
|
Chris@16
|
119 /// Get period_type for: Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7]
|
Chris@16
|
120 ///
|
Chris@16
|
121 inline period_type day_of_week_local(){ return period_type(marks::day_of_week_local); }
|
Chris@16
|
122 ///
|
Chris@16
|
123 /// Get period_type for: 24 clock hour [0..23]
|
Chris@16
|
124 ///
|
Chris@16
|
125 inline period_type hour(){ return period_type(marks::hour); }
|
Chris@16
|
126 ///
|
Chris@16
|
127 /// Get period_type for: 12 clock hour [0..11]
|
Chris@16
|
128 ///
|
Chris@16
|
129 inline period_type hour_12(){ return period_type(marks::hour_12); }
|
Chris@16
|
130 ///
|
Chris@16
|
131 /// Get period_type for: am or pm marker [0..1]
|
Chris@16
|
132 ///
|
Chris@16
|
133 inline period_type am_pm(){ return period_type(marks::am_pm); }
|
Chris@16
|
134 ///
|
Chris@16
|
135 /// Get period_type for: minute [0..59]
|
Chris@16
|
136 ///
|
Chris@16
|
137 inline period_type minute(){ return period_type(marks::minute); }
|
Chris@16
|
138 ///
|
Chris@16
|
139 /// Get period_type for: second [0..59]
|
Chris@16
|
140 ///
|
Chris@16
|
141 inline period_type second(){ return period_type(marks::second); }
|
Chris@16
|
142 ///
|
Chris@16
|
143 /// Get period_type for: The week number in the year
|
Chris@16
|
144 ///
|
Chris@16
|
145 inline period_type week_of_year(){ return period_type(marks::week_of_year); }
|
Chris@16
|
146 ///
|
Chris@16
|
147 /// Get period_type for: The week number within current month
|
Chris@16
|
148 ///
|
Chris@16
|
149 inline period_type week_of_month(){ return period_type(marks::week_of_month); }
|
Chris@16
|
150 ///
|
Chris@16
|
151 /// Get period_type for: First day of week, constant, for example Sunday in US = 1, Monday in France = 2
|
Chris@16
|
152 ///
|
Chris@16
|
153 inline period_type first_day_of_week(){ return period_type(marks::first_day_of_week); }
|
Chris@16
|
154
|
Chris@16
|
155 ///
|
Chris@16
|
156 /// Get date_time_period for: Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
|
Chris@16
|
157 ///
|
Chris@16
|
158 inline date_time_period era(int v) { return date_time_period(era(),v); }
|
Chris@16
|
159 ///
|
Chris@16
|
160 /// Get date_time_period for: Year, it is calendar specific, for example 2011 in Gregorian calendar.
|
Chris@16
|
161 ///
|
Chris@16
|
162 inline date_time_period year(int v) { return date_time_period(year(),v); }
|
Chris@16
|
163 ///
|
Chris@16
|
164 /// Get date_time_period for: Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
|
Chris@16
|
165 ///
|
Chris@16
|
166 inline date_time_period extended_year(int v) { return date_time_period(extended_year(),v); }
|
Chris@16
|
167 ///
|
Chris@16
|
168 /// Get date_time_period for: The month of year, calendar specific, in Gregorian [0..11]
|
Chris@16
|
169 ///
|
Chris@16
|
170 inline date_time_period month(int v) { return date_time_period(month(),v); }
|
Chris@16
|
171 ///
|
Chris@16
|
172 /// Get date_time_period for: The day of month, calendar specific, in Gregorian [1..31]
|
Chris@16
|
173 ///
|
Chris@16
|
174 inline date_time_period day(int v) { return date_time_period(day(),v); }
|
Chris@16
|
175 ///
|
Chris@16
|
176 /// Get date_time_period for: The number of day in year, starting from 1, in Gregorian [1..366]
|
Chris@16
|
177 ///
|
Chris@16
|
178 inline date_time_period day_of_year(int v) { return date_time_period(day_of_year(),v); }
|
Chris@16
|
179 ///
|
Chris@16
|
180 /// Get date_time_period for: Day of week, Sunday=1, Monday=2,..., Saturday=7.
|
Chris@16
|
181 ///
|
Chris@16
|
182 /// Note that updating this value respects local day of week, so for example,
|
Chris@16
|
183 /// If first day of week is Monday and the current day is Tuesday then setting
|
Chris@16
|
184 /// the value to Sunday (1) would forward the date by 5 days forward and not backward
|
Chris@16
|
185 /// by two days as it could be expected if the numbers were taken as is.
|
Chris@16
|
186 ///
|
Chris@16
|
187 inline date_time_period day_of_week(int v) { return date_time_period(day_of_week(),v); }
|
Chris@16
|
188 ///
|
Chris@16
|
189 /// Get date_time_period for: Original number of the day of the week in month. For example 1st Sunday,
|
Chris@16
|
190 /// 2nd Sunday, etc. in Gregorian [1..5]
|
Chris@16
|
191 ///
|
Chris@16
|
192 inline date_time_period day_of_week_in_month(int v) { return date_time_period(day_of_week_in_month(),v); }
|
Chris@16
|
193 ///
|
Chris@16
|
194 /// Get date_time_period for: Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7]
|
Chris@16
|
195 ///
|
Chris@16
|
196 inline date_time_period day_of_week_local(int v) { return date_time_period(day_of_week_local(),v); }
|
Chris@16
|
197 ///
|
Chris@16
|
198 /// Get date_time_period for: 24 clock hour [0..23]
|
Chris@16
|
199 ///
|
Chris@16
|
200 inline date_time_period hour(int v) { return date_time_period(hour(),v); }
|
Chris@16
|
201 ///
|
Chris@16
|
202 /// Get date_time_period for: 12 clock hour [0..11]
|
Chris@16
|
203 ///
|
Chris@16
|
204 inline date_time_period hour_12(int v) { return date_time_period(hour_12(),v); }
|
Chris@16
|
205 ///
|
Chris@16
|
206 /// Get date_time_period for: am or pm marker [0..1]
|
Chris@16
|
207 ///
|
Chris@16
|
208 inline date_time_period am_pm(int v) { return date_time_period(am_pm(),v); }
|
Chris@16
|
209 ///
|
Chris@16
|
210 /// Get date_time_period for: minute [0..59]
|
Chris@16
|
211 ///
|
Chris@16
|
212 inline date_time_period minute(int v) { return date_time_period(minute(),v); }
|
Chris@16
|
213 ///
|
Chris@16
|
214 /// Get date_time_period for: second [0..59]
|
Chris@16
|
215 ///
|
Chris@16
|
216 inline date_time_period second(int v) { return date_time_period(second(),v); }
|
Chris@16
|
217 ///
|
Chris@16
|
218 /// Get date_time_period for: The week number in the year
|
Chris@16
|
219 ///
|
Chris@16
|
220 inline date_time_period week_of_year(int v) { return date_time_period(week_of_year(),v); }
|
Chris@16
|
221 ///
|
Chris@16
|
222 /// Get date_time_period for: The week number within current month
|
Chris@16
|
223 ///
|
Chris@16
|
224 inline date_time_period week_of_month(int v) { return date_time_period(week_of_month(),v); }
|
Chris@16
|
225 ///
|
Chris@16
|
226 /// Get date_time_period for: First day of week, constant, for example Sunday in US = 1, Monday in France = 2
|
Chris@16
|
227 ///
|
Chris@16
|
228 inline date_time_period first_day_of_week(int v) { return date_time_period(first_day_of_week(),v); }
|
Chris@16
|
229
|
Chris@16
|
230 ///
|
Chris@16
|
231 /// Get predefined constant for January
|
Chris@16
|
232 ///
|
Chris@16
|
233 inline date_time_period january() { return date_time_period(month(),0); }
|
Chris@16
|
234 ///
|
Chris@16
|
235 /// Get predefined constant for February
|
Chris@16
|
236 ///
|
Chris@16
|
237 inline date_time_period february() { return date_time_period(month(),1); }
|
Chris@16
|
238 ///
|
Chris@16
|
239 /// Get predefined constant for March
|
Chris@16
|
240 ///
|
Chris@16
|
241 inline date_time_period march() { return date_time_period(month(),2); }
|
Chris@16
|
242 ///
|
Chris@16
|
243 /// Get predefined constant for April
|
Chris@16
|
244 ///
|
Chris@16
|
245 inline date_time_period april() { return date_time_period(month(),3); }
|
Chris@16
|
246 ///
|
Chris@16
|
247 /// Get predefined constant for May
|
Chris@16
|
248 ///
|
Chris@16
|
249 inline date_time_period may() { return date_time_period(month(),4); }
|
Chris@16
|
250 ///
|
Chris@16
|
251 /// Get predefined constant for June
|
Chris@16
|
252 ///
|
Chris@16
|
253 inline date_time_period june() { return date_time_period(month(),5); }
|
Chris@16
|
254 ///
|
Chris@16
|
255 /// Get predefined constant for July
|
Chris@16
|
256 ///
|
Chris@16
|
257 inline date_time_period july() { return date_time_period(month(),6); }
|
Chris@16
|
258 ///
|
Chris@16
|
259 /// Get predefined constant for August
|
Chris@16
|
260 ///
|
Chris@16
|
261 inline date_time_period august() { return date_time_period(month(),7); }
|
Chris@16
|
262 ///
|
Chris@16
|
263 /// Get predefined constant for September
|
Chris@16
|
264 ///
|
Chris@16
|
265 inline date_time_period september() { return date_time_period(month(),8); }
|
Chris@16
|
266 ///
|
Chris@16
|
267 /// Get predefined constant for October
|
Chris@16
|
268 ///
|
Chris@16
|
269 inline date_time_period october() { return date_time_period(month(),9); }
|
Chris@16
|
270 ///
|
Chris@16
|
271 /// Get predefined constant for November
|
Chris@16
|
272 ///
|
Chris@16
|
273 inline date_time_period november() { return date_time_period(month(),10); }
|
Chris@16
|
274 ///
|
Chris@16
|
275 /// Get predefined constant for December
|
Chris@16
|
276 ///
|
Chris@16
|
277 inline date_time_period december() { return date_time_period(month(),11); }
|
Chris@16
|
278
|
Chris@16
|
279 ///
|
Chris@16
|
280 /// Get predefined constant for Sunday
|
Chris@16
|
281 ///
|
Chris@16
|
282 inline date_time_period sunday() { return date_time_period(day_of_week(),1); }
|
Chris@16
|
283 ///
|
Chris@16
|
284 /// Get predefined constant for Monday
|
Chris@16
|
285 ///
|
Chris@16
|
286 inline date_time_period monday() { return date_time_period(day_of_week(),2); }
|
Chris@16
|
287 ///
|
Chris@16
|
288 /// Get predefined constant for Tuesday
|
Chris@16
|
289 ///
|
Chris@16
|
290 inline date_time_period tuesday() { return date_time_period(day_of_week(),3); }
|
Chris@16
|
291 ///
|
Chris@16
|
292 /// Get predefined constant for Wednesday
|
Chris@16
|
293 ///
|
Chris@16
|
294 inline date_time_period wednesday() { return date_time_period(day_of_week(),4); }
|
Chris@16
|
295 ///
|
Chris@16
|
296 /// Get predefined constant for Thursday
|
Chris@16
|
297 ///
|
Chris@16
|
298 inline date_time_period thursday() { return date_time_period(day_of_week(),5); }
|
Chris@16
|
299 ///
|
Chris@16
|
300 /// Get predefined constant for Friday
|
Chris@16
|
301 ///
|
Chris@16
|
302 inline date_time_period friday() { return date_time_period(day_of_week(),6); }
|
Chris@16
|
303 ///
|
Chris@16
|
304 /// Get predefined constant for Saturday
|
Chris@16
|
305 ///
|
Chris@16
|
306 inline date_time_period saturday() { return date_time_period(day_of_week(),7); }
|
Chris@16
|
307 ///
|
Chris@16
|
308 /// Get predefined constant for AM (Ante Meridiem)
|
Chris@16
|
309 ///
|
Chris@16
|
310 inline date_time_period am() { return date_time_period(am_pm(),0); }
|
Chris@16
|
311 ///
|
Chris@16
|
312 /// Get predefined constant for PM (Post Meridiem)
|
Chris@16
|
313 ///
|
Chris@16
|
314 inline date_time_period pm() { return date_time_period(am_pm(),1); }
|
Chris@16
|
315
|
Chris@16
|
316 ///
|
Chris@16
|
317 /// convert period_type to date_time_period(f,1)
|
Chris@16
|
318 ///
|
Chris@16
|
319 inline date_time_period operator+(period::period_type f)
|
Chris@16
|
320 {
|
Chris@16
|
321 return date_time_period(f);
|
Chris@16
|
322 }
|
Chris@16
|
323 ///
|
Chris@16
|
324 /// convert period_type to date_time_period(f,-1)
|
Chris@16
|
325 ///
|
Chris@16
|
326 inline date_time_period operator-(period::period_type f)
|
Chris@16
|
327 {
|
Chris@16
|
328 return date_time_period(f,-1);
|
Chris@16
|
329 }
|
Chris@16
|
330
|
Chris@16
|
331 ///
|
Chris@16
|
332 /// Create date_time_period of type \a f with value \a v.
|
Chris@16
|
333 ///
|
Chris@16
|
334 template<typename T>
|
Chris@16
|
335 date_time_period operator*(period::period_type f,T v)
|
Chris@16
|
336 {
|
Chris@16
|
337 return date_time_period(f,v);
|
Chris@16
|
338 }
|
Chris@16
|
339
|
Chris@16
|
340 ///
|
Chris@16
|
341 /// Create date_time_period of type \a f with value \a v.
|
Chris@16
|
342 ///
|
Chris@16
|
343 template<typename T>
|
Chris@16
|
344 date_time_period operator*(T v,period::period_type f)
|
Chris@16
|
345 {
|
Chris@16
|
346 return date_time_period(f,v);
|
Chris@16
|
347 }
|
Chris@16
|
348 ///
|
Chris@16
|
349 /// Create date_time_period of type \a f with value \a v.
|
Chris@16
|
350 ///
|
Chris@16
|
351 template<typename T>
|
Chris@16
|
352 date_time_period operator*(T v,date_time_period f)
|
Chris@16
|
353 {
|
Chris@16
|
354 return date_time_period(f.type,f.value*v);
|
Chris@16
|
355 }
|
Chris@16
|
356
|
Chris@16
|
357 ///
|
Chris@16
|
358 /// Create date_time_period of type \a f with value \a v.
|
Chris@16
|
359 ///
|
Chris@16
|
360 template<typename T>
|
Chris@16
|
361 date_time_period operator*(date_time_period f,T v)
|
Chris@16
|
362 {
|
Chris@16
|
363 return date_time_period(f.type,f.value*v);
|
Chris@16
|
364 }
|
Chris@16
|
365
|
Chris@16
|
366
|
Chris@16
|
367 } // period
|
Chris@16
|
368
|
Chris@16
|
369
|
Chris@16
|
370 ///
|
Chris@16
|
371 /// \brief this class that represents a set of periods,
|
Chris@16
|
372 ///
|
Chris@16
|
373 /// It is generally created by operations on periods:
|
Chris@16
|
374 /// 1995*year + 3*month + 1*day. Note: operations are not commutative.
|
Chris@16
|
375 ///
|
Chris@16
|
376 class date_time_period_set {
|
Chris@16
|
377 public:
|
Chris@16
|
378
|
Chris@16
|
379 ///
|
Chris@16
|
380 /// Default constructor - empty set
|
Chris@16
|
381 ///
|
Chris@16
|
382 date_time_period_set()
|
Chris@16
|
383 {
|
Chris@16
|
384 }
|
Chris@16
|
385 ///
|
Chris@16
|
386 /// Create a set of single period with value 1
|
Chris@16
|
387 ///
|
Chris@16
|
388 date_time_period_set(period::period_type f)
|
Chris@16
|
389 {
|
Chris@16
|
390 basic_[0]=date_time_period(f);
|
Chris@16
|
391 }
|
Chris@16
|
392 ///
|
Chris@16
|
393 /// Create a set of single period \a fl
|
Chris@16
|
394 ///
|
Chris@16
|
395 date_time_period_set(date_time_period const &fl)
|
Chris@16
|
396 {
|
Chris@16
|
397 basic_[0]=fl;
|
Chris@16
|
398 }
|
Chris@16
|
399 ///
|
Chris@16
|
400 /// Append date_time_period \a f to the set
|
Chris@16
|
401 ///
|
Chris@16
|
402 void add(date_time_period f)
|
Chris@16
|
403 {
|
Chris@16
|
404 size_t n=size();
|
Chris@16
|
405 if(n < 4)
|
Chris@16
|
406 basic_[n]=f;
|
Chris@16
|
407 else
|
Chris@16
|
408 periods_.push_back(f);
|
Chris@16
|
409 }
|
Chris@16
|
410 ///
|
Chris@16
|
411 /// Get number if items in list
|
Chris@16
|
412 ///
|
Chris@16
|
413 size_t size() const
|
Chris@16
|
414 {
|
Chris@16
|
415 if(basic_[0].type == period::period_type())
|
Chris@16
|
416 return 0;
|
Chris@16
|
417 if(basic_[1].type == period::period_type())
|
Chris@16
|
418 return 1;
|
Chris@16
|
419 if(basic_[2].type == period::period_type())
|
Chris@16
|
420 return 2;
|
Chris@16
|
421 if(basic_[3].type == period::period_type())
|
Chris@16
|
422 return 3;
|
Chris@16
|
423 return 4+periods_.size();
|
Chris@16
|
424 }
|
Chris@16
|
425 ///
|
Chris@16
|
426 /// Get item at position \a n the set, n should be in range [0,size)
|
Chris@16
|
427 ///
|
Chris@16
|
428 date_time_period const &operator[](size_t n) const
|
Chris@16
|
429 {
|
Chris@16
|
430 if(n >= size())
|
Chris@16
|
431 throw std::out_of_range("Invalid index to date_time_period");
|
Chris@16
|
432 if(n < 4)
|
Chris@16
|
433 return basic_[n];
|
Chris@16
|
434 else
|
Chris@16
|
435 return periods_[n-4];
|
Chris@16
|
436 }
|
Chris@16
|
437 private:
|
Chris@16
|
438 date_time_period basic_[4];
|
Chris@16
|
439 std::vector<date_time_period> periods_;
|
Chris@16
|
440 };
|
Chris@16
|
441
|
Chris@16
|
442
|
Chris@16
|
443 ///
|
Chris@16
|
444 /// Append two periods sets. Note this operator is not commutative
|
Chris@16
|
445 ///
|
Chris@16
|
446 inline date_time_period_set operator+(date_time_period_set const &a,date_time_period_set const &b)
|
Chris@16
|
447 {
|
Chris@16
|
448 date_time_period_set s(a);
|
Chris@16
|
449 for(unsigned i=0;i<b.size();i++)
|
Chris@16
|
450 s.add(b[i]);
|
Chris@16
|
451 return s;
|
Chris@16
|
452 }
|
Chris@16
|
453
|
Chris@16
|
454 ///
|
Chris@16
|
455 /// Append two period sets when all periods of set \b change their sign
|
Chris@16
|
456 ///
|
Chris@16
|
457 inline date_time_period_set operator-(date_time_period_set const &a,date_time_period_set const &b)
|
Chris@16
|
458 {
|
Chris@16
|
459 date_time_period_set s(a);
|
Chris@16
|
460 for(unsigned i=0;i<b.size();i++)
|
Chris@16
|
461 s.add(-b[i]);
|
Chris@16
|
462 return s;
|
Chris@16
|
463 }
|
Chris@16
|
464
|
Chris@16
|
465
|
Chris@16
|
466 ///
|
Chris@16
|
467 /// \brief this class provides an access to general calendar information.
|
Chris@16
|
468 ///
|
Chris@16
|
469 /// This information is not connected to specific date but generic to locale, and timezone.
|
Chris@16
|
470 /// It is used in obtaining general information about calendar and is essential for creation of
|
Chris@16
|
471 /// date_time objects.
|
Chris@16
|
472 ///
|
Chris@16
|
473 class BOOST_LOCALE_DECL calendar {
|
Chris@16
|
474 public:
|
Chris@16
|
475
|
Chris@16
|
476 ///
|
Chris@16
|
477 /// Create calendar taking locale and timezone information from ios_base instance.
|
Chris@16
|
478 ///
|
Chris@16
|
479 /// \note throws std::bad_cast if ios does not have a locale with installed \ref calendar_facet
|
Chris@16
|
480 /// facet installed
|
Chris@16
|
481 ///
|
Chris@16
|
482 calendar(std::ios_base &ios);
|
Chris@16
|
483 ///
|
Chris@16
|
484 /// Create calendar with locale \a l and time_zone \a zone
|
Chris@16
|
485 ///
|
Chris@16
|
486 /// \note throws std::bad_cast if loc does not have \ref calendar_facet facet installed
|
Chris@16
|
487 ///
|
Chris@16
|
488 calendar(std::locale const &l,std::string const &zone);
|
Chris@16
|
489 ///
|
Chris@16
|
490 /// Create calendar with locale \a l and default timezone
|
Chris@16
|
491 ///
|
Chris@16
|
492 /// \note throws std::bad_cast if loc does not have \ref calendar_facet facet installed
|
Chris@16
|
493 ///
|
Chris@16
|
494 calendar(std::locale const &l);
|
Chris@16
|
495 ///
|
Chris@16
|
496 /// Create calendar with default locale and timezone \a zone
|
Chris@16
|
497 ///
|
Chris@16
|
498 /// \note throws std::bad_cast if global locale does not have \ref calendar_facet facet installed
|
Chris@16
|
499 ///
|
Chris@16
|
500 calendar(std::string const &zone);
|
Chris@16
|
501 ///
|
Chris@16
|
502 /// Create calendar with default locale and timezone
|
Chris@16
|
503 ///
|
Chris@16
|
504 /// \note throws std::bad_cast if global locale does not have \ref calendar_facet facet installed
|
Chris@16
|
505 ///
|
Chris@16
|
506 calendar();
|
Chris@16
|
507 ~calendar();
|
Chris@16
|
508
|
Chris@16
|
509 ///
|
Chris@16
|
510 /// copy calendar
|
Chris@16
|
511 ///
|
Chris@16
|
512 calendar(calendar const &other);
|
Chris@16
|
513 ///
|
Chris@16
|
514 /// assign calendar
|
Chris@16
|
515 ///
|
Chris@16
|
516 calendar const &operator=(calendar const &other);
|
Chris@16
|
517
|
Chris@16
|
518 ///
|
Chris@16
|
519 /// Get minimum value for period f, For example for period::day it is 1.
|
Chris@16
|
520 ///
|
Chris@16
|
521 int minimum(period::period_type f) const;
|
Chris@16
|
522 ///
|
Chris@16
|
523 /// Get greatest possible minimum value for period f, For example for period::day it is 1, but may be different for other calendars.
|
Chris@16
|
524 ///
|
Chris@16
|
525 int greatest_minimum(period::period_type f) const;
|
Chris@16
|
526 ///
|
Chris@16
|
527 /// Get maximum value for period f, For example for Gregorian calendar's maximum period::day it is 31.
|
Chris@16
|
528 ///
|
Chris@16
|
529 int maximum(period::period_type f) const;
|
Chris@16
|
530 ///
|
Chris@16
|
531 /// Get least maximum value for period f, For example for Gregorian calendar's maximum period::day it is 28.
|
Chris@16
|
532 ///
|
Chris@16
|
533 int least_maximum(period::period_type f) const;
|
Chris@16
|
534
|
Chris@16
|
535 ///
|
Chris@16
|
536 /// Get first day of week for specific calendar, for example for US it is 1 - Sunday for France it is 2 - Monday
|
Chris@16
|
537 int first_day_of_week() const;
|
Chris@16
|
538
|
Chris@16
|
539 ///
|
Chris@16
|
540 /// get calendar's locale
|
Chris@16
|
541 ///
|
Chris@16
|
542 std::locale get_locale() const;
|
Chris@16
|
543 ///
|
Chris@16
|
544 /// get calendar's time zone
|
Chris@16
|
545 ///
|
Chris@16
|
546 std::string get_time_zone() const;
|
Chris@16
|
547
|
Chris@16
|
548 ///
|
Chris@16
|
549 /// Check if the calendar is Gregorian
|
Chris@16
|
550 ///
|
Chris@16
|
551 bool is_gregorian() const;
|
Chris@16
|
552
|
Chris@16
|
553 ///
|
Chris@16
|
554 /// Compare calendars for equivalence: i.e. calendar types, time zones etc.
|
Chris@16
|
555 ///
|
Chris@16
|
556 bool operator==(calendar const &other) const;
|
Chris@16
|
557 ///
|
Chris@16
|
558 /// Opposite of ==
|
Chris@16
|
559 ///
|
Chris@16
|
560 bool operator!=(calendar const &other) const;
|
Chris@16
|
561
|
Chris@16
|
562 private:
|
Chris@16
|
563 friend class date_time;
|
Chris@16
|
564 std::locale locale_;
|
Chris@16
|
565 std::string tz_;
|
Chris@16
|
566 hold_ptr<abstract_calendar> impl_;
|
Chris@16
|
567 };
|
Chris@16
|
568
|
Chris@16
|
569 ///
|
Chris@16
|
570 /// \brief this class represents a date time and allows to perform various operation according to the
|
Chris@16
|
571 /// locale settings.
|
Chris@16
|
572 ///
|
Chris@16
|
573 /// This class allows to manipulate various aspects of dates and times easily using arithmetic operations with
|
Chris@16
|
574 /// periods.
|
Chris@16
|
575 ///
|
Chris@16
|
576 /// General arithmetic functions:
|
Chris@16
|
577 ///
|
Chris@16
|
578 /// - date_time + date_time_period_set = date_time: move time point forward by specific periods like date_time + month;
|
Chris@16
|
579 /// - date_time - date_time_period_set = date_time: move time point backward by specific periods like date_time - month;
|
Chris@16
|
580 /// - date_time << date_time_period_set = date_time: roll time point forward by specific periods with rolling to begin if overflows: like "2010-01-31" << 2* day == "2010-01-02" instead of "2010-02-02"
|
Chris@16
|
581 /// - date_time >> date_time_period_set = date_time: roll time point backward by specific periods with rolling to end if overflows: like "2010-01-02" >> 2* day == "2010-01-31" instead of "2009-12-30"
|
Chris@16
|
582 /// - date_time / period_type = int - current period value: like "2010-12-21" / month == 12. "2010-12-21" / year = 2010
|
Chris@16
|
583 /// - (date_time - date_time) / period_type = int: distance between dates in period_type. Like ("2010-12-01" - "2008-12-01") / month = 24.
|
Chris@16
|
584 ///
|
Chris@16
|
585 /// You can also assign specific periods using assignment operator like:
|
Chris@16
|
586 /// some_time = year * 1995 that sets the year to 1995.
|
Chris@16
|
587 ///
|
Chris@16
|
588 ///
|
Chris@16
|
589
|
Chris@16
|
590 class BOOST_LOCALE_DECL date_time {
|
Chris@16
|
591 public:
|
Chris@16
|
592
|
Chris@16
|
593 ///
|
Chris@16
|
594 /// Dafault constructor, uses default calendar initialized date_time object to current time.
|
Chris@16
|
595 ///
|
Chris@16
|
596 /// \note throws std::bad_cast if the global locale does not have \ref calendar_facet facet installed
|
Chris@16
|
597 ///
|
Chris@16
|
598 date_time();
|
Chris@16
|
599 ///
|
Chris@16
|
600 /// copy date_time
|
Chris@16
|
601 ///
|
Chris@16
|
602 date_time(date_time const &other);
|
Chris@16
|
603 ///
|
Chris@16
|
604 /// copy date_time and change some fields according to the \a set
|
Chris@16
|
605 ///
|
Chris@16
|
606 date_time(date_time const &other,date_time_period_set const &set);
|
Chris@16
|
607 ///
|
Chris@16
|
608 /// assign the date_time
|
Chris@16
|
609 ///
|
Chris@16
|
610 date_time const &operator=(date_time const &other);
|
Chris@16
|
611 ~date_time();
|
Chris@16
|
612
|
Chris@16
|
613 ///
|
Chris@16
|
614 /// Create a date_time object using POSIX time \a time and default calendar
|
Chris@16
|
615 ///
|
Chris@16
|
616 /// \note throws std::bad_cast if the global locale does not have \ref calendar_facet facet installed
|
Chris@16
|
617 ///
|
Chris@16
|
618 date_time(double time);
|
Chris@16
|
619 ///
|
Chris@16
|
620 /// Create a date_time object using POSIX time \a time and calendar \a cal
|
Chris@16
|
621 ///
|
Chris@16
|
622 date_time(double time,calendar const &cal);
|
Chris@16
|
623 ///
|
Chris@16
|
624 /// Create a date_time object using calendar \a cal and initializes it to current time.
|
Chris@16
|
625 ///
|
Chris@16
|
626 date_time(calendar const &cal);
|
Chris@16
|
627
|
Chris@16
|
628 ///
|
Chris@16
|
629 /// Create a date_time object using default calendar and define values given in \a set
|
Chris@16
|
630 ///
|
Chris@16
|
631 /// \note throws std::bad_cast if the global locale does not have \ref calendar_facet facet installed
|
Chris@16
|
632 ///
|
Chris@16
|
633 date_time(date_time_period_set const &set);
|
Chris@16
|
634 ///
|
Chris@16
|
635 /// Create a date_time object using calendar \a cal and define values given in \a set
|
Chris@16
|
636 ///
|
Chris@16
|
637 date_time(date_time_period_set const &set,calendar const &cal);
|
Chris@16
|
638
|
Chris@16
|
639
|
Chris@16
|
640 ///
|
Chris@16
|
641 /// assign values to various periods in set \a f
|
Chris@16
|
642 ///
|
Chris@16
|
643 date_time const &operator=(date_time_period_set const &f);
|
Chris@16
|
644
|
Chris@16
|
645 ///
|
Chris@16
|
646 /// set specific period \a f value to \a v
|
Chris@16
|
647 ///
|
Chris@16
|
648 void set(period::period_type f,int v);
|
Chris@16
|
649 ///
|
Chris@16
|
650 /// get specific period \a f value
|
Chris@16
|
651 ///
|
Chris@16
|
652 int get(period::period_type f) const;
|
Chris@16
|
653
|
Chris@16
|
654 ///
|
Chris@16
|
655 /// syntactic sugar for get(f)
|
Chris@16
|
656 ///
|
Chris@16
|
657 int operator/(period::period_type f) const
|
Chris@16
|
658 {
|
Chris@16
|
659 return get(f);
|
Chris@16
|
660 }
|
Chris@16
|
661
|
Chris@16
|
662 ///
|
Chris@16
|
663 /// add single period f to the current date_time
|
Chris@16
|
664 ///
|
Chris@16
|
665 date_time operator+(period::period_type f) const
|
Chris@16
|
666 {
|
Chris@16
|
667 return *this+date_time_period(f);
|
Chris@16
|
668 }
|
Chris@16
|
669
|
Chris@16
|
670 ///
|
Chris@16
|
671 /// subtract single period f from the current date_time
|
Chris@16
|
672 ///
|
Chris@16
|
673 date_time operator-(period::period_type f) const
|
Chris@16
|
674 {
|
Chris@16
|
675 return *this-date_time_period(f);
|
Chris@16
|
676 }
|
Chris@16
|
677
|
Chris@16
|
678 ///
|
Chris@16
|
679 /// add single period f to the current date_time
|
Chris@16
|
680 ///
|
Chris@16
|
681 date_time const &operator+=(period::period_type f)
|
Chris@16
|
682 {
|
Chris@16
|
683 return *this+=date_time_period(f);
|
Chris@16
|
684 }
|
Chris@16
|
685 ///
|
Chris@16
|
686 /// subtract single period f from the current date_time
|
Chris@16
|
687 ///
|
Chris@16
|
688 date_time const &operator-=(period::period_type f)
|
Chris@16
|
689 {
|
Chris@16
|
690 return *this-=date_time_period(f);
|
Chris@16
|
691 }
|
Chris@16
|
692
|
Chris@16
|
693 ///
|
Chris@16
|
694 /// roll forward a date by single period f.
|
Chris@16
|
695 ///
|
Chris@16
|
696 date_time operator<<(period::period_type f) const
|
Chris@16
|
697 {
|
Chris@16
|
698 return *this<<date_time_period(f);
|
Chris@16
|
699 }
|
Chris@16
|
700
|
Chris@16
|
701 ///
|
Chris@16
|
702 /// roll backward a date by single period f.
|
Chris@16
|
703 ///
|
Chris@16
|
704 date_time operator>>(period::period_type f) const
|
Chris@16
|
705 {
|
Chris@16
|
706 return *this>>date_time_period(f);
|
Chris@16
|
707 }
|
Chris@16
|
708
|
Chris@16
|
709 ///
|
Chris@16
|
710 /// roll forward a date by single period f.
|
Chris@16
|
711 ///
|
Chris@16
|
712 date_time const &operator<<=(period::period_type f)
|
Chris@16
|
713 {
|
Chris@16
|
714 return *this<<=date_time_period(f);
|
Chris@16
|
715 }
|
Chris@16
|
716 ///
|
Chris@16
|
717 /// roll backward a date by single period f.
|
Chris@16
|
718 ///
|
Chris@16
|
719 date_time const &operator>>=(period::period_type f)
|
Chris@16
|
720 {
|
Chris@16
|
721 return *this>>=date_time_period(f);
|
Chris@16
|
722 }
|
Chris@16
|
723
|
Chris@16
|
724 ///
|
Chris@16
|
725 /// add date_time_period to the current date_time
|
Chris@16
|
726 ///
|
Chris@16
|
727 date_time operator+(date_time_period const &v) const;
|
Chris@16
|
728 ///
|
Chris@16
|
729 /// subtract date_time_period from the current date_time
|
Chris@16
|
730 ///
|
Chris@16
|
731 date_time operator-(date_time_period const &v) const;
|
Chris@16
|
732 ///
|
Chris@16
|
733 /// add date_time_period to the current date_time
|
Chris@16
|
734 ///
|
Chris@16
|
735 date_time const &operator+=(date_time_period const &v);
|
Chris@16
|
736 ///
|
Chris@16
|
737 /// subtract date_time_period from the current date_time
|
Chris@16
|
738 ///
|
Chris@16
|
739 date_time const &operator-=(date_time_period const &v);
|
Chris@16
|
740
|
Chris@16
|
741 ///
|
Chris@16
|
742 /// roll current date_time forward by date_time_period v
|
Chris@16
|
743 ///
|
Chris@16
|
744 date_time operator<<(date_time_period const &v) const;
|
Chris@16
|
745 ///
|
Chris@16
|
746 /// roll current date_time backward by date_time_period v
|
Chris@16
|
747 ///
|
Chris@16
|
748 date_time operator>>(date_time_period const &v) const ;
|
Chris@16
|
749 ///
|
Chris@16
|
750 /// roll current date_time forward by date_time_period v
|
Chris@16
|
751 ///
|
Chris@16
|
752 date_time const &operator<<=(date_time_period const &v);
|
Chris@16
|
753 ///
|
Chris@16
|
754 /// roll current date_time backward by date_time_period v
|
Chris@16
|
755 ///
|
Chris@16
|
756 date_time const &operator>>=(date_time_period const &v);
|
Chris@16
|
757
|
Chris@16
|
758 ///
|
Chris@16
|
759 /// add date_time_period_set v to the current date_time
|
Chris@16
|
760 ///
|
Chris@16
|
761 date_time operator+(date_time_period_set const &v) const;
|
Chris@16
|
762 ///
|
Chris@16
|
763 /// subtract date_time_period_set v from the current date_time
|
Chris@16
|
764 ///
|
Chris@16
|
765 date_time operator-(date_time_period_set const &v) const;
|
Chris@16
|
766 ///
|
Chris@16
|
767 /// add date_time_period_set v to the current date_time
|
Chris@16
|
768 ///
|
Chris@16
|
769 date_time const &operator+=(date_time_period_set const &v);
|
Chris@16
|
770 ///
|
Chris@16
|
771 /// subtract date_time_period_set v from the current date_time
|
Chris@16
|
772 ///
|
Chris@16
|
773 date_time const &operator-=(date_time_period_set const &v);
|
Chris@16
|
774
|
Chris@16
|
775 ///
|
Chris@16
|
776 /// roll current date_time forward by date_time_period_set v
|
Chris@16
|
777 ///
|
Chris@16
|
778 date_time operator<<(date_time_period_set const &v) const;
|
Chris@16
|
779 ///
|
Chris@16
|
780 /// roll current date_time backward by date_time_period_set v
|
Chris@16
|
781 ///
|
Chris@16
|
782 date_time operator>>(date_time_period_set const &v) const ;
|
Chris@16
|
783 ///
|
Chris@16
|
784 /// roll current date_time forward by date_time_period_set v
|
Chris@16
|
785 ///
|
Chris@16
|
786 date_time const &operator<<=(date_time_period_set const &v);
|
Chris@16
|
787 ///
|
Chris@16
|
788 /// roll current date_time backward by date_time_period_set v
|
Chris@16
|
789 ///
|
Chris@16
|
790 date_time const &operator>>=(date_time_period_set const &v);
|
Chris@16
|
791
|
Chris@16
|
792 ///
|
Chris@16
|
793 /// Get POSIX time
|
Chris@16
|
794 ///
|
Chris@16
|
795 /// The POSIX time is number of seconds since January 1st, 1970 00:00 UTC, ignoring leap seconds.
|
Chris@16
|
796 ///
|
Chris@16
|
797 double time() const;
|
Chris@16
|
798 ///
|
Chris@16
|
799 /// set POSIX time
|
Chris@16
|
800 ///
|
Chris@16
|
801 /// The POSIX time is number of seconds since January 1st, 1970 00:00 UTC, ignoring leap seconds.
|
Chris@16
|
802 /// This time can be fetched from Operating system clock using C function time, gettimeofday and others.
|
Chris@16
|
803 ///
|
Chris@16
|
804 void time(double v);
|
Chris@16
|
805
|
Chris@16
|
806 ///
|
Chris@16
|
807 /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
|
Chris@16
|
808 ///
|
Chris@16
|
809 bool operator==(date_time const &other) const;
|
Chris@16
|
810 ///
|
Chris@16
|
811 /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
|
Chris@16
|
812 ///
|
Chris@16
|
813 bool operator!=(date_time const &other) const;
|
Chris@16
|
814 ///
|
Chris@16
|
815 /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
|
Chris@16
|
816 ///
|
Chris@16
|
817 bool operator<(date_time const &other) const;
|
Chris@16
|
818 ///
|
Chris@16
|
819 /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
|
Chris@16
|
820 ///
|
Chris@16
|
821 bool operator>(date_time const &other) const;
|
Chris@16
|
822 ///
|
Chris@16
|
823 /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
|
Chris@16
|
824 ///
|
Chris@16
|
825 bool operator<=(date_time const &other) const;
|
Chris@16
|
826 ///
|
Chris@16
|
827 /// compare date_time in the timeline (ignores difference in calendar, timezone etc)
|
Chris@16
|
828 ///
|
Chris@16
|
829 bool operator>=(date_time const &other) const;
|
Chris@16
|
830
|
Chris@16
|
831 ///
|
Chris@16
|
832 /// swaps two dates - efficient, does not throw
|
Chris@16
|
833 ///
|
Chris@16
|
834 void swap(date_time &other);
|
Chris@16
|
835
|
Chris@16
|
836 ///
|
Chris@16
|
837 /// calculate the distance from this date_time to \a other in terms of perios \a f
|
Chris@16
|
838 ///
|
Chris@16
|
839 int difference(date_time const &other,period::period_type f) const;
|
Chris@16
|
840
|
Chris@16
|
841 ///
|
Chris@16
|
842 /// Get minimal possible value for *this time point for a period \a f.
|
Chris@16
|
843 ///
|
Chris@16
|
844 int minimum(period::period_type f) const;
|
Chris@16
|
845 ///
|
Chris@16
|
846 /// Get minimal possible value for *this time point for a period \a f. For example
|
Chris@16
|
847 /// in February maximum(day) may be 28 or 29, in January maximum(day)==31
|
Chris@16
|
848 ///
|
Chris@16
|
849 int maximum(period::period_type f) const;
|
Chris@16
|
850
|
Chris@16
|
851 ///
|
Chris@16
|
852 /// Check if *this time point is in daylight saving time
|
Chris@16
|
853 ///
|
Chris@16
|
854 bool is_in_daylight_saving_time() const;
|
Chris@16
|
855
|
Chris@16
|
856 private:
|
Chris@16
|
857 hold_ptr<abstract_calendar> impl_;
|
Chris@16
|
858 };
|
Chris@16
|
859
|
Chris@16
|
860 ///
|
Chris@16
|
861 /// Writes date_time \a t to output stream \a out.
|
Chris@16
|
862 ///
|
Chris@16
|
863 /// This function uses locale, calendar and time zone of the target stream \a in.
|
Chris@16
|
864 ///
|
Chris@16
|
865 /// For example:
|
Chris@16
|
866 /// \code
|
Chris@16
|
867 /// date_time now(time(0),hebrew_calendar)
|
Chris@16
|
868 /// cout << "Year: " << period::year(now) <<" Full Date:"<< now;
|
Chris@16
|
869 /// \endcode
|
Chris@16
|
870 ///
|
Chris@16
|
871 /// The output may be Year:5770 Full Date:Jan 1, 2010
|
Chris@16
|
872 ///
|
Chris@16
|
873 template<typename CharType>
|
Chris@16
|
874 std::basic_ostream<CharType> &operator<<(std::basic_ostream<CharType> &out,date_time const &t)
|
Chris@16
|
875 {
|
Chris@16
|
876 double time_point = t.time();
|
Chris@16
|
877 uint64_t display_flags = ios_info::get(out).display_flags();
|
Chris@16
|
878 if (
|
Chris@16
|
879 display_flags == flags::date
|
Chris@16
|
880 || display_flags == flags::time
|
Chris@16
|
881 || display_flags == flags::datetime
|
Chris@16
|
882 || display_flags == flags::strftime
|
Chris@16
|
883 )
|
Chris@16
|
884 {
|
Chris@16
|
885 out << time_point;
|
Chris@16
|
886 }
|
Chris@16
|
887 else {
|
Chris@16
|
888 ios_info::get(out).display_flags(flags::datetime);
|
Chris@16
|
889 out << time_point;
|
Chris@16
|
890 ios_info::get(out).display_flags(display_flags);
|
Chris@16
|
891 }
|
Chris@16
|
892 return out;
|
Chris@16
|
893 }
|
Chris@16
|
894
|
Chris@16
|
895 ///
|
Chris@16
|
896 /// Reads date_time \a t from output stream \a in
|
Chris@16
|
897 ///
|
Chris@16
|
898 /// This function uses locale, calendar and time zone of the source stream \a in.
|
Chris@16
|
899 ///
|
Chris@16
|
900 template<typename CharType>
|
Chris@16
|
901 std::basic_istream<CharType> &operator>>(std::basic_istream<CharType> &in,date_time &t)
|
Chris@16
|
902 {
|
Chris@16
|
903 double v;
|
Chris@16
|
904 uint64_t display_flags = ios_info::get(in).display_flags();
|
Chris@16
|
905 if (
|
Chris@16
|
906 display_flags == flags::date
|
Chris@16
|
907 || display_flags == flags::time
|
Chris@16
|
908 || display_flags == flags::datetime
|
Chris@16
|
909 || display_flags == flags::strftime
|
Chris@16
|
910 )
|
Chris@16
|
911 {
|
Chris@16
|
912 in >> v;
|
Chris@16
|
913 }
|
Chris@16
|
914 else {
|
Chris@16
|
915 ios_info::get(in).display_flags(flags::datetime);
|
Chris@16
|
916 in >> v;
|
Chris@16
|
917 ios_info::get(in).display_flags(display_flags);
|
Chris@16
|
918 }
|
Chris@16
|
919 if(!in.fail())
|
Chris@16
|
920 t.time(v);
|
Chris@16
|
921 return in;
|
Chris@16
|
922 }
|
Chris@16
|
923
|
Chris@16
|
924 ///
|
Chris@16
|
925 /// \brief This class represents a period: a pair of two date_time objects.
|
Chris@16
|
926 ///
|
Chris@16
|
927 /// It is generally used as syntactic sugar to calculate difference between two dates.
|
Chris@16
|
928 ///
|
Chris@16
|
929 /// Note: it stores references to the original objects, so it is not recommended to be used
|
Chris@16
|
930 /// outside of the equation you calculate the difference in.
|
Chris@16
|
931 ///
|
Chris@16
|
932 class date_time_duration {
|
Chris@16
|
933 public:
|
Chris@16
|
934
|
Chris@16
|
935 ///
|
Chris@16
|
936 /// Create an object were \a first represents earlier point on time line and \a second is later
|
Chris@16
|
937 /// point.
|
Chris@16
|
938 ///
|
Chris@16
|
939 date_time_duration(date_time const &first,date_time const &second) :
|
Chris@16
|
940 s_(first),
|
Chris@16
|
941 e_(second)
|
Chris@16
|
942 {
|
Chris@16
|
943 }
|
Chris@16
|
944
|
Chris@16
|
945 ///
|
Chris@16
|
946 /// find a difference in terms of period_type \a f
|
Chris@16
|
947 ///
|
Chris@16
|
948 int get(period::period_type f) const
|
Chris@16
|
949 {
|
Chris@16
|
950 return start().difference(end(),f);
|
Chris@16
|
951 }
|
Chris@16
|
952
|
Chris@16
|
953 ///
|
Chris@16
|
954 /// Syntactic sugar for get(f)
|
Chris@16
|
955 ///
|
Chris@16
|
956 int operator / (period::period_type f) const
|
Chris@16
|
957 {
|
Chris@16
|
958 return start().difference(end(),f);
|
Chris@16
|
959 }
|
Chris@16
|
960
|
Chris@16
|
961 ///
|
Chris@16
|
962 /// Get starting point
|
Chris@16
|
963 ///
|
Chris@16
|
964 date_time const &start() const { return s_; }
|
Chris@16
|
965 ///
|
Chris@16
|
966 /// Get ending point
|
Chris@16
|
967 ///
|
Chris@16
|
968 date_time const &end() const { return e_; }
|
Chris@16
|
969 private:
|
Chris@16
|
970 date_time const &s_;
|
Chris@16
|
971 date_time const &e_;
|
Chris@16
|
972 };
|
Chris@16
|
973
|
Chris@16
|
974 ///
|
Chris@16
|
975 /// Calculates the difference between two dates, the left operand is a later point on time line.
|
Chris@16
|
976 /// Returns date_time_duration object.
|
Chris@16
|
977 ///
|
Chris@16
|
978 inline date_time_duration operator-(date_time const &later,date_time const &earlier)
|
Chris@16
|
979 {
|
Chris@16
|
980 return date_time_duration(earlier,later);
|
Chris@16
|
981 }
|
Chris@16
|
982
|
Chris@16
|
983
|
Chris@16
|
984 namespace period {
|
Chris@16
|
985 ///
|
Chris@16
|
986 /// Extract from date_time numerical value of Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
|
Chris@16
|
987 ///
|
Chris@16
|
988 inline int era(date_time const &dt) { return dt.get(era()); }
|
Chris@16
|
989 ///
|
Chris@16
|
990 /// Extract from date_time numerical value of Year, it is calendar specific, for example 2011 in Gregorian calendar.
|
Chris@16
|
991 ///
|
Chris@16
|
992 inline int year(date_time const &dt) { return dt.get(year()); }
|
Chris@16
|
993 ///
|
Chris@16
|
994 /// Extract from date_time numerical value of Extended year for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1.
|
Chris@16
|
995 ///
|
Chris@16
|
996 inline int extended_year(date_time const &dt) { return dt.get(extended_year()); }
|
Chris@16
|
997 ///
|
Chris@16
|
998 /// Extract from date_time numerical value of The month of year, calendar specific, in Gregorian [0..11]
|
Chris@16
|
999 ///
|
Chris@16
|
1000 inline int month(date_time const &dt) { return dt.get(month()); }
|
Chris@16
|
1001 ///
|
Chris@16
|
1002 /// Extract from date_time numerical value of The day of month, calendar specific, in Gregorian [1..31]
|
Chris@16
|
1003 ///
|
Chris@16
|
1004 inline int day(date_time const &dt) { return dt.get(day()); }
|
Chris@16
|
1005 ///
|
Chris@16
|
1006 /// Extract from date_time numerical value of The number of day in year, starting from 1, in Gregorian [1..366]
|
Chris@16
|
1007 ///
|
Chris@16
|
1008 inline int day_of_year(date_time const &dt) { return dt.get(day_of_year()); }
|
Chris@16
|
1009 ///
|
Chris@16
|
1010 /// Extract from date_time numerical value of Day of week, Sunday=1, Monday=2,..., Saturday=7.
|
Chris@16
|
1011 ///
|
Chris@16
|
1012 /// Note that updating this value respects local day of week, so for example,
|
Chris@16
|
1013 /// If first day of week is Monday and the current day is Tuesday then setting
|
Chris@16
|
1014 /// the value to Sunday (1) would forward the date by 5 days forward and not backward
|
Chris@16
|
1015 /// by two days as it could be expected if the numbers were taken as is.
|
Chris@16
|
1016 ///
|
Chris@16
|
1017 inline int day_of_week(date_time const &dt) { return dt.get(day_of_week()); }
|
Chris@16
|
1018 ///
|
Chris@16
|
1019 /// Extract from date_time numerical value of Original number of the day of the week in month. For example 1st Sunday,
|
Chris@16
|
1020 /// 2nd Sunday, etc. in Gregorian [1..5]
|
Chris@16
|
1021 ///
|
Chris@16
|
1022 inline int day_of_week_in_month(date_time const &dt) { return dt.get(day_of_week_in_month()); }
|
Chris@16
|
1023 ///
|
Chris@16
|
1024 /// Extract from date_time numerical value of Local day of week, for example in France Monday is 1, in US Sunday is 1, [1..7]
|
Chris@16
|
1025 ///
|
Chris@16
|
1026 inline int day_of_week_local(date_time const &dt) { return dt.get(day_of_week_local()); }
|
Chris@16
|
1027 ///
|
Chris@16
|
1028 /// Extract from date_time numerical value of 24 clock hour [0..23]
|
Chris@16
|
1029 ///
|
Chris@16
|
1030 inline int hour(date_time const &dt) { return dt.get(hour()); }
|
Chris@16
|
1031 ///
|
Chris@16
|
1032 /// Extract from date_time numerical value of 12 clock hour [0..11]
|
Chris@16
|
1033 ///
|
Chris@16
|
1034 inline int hour_12(date_time const &dt) { return dt.get(hour_12()); }
|
Chris@16
|
1035 ///
|
Chris@16
|
1036 /// Extract from date_time numerical value of am or pm marker [0..1]
|
Chris@16
|
1037 ///
|
Chris@16
|
1038 inline int am_pm(date_time const &dt) { return dt.get(am_pm()); }
|
Chris@16
|
1039 ///
|
Chris@16
|
1040 /// Extract from date_time numerical value of minute [0..59]
|
Chris@16
|
1041 ///
|
Chris@16
|
1042 inline int minute(date_time const &dt) { return dt.get(minute()); }
|
Chris@16
|
1043 ///
|
Chris@16
|
1044 /// Extract from date_time numerical value of second [0..59]
|
Chris@16
|
1045 ///
|
Chris@16
|
1046 inline int second(date_time const &dt) { return dt.get(second()); }
|
Chris@16
|
1047 ///
|
Chris@16
|
1048 /// Extract from date_time numerical value of The week number in the year
|
Chris@16
|
1049 ///
|
Chris@16
|
1050 inline int week_of_year(date_time const &dt) { return dt.get(week_of_year()); }
|
Chris@16
|
1051 ///
|
Chris@16
|
1052 /// Extract from date_time numerical value of The week number within current month
|
Chris@16
|
1053 ///
|
Chris@16
|
1054 inline int week_of_month(date_time const &dt) { return dt.get(week_of_month()); }
|
Chris@16
|
1055 ///
|
Chris@16
|
1056 /// Extract from date_time numerical value of First day of week, constant, for example Sunday in US = 1, Monday in France = 2
|
Chris@16
|
1057 ///
|
Chris@16
|
1058 inline int first_day_of_week(date_time const &dt) { return dt.get(first_day_of_week()); }
|
Chris@16
|
1059
|
Chris@16
|
1060 ///
|
Chris@16
|
1061 /// Extract from date_time_duration numerical value of duration in Era i.e. AC, BC in Gregorian and Julian calendar, range [0,1]
|
Chris@16
|
1062 ///
|
Chris@16
|
1063 inline int era(date_time_duration const &dt) { return dt.get(era()); }
|
Chris@16
|
1064 ///
|
Chris@16
|
1065 /// Extract from date_time_duration numerical value of duration in years
|
Chris@16
|
1066 ///
|
Chris@16
|
1067 inline int year(date_time_duration const &dt) { return dt.get(year()); }
|
Chris@16
|
1068 ///
|
Chris@16
|
1069 /// Extract from date_time_duration numerical value of duration in extended years (for Gregorian/Julian calendars, where 1 BC == 0, 2 BC == -1).
|
Chris@16
|
1070 ///
|
Chris@16
|
1071 inline int extended_year(date_time_duration const &dt) { return dt.get(extended_year()); }
|
Chris@16
|
1072 ///
|
Chris@16
|
1073 /// Extract from date_time_duration numerical value of duration in months
|
Chris@16
|
1074 ///
|
Chris@16
|
1075 inline int month(date_time_duration const &dt) { return dt.get(month()); }
|
Chris@16
|
1076 ///
|
Chris@16
|
1077 /// Extract from date_time_duration numerical value of duration in days of month
|
Chris@16
|
1078 ///
|
Chris@16
|
1079 inline int day(date_time_duration const &dt) { return dt.get(day()); }
|
Chris@16
|
1080 ///
|
Chris@16
|
1081 /// Extract from date_time_duration numerical value of duration in days of year
|
Chris@16
|
1082 ///
|
Chris@16
|
1083 inline int day_of_year(date_time_duration const &dt) { return dt.get(day_of_year()); }
|
Chris@16
|
1084 ///
|
Chris@16
|
1085 /// Extract from date_time_duration numerical value of duration in days of week
|
Chris@16
|
1086 ///
|
Chris@16
|
1087 inline int day_of_week(date_time_duration const &dt) { return dt.get(day_of_week()); }
|
Chris@16
|
1088 ///
|
Chris@16
|
1089 /// Extract from date_time_duration numerical value of duration in original number of the day of the week in month
|
Chris@16
|
1090 ///
|
Chris@16
|
1091 inline int day_of_week_in_month(date_time_duration const &dt) { return dt.get(day_of_week_in_month()); }
|
Chris@16
|
1092 ///
|
Chris@16
|
1093 /// Extract from date_time_duration numerical value of duration in local day of week
|
Chris@16
|
1094 ///
|
Chris@16
|
1095 inline int day_of_week_local(date_time_duration const &dt) { return dt.get(day_of_week_local()); }
|
Chris@16
|
1096 ///
|
Chris@16
|
1097 /// Extract from date_time_duration numerical value of duration in hours
|
Chris@16
|
1098 ///
|
Chris@16
|
1099 inline int hour(date_time_duration const &dt) { return dt.get(hour()); }
|
Chris@16
|
1100 ///
|
Chris@16
|
1101 /// Extract from date_time_duration numerical value of duration in 12 clock hours
|
Chris@16
|
1102 ///
|
Chris@16
|
1103 inline int hour_12(date_time_duration const &dt) { return dt.get(hour_12()); }
|
Chris@16
|
1104 ///
|
Chris@16
|
1105 /// Extract from date_time_duration numerical value of duration in am or pm markers
|
Chris@16
|
1106 ///
|
Chris@16
|
1107 inline int am_pm(date_time_duration const &dt) { return dt.get(am_pm()); }
|
Chris@16
|
1108 ///
|
Chris@16
|
1109 /// Extract from date_time_duration numerical value of duration in minutes
|
Chris@16
|
1110 ///
|
Chris@16
|
1111 inline int minute(date_time_duration const &dt) { return dt.get(minute()); }
|
Chris@16
|
1112 ///
|
Chris@16
|
1113 /// Extract from date_time_duration numerical value of duration in seconds
|
Chris@16
|
1114 ///
|
Chris@16
|
1115 inline int second(date_time_duration const &dt) { return dt.get(second()); }
|
Chris@16
|
1116 ///
|
Chris@16
|
1117 /// Extract from date_time_duration numerical value of duration in the week number in the year
|
Chris@16
|
1118 ///
|
Chris@16
|
1119 inline int week_of_year(date_time_duration const &dt) { return dt.get(week_of_year()); }
|
Chris@16
|
1120 ///
|
Chris@16
|
1121 /// Extract from date_time_duration numerical value of duration in The week number within current month
|
Chris@16
|
1122 ///
|
Chris@16
|
1123 inline int week_of_month(date_time_duration const &dt) { return dt.get(week_of_month()); }
|
Chris@16
|
1124 ///
|
Chris@16
|
1125 /// Extract from date_time_duration numerical value of duration in the first day of week
|
Chris@16
|
1126 ///
|
Chris@16
|
1127 inline int first_day_of_week(date_time_duration const &dt) { return dt.get(first_day_of_week()); }
|
Chris@16
|
1128
|
Chris@16
|
1129
|
Chris@16
|
1130 }
|
Chris@16
|
1131
|
Chris@16
|
1132 /// @}
|
Chris@16
|
1133
|
Chris@16
|
1134
|
Chris@16
|
1135 } // locale
|
Chris@16
|
1136 } // boost
|
Chris@16
|
1137
|
Chris@16
|
1138 #ifdef BOOST_MSVC
|
Chris@16
|
1139 #pragma warning(pop)
|
Chris@16
|
1140 #endif
|
Chris@16
|
1141
|
Chris@16
|
1142
|
Chris@16
|
1143 #endif
|
Chris@16
|
1144 ///
|
Chris@16
|
1145 /// \example calendar.cpp
|
Chris@16
|
1146 ///
|
Chris@16
|
1147 /// Example of using date_time functions for generating calendar for current year.
|
Chris@16
|
1148 ///
|
Chris@16
|
1149
|
Chris@16
|
1150 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|