3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
18 #ifndef _STLP_TIME_FACETS_C
19 #define _STLP_TIME_FACETS_C
21 #ifndef _STLP_INTERNAL_TIME_FACETS_H
22 # include <stl/_time_facets.h>
25 #ifndef _STLP_INTERNAL_NUM_PUT_H
26 # include <stl/_num_put.h>
29 #ifndef _STLP_INTERNAL_NUM_GET_H
30 # include <stl/_num_get.h>
35 //----------------------------------------------------------------------
36 // Declarations of static template members.
37 #if (_STLP_STATIC_TEMPLATE_DATA > 0)
39 # if !defined (__BORLANDC__)
40 template <class _CharT, class _InputIterator>
41 locale::id time_get<_CharT, _InputIterator>::id;
43 template <class _CharT, class _OutputIterator>
44 locale::id time_put<_CharT, _OutputIterator>::id;
47 # if (defined (__CYGWIN__) || defined (__MINGW32__)) && \
48 defined (_STLP_USE_DYNAMIC_LIB) && !defined (__BUILDING_STLPORT)
50 * Under cygwin, when STLport is used as a shared library, the id needs
51 * to be specified as imported otherwise they will be duplicated in the
55 _STLP_DECLSPEC locale::id time_get<char, istreambuf_iterator<char, char_traits<char> > >::id;
58 _STLP_DECLSPEC locale::id time_get<char, const char*>::id;
62 _STLP_DECLSPEC locale::id time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id;
65 _STLP_DECLSPEC locale::id time_put<char, char*>::id;
68 # ifndef _STLP_NO_WCHAR_T
70 _STLP_DECLSPEC locale::id time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
73 _STLP_DECLSPEC locale::id time_get<wchar_t, const wchar_t*>::id;
77 _STLP_DECLSPEC locale::id time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
80 _STLP_DECLSPEC locale::id time_put<wchar_t, wchar_t*>::id;
82 # endif /* _STLP_NO_WCHAR_T */
83 # endif /* __CUGWIN__ && _STLP_USE_DYNAMIC_LIB */
85 #else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
87 //typedef time_get<char, const char*> time_get_char;
88 typedef time_get<char, istreambuf_iterator<char, char_traits<char> > > time_get_char_2;
89 //typedef time_put<char, char*> time_put_char;
90 typedef time_put<char, ostreambuf_iterator<char, char_traits<char> > > time_put_char_2;
92 //__DECLARE_INSTANCE(locale::id, time_get_char::id, );
93 __DECLARE_INSTANCE(locale::id, time_get_char_2::id, );
94 //__DECLARE_INSTANCE(locale::id, time_put_char::id, );
95 __DECLARE_INSTANCE(locale::id, time_put_char_2::id, );
97 # if !defined (_STLP_NO_WCHAR_T)
99 //typedef time_get<wchar_t, const wchar_t*> time_get_wchar_t;
100 typedef time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_get_wchar_t_2;
101 //typedef time_put<wchar_t, wchar_t*> time_put_wchar_t;
102 typedef time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > time_put_wchar_t_2;
104 //__DECLARE_INSTANCE(locale::id, time_get_wchar_t::id, );
105 __DECLARE_INSTANCE(locale::id, time_get_wchar_t_2::id, );
106 //__DECLARE_INSTANCE(locale::id, time_put_wchar_t::id, );
107 __DECLARE_INSTANCE(locale::id, time_put_wchar_t_2::id, );
111 #endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
113 _STLP_MOVE_TO_PRIV_NAMESPACE
115 template <class _InIt, class _CharT>
116 const string* _STLP_CALL
117 __match(_InIt& __first, _InIt& __last, const string *__name, const string *__name_end,
118 const ctype<_CharT>& __ct) {
119 typedef ptrdiff_t difference_type;
120 difference_type __n = __name_end - __name;
123 difference_type __check_count = __n;
124 bool __do_check[_MAXNAMES];
125 const string* __matching_name[_MAX_NAME_LENGTH];
127 for (__i = 0; __i < _MAXNAMES; ++__i)
128 __do_check[__i] = true;
130 for (__i = 0; __i < _MAX_NAME_LENGTH; ++__i)
131 __matching_name[__i] = __name_end;
133 while (__first != __last) {
134 for (__i = 0; __i < __n; ++__i) {
135 if (__do_check[__i]) {
136 if (*__first == __ct.widen(__name[__i][__pos])) {
137 if (__pos == (__name[__i].size() - 1)) {
139 __matching_name[__pos + 1] = __name + __i;
141 if (__check_count == 0) {
150 if (__check_count == 0)
151 return __matching_name[__pos];
159 return __matching_name[__pos];
162 // __get_formatted_time reads input that is assumed to be formatted
163 // according to the rules for the C strftime function (C standard,
164 // 7.12.3.5). This function is used to implement the do_get_time
165 // and do_get_date virtual functions, which depend on the locale
166 // specifications for the time and day formats respectively.
167 // Note the catchall default case, intended mainly for the '%Z'
168 // format designator, which does not make sense here since the
169 // representation of timezones is not part of the locale.
171 // The case branches are implemented either by doing a match using
172 // the appopriate name table or by doing a __get_integer_nogroup.
174 // 'y' format is assumed to mean that the input represents years
175 // since 1900. That is, 2002 should be represented as 102. There
176 // is no century-guessing.
178 // The match is successful if and only if the second component of the
179 // return value is format_end.
181 // Note that the antepenultimate parameter is being used only to determine
182 // the correct overloading for the calls to __get_integer_nogroup.
183 template <class _InIt1, class _Ch>
184 string::const_iterator _STLP_CALL
185 __get_formatted_time _STLP_WEAK (_InIt1 __first, _InIt1 __last,
186 string::const_iterator __format, string::const_iterator __format_end,
187 _Ch*, const _Time_Info& __table,
188 const ios_base& __s, ios_base::iostate& __err, tm* __t) {
189 const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __s._M_ctype_facet());
190 while (__first != __last && __format != __format_end) {
191 if (*__format == '%') {
193 char __c = *__format;
194 if (__c == '#') { //MS extension
201 const string* __pr = __match(__first, __last,
202 __table._M_dayname + 0, __table._M_dayname + 7,
204 if (__pr == __table._M_dayname + 7)
206 __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname);
211 const string* __pr = __match(__first, __last,
212 __table._M_dayname + 7, __table._M_dayname + 14,
214 if (__pr == __table._M_dayname + 14)
216 __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname - 7);
221 const string* __pr = __match(__first, __last,
222 __table._M_monthname + 0, __table._M_monthname + 12,
224 if (__pr == __table._M_monthname + 12)
226 __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname);
231 const string* __pr = __match(__first, __last,
232 __table._M_monthname + 12, __table._M_monthname + 24,
234 if (__pr == __table._M_monthname + 24)
236 __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname - 12);
241 bool __pr = __get_decimal_integer(__first, __last, __t->tm_mday, __STATIC_CAST(_Ch*, 0));
242 if (!__pr || __t->tm_mday < 1 || __t->tm_mday > 31) {
243 __err |= ios_base::failbit;
249 case 'H': case 'I': {
250 bool __pr = __get_decimal_integer(__first, __last, __t->tm_hour, __STATIC_CAST(_Ch*, 0));
257 bool __pr = __get_decimal_integer(__first, __last, __t->tm_yday, __STATIC_CAST(_Ch*, 0));
264 bool __pr = __get_decimal_integer(__first, __last, __t->tm_mon, __STATIC_CAST(_Ch*, 0));
266 if (!__pr || __t->tm_mon < 0 || __t->tm_mon > 11) {
267 __err |= ios_base::failbit;
274 bool __pr = __get_decimal_integer(__first, __last, __t->tm_min, __STATIC_CAST(_Ch*, 0));
281 const string* __pr = __match(__first, __last,
282 __table._M_am_pm + 0, __table._M_am_pm + 2, __ct);
283 if (__pr == __table._M_am_pm + 2)
285 // 12:00 PM <=> 12:00, 12:00 AM <=> 00:00
286 if (__pr == __table._M_am_pm + 1 && __t->tm_hour != 12 )
288 if (__pr == __table._M_am_pm && __t->tm_hour == 12 )
294 bool __pr = __get_decimal_integer(__first, __last, __t->tm_sec, __STATIC_CAST(_Ch*, 0));
301 bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0));
308 bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0));
309 __t->tm_year -= 1900;
320 if (*__first++ != __ct.widen(*__format)) break;
329 template <class _InIt, class _CharT>
331 __get_short_or_long_dayname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct,
332 const _Time_Info& __table, tm* __t) {
334 __match(__first, __last, __table._M_dayname + 0, __table._M_dayname + 14, __ct);
335 __t->tm_wday = __STATIC_CAST(int, (__pr - __table._M_dayname) % 7);
336 return __pr != __table._M_dayname + 14;
339 template <class _InIt, class _CharT>
341 __get_short_or_long_monthname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct,
342 const _Time_Info& __table, tm* __t) {
344 __match(__first, __last, __table._M_monthname + 0, __table._M_monthname + 24, __ct);
345 __t->tm_mon = __STATIC_CAST(int, (__pr - __table._M_monthname) % 12);
346 return __pr != __table._M_monthname + 24;
349 #if !defined (_STLP_NO_WCHAR_T)
350 template <class _OuIt>
352 __put_time(char * __first, char * __last, _OuIt __out_ite,
353 const ios_base& __s, wchar_t) {
354 const ctype<wchar_t>& __ct = *__STATIC_CAST(const ctype<wchar_t>*, __s._M_ctype_facet());
356 __ct.widen(__first, __last, __wbuf);
357 ptrdiff_t __len = __last - __first;
358 wchar_t * __eend = __wbuf + __len;
359 return copy((wchar_t*)__wbuf, __eend, __out_ite);
363 _STLP_MOVE_TO_STD_NAMESPACE
365 template <class _Ch, class _InIt>
367 time_get<_Ch, _InIt>::do_get_date(_InIt __s, _InIt __end,
368 ios_base& __str, ios_base::iostate& __err,
370 typedef string::const_iterator string_iterator;
372 string_iterator __format = _M_timeinfo._M_date_format.begin();
373 string_iterator __format_end = _M_timeinfo._M_date_format.end();
375 string_iterator __result
376 = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end,
377 __STATIC_CAST(_Ch*, 0), _M_timeinfo,
379 if (__result == __format_end)
380 __err = ios_base::goodbit;
382 __err = ios_base::failbit;
384 __err |= ios_base::eofbit;
389 template <class _Ch, class _InIt>
391 time_get<_Ch, _InIt>::do_get_time(_InIt __s, _InIt __end,
392 ios_base& __str, ios_base::iostate& __err,
394 typedef string::const_iterator string_iterator;
395 string_iterator __format = _M_timeinfo._M_time_format.begin();
396 string_iterator __format_end = _M_timeinfo._M_time_format.end();
398 string_iterator __result
399 = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end,
400 __STATIC_CAST(_Ch*, 0), _M_timeinfo,
402 __err = __result == __format_end ? ios_base::goodbit
405 __err |= ios_base::eofbit;
409 template <class _Ch, class _InIt>
411 time_get<_Ch, _InIt>::do_get_year(_InIt __s, _InIt __end,
412 ios_base&, ios_base::iostate& __err,
415 __err = ios_base::failbit | ios_base::eofbit;
419 bool __pr = _STLP_PRIV __get_decimal_integer(__s, __end, __t->tm_year, __STATIC_CAST(_Ch*, 0));
420 __t->tm_year -= 1900;
421 __err = __pr ? ios_base::goodbit : ios_base::failbit;
423 __err |= ios_base::eofbit;
428 template <class _Ch, class _InIt>
430 time_get<_Ch, _InIt>::do_get_weekday(_InIt __s, _InIt __end,
431 ios_base &__str, ios_base::iostate &__err,
433 const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet());
435 _STLP_PRIV __get_short_or_long_dayname(__s, __end, __ct, _M_timeinfo, __t);
437 __err = ios_base::goodbit;
439 __err = ios_base::failbit;
441 __err |= ios_base::eofbit;
446 template <class _Ch, class _InIt>
448 time_get<_Ch, _InIt>::do_get_monthname(_InIt __s, _InIt __end,
449 ios_base &__str, ios_base::iostate &__err,
451 const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet());
453 _STLP_PRIV __get_short_or_long_monthname(__s, __end, __ct, _M_timeinfo, __t);
455 __err = ios_base::goodbit;
457 __err = ios_base::failbit;
459 __err |= ios_base::eofbit;
464 template<class _Ch, class _OutputIter>
466 time_put<_Ch,_OutputIter>::put(_OutputIter __s, ios_base& __f, _Ch __fill,
467 const tm* __tmb, const _Ch* __pat,
468 const _Ch* __pat_end) const {
469 // locale __loc = __f.getloc();
470 // const ctype<_Ch>& _Ct = use_facet<ctype<_Ch> >(__loc);
471 const ctype<_Ch>& _Ct = *__STATIC_CAST(const ctype<_Ch>*, __f._M_ctype_facet());
472 while (__pat != __pat_end) {
473 char __c = _Ct.narrow(*__pat, 0);
477 __c = _Ct.narrow(*__pat++, 0);
478 if (__c == '#') { // MS extension
480 __c = _Ct.narrow(*__pat++, 0);
482 __s = do_put(__s, __f, __fill, __tmb, __c, __mod);
490 template<class _Ch, class _OutputIter>
492 time_put<_Ch,_OutputIter>::do_put(_OutputIter __s, ios_base& __f, _Ch /* __fill */,
493 const tm* __tmb, char __format,
494 char __modifier ) const {
496 char * __iend = _STLP_PRIV __write_formatted_time(_STLP_ARRAY_AND_SIZE(__buf),
497 __format, __modifier, _M_timeinfo, __tmb);
498 // locale __loc = __f.getloc();
499 return _STLP_PRIV __put_time(__buf, __iend, __s, __f, _Ch());
504 #endif /* _STLP_TIME_FACETS_C */