source: NonGTP/Boost/boost/date_time/time_formatting_streams.hpp @ 857

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