2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 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 #ifndef _STLP_TIME_FACETS_C
21 #define _STLP_TIME_FACETS_C
23 #ifndef _STLP_INTERNAL_TIME_FACETS_H
24 # include <stl/_time_facets.h>
27 #ifndef _STLP_INTERNAL_NUM_PUT_H
28 # include <stl/_num_put.h>
31 #ifndef _STLP_INTERNAL_NUM_GET_H
32 # include <stl/_num_get.h>
37 //----------------------------------------------------------------------
38 // Declarations of static template members.
39 #if (_STLP_STATIC_TEMPLATE_DATA > 0)
40 # if !defined(__SYMBIAN32__WSD__)
41 # if !defined (__BORLANDC__) && (defined (__SYMBIAN32__) && defined(_STLP_DESIGNATED_DLL))
42 template <class _CharT, class _InputIterator>
43 locale::id time_get<_CharT, _InputIterator>::id;
45 template <class _CharT, class _OutputIterator>
46 locale::id time_put<_CharT, _OutputIterator>::id;
49 # if ((defined (__CYGWIN__) || defined (__MINGW32__)) && \
50 defined (_STLP_USE_DYNAMIC_LIB) && !defined (__BUILDING_STLPORT)) || (defined (__SYMBIAN32__) && defined(_STLP_DESIGNATED_DLL))
52 * Under cygwin, when STLport is used as a shared library, the id needs
53 * to be specified as imported otherwise they will be duplicated in the
56 # if defined (__SYMBIAN32__)
58 locale::id time_get<char, istreambuf_iterator<char, char_traits<char> > >::id; //_STLP_DECLSPEC removed. data should not be exported in symbian
61 _STLP_DECLSPEC locale::id time_get<char, const char*>::id;
65 locale::id time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id;
68 _STLP_DECLSPEC locale::id time_put<char, char*>::id;
71 # ifndef _STLP_NO_WCHAR_T
73 locale::id time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
76 _STLP_DECLSPEC locale::id time_get<wchar_t, const wchar_t*>::id;
80 locale::id time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
83 _STLP_DECLSPEC locale::id time_put<wchar_t, wchar_t*>::id;
85 # endif /* _STLP_NO_WCHAR_T */
89 _STLP_DECLSPEC locale::id time_get<char, istreambuf_iterator<char, char_traits<char> > >::id;
92 _STLP_DECLSPEC locale::id time_get<char, const char*>::id;
96 _STLP_DECLSPEC locale::id time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id;
99 _STLP_DECLSPEC locale::id time_put<char, char*>::id;
102 # ifndef _STLP_NO_WCHAR_T
104 _STLP_DECLSPEC locale::id time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
107 _STLP_DECLSPEC locale::id time_get<wchar_t, const wchar_t*>::id;
111 _STLP_DECLSPEC locale::id time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
114 _STLP_DECLSPEC locale::id time_put<wchar_t, wchar_t*>::id;
116 # endif /* _STLP_NO_WCHAR_T */
118 # endif //__SYMBIAN32__
119 # endif /* _STLP_NO_WCHAR_T */
120 # endif /* __CUGWIN__ && _STLP_USE_DYNAMIC_LIB */
122 #else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
124 //typedef time_get<char, const char*> time_get_char;
125 typedef time_get<char, istreambuf_iterator<char, char_traits<char> > > time_get_char_2;
126 //typedef time_put<char, char*> time_put_char;
127 typedef time_put<char, ostreambuf_iterator<char, char_traits<char> > > time_put_char_2;
129 //__DECLARE_INSTANCE(locale::id, time_get_char::id, );
130 __DECLARE_INSTANCE(locale::id, time_get_char_2::id, );
131 //__DECLARE_INSTANCE(locale::id, time_put_char::id, );
132 __DECLARE_INSTANCE(locale::id, time_put_char_2::id, );
134 # if !defined (_STLP_NO_WCHAR_T)
136 //typedef time_get<wchar_t, const wchar_t*> time_get_wchar_t;
137 typedef time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_get_wchar_t_2;
138 //typedef time_put<wchar_t, wchar_t*> time_put_wchar_t;
139 typedef time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_put_wchar_t_2;
141 //__DECLARE_INSTANCE(locale::id, time_get_wchar_t::id, );
142 __DECLARE_INSTANCE(locale::id, time_get_wchar_t_2::id, );
143 //__DECLARE_INSTANCE(locale::id, time_put_wchar_t::id, );
144 __DECLARE_INSTANCE(locale::id, time_put_wchar_t_2::id, );
148 #endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
150 _STLP_MOVE_TO_PRIV_NAMESPACE
152 template <class _InIt, class _CharT>
153 const string* _STLP_CALL
154 __match(_InIt& __first, _InIt& __last, const string *__name, const string *__name_end,
155 const ctype<_CharT>& __ct) {
156 typedef ptrdiff_t difference_type;
157 difference_type __n = __name_end - __name;
160 difference_type __check_count = __n;
161 bool __do_check[_MAXNAMES];
162 const string* __matching_name[_MAX_NAME_LENGTH];
164 for (__i = 0; __i < _MAXNAMES; ++__i)
165 __do_check[__i] = true;
167 for (__i = 0; __i < _MAX_NAME_LENGTH; ++__i)
168 __matching_name[__i] = __name_end;
170 while (__first != __last) {
171 for (__i = 0; __i < __n; ++__i) {
172 if (__do_check[__i]) {
173 if (*__first == __ct.widen(__name[__i][__pos])) {
174 if (__pos == (__name[__i].size() - 1)) {
176 __matching_name[__pos + 1] = __name + __i;
178 if (__check_count == 0) {
187 if (__check_count == 0)
188 return __matching_name[__pos];
196 return __matching_name[__pos];
199 // __get_formatted_time reads input that is assumed to be formatted
200 // according to the rules for the C strftime function (C standard,
201 // 7.12.3.5). This function is used to implement the do_get_time
202 // and do_get_date virtual functions, which depend on the locale
203 // specifications for the time and day formats respectively.
204 // Note the catchall default case, intended mainly for the '%Z'
205 // format designator, which does not make sense here since the
206 // representation of timezones is not part of the locale.
208 // The case branches are implemented either by doing a match using
209 // the appopriate name table or by doing a __get_integer_nogroup.
211 // 'y' format is assumed to mean that the input represents years
212 // since 1900. That is, 2002 should be represented as 102. There
213 // is no century-guessing.
215 // The match is successful if and only if the second component of the
216 // return value is format_end.
218 // Note that the antepenultimate parameter is being used only to determine
219 // the correct overloading for the calls to __get_integer_nogroup.
220 template <class _InIt1, class _Ch>
221 string::const_iterator _STLP_CALL
222 __get_formatted_time _STLP_WEAK (_InIt1 __first, _InIt1 __last,
223 string::const_iterator __format, string::const_iterator __format_end,
224 _Ch*, const _Time_Info& __table,
225 const ios_base& __s, ios_base::iostate& __err, tm* __t) {
226 const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __s._M_ctype_facet());
227 while (__first != __last && __format != __format_end) {
228 if (*__format == '%') {
230 char __c = *__format;
231 if (__c == '#') { //MS extension
238 const string* __pr = __match(__first, __last,
239 __table._M_dayname + 0, __table._M_dayname + 7,
241 if (__pr == __table._M_dayname + 7)
243 __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname);
248 const string* __pr = __match(__first, __last,
249 __table._M_dayname + 7, __table._M_dayname + 14,
251 if (__pr == __table._M_dayname + 14)
253 __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname - 7);
258 const string* __pr = __match(__first, __last,
259 __table._M_monthname + 0, __table._M_monthname + 12,
261 if (__pr == __table._M_monthname + 12)
263 __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname);
268 const string* __pr = __match(__first, __last,
269 __table._M_monthname + 12, __table._M_monthname + 24,
271 if (__pr == __table._M_monthname + 24)
273 __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname - 12);
278 bool __pr = __get_decimal_integer(__first, __last, __t->tm_mday, __STATIC_CAST(_Ch*, 0));
279 if (!__pr || __t->tm_mday < 1 || __t->tm_mday > 31) {
280 __err |= ios_base::failbit;
286 case 'H': case 'I': {
287 bool __pr = __get_decimal_integer(__first, __last, __t->tm_hour, __STATIC_CAST(_Ch*, 0));
294 bool __pr = __get_decimal_integer(__first, __last, __t->tm_yday, __STATIC_CAST(_Ch*, 0));
301 bool __pr = __get_decimal_integer(__first, __last, __t->tm_mon, __STATIC_CAST(_Ch*, 0));
303 if (!__pr || __t->tm_mon < 0 || __t->tm_mon > 11) {
304 __err |= ios_base::failbit;
311 bool __pr = __get_decimal_integer(__first, __last, __t->tm_min, __STATIC_CAST(_Ch*, 0));
318 const string* __pr = __match(__first, __last,
319 __table._M_am_pm + 0, __table._M_am_pm + 2, __ct);
320 if (__pr == __table._M_am_pm + 2)
322 // 12:00 PM <=> 12:00, 12:00 AM <=> 00:00
323 if (__pr == __table._M_am_pm + 1 && __t->tm_hour != 12 )
325 if (__pr == __table._M_am_pm && __t->tm_hour == 12 )
331 bool __pr = __get_decimal_integer(__first, __last, __t->tm_sec, __STATIC_CAST(_Ch*, 0));
338 bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0));
345 bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0));
346 __t->tm_year -= 1900;
357 if (*__first++ != __ct.widen(*__format)) break;
366 template <class _InIt, class _CharT>
368 __get_short_or_long_dayname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct,
369 const _Time_Info& __table, tm* __t) {
371 __match(__first, __last, __table._M_dayname + 0, __table._M_dayname + 14, __ct);
372 __t->tm_wday = __STATIC_CAST(int, (__pr - __table._M_dayname) % 7);
373 return __pr != __table._M_dayname + 14;
376 template <class _InIt, class _CharT>
378 __get_short_or_long_monthname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct,
379 const _Time_Info& __table, tm* __t) {
381 __match(__first, __last, __table._M_monthname + 0, __table._M_monthname + 24, __ct);
382 __t->tm_mon = __STATIC_CAST(int, (__pr - __table._M_monthname) % 12);
383 return __pr != __table._M_monthname + 24;
386 #if !defined (_STLP_NO_WCHAR_T)
387 template <class _OuIt>
389 __put_time(char * __first, char * __last, _OuIt __out_ite,
390 const ios_base& __s, wchar_t) {
391 const ctype<wchar_t>& __ct = *__STATIC_CAST(const ctype<wchar_t>*, __s._M_ctype_facet());
393 __ct.widen(__first, __last, __wbuf);
394 ptrdiff_t __len = __last - __first;
395 wchar_t * __eend = __wbuf + __len;
396 return copy((wchar_t*)__wbuf, __eend, __out_ite);
400 _STLP_MOVE_TO_STD_NAMESPACE
402 template <class _Ch, class _InIt>
404 time_get<_Ch, _InIt>::do_get_date(_InIt __s, _InIt __end,
405 ios_base& __str, ios_base::iostate& __err,
407 typedef string::const_iterator string_iterator;
409 string_iterator __format = _M_timeinfo._M_date_format.begin();
410 string_iterator __format_end = _M_timeinfo._M_date_format.end();
412 string_iterator __result
413 = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end,
414 __STATIC_CAST(_Ch*, 0), _M_timeinfo,
416 if (__result == __format_end)
417 __err = ios_base::goodbit;
419 __err = ios_base::failbit;
421 __err |= ios_base::eofbit;
426 template <class _Ch, class _InIt>
428 time_get<_Ch, _InIt>::do_get_time(_InIt __s, _InIt __end,
429 ios_base& __str, ios_base::iostate& __err,
431 typedef string::const_iterator string_iterator;
432 string_iterator __format = _M_timeinfo._M_time_format.begin();
433 string_iterator __format_end = _M_timeinfo._M_time_format.end();
435 string_iterator __result
436 = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end,
437 __STATIC_CAST(_Ch*, 0), _M_timeinfo,
439 __err = __result == __format_end ? ios_base::goodbit
442 __err |= ios_base::eofbit;
446 template <class _Ch, class _InIt>
448 time_get<_Ch, _InIt>::do_get_year(_InIt __s, _InIt __end,
449 ios_base&, ios_base::iostate& __err,
452 __err = ios_base::failbit | ios_base::eofbit;
456 bool __pr = _STLP_PRIV __get_decimal_integer(__s, __end, __t->tm_year, __STATIC_CAST(_Ch*, 0));
457 __t->tm_year -= 1900;
458 __err = __pr ? ios_base::goodbit : ios_base::failbit;
460 __err |= ios_base::eofbit;
465 template <class _Ch, class _InIt>
467 time_get<_Ch, _InIt>::do_get_weekday(_InIt __s, _InIt __end,
468 ios_base &__str, ios_base::iostate &__err,
470 const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet());
472 _STLP_PRIV __get_short_or_long_dayname(__s, __end, __ct, _M_timeinfo, __t);
474 __err = ios_base::goodbit;
476 __err = ios_base::failbit;
478 __err |= ios_base::eofbit;
483 template <class _Ch, class _InIt>
485 time_get<_Ch, _InIt>::do_get_monthname(_InIt __s, _InIt __end,
486 ios_base &__str, ios_base::iostate &__err,
488 const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet());
490 _STLP_PRIV __get_short_or_long_monthname(__s, __end, __ct, _M_timeinfo, __t);
492 __err = ios_base::goodbit;
494 __err = ios_base::failbit;
496 __err |= ios_base::eofbit;
501 template<class _Ch, class _OutputIter>
503 time_put<_Ch,_OutputIter>::put(_OutputIter __s, ios_base& __f, _Ch __fill,
504 const tm* __tmb, const _Ch* __pat,
505 const _Ch* __pat_end) const {
506 // locale __loc = __f.getloc();
507 // const ctype<_Ch>& _Ct = use_facet<ctype<_Ch> >(__loc);
508 const ctype<_Ch>& _Ct = *__STATIC_CAST(const ctype<_Ch>*, __f._M_ctype_facet());
509 while (__pat != __pat_end) {
510 char __c = _Ct.narrow(*__pat, 0);
514 __c = _Ct.narrow(*__pat++, 0);
515 if (__c == '#') { // MS extension
517 __c = _Ct.narrow(*__pat++, 0);
519 __s = do_put(__s, __f, __fill, __tmb, __c, __mod);
527 template<class _Ch, class _OutputIter>
529 time_put<_Ch,_OutputIter>::do_put(_OutputIter __s, ios_base& __f, _Ch /* __fill */,
530 const tm* __tmb, char __format,
531 char __modifier ) const {
533 char * __iend = _STLP_PRIV __write_formatted_time(_STLP_ARRAY_AND_SIZE(__buf),
534 __format, __modifier, _M_timeinfo, __tmb);
535 // locale __loc = __f.getloc();
536 return _STLP_PRIV __put_time(__buf, __iend, __s, __f, _Ch());
541 #endif /* _STLP_TIME_FACETS_C */