2 * © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
20 // WARNING: This is an internal header file, included by other C++
21 // standard library headers. You should not attempt to use this header
25 #ifndef _STLP_INTERNAL_TIME_FACETS_H
26 #define _STLP_INTERNAL_TIME_FACETS_H
29 # include <ctime> // Needed (for struct tm) by time facets
32 #include <stl/c_locale.h>
33 #include <stl/_ios_base.h>
37 // Template functions used by time_get
39 // Matching input against a list of names
41 // Alphabetic input of the names of months and the names
42 // of weekdays requires matching input against a list of names.
43 // We use a simple generic algorithm to accomplish this. This
44 // algorithm is not very efficient, especially for longer lists
45 // of names, but it probably does not matter for the initial
46 // implementation and it may never matter, since we do not expect
47 // this kind of input to be used very often. The algorithm
48 // could be improved fairly simply by creating a new list of
49 // names still in the running at each iteration. A more sophisticated
50 // approach would be to build a trie to do the matching.
52 // We compare each character of the input to the corresponding
53 // character of each name on the list that has not been eliminated,
54 // either because every character in the name has already been
55 // matched, or because some character has not been matched. We
56 // continue only as long as there are some names that have not been
59 // We do not really need a random access iterator (a forward iterator
60 // would do), but the extra generality makes the notation clumsier,
61 // and we don't really need it.
63 // We can recognize a failed match by the fact that the second
64 // component of the return value will be __name_end.
67 #define _MAX_NAME_LENGTH 64
69 // Both time_get and time_put need a structure of type _Time_Info
70 // to provide names and abbreviated names for months and days,
71 // as well as the am/pm designator. The month and weekday tables
72 // have the all the abbreviated names before all the full names.
73 // The _Time_Info tables are initialized using the non-template
74 // function _Init_timeinfo, which has two overloadings: one
75 // with a single reference parameter for the table to be initialized,
76 // and one with a second _Locale_time * parameter. The first form
77 // is called by the default constructor and the second by a special
78 // constructor invoked from the _byname subclass constructor to
79 // construct the base class.
81 class _STLP_CLASS_DECLSPEC _Time_Info {
83 string _M_dayname[14];
84 string _M_monthname[24];
86 string _M_time_format;
87 string _M_date_format;
88 string _M_date_time_format;
89 string _M_long_date_format;
90 string _M_long_date_time_format;
93 _STLP_DECLSPEC void _STLP_CALL _Init_timeinfo(_Time_Info&);
94 _STLP_DECLSPEC void _STLP_CALL _Init_timeinfo(_Time_Info&, _Locale_time*);
96 class _STLP_CLASS_DECLSPEC time_base {
98 enum dateorder {no_order, dmy, mdy, ymd, ydm};
102 template <class _Ch, __DFL_TMPL_PARAM( _InIt , istreambuf_iterator<_Ch>) >
103 class time_get : public locale::facet, public time_base
105 friend class _Locale;
108 typedef _Ch char_type;
109 typedef _InIt iter_type;
111 explicit time_get(size_t __refs = 0) : _BaseFacet(__refs) {
112 _Init_timeinfo(_M_timeinfo);
114 dateorder date_order() const { return do_date_order(); }
115 iter_type get_time(iter_type __s, iter_type __end, ios_base& __str,
116 ios_base::iostate& __err, tm* __t) const
117 { return do_get_time(__s, __end, __str, __err, __t); }
118 iter_type get_date(iter_type __s, iter_type __end, ios_base& __str,
119 ios_base::iostate& __err, tm* __t) const
120 { return do_get_date(__s, __end, __str, __err, __t); }
121 iter_type get_weekday(iter_type __s, iter_type __end, ios_base& __str,
122 ios_base::iostate& __err, tm* __t) const
123 { return do_get_weekday(__s, __end, __str, __err, __t); }
124 iter_type get_monthname(iter_type __s, iter_type __end, ios_base& __str,
125 ios_base::iostate& __err, tm* __t) const
126 { return do_get_monthname(__s, __end, __str, __err, __t); }
127 iter_type get_year(iter_type __s, iter_type __end, ios_base& __str,
128 ios_base::iostate& __err, tm* __t) const
129 { return do_get_year(__s, __end, __str, __err, __t); }
131 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
132 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId();
133 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(istreambuf_iterator<wchar_t, char_traits<wchar_t> >*);
134 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(const wchar_t**);
135 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(istreambuf_iterator<char, char_traits<char> >*);
136 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(const char**);
138 _STLP_STATIC_MEMBER_DECLSPEC static locale::id id;
142 _Time_Info _M_timeinfo;
144 time_get(_Locale_time *, size_t __refs) : _BaseFacet(__refs) {}
148 virtual dateorder do_date_order() const {return no_order;}
150 virtual iter_type do_get_time(iter_type __s, iter_type __end,
151 ios_base&, ios_base::iostate& __err,
154 virtual iter_type do_get_date(iter_type __s, iter_type __end,
155 ios_base&, ios_base::iostate& __err,
158 virtual iter_type do_get_weekday(iter_type __s, iter_type __end,
160 ios_base::iostate& __err,
162 virtual iter_type do_get_monthname(iter_type __s, iter_type __end,
164 ios_base::iostate& __err,
167 virtual iter_type do_get_year(iter_type __s, iter_type __end,
168 ios_base&, ios_base::iostate& __err,
172 time_base::dateorder _STLP_CALL
173 _STLP_DECLSPEC __get_date_order(_Locale_time*);
174 _Locale_time* _STLP_CALL __acquire_time(const char* __name);
175 void _STLP_CALL __release_time(_Locale_time* __time);
177 template <class _Ch, __DFL_TMPL_PARAM( _InIt , istreambuf_iterator<_Ch>) >
178 class time_get_byname : public time_get<_Ch, _InIt>
181 typedef time_base::dateorder dateorder;
182 typedef _InIt iter_type;
184 explicit time_get_byname(const char* __name, size_t __refs = 0)
185 : time_get<_Ch, _InIt>((_Locale_time*) 0, __refs),
186 _M_time(__acquire_time(__name))
187 { _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
190 ~time_get_byname() { __release_time(_M_time); }
191 dateorder do_date_order() const { return __get_date_order(_M_time); }
193 _Locale_time* _M_time;
198 // For the formats 'x, 'X', and 'c', do_put calls the first form of
199 // put with the pattern obtained from _M_timeinfo._M_date_format or
200 // _M_timeinfo._M_time_format.
202 // Helper function: __ takes a single-character
203 // format. As indicated by the foregoing remark, this will never be
206 _STLP_DECLSPEC char * _STLP_CALL
207 __write_formatted_time(char * __buf, char __format, char __modifier,
208 const _Time_Info& __table, const tm* __t);
210 template <class _OuIt>
211 inline _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out,
212 const ios_base& /* __loc */, char) {
213 return copy(__first, __last, __out);
216 # ifndef _STLP_NO_WCHAR_T
217 template <class _OuIt>
218 _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out,
219 const ios_base& __s, wchar_t);
222 template<class _Ch, __DFL_TMPL_PARAM( _OutputIter , ostreambuf_iterator<_Ch> ) >
223 class time_put : public locale::facet, public time_base
225 friend class _Locale;
227 typedef _Ch char_type;
228 typedef _OutputIter iter_type;
230 explicit time_put(size_t __refs = 0) : _BaseFacet(__refs) {
231 _Init_timeinfo(_M_timeinfo);
234 _OutputIter put(iter_type __s, ios_base& __f, _Ch __fill,
236 const _Ch* __pat, const _Ch* __pat_end) const;
238 _OutputIter put(iter_type __s, ios_base& __f, _Ch __fill,
239 const tm* __tmb, char __format, char __modifier = 0) const {
240 return do_put(__s, __f, __fill, __tmb, __format, __modifier);
243 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
244 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId();
245 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(ostreambuf_iterator<wchar_t, char_traits<wchar_t> >*);
246 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(wchar_t**);
247 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(ostreambuf_iterator<char, char_traits<char> >*);
248 _STLP_STATIC_MEMBER_DECLSPEC static locale::id& GetFacetLocaleId(char**);
250 _STLP_STATIC_MEMBER_DECLSPEC static locale::id id;
254 _Time_Info _M_timeinfo;
256 time_put(_Locale_time* /*__time*/, size_t __refs) : _BaseFacet(__refs) {
257 // _Init_timeinfo(_M_timeinfo, __time);
261 virtual iter_type do_put(iter_type __s, ios_base& __f,
262 char_type /* __fill */, const tm* __tmb,
263 char __format, char /* __modifier */) const;
266 template <class _Ch, __DFL_TMPL_PARAM( _InIt , ostreambuf_iterator<_Ch> ) >
267 class time_put_byname : public time_put<_Ch, _InIt>
269 friend class _Locale;
271 typedef time_base::dateorder dateorder;
272 typedef _InIt iter_type;
273 typedef _Ch char_type;
275 explicit time_put_byname(const char * __name, size_t __refs = 0)
276 : time_put<_Ch, _InIt>((_Locale_time*) 0, __refs),
277 _M_time(__acquire_time(__name))
278 { _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
281 ~time_put_byname() { __release_time(_M_time); }
284 _Locale_time* _M_time;
287 # ifdef _STLP_USE_TEMPLATE_EXPORT
288 _STLP_EXPORT_TEMPLATE_CLASS time_get<char, istreambuf_iterator<char, char_traits<char> > >;
289 _STLP_EXPORT_TEMPLATE_CLASS time_put<char, ostreambuf_iterator<char, char_traits<char> > >;
290 // _STLP_EXPORT_TEMPLATE_CLASS time_get<char, const char*>;
291 // _STLP_EXPORT_TEMPLATE_CLASS time_put<char, char*>;
292 # ifndef _STLP_NO_WCHAR_T
293 _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
294 _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
295 // _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, const wchar_t*>;
296 // _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, wchar_t*>;
297 # endif /* INSTANTIATE_WIDE_STREAMS */
301 # if defined (__BORLANDC__) && defined (_RTLDLL)
302 inline void _Stl_loc_init_time_facets() {
304 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
305 time_get<char, istreambuf_iterator<char, char_traits<char> > >::GetFacetLocaleId()._M_index = 16;
306 time_get<char, const char*>::GetFacetLocaleId()._M_index = 17;
307 time_put<char, ostreambuf_iterator<char, char_traits<char> > >::GetFacetLocaleId()._M_index = 18;
308 time_put<char, char*>::GetFacetLocaleId()._M_index = 19;
310 time_get<char, istreambuf_iterator<char, char_traits<char> > >::GetFacetLocaleId()._M_index = 16;
311 time_get<char, const char*>::id._M_index = 17;
312 time_put<char, ostreambuf_iterator<char, char_traits<char> > >::GetFacetLocaleId()._M_index = 18;
313 time_put<char, char*>::id._M_index = 19;
316 # ifndef _STLP_NO_WCHAR_T
317 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
318 time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::GetFacetLocaleId()._M_index = 35;
319 time_get<wchar_t, const wchar_t*>::GetFacetLocaleId()._M_index = 36;
320 time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::GetFacetLocaleId()._M_index = 37;
321 time_put<wchar_t, wchar_t*>::GetFacetLocaleId()._M_index = 38;
323 time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 35;
324 time_get<wchar_t, const wchar_t*>::id._M_index = 36;
325 time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 37;
326 time_put<wchar_t, wchar_t*>::id._M_index = 38;
327 #endif //__SYMBIAN32__
328 # endif //!_STLP_NO_WCHAR_T
335 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
336 # include <stl/_time_facets.c>
339 #endif /* _STLP_INTERNAL_TIME_FACETS_H */