Chris@16
|
1 #ifndef DATE_TIME_TIME_FORMATTING_STREAMS_HPP___
|
Chris@16
|
2 #define DATE_TIME_TIME_FORMATTING_STREAMS_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2002,2003 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, Bart Garst
|
Chris@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/date_time/compiler_config.hpp>
|
Chris@16
|
13
|
Chris@16
|
14 #ifndef BOOST_DATE_TIME_NO_LOCALE
|
Chris@16
|
15
|
Chris@16
|
16 #include <locale>
|
Chris@16
|
17 #include <iomanip>
|
Chris@16
|
18 #include <iostream>
|
Chris@16
|
19 #include <boost/date_time/date_formatting_locales.hpp>
|
Chris@16
|
20 #include <boost/date_time/time_resolution_traits.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost {
|
Chris@16
|
23 namespace date_time {
|
Chris@16
|
24
|
Chris@16
|
25
|
Chris@16
|
26 //! Put a time type into a stream using appropriate facets
|
Chris@16
|
27 template<class time_duration_type,
|
Chris@16
|
28 class charT = char>
|
Chris@16
|
29 class ostream_time_duration_formatter
|
Chris@16
|
30 {
|
Chris@16
|
31 public:
|
Chris@16
|
32 typedef std::basic_ostream<charT> ostream_type;
|
Chris@16
|
33 typedef typename time_duration_type::fractional_seconds_type fractional_seconds_type;
|
Chris@16
|
34
|
Chris@16
|
35 //! Put time into an ostream
|
Chris@16
|
36 static void duration_put(const time_duration_type& td,
|
Chris@16
|
37 ostream_type& os)
|
Chris@16
|
38 {
|
Chris@16
|
39 if(td.is_special()) {
|
Chris@16
|
40 os << td.get_rep();
|
Chris@16
|
41 }
|
Chris@16
|
42 else {
|
Chris@16
|
43 charT fill_char = '0';
|
Chris@16
|
44 if(td.is_negative()) {
|
Chris@16
|
45 os << '-';
|
Chris@16
|
46 }
|
Chris@16
|
47 os << std::setw(2) << std::setfill(fill_char)
|
Chris@16
|
48 << absolute_value(td.hours()) << ":";
|
Chris@16
|
49 os << std::setw(2) << std::setfill(fill_char)
|
Chris@16
|
50 << absolute_value(td.minutes()) << ":";
|
Chris@16
|
51 os << std::setw(2) << std::setfill(fill_char)
|
Chris@16
|
52 << absolute_value(td.seconds());
|
Chris@16
|
53 fractional_seconds_type frac_sec =
|
Chris@16
|
54 absolute_value(td.fractional_seconds());
|
Chris@16
|
55 if (frac_sec != 0) {
|
Chris@16
|
56 os << "."
|
Chris@16
|
57 << std::setw(time_duration_type::num_fractional_digits())
|
Chris@16
|
58 << std::setfill(fill_char)
|
Chris@16
|
59 << frac_sec;
|
Chris@16
|
60 }
|
Chris@16
|
61 } // else
|
Chris@16
|
62 } // duration_put
|
Chris@16
|
63 }; //class ostream_time_duration_formatter
|
Chris@16
|
64
|
Chris@16
|
65 //! Put a time type into a stream using appropriate facets
|
Chris@16
|
66 template<class time_type,
|
Chris@16
|
67 class charT = char>
|
Chris@16
|
68 class ostream_time_formatter
|
Chris@16
|
69 {
|
Chris@16
|
70 public:
|
Chris@16
|
71 typedef std::basic_ostream<charT> ostream_type;
|
Chris@16
|
72 typedef typename time_type::date_type date_type;
|
Chris@16
|
73 typedef typename time_type::time_duration_type time_duration_type;
|
Chris@16
|
74 typedef ostream_time_duration_formatter<time_duration_type, charT> duration_formatter;
|
Chris@16
|
75
|
Chris@16
|
76 //! Put time into an ostream
|
Chris@16
|
77 static void time_put(const time_type& t,
|
Chris@16
|
78 ostream_type& os)
|
Chris@16
|
79 {
|
Chris@16
|
80 date_type d = t.date();
|
Chris@16
|
81 os << d;
|
Chris@16
|
82 if(!d.is_infinity() && !d.is_not_a_date())
|
Chris@16
|
83 {
|
Chris@16
|
84 os << " "; //TODO: fix the separator here.
|
Chris@16
|
85 duration_formatter::duration_put(t.time_of_day(), os);
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 } // time_to_ostream
|
Chris@16
|
89 }; //class ostream_time_formatter
|
Chris@16
|
90
|
Chris@16
|
91
|
Chris@16
|
92 //! Put a time period into a stream using appropriate facets
|
Chris@16
|
93 template<class time_period_type,
|
Chris@16
|
94 class charT = char>
|
Chris@16
|
95 class ostream_time_period_formatter
|
Chris@16
|
96 {
|
Chris@16
|
97 public:
|
Chris@16
|
98 typedef std::basic_ostream<charT> ostream_type;
|
Chris@16
|
99 typedef typename time_period_type::point_type time_type;
|
Chris@16
|
100 typedef ostream_time_formatter<time_type, charT> time_formatter;
|
Chris@16
|
101
|
Chris@16
|
102 //! Put time into an ostream
|
Chris@16
|
103 static void period_put(const time_period_type& tp,
|
Chris@16
|
104 ostream_type& os)
|
Chris@16
|
105 {
|
Chris@16
|
106 os << '['; //TODO: facet or manipulator for periods?
|
Chris@16
|
107 time_formatter::time_put(tp.begin(), os);
|
Chris@16
|
108 os << '/'; //TODO: facet or manipulator for periods?
|
Chris@16
|
109 time_formatter::time_put(tp.last(), os);
|
Chris@16
|
110 os << ']';
|
Chris@16
|
111
|
Chris@16
|
112 } // period_put
|
Chris@16
|
113
|
Chris@16
|
114 }; //class ostream_time_period_formatter
|
Chris@16
|
115
|
Chris@16
|
116
|
Chris@16
|
117
|
Chris@16
|
118 } } //namespace date_time
|
Chris@16
|
119
|
Chris@16
|
120 #endif //BOOST_DATE_TIME_NO_LOCALE
|
Chris@16
|
121
|
Chris@16
|
122 #endif
|