Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef DATE_TIME_STRINGS_FROM_FACET__HPP___
0002 #define DATE_TIME_STRINGS_FROM_FACET__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
0009  * $Date$
0010  */
0011 
0012 #include <cstring>
0013 #include <sstream>
0014 #include <string>
0015 #include <vector>
0016 #include <locale>
0017 #include <iterator>
0018 
0019 namespace boost { namespace date_time {
0020 
0021 //! This function gathers up all the month strings from a std::locale
0022 /*! Using the time_put facet, this function creates a collection of
0023  *  all the month strings from a locale.  This is handy when building
0024  *  custom date parsers or formatters that need to be localized.
0025  *
0026  *@param charT The type of char to use when gathering typically char
0027  *             or wchar_t.
0028  *@param locale The locale to use when gathering the strings
0029  *@param short_strings True(default) to gather short strings,
0030  *                     false for long strings.
0031  *@return A vector of strings containing the strings in order. eg:
0032  *        Jan, Feb, Mar, etc.
0033  */
0034 template<typename charT>
0035 std::vector<std::basic_string<charT> >
0036 gather_month_strings(const std::locale& locale, bool short_strings=true)
0037 {
0038   typedef std::basic_string<charT> string_type;
0039   typedef std::vector<string_type> collection_type;
0040   typedef std::ostreambuf_iterator<charT> ostream_iter_type;
0041   typedef std::basic_ostringstream<charT> stringstream_type;
0042   typedef std::time_put<charT>           time_put_facet_type;
0043   charT short_fmt[3] = { '%', 'b' };
0044   charT long_fmt[3]  = { '%', 'B' };
0045   collection_type months;
0046   string_type outfmt(short_fmt);
0047   if (!short_strings) {
0048     outfmt = long_fmt;
0049   }
0050   {
0051     //grab the needed strings by using the locale to
0052     //output each month
0053     const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
0054     tm tm_value;
0055     std::memset(&tm_value, 0, sizeof(tm_value));
0056     for (int m=0; m < 12; m++) {
0057       tm_value.tm_mon = m;
0058       stringstream_type ss;
0059       ostream_iter_type oitr(ss);
0060       std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
0061                                                       &tm_value,
0062                                                       p_outfmt,
0063                                                       p_outfmt_end);
0064       months.push_back(ss.str());
0065     }
0066   }
0067   return months;
0068 }
0069 
0070 //! This function gathers up all the weekday strings from a std::locale
0071 /*! Using the time_put facet, this function creates a collection of
0072  *  all the weekday strings from a locale starting with the string for
0073  *  'Sunday'.  This is handy when building custom date parsers or
0074  *  formatters that need to be localized.
0075  *
0076  *@param charT The type of char to use when gathering typically char
0077  *             or wchar_t.
0078  *@param locale The locale to use when gathering the strings
0079  *@param short_strings True(default) to gather short strings,
0080  *                     false for long strings.
0081  *@return A vector of strings containing the weekdays in order. eg:
0082  *        Sun, Mon, Tue, Wed, Thu, Fri, Sat
0083  */
0084 template<typename charT>
0085 std::vector<std::basic_string<charT> >
0086 gather_weekday_strings(const std::locale& locale, bool short_strings=true)
0087 {
0088   typedef std::basic_string<charT> string_type;
0089   typedef std::vector<string_type> collection_type;
0090   typedef std::ostreambuf_iterator<charT> ostream_iter_type;
0091   typedef std::basic_ostringstream<charT> stringstream_type;
0092   typedef std::time_put<charT>           time_put_facet_type;
0093   charT short_fmt[3] = { '%', 'a' };
0094   charT long_fmt[3]  = { '%', 'A' };
0095 
0096   collection_type weekdays;
0097 
0098 
0099   string_type outfmt(short_fmt);
0100   if (!short_strings) {
0101     outfmt = long_fmt;
0102   }
0103   {
0104     //grab the needed strings by using the locale to
0105     //output each month / weekday
0106     const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
0107     tm tm_value;
0108     std::memset(&tm_value, 0, sizeof(tm_value));
0109     for (int i=0; i < 7; i++) {
0110       tm_value.tm_wday = i;
0111       stringstream_type ss;
0112       ostream_iter_type oitr(ss);
0113       std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
0114                                                       &tm_value,
0115                                                       p_outfmt,
0116                                                       p_outfmt_end);
0117 
0118       weekdays.push_back(ss.str());
0119     }
0120   }
0121   return weekdays;
0122 }
0123 
0124 } } //namespace
0125 
0126 
0127 #endif