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_HPP___
0002 #define DATE_TIME_DATE_FORMATTING_HPP___
0003 
0004 /* Copyright (c) 2002-2004 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 #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 /* NOTE: "formatter" code for older compilers, ones that define 
0020  * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in  
0021  * date_formatting_limited.hpp
0022  */
0023 
0024 namespace boost {
0025 namespace date_time {
0026 
0027   //! Formats a month as as string into an ostream
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     //! Formats a month as as string into an ostream
0034     /*! This function demands that month_type provide
0035      *  functions for converting to short and long strings
0036      *  if that capability is used.
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     } // format_month
0065   };
0066 
0067 
0068   //! Convert ymd to a standard string formatting policies
0069   template<class ymd_type, class format_type, class charT=char>
0070   class ymd_formatter
0071   {
0072   public:
0073     //! Convert ymd to a standard string formatting policies
0074     /*! This is standard code for handling date formatting with
0075      *  year-month-day based date information.  This function 
0076      *  uses the format_type to control whether the string will
0077      *  contain separator characters, and if so what the character
0078      *  will be.  In addtion, it can format the month as either
0079      *  an integer or a string as controled by the formatting 
0080      *  policy
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       // Temporarily switch to classic locale to prevent possible formatting
0088       // of year with comma or other character (for example 2,008).
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       //this name is a bit ugly, oh well....
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   //! Convert a date to string using format policies
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     //! Convert to a date to standard string using format policies
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 } } //namespace date_time
0134 
0135 
0136 #endif
0137