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