Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef _DATE_TIME_DATE_GENERATOR_FORMATTER__HPP___
0002 #define _DATE_TIME_DATE_GENERATOR_FORMATTER__HPP___
0003 
0004 /* Copyright (c) 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 <iostream>
0013 #include <string>
0014 #include <vector>
0015 #include <algorithm>
0016 #include "boost/date_time/date_generators.hpp"
0017 
0018 namespace boost {
0019 namespace date_time {
0020 
0021   //! Formats date_generators for output
0022   /*! Formatting of date_generators follows specific orders for the 
0023    * various types of date_generators.
0024    * - partial_date                     => "dd Month"
0025    * - nth_day_of_the_week_in_month     => "nth weekday of month"
0026    * - first_day_of_the_week_in_month   => "first weekday of month"
0027    * - last_day_of_the_week_in_month    => "last weekday of month"
0028    * - first_day_of_the_week_after      => "weekday after"
0029    * - first_day_of_the_week_before     => "weekday before"
0030    * While the order of the elements in these phrases cannot be changed, 
0031    * the elements themselves can be. Weekday and Month get their formats
0032    * and names from the date_facet. The remaining elements are stored in 
0033    * the date_generator_formatter and can be customized upon construction
0034    * or via a member function. The default elements are those shown in the 
0035    * examples above.
0036    */
0037   template <class date_type, class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
0038   class date_generator_formatter {
0039     public:
0040       typedef partial_date<date_type>          partial_date_type;
0041       typedef nth_kday_of_month<date_type>     nth_kday_type;
0042       typedef first_kday_of_month<date_type>   first_kday_type;
0043       typedef last_kday_of_month<date_type>    last_kday_type;
0044       typedef first_kday_after<date_type>      kday_after_type;
0045       typedef first_kday_before<date_type>     kday_before_type;
0046 
0047       typedef CharT char_type;
0048       typedef std::basic_string<char_type> string_type;
0049       typedef std::vector<string_type> collection_type;
0050       static const char_type first_string[6];
0051       static const char_type second_string[7];
0052       static const char_type third_string[6];
0053       static const char_type fourth_string[7];
0054       static const char_type fifth_string[6];
0055       static const char_type last_string[5];
0056       static const char_type before_string[8];
0057       static const char_type after_string[6];
0058       static const char_type of_string[3];
0059 
0060       enum phrase_elements {first=0, second, third, fourth, fifth, last,
0061                          before, after, of, number_of_phrase_elements};
0062 
0063       //! Default format elements used
0064       date_generator_formatter()
0065       {
0066         phrase_strings.reserve(number_of_phrase_elements);
0067         phrase_strings.push_back(string_type(first_string));
0068         phrase_strings.push_back(string_type(second_string));
0069         phrase_strings.push_back(string_type(third_string));
0070         phrase_strings.push_back(string_type(fourth_string));
0071         phrase_strings.push_back(string_type(fifth_string));
0072         phrase_strings.push_back(string_type(last_string));
0073         phrase_strings.push_back(string_type(before_string));
0074         phrase_strings.push_back(string_type(after_string));
0075         phrase_strings.push_back(string_type(of_string));
0076       }
0077 
0078       //! Constructor that allows for a custom set of phrase elements
0079       date_generator_formatter(const string_type& first_str,
0080                                const string_type& second_str,
0081                                const string_type& third_str,
0082                                const string_type& fourth_str,
0083                                const string_type& fifth_str,
0084                                const string_type& last_str,
0085                                const string_type& before_str,
0086                                const string_type& after_str,
0087                                const string_type& of_str)
0088       {
0089         phrase_strings.reserve(number_of_phrase_elements);
0090         phrase_strings.push_back(first_str);
0091         phrase_strings.push_back(second_str);
0092         phrase_strings.push_back(third_str);
0093         phrase_strings.push_back(fourth_str);
0094         phrase_strings.push_back(fifth_str);
0095         phrase_strings.push_back(last_str);
0096         phrase_strings.push_back(before_str);
0097         phrase_strings.push_back(after_str);
0098         phrase_strings.push_back(of_str);
0099       }
0100 
0101       //! Replace the set of phrase elements with those contained in new_strings
0102       /*! The order of the strings in the given collection is important.
0103        * They must follow: 
0104        *  - first, second, third, fourth, fifth, last, before, after, of. 
0105        *
0106        * It is not necessary to send in a complete set if only a few 
0107        * elements are to be replaced as long as the correct beg_pos is used.
0108        *
0109        * Ex: To keep the default first through fifth elements, but replace 
0110        * the rest with a collection of: 
0111        *  - "final", "prior", "following", "in". 
0112        * The beg_pos of date_generator_formatter::last would be used.
0113        */
0114       void elements(const collection_type& new_strings,
0115                     phrase_elements beg_pos=first)
0116       {
0117         if(beg_pos < number_of_phrase_elements) {
0118           typename collection_type::iterator itr = phrase_strings.begin();
0119           itr += beg_pos;
0120           std::copy(new_strings.begin(), new_strings.end(),
0121                     itr);
0122                     //phrase_strings.begin());
0123         }
0124       }
0125 
0126       //!Put a partial_date => "dd Month"
0127       template<class facet_type>
0128       OutItrT put_partial_date(OutItrT next, std::ios_base& a_ios,
0129                                CharT a_fill, const partial_date_type& pd,
0130                                const facet_type& facet) const
0131       {
0132         facet.put(next, a_ios, a_fill, pd.day());
0133         next = a_fill; //TODO change this ???
0134         facet.put(next, a_ios, a_fill, pd.month());
0135         return next;
0136       }
0137 
0138       //! Put an nth_day_of_the_week_in_month => "nth weekday of month"
0139       template<class facet_type>
0140       OutItrT put_nth_kday(OutItrT next, std::ios_base& a_ios,
0141                            CharT a_fill, const nth_kday_type& nkd,
0142                            const facet_type& facet) const
0143       {
0144         put_string(next, phrase_strings[nkd.nth_week() -1]);
0145         next = a_fill; //TODO change this ???
0146         facet.put(next, a_ios, a_fill, nkd.day_of_week());
0147         next = a_fill; //TODO change this ???
0148         put_string(next, string_type(of_string));
0149         next = a_fill; //TODO change this ???
0150         facet.put(next, a_ios, a_fill, nkd.month());
0151         return next;
0152       }
0153 
0154       //! Put a first_day_of_the_week_in_month => "first weekday of month"
0155       template<class facet_type>
0156       OutItrT put_first_kday(OutItrT next, std::ios_base& a_ios,
0157                              CharT a_fill, const first_kday_type& fkd,
0158                              const facet_type& facet) const
0159       {
0160         put_string(next, phrase_strings[first]);
0161         next = a_fill; //TODO change this ???
0162         facet.put(next, a_ios, a_fill, fkd.day_of_week());
0163         next = a_fill; //TODO change this ???
0164         put_string(next, string_type(of_string));
0165         next = a_fill; //TODO change this ???
0166         facet.put(next, a_ios, a_fill, fkd.month());
0167         return next;
0168       }
0169 
0170       //! Put a last_day_of_the_week_in_month => "last weekday of month"
0171       template<class facet_type>
0172       OutItrT put_last_kday(OutItrT next, std::ios_base& a_ios,
0173                            CharT a_fill, const last_kday_type& lkd,
0174                            const facet_type& facet) const
0175       {
0176         put_string(next, phrase_strings[last]);
0177         next = a_fill; //TODO change this ???
0178         facet.put(next, a_ios, a_fill, lkd.day_of_week());
0179         next = a_fill; //TODO change this ???
0180         put_string(next, string_type(of_string));
0181         next = a_fill; //TODO change this ???
0182         facet.put(next, a_ios, a_fill, lkd.month());
0183         return next;
0184       }
0185 
0186       //! Put a first_day_of_the_week_before => "weekday before"
0187       template<class facet_type>
0188       OutItrT put_kday_before(OutItrT next, std::ios_base& a_ios,
0189                               CharT a_fill, const kday_before_type& fkb,
0190                               const facet_type& facet) const
0191       {
0192         facet.put(next, a_ios, a_fill, fkb.day_of_week());
0193         next = a_fill; //TODO change this ???
0194         put_string(next, phrase_strings[before]);
0195         return next;
0196       }
0197 
0198       //! Put a first_day_of_the_week_after => "weekday after"
0199       template<class facet_type>
0200       OutItrT put_kday_after(OutItrT next, std::ios_base& a_ios,
0201                              CharT a_fill, const kday_after_type& fka,
0202                              const facet_type& facet) const
0203       {
0204         facet.put(next, a_ios, a_fill, fka.day_of_week());
0205         next = a_fill; //TODO change this ???
0206         put_string(next, phrase_strings[after]);
0207         return next;
0208       }
0209 
0210 
0211     private:
0212       collection_type phrase_strings;
0213 
0214       //! helper function to put the various member string into stream
0215       OutItrT put_string(OutItrT next, const string_type& str) const
0216       {
0217         typename string_type::const_iterator itr = str.begin();
0218         while(itr != str.end()) {
0219           *next = *itr;
0220           ++itr;
0221           ++next;
0222         }
0223         return next;
0224       }
0225   };
0226 
0227   template<class date_type, class CharT, class OutItrT>
0228   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0229   date_generator_formatter<date_type, CharT, OutItrT>::first_string[6] = 
0230     {'f','i','r','s','t'};
0231   template<class date_type, class CharT, class OutItrT>
0232   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0233   date_generator_formatter<date_type, CharT, OutItrT>::second_string[7] = 
0234     {'s','e','c','o','n','d'};
0235   template<class date_type, class CharT, class OutItrT>
0236   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0237   date_generator_formatter<date_type, CharT, OutItrT>::third_string[6] = 
0238     {'t','h','i','r','d'};
0239   template<class date_type, class CharT, class OutItrT>
0240   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0241   date_generator_formatter<date_type, CharT, OutItrT>::fourth_string[7] = 
0242     {'f','o','u','r','t','h'};
0243   template<class date_type, class CharT, class OutItrT>
0244   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0245   date_generator_formatter<date_type, CharT, OutItrT>::fifth_string[6] = 
0246     {'f','i','f','t','h'};
0247   template<class date_type, class CharT, class OutItrT>
0248   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0249   date_generator_formatter<date_type, CharT, OutItrT>::last_string[5] = 
0250     {'l','a','s','t'};
0251   template<class date_type, class CharT, class OutItrT>
0252   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0253   date_generator_formatter<date_type, CharT, OutItrT>::before_string[8] = 
0254     {'b','e','f','o','r','e'};
0255   template<class date_type, class CharT, class OutItrT>
0256   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0257   date_generator_formatter<date_type, CharT, OutItrT>::after_string[6] = 
0258     {'a','f','t','e','r'};
0259   template<class date_type, class CharT, class OutItrT>
0260   const typename date_generator_formatter<date_type, CharT, OutItrT>::char_type
0261   date_generator_formatter<date_type, CharT, OutItrT>::of_string[3] = 
0262     {'o','f'};
0263 } } // namespaces
0264 
0265 #endif // _DATE_TIME_DATE_GENERATOR_FORMATTER__HPP___