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