Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:37

0001 #ifndef DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
0002 #define DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
0003 
0004 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
0010  */
0011 
0012 
0013 #include "boost/date_time/locale_config.hpp" // set BOOST_DATE_TIME_NO_LOCALE
0014 
0015 #ifndef BOOST_DATE_TIME_NO_LOCALE
0016 
0017 #include "boost/date_time/iso_format.hpp"
0018 #include "boost/date_time/date_names_put.hpp"
0019 #include "boost/date_time/parse_format_base.hpp"
0020 #include <boost/io/ios_state.hpp>
0021 //#include <string>
0022 #include <sstream>
0023 #include <iomanip>
0024 
0025 
0026 namespace boost {
0027 namespace date_time {
0028 
0029   //! Formats a month as as string into an ostream
0030   template<class facet_type,
0031            class charT = char>
0032   class ostream_month_formatter
0033   {
0034   public:
0035     typedef typename facet_type::month_type month_type;
0036     typedef std::basic_ostream<charT> ostream_type;
0037 
0038     //! Formats a month as as string into an output iterator
0039     static void format_month(const month_type& month,
0040                              ostream_type& os,
0041                              const facet_type& f)
0042     {
0043 
0044       switch (f.month_format())
0045       {
0046         case month_as_short_string:
0047         {
0048           std::ostreambuf_iterator<charT> oitr(os);
0049           f.put_month_short(oitr, month.as_enum());
0050           break;
0051         }
0052         case month_as_long_string:
0053         {
0054           std::ostreambuf_iterator<charT> oitr(os);
0055           f.put_month_long(oitr, month.as_enum());
0056           break;
0057         }
0058         case month_as_integer:
0059         {
0060           boost::io::basic_ios_fill_saver<charT> ifs(os);
0061           os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
0062           break;
0063         }
0064 
0065       }
0066     } // format_month
0067 
0068   };
0069 
0070 
0071   //! Formats a weekday
0072   template<class weekday_type,
0073            class facet_type,
0074            class charT = char>
0075   class ostream_weekday_formatter
0076   {
0077   public:
0078     typedef typename facet_type::month_type month_type;
0079     typedef std::basic_ostream<charT> ostream_type;
0080 
0081     //! Formats a month as as string into an output iterator
0082     static void format_weekday(const weekday_type& wd,
0083                                ostream_type& os,
0084                                const facet_type& f,
0085                                bool  as_long_string)
0086     {
0087 
0088       std::ostreambuf_iterator<charT> oitr(os);
0089       if (as_long_string) {
0090         f.put_weekday_long(oitr, wd.as_enum());
0091       }
0092       else {
0093         f.put_weekday_short(oitr, wd.as_enum());
0094       }
0095 
0096     } // format_weekday
0097 
0098   };
0099 
0100 
0101   //! Convert ymd to a standard string formatting policies
0102   template<class ymd_type,
0103            class facet_type,
0104            class charT = char>
0105   class ostream_ymd_formatter
0106   {
0107   public:
0108     typedef typename ymd_type::month_type month_type;
0109     typedef ostream_month_formatter<facet_type, charT> month_formatter_type;
0110     typedef std::basic_ostream<charT> ostream_type;
0111     typedef std::basic_string<charT> foo_type;
0112 
0113     //! Convert ymd to a standard string formatting policies
0114     /*! This is standard code for handling date formatting with
0115      *  year-month-day based date information.  This function
0116      *  uses the format_type to control whether the string will
0117      *  contain separator characters, and if so what the character
0118      *  will be.  In addtion, it can format the month as either
0119      *  an integer or a string as controled by the formatting
0120      *  policy
0121      */
0122     //     static string_type ymd_to_string(ymd_type ymd)
0123 //     {
0124 //       std::ostringstream ss;
0125 //       facet_type dnp;
0126 //       ymd_put(ymd, ss, dnp);
0127 //       return ss.str();
0128 //       }
0129 
0130 
0131     // Put ymd to ostream -- part of ostream refactor
0132     static void ymd_put(ymd_type ymd,
0133                         ostream_type& os,
0134                         const facet_type& f)
0135     {
0136       boost::io::basic_ios_fill_saver<charT> ifs(os);
0137       std::ostreambuf_iterator<charT> oitr(os);
0138       switch (f.date_order()) {
0139         case ymd_order_iso: {
0140           os << ymd.year;
0141           if (f.has_date_sep_chars()) {
0142             f.month_sep_char(oitr);
0143           }
0144           month_formatter_type::format_month(ymd.month, os, f);
0145           if (f.has_date_sep_chars()) {
0146             f.day_sep_char(oitr);
0147           }
0148           os  << std::setw(2) << std::setfill(os.widen('0'))
0149               << ymd.day;
0150           break;
0151         }
0152         case ymd_order_us: {
0153           month_formatter_type::format_month(ymd.month, os, f);
0154           if (f.has_date_sep_chars()) {
0155           f.day_sep_char(oitr);
0156           }
0157           os  << std::setw(2) << std::setfill(os.widen('0'))
0158             << ymd.day;
0159           if (f.has_date_sep_chars()) {
0160             f.month_sep_char(oitr);
0161           }
0162           os << ymd.year;
0163           break;
0164         }
0165         case ymd_order_dmy: {
0166           os  << std::setw(2) << std::setfill(os.widen('0'))
0167               << ymd.day;
0168           if (f.has_date_sep_chars()) {
0169             f.day_sep_char(oitr);
0170           }
0171           month_formatter_type::format_month(ymd.month, os, f);
0172           if (f.has_date_sep_chars()) {
0173             f.month_sep_char(oitr);
0174           }
0175           os << ymd.year;
0176           break;
0177         }
0178       }
0179     }
0180   };
0181 
0182 
0183   //! Convert a date to string using format policies
0184   template<class date_type,
0185            class facet_type,
0186            class charT = char>
0187   class ostream_date_formatter
0188   {
0189   public:
0190     typedef std::basic_ostream<charT> ostream_type;
0191     typedef typename date_type::ymd_type ymd_type;
0192 
0193     //! Put date into an ostream
0194     static void date_put(const date_type& d,
0195                          ostream_type& os,
0196                          const facet_type& f)
0197     {
0198       special_values sv = d.as_special();
0199       if (sv == not_special) {
0200         ymd_type ymd = d.year_month_day();
0201         ostream_ymd_formatter<ymd_type, facet_type, charT>::ymd_put(ymd, os, f);
0202       }
0203       else { // output a special value
0204         std::ostreambuf_iterator<charT> coi(os);
0205         f.put_special_value(coi, sv);
0206       }
0207     }
0208 
0209 
0210     //! Put date into an ostream
0211     static void date_put(const date_type& d,
0212                          ostream_type& os)
0213     {
0214       //retrieve the local from the ostream
0215       std::locale locale = os.getloc();
0216       if (std::has_facet<facet_type>(locale)) {
0217         const facet_type& f = std::use_facet<facet_type>(locale);
0218         date_put(d, os, f);
0219       }
0220       else {
0221         //default to something sensible if no facet installed
0222         facet_type default_facet;
0223         date_put(d, os, default_facet);
0224       }
0225     } // date_to_ostream 
0226   }; //class date_formatter
0227 
0228 
0229 } } //namespace date_time
0230 
0231 #endif
0232 
0233 #endif
0234