| 1 | #ifndef DATE_TIME_STRINGS_FROM_FACET__HPP___ | 
|---|
| 2 | #define DATE_TIME_STRINGS_FROM_FACET__HPP___ | 
|---|
| 3 |  | 
|---|
| 4 | /* Copyright (c) 2004 CrystalClear Software, Inc. | 
|---|
| 5 |  * Use, modification and distribution is subject to the | 
|---|
| 6 |  * Boost Software License, Version 1.0. (See accompanying | 
|---|
| 7 |  * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) | 
|---|
| 8 |  * Author: Jeff Garland | 
|---|
| 9 |  * $Date: 2006/02/18 20:58:01 $ | 
|---|
| 10 |  */ | 
|---|
| 11 |  | 
|---|
| 12 | #include <sstream> | 
|---|
| 13 | #include <string> | 
|---|
| 14 | #include <vector> | 
|---|
| 15 | #include <locale> | 
|---|
| 16 |  | 
|---|
| 17 | namespace boost { namespace date_time { | 
|---|
| 18 |  | 
|---|
| 19 | //! This function gathers up all the month strings from a std::locale | 
|---|
| 20 | /*! Using the time_put facet, this function creates a collection of | 
|---|
| 21 |  *  all the month strings from a locale.  This is handy when building | 
|---|
| 22 |  *  custom date parsers or formatters that need to be localized. | 
|---|
| 23 |  * | 
|---|
| 24 |  *@param charT The type of char to use when gathering typically char | 
|---|
| 25 |  *             or wchar_t. | 
|---|
| 26 |  *@param locale The locale to use when gathering the strings | 
|---|
| 27 |  *@param short_strings True(default) to gather short strings, | 
|---|
| 28 |  *                     false for long strings. | 
|---|
| 29 |  *@return A vector of strings containing the strings in order. eg: | 
|---|
| 30 |  *        Jan, Feb, Mar, etc. | 
|---|
| 31 |  */ | 
|---|
| 32 | template<typename charT> | 
|---|
| 33 | std::vector<std::basic_string<charT> > | 
|---|
| 34 | gather_month_strings(const std::locale& locale, bool short_strings=true) | 
|---|
| 35 | { | 
|---|
| 36 |   typedef std::basic_string<charT> string_type; | 
|---|
| 37 |   typedef std::vector<string_type> collection_type; | 
|---|
| 38 |   typedef std::basic_ostringstream<charT> ostream_type; | 
|---|
| 39 |   typedef std::ostreambuf_iterator<charT> ostream_iter_type; | 
|---|
| 40 |   typedef std::basic_ostringstream<charT> stringstream_type; | 
|---|
| 41 |   typedef std::time_put<charT>           time_put_facet_type; | 
|---|
| 42 |   charT short_fmt[3] = { '%', 'b' }; | 
|---|
| 43 |   charT long_fmt[3]  = { '%', 'B' }; | 
|---|
| 44 |   collection_type months; | 
|---|
| 45 |   string_type outfmt(short_fmt); | 
|---|
| 46 |   if (!short_strings) { | 
|---|
| 47 |     outfmt = long_fmt; | 
|---|
| 48 |   } | 
|---|
| 49 |   { | 
|---|
| 50 |     //grab the needed strings by using the locale to | 
|---|
| 51 |     //output each month | 
|---|
| 52 |     for (int m=0; m < 12; m++) { | 
|---|
| 53 |       tm tm_value; | 
|---|
| 54 |       tm_value.tm_mon = m; | 
|---|
| 55 |       stringstream_type ss; | 
|---|
| 56 |       ostream_iter_type oitr(ss); | 
|---|
| 57 |       std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(), | 
|---|
| 58 |                                                       &tm_value, | 
|---|
| 59 |                                                       &*outfmt.begin(), | 
|---|
| 60 |                                                       &*outfmt.begin()+outfmt.size()); | 
|---|
| 61 |       months.push_back(ss.str()); | 
|---|
| 62 |     } | 
|---|
| 63 |   } | 
|---|
| 64 |   return months; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | //! This function gathers up all the weekday strings from a std::locale | 
|---|
| 68 | /*! Using the time_put facet, this function creates a collection of | 
|---|
| 69 |  *  all the weekday strings from a locale starting with the string for | 
|---|
| 70 |  *  'Sunday'.  This is handy when building custom date parsers or | 
|---|
| 71 |  *  formatters that need to be localized. | 
|---|
| 72 |  * | 
|---|
| 73 |  *@param charT The type of char to use when gathering typically char | 
|---|
| 74 |  *             or wchar_t. | 
|---|
| 75 |  *@param locale The locale to use when gathering the strings | 
|---|
| 76 |  *@param short_strings True(default) to gather short strings, | 
|---|
| 77 |  *                     false for long strings. | 
|---|
| 78 |  *@return A vector of strings containing the weekdays in order. eg: | 
|---|
| 79 |  *        Sun, Mon, Tue, Wed, Thu, Fri, Sat | 
|---|
| 80 |  */ | 
|---|
| 81 | template<typename charT> | 
|---|
| 82 | std::vector<std::basic_string<charT> > | 
|---|
| 83 | gather_weekday_strings(const std::locale& locale, bool short_strings=true) | 
|---|
| 84 | { | 
|---|
| 85 |   typedef std::basic_string<charT> string_type; | 
|---|
| 86 |   typedef std::vector<string_type> collection_type; | 
|---|
| 87 |   typedef std::basic_ostringstream<charT> ostream_type; | 
|---|
| 88 |   typedef std::ostreambuf_iterator<charT> ostream_iter_type; | 
|---|
| 89 |   typedef std::basic_ostringstream<charT> stringstream_type; | 
|---|
| 90 |   typedef std::time_put<charT>           time_put_facet_type; | 
|---|
| 91 |   charT short_fmt[3] = { '%', 'a' }; | 
|---|
| 92 |   charT long_fmt[3]  = { '%', 'A' }; | 
|---|
| 93 |  | 
|---|
| 94 |   collection_type weekdays; | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 |   string_type outfmt(short_fmt); | 
|---|
| 98 |   if (!short_strings) { | 
|---|
| 99 |     outfmt = long_fmt; | 
|---|
| 100 |   } | 
|---|
| 101 |   { | 
|---|
| 102 |     //grab the needed strings by using the locale to | 
|---|
| 103 |     //output each month / weekday | 
|---|
| 104 |     for (int i=0; i < 7; i++) { | 
|---|
| 105 |       tm tm_value; | 
|---|
| 106 |       tm_value.tm_wday = i; | 
|---|
| 107 |       stringstream_type ss; | 
|---|
| 108 |       ostream_iter_type oitr(ss); | 
|---|
| 109 |       std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(), | 
|---|
| 110 |                                                       &tm_value, | 
|---|
| 111 |                                                       &*outfmt.begin(), | 
|---|
| 112 |                                                       &*outfmt.begin()+outfmt.size()); | 
|---|
| 113 |  | 
|---|
| 114 |       weekdays.push_back(ss.str()); | 
|---|
| 115 |     } | 
|---|
| 116 |   } | 
|---|
| 117 |   return weekdays; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | } } //namespace | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | #endif | 
|---|