Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:22

0001 #ifndef DATE_TIME_DATE_FORMATTING_LIMITED_HPP___
0002 #define DATE_TIME_DATE_FORMATTING_LIMITED_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 <string>
0015 #include <sstream>
0016 #include <iomanip>
0017 
0018 
0019 namespace boost {
0020 namespace date_time {
0021 
0022   //! Formats a month as as string into an ostream
0023   template<class month_type, class format_type>
0024   class month_formatter
0025   {
0026   public:
0027     //! Formats a month as as string into an ostream
0028     /*! This function demands that month_type provide
0029      *  functions for converting to short and long strings
0030      *  if that capability is used.
0031      */
0032     static std::ostream& format_month(const month_type& month,
0033                                       std::ostream& os)
0034     {
0035       switch (format_type::month_format()) 
0036       {
0037         case month_as_short_string: 
0038         { 
0039           os << month.as_short_string(); 
0040           break;
0041         }
0042         case month_as_long_string: 
0043         { 
0044           os << month.as_long_string(); 
0045           break;
0046         }
0047         case month_as_integer: 
0048         { 
0049           os << std::setw(2) << std::setfill('0') << month.as_number();
0050           break;
0051         }
0052      
0053       }
0054       return os;
0055     } // format_month
0056   };
0057 
0058 
0059   //! Convert ymd to a standard string formatting policies
0060   template<class ymd_type, class format_type>
0061   class ymd_formatter
0062   {
0063   public:
0064     //! Convert ymd to a standard string formatting policies
0065     /*! This is standard code for handling date formatting with
0066      *  year-month-day based date information.  This function 
0067      *  uses the format_type to control whether the string will
0068      *  contain separator characters, and if so what the character
0069      *  will be.  In addtion, it can format the month as either
0070      *  an integer or a string as controled by the formatting 
0071      *  policy
0072      */ 
0073     static std::string ymd_to_string(ymd_type ymd)
0074     {
0075       typedef typename ymd_type::month_type month_type;
0076       std::ostringstream ss;
0077       ss << ymd.year;
0078       if (format_type::has_date_sep_chars()) {
0079         ss << format_type::month_sep_char();
0080       }
0081       //this name is a bit ugly, oh well....
0082       month_formatter<month_type,format_type>::format_month(ymd.month, ss);
0083       if (format_type::has_date_sep_chars()) {
0084         ss << format_type::day_sep_char();
0085       }
0086       ss  << std::setw(2) << std::setfill('0') 
0087           << ymd.day;
0088       return ss.str();
0089     }
0090   };
0091 
0092 
0093   //! Convert a date to string using format policies
0094   template<class date_type, class format_type>
0095   class date_formatter
0096   {
0097   public:
0098     //! Convert to a date to standard string using format policies
0099     static std::string date_to_string(date_type d)
0100     {
0101       typedef typename date_type::ymd_type ymd_type;
0102       if (d.is_not_a_date()) {
0103         return format_type::not_a_date();
0104       }
0105       if (d.is_neg_infinity()) {
0106         return format_type::neg_infinity();
0107       }
0108       if (d.is_pos_infinity()) {
0109         return format_type::pos_infinity();
0110       }
0111       ymd_type ymd = d.year_month_day();
0112       return ymd_formatter<ymd_type, format_type>::ymd_to_string(ymd);
0113     }    
0114   };
0115 
0116 
0117 } } //namespace date_time
0118 
0119 
0120 #endif
0121