1 #ifndef DATE_TIME_DATE_FORMATTING_LIMITED_HPP___
2 #define DATE_TIME_DATE_FORMATTING_LIMITED_HPP___
3 
4 /* Copyright (c) 2002-2004 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/iso_format.hpp"
13 #include "boost/date_time/compiler_config.hpp"
14 #include <string>
15 #include <sstream>
16 #include <iomanip>
17 
18 
19 namespace boost {
20 namespace date_time {
21 
22   //! Formats a month as as string into an ostream
23   template<class month_type, class format_type>
24   class month_formatter
25   {
26   public:
27     //! Formats a month as as string into an ostream
28     /*! This function demands that month_type provide
29      *  functions for converting to short and long strings
30      *  if that capability is used.
31      */
format_month(const month_type & month,std::ostream & os)32     static std::ostream& format_month(const month_type& month,
33                                       std::ostream& os)
34     {
35       switch (format_type::month_format())
36       {
37         case month_as_short_string:
38         {
39           os << month.as_short_string();
40           break;
41         }
42         case month_as_long_string:
43         {
44           os << month.as_long_string();
45           break;
46         }
47         case month_as_integer:
48         {
49           os << std::setw(2) << std::setfill('0') << month.as_number();
50           break;
51         }
52 
53       }
54       return os;
55     } // format_month
56   };
57 
58 
59   //! Convert ymd to a standard string formatting policies
60   template<class ymd_type, class format_type>
61   class ymd_formatter
62   {
63   public:
64     //! Convert ymd to a standard string formatting policies
65     /*! This is standard code for handling date formatting with
66      *  year-month-day based date information.  This function
67      *  uses the format_type to control whether the string will
68      *  contain separator characters, and if so what the character
69      *  will be.  In addtion, it can format the month as either
70      *  an integer or a string as controled by the formatting
71      *  policy
72      */
ymd_to_string(ymd_type ymd)73     static std::string ymd_to_string(ymd_type ymd)
74     {
75       typedef typename ymd_type::month_type month_type;
76       std::ostringstream ss;
77       ss << ymd.year;
78       if (format_type::has_date_sep_chars()) {
79         ss << format_type::month_sep_char();
80       }
81       //this name is a bit ugly, oh well....
82       month_formatter<month_type,format_type>::format_month(ymd.month, ss);
83       if (format_type::has_date_sep_chars()) {
84         ss << format_type::day_sep_char();
85       }
86       ss  << std::setw(2) << std::setfill('0')
87           << ymd.day;
88       return ss.str();
89     }
90   };
91 
92 
93   //! Convert a date to string using format policies
94   template<class date_type, class format_type>
95   class date_formatter
96   {
97   public:
98     //! Convert to a date to standard string using format policies
date_to_string(date_type d)99     static std::string date_to_string(date_type d)
100     {
101       typedef typename date_type::ymd_type ymd_type;
102       if (d.is_not_a_date()) {
103         return format_type::not_a_date();
104       }
105       if (d.is_neg_infinity()) {
106         return format_type::neg_infinity();
107       }
108       if (d.is_pos_infinity()) {
109         return format_type::pos_infinity();
110       }
111       ymd_type ymd = d.year_month_day();
112       return ymd_formatter<ymd_type, format_type>::ymd_to_string(ymd);
113     }
114   };
115 
116 
117 } } //namespace date_time
118 
119 
120 #endif
121 
122