New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
strings_from_facet.hpp in vendors/XIOS/current/extern/boost/include/boost/date_time – NEMO

source: vendors/XIOS/current/extern/boost/include/boost/date_time/strings_from_facet.hpp @ 3408

Last change on this file since 3408 was 3408, checked in by rblod, 12 years ago

importing initial XIOS vendor drop

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
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.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland
9 * $Date: 2009-02-01 06:29:43 -0500 (Sun, 01 Feb 2009) $
10 */
11
12#include <sstream>
13#include <string>
14#include <vector>
15#include <locale>
16
17namespace 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 */
32template<typename charT>
33std::vector<std::basic_string<charT> >
34gather_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    const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
53    for (int m=0; m < 12; m++) {
54      tm tm_value;
55      tm_value.tm_mon = m;
56      stringstream_type ss;
57      ostream_iter_type oitr(ss);
58      std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
59                                                      &tm_value,
60                                                      p_outfmt,
61                                                      p_outfmt_end);
62      months.push_back(ss.str());
63    }
64  }
65  return months;
66}
67
68//! This function gathers up all the weekday strings from a std::locale
69/*! Using the time_put facet, this function creates a collection of
70 *  all the weekday strings from a locale starting with the string for
71 *  'Sunday'.  This is handy when building custom date parsers or
72 *  formatters that need to be localized.
73 *
74 *@param charT The type of char to use when gathering typically char
75 *             or wchar_t.
76 *@param locale The locale to use when gathering the strings
77 *@param short_strings True(default) to gather short strings,
78 *                     false for long strings.
79 *@return A vector of strings containing the weekdays in order. eg:
80 *        Sun, Mon, Tue, Wed, Thu, Fri, Sat
81 */
82template<typename charT>
83std::vector<std::basic_string<charT> >
84gather_weekday_strings(const std::locale& locale, bool short_strings=true)
85{
86  typedef std::basic_string<charT> string_type;
87  typedef std::vector<string_type> collection_type;
88  typedef std::basic_ostringstream<charT> ostream_type;
89  typedef std::ostreambuf_iterator<charT> ostream_iter_type;
90  typedef std::basic_ostringstream<charT> stringstream_type;
91  typedef std::time_put<charT>           time_put_facet_type;
92  charT short_fmt[3] = { '%', 'a' };
93  charT long_fmt[3]  = { '%', 'A' };
94
95  collection_type weekdays;
96
97
98  string_type outfmt(short_fmt);
99  if (!short_strings) {
100    outfmt = long_fmt;
101  }
102  {
103    //grab the needed strings by using the locale to
104    //output each month / weekday
105    const charT* p_outfmt = outfmt.c_str(), *p_outfmt_end = p_outfmt + outfmt.size();
106    for (int i=0; i < 7; i++) {
107      tm tm_value;
108      tm_value.tm_wday = i;
109      stringstream_type ss;
110      ostream_iter_type oitr(ss);
111      std::use_facet<time_put_facet_type>(locale).put(oitr, ss, ss.fill(),
112                                                      &tm_value,
113                                                      p_outfmt,
114                                                      p_outfmt_end);
115
116      weekdays.push_back(ss.str());
117    }
118  }
119  return weekdays;
120}
121
122} } //namespace
123
124
125#endif
Note: See TracBrowser for help on using the repository browser.