Chris@16: #ifndef DATE_TIME_STRINGS_FROM_FACET__HPP___ Chris@16: #define DATE_TIME_STRINGS_FROM_FACET__HPP___ Chris@16: Chris@16: /* Copyright (c) 2004 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace date_time { Chris@16: Chris@16: //! This function gathers up all the month strings from a std::locale Chris@16: /*! Using the time_put facet, this function creates a collection of Chris@16: * all the month strings from a locale. This is handy when building Chris@16: * custom date parsers or formatters that need to be localized. Chris@16: * Chris@16: *@param charT The type of char to use when gathering typically char Chris@16: * or wchar_t. Chris@16: *@param locale The locale to use when gathering the strings Chris@16: *@param short_strings True(default) to gather short strings, Chris@16: * false for long strings. Chris@16: *@return A vector of strings containing the strings in order. eg: Chris@16: * Jan, Feb, Mar, etc. Chris@16: */ Chris@16: template Chris@16: std::vector > Chris@16: gather_month_strings(const std::locale& locale, bool short_strings=true) Chris@16: { Chris@16: typedef std::basic_string string_type; Chris@16: typedef std::vector collection_type; Chris@16: typedef std::ostreambuf_iterator ostream_iter_type; Chris@16: typedef std::basic_ostringstream stringstream_type; Chris@16: typedef std::time_put time_put_facet_type; Chris@16: charT short_fmt[3] = { '%', 'b' }; Chris@16: charT long_fmt[3] = { '%', 'B' }; Chris@16: collection_type months; Chris@16: string_type outfmt(short_fmt); Chris@16: if (!short_strings) { Chris@16: outfmt = long_fmt; Chris@16: } Chris@16: { Chris@16: //grab the needed strings by using the locale to Chris@16: //output each month Chris@16: const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size(); Chris@16: tm tm_value; Chris@16: memset(&tm_value, 0, sizeof(tm_value)); Chris@16: for (int m=0; m < 12; m++) { Chris@16: tm_value.tm_mon = m; Chris@16: stringstream_type ss; Chris@16: ostream_iter_type oitr(ss); Chris@16: std::use_facet(locale).put(oitr, ss, ss.fill(), Chris@16: &tm_value, Chris@16: p_outfmt, Chris@16: p_outfmt_end); Chris@16: months.push_back(ss.str()); Chris@16: } Chris@16: } Chris@16: return months; Chris@16: } Chris@16: Chris@16: //! This function gathers up all the weekday strings from a std::locale Chris@16: /*! Using the time_put facet, this function creates a collection of Chris@16: * all the weekday strings from a locale starting with the string for Chris@16: * 'Sunday'. This is handy when building custom date parsers or Chris@16: * formatters that need to be localized. Chris@16: * Chris@16: *@param charT The type of char to use when gathering typically char Chris@16: * or wchar_t. Chris@16: *@param locale The locale to use when gathering the strings Chris@16: *@param short_strings True(default) to gather short strings, Chris@16: * false for long strings. Chris@16: *@return A vector of strings containing the weekdays in order. eg: Chris@16: * Sun, Mon, Tue, Wed, Thu, Fri, Sat Chris@16: */ Chris@16: template Chris@16: std::vector > Chris@16: gather_weekday_strings(const std::locale& locale, bool short_strings=true) Chris@16: { Chris@16: typedef std::basic_string string_type; Chris@16: typedef std::vector collection_type; Chris@16: typedef std::ostreambuf_iterator ostream_iter_type; Chris@16: typedef std::basic_ostringstream stringstream_type; Chris@16: typedef std::time_put time_put_facet_type; Chris@16: charT short_fmt[3] = { '%', 'a' }; Chris@16: charT long_fmt[3] = { '%', 'A' }; Chris@16: Chris@16: collection_type weekdays; Chris@16: Chris@16: Chris@16: string_type outfmt(short_fmt); Chris@16: if (!short_strings) { Chris@16: outfmt = long_fmt; Chris@16: } Chris@16: { Chris@16: //grab the needed strings by using the locale to Chris@16: //output each month / weekday Chris@16: const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size(); Chris@16: tm tm_value; Chris@16: memset(&tm_value, 0, sizeof(tm_value)); Chris@16: for (int i=0; i < 7; i++) { Chris@16: tm_value.tm_wday = i; Chris@16: stringstream_type ss; Chris@16: ostream_iter_type oitr(ss); Chris@16: std::use_facet(locale).put(oitr, ss, ss.fill(), Chris@16: &tm_value, Chris@16: p_outfmt, Chris@16: p_outfmt_end); Chris@16: Chris@16: weekdays.push_back(ss.str()); Chris@16: } Chris@16: } Chris@16: return weekdays; Chris@16: } Chris@16: Chris@16: } } //namespace Chris@16: Chris@16: Chris@16: #endif