File indexing completed on 2025-01-18 09:30:37
0001 #ifndef DATE_TIME_DATE_FORMATTING_HPP___
0002 #define DATE_TIME_DATE_FORMATTING_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include "boost/date_time/iso_format.hpp"
0013 #include "boost/date_time/compiler_config.hpp"
0014 #include <boost/io/ios_state.hpp>
0015 #include <string>
0016 #include <sstream>
0017 #include <iomanip>
0018
0019
0020
0021
0022
0023
0024 namespace boost {
0025 namespace date_time {
0026
0027
0028 template<class month_type, class format_type, class charT=char>
0029 class month_formatter
0030 {
0031 typedef std::basic_ostream<charT> ostream_type;
0032 public:
0033
0034
0035
0036
0037
0038 static ostream_type& format_month(const month_type& month,
0039 ostream_type &os)
0040 {
0041 switch (format_type::month_format())
0042 {
0043 case month_as_short_string:
0044 {
0045 os << month.as_short_string();
0046 break;
0047 }
0048 case month_as_long_string:
0049 {
0050 os << month.as_long_string();
0051 break;
0052 }
0053 case month_as_integer:
0054 {
0055 boost::io::basic_ios_fill_saver<charT> ifs(os);
0056 os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
0057 break;
0058 }
0059 default:
0060 break;
0061
0062 }
0063 return os;
0064 }
0065 };
0066
0067
0068
0069 template<class ymd_type, class format_type, class charT=char>
0070 class ymd_formatter
0071 {
0072 public:
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 static std::basic_string<charT> ymd_to_string(ymd_type ymd)
0083 {
0084 typedef typename ymd_type::month_type month_type;
0085 std::basic_ostringstream<charT> ss;
0086
0087
0088
0089 ss.imbue(std::locale::classic());
0090 ss << ymd.year;
0091 ss.imbue(std::locale());
0092
0093 if (format_type::has_date_sep_chars()) {
0094 ss << format_type::month_sep_char();
0095 }
0096
0097 month_formatter<month_type,format_type,charT>::format_month(ymd.month, ss);
0098 if (format_type::has_date_sep_chars()) {
0099 ss << format_type::day_sep_char();
0100 }
0101 ss << std::setw(2) << std::setfill(ss.widen('0'))
0102 << ymd.day;
0103 return ss.str();
0104 }
0105 };
0106
0107
0108
0109 template<class date_type, class format_type, class charT=char>
0110 class date_formatter
0111 {
0112 public:
0113 typedef std::basic_string<charT> string_type;
0114
0115 static string_type date_to_string(date_type d)
0116 {
0117 typedef typename date_type::ymd_type ymd_type;
0118 if (d.is_not_a_date()) {
0119 return string_type(format_type::not_a_date());
0120 }
0121 if (d.is_neg_infinity()) {
0122 return string_type(format_type::neg_infinity());
0123 }
0124 if (d.is_pos_infinity()) {
0125 return string_type(format_type::pos_infinity());
0126 }
0127 ymd_type ymd = d.year_month_day();
0128 return ymd_formatter<ymd_type, format_type, charT>::ymd_to_string(ymd);
0129 }
0130 };
0131
0132
0133 } }
0134
0135
0136 #endif
0137