williamr@2: /* williamr@4: * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. williamr@2: * williamr@2: * Copyright (c) 1999 williamr@2: * Silicon Graphics Computer Systems, Inc. williamr@2: * williamr@4: * Copyright (c) 1999 williamr@2: * Boris Fomitchev williamr@2: * williamr@2: * This material is provided "as is", with absolutely no warranty expressed williamr@2: * or implied. Any use is at your own risk. williamr@2: * williamr@4: * Permission to use or copy this software for any purpose is hereby granted williamr@2: * without fee, provided the above notices are retained on all copies. williamr@2: * Permission to modify the code and to distribute modified code is granted, williamr@2: * provided the above notices are retained, and a notice that the code was williamr@2: * modified is included with the above copyright notice. williamr@2: * williamr@4: */ williamr@2: #ifndef _STLP_TIME_FACETS_C williamr@2: #define _STLP_TIME_FACETS_C williamr@2: williamr@2: #ifndef _STLP_INTERNAL_TIME_FACETS_H williamr@4: # include williamr@2: #endif williamr@2: williamr@2: #ifndef _STLP_INTERNAL_NUM_PUT_H williamr@4: # include williamr@2: #endif williamr@2: williamr@2: #ifndef _STLP_INTERNAL_NUM_GET_H williamr@4: # include williamr@2: #endif williamr@2: williamr@2: _STLP_BEGIN_NAMESPACE williamr@2: williamr@2: //---------------------------------------------------------------------- williamr@2: // Declarations of static template members. williamr@4: #if (_STLP_STATIC_TEMPLATE_DATA > 0) williamr@4: # if !defined(__SYMBIAN32__WSD__) williamr@4: # if !defined (__BORLANDC__) && (defined (__SYMBIAN32__) && defined(_STLP_DESIGNATED_DLL)) williamr@2: template williamr@2: locale::id time_get<_CharT, _InputIterator>::id; williamr@2: williamr@2: template williamr@2: locale::id time_put<_CharT, _OutputIterator>::id; williamr@4: # endif williamr@2: williamr@4: # if ((defined (__CYGWIN__) || defined (__MINGW32__)) && \ williamr@4: defined (_STLP_USE_DYNAMIC_LIB) && !defined (__BUILDING_STLPORT)) || (defined (__SYMBIAN32__) && defined(_STLP_DESIGNATED_DLL)) williamr@4: /* williamr@4: * Under cygwin, when STLport is used as a shared library, the id needs williamr@4: * to be specified as imported otherwise they will be duplicated in the williamr@4: * calling executable. williamr@4: */ williamr@4: # if defined (__SYMBIAN32__) williamr@4: template <> williamr@4: locale::id time_get > >::id; //_STLP_DECLSPEC removed. data should not be exported in symbian williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_get::id; williamr@4: */ williamr@2: williamr@4: template <> williamr@4: locale::id time_put > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_put::id; williamr@4: */ williamr@2: williamr@4: # ifndef _STLP_NO_WCHAR_T williamr@4: template <> williamr@4: locale::id time_get > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_get::id; williamr@4: */ williamr@4: williamr@4: template <> williamr@4: locale::id time_put > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_put::id; williamr@4: */ williamr@4: # endif /* _STLP_NO_WCHAR_T */ williamr@4: williamr@4: # else williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_get > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_get::id; williamr@4: */ williamr@4: williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_put > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_put::id; williamr@4: */ williamr@4: williamr@4: # ifndef _STLP_NO_WCHAR_T williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_get > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_get::id; williamr@4: */ williamr@4: williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_put > >::id; williamr@4: /* williamr@4: template <> williamr@4: _STLP_DECLSPEC locale::id time_put::id; williamr@4: */ williamr@4: # endif /* _STLP_NO_WCHAR_T */ williamr@4: williamr@4: # endif //__SYMBIAN32__ williamr@4: # endif /* _STLP_NO_WCHAR_T */ williamr@4: # endif /* __CUGWIN__ && _STLP_USE_DYNAMIC_LIB */ williamr@4: williamr@4: #else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */ williamr@4: williamr@4: //typedef time_get time_get_char; williamr@4: typedef time_get > > time_get_char_2; williamr@4: //typedef time_put time_put_char; williamr@4: typedef time_put > > time_put_char_2; williamr@4: williamr@4: //__DECLARE_INSTANCE(locale::id, time_get_char::id, ); williamr@2: __DECLARE_INSTANCE(locale::id, time_get_char_2::id, ); williamr@4: //__DECLARE_INSTANCE(locale::id, time_put_char::id, ); williamr@2: __DECLARE_INSTANCE(locale::id, time_put_char_2::id, ); williamr@2: williamr@4: # if !defined (_STLP_NO_WCHAR_T) williamr@2: williamr@4: //typedef time_get time_get_wchar_t; williamr@4: typedef time_get > > time_get_wchar_t_2; williamr@4: //typedef time_put time_put_wchar_t; williamr@4: typedef time_put > > time_put_wchar_t_2; williamr@2: williamr@4: //__DECLARE_INSTANCE(locale::id, time_get_wchar_t::id, ); williamr@2: __DECLARE_INSTANCE(locale::id, time_get_wchar_t_2::id, ); williamr@4: //__DECLARE_INSTANCE(locale::id, time_put_wchar_t::id, ); williamr@2: __DECLARE_INSTANCE(locale::id, time_put_wchar_t_2::id, ); williamr@2: williamr@4: # endif williamr@2: williamr@4: #endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */ williamr@2: williamr@4: _STLP_MOVE_TO_PRIV_NAMESPACE williamr@4: williamr@4: template williamr@4: const string* _STLP_CALL williamr@4: __match(_InIt& __first, _InIt& __last, const string *__name, const string *__name_end, williamr@4: const ctype<_CharT>& __ct) { williamr@4: typedef ptrdiff_t difference_type; williamr@2: difference_type __n = __name_end - __name; williamr@2: difference_type __i; williamr@4: size_t __pos = 0; williamr@2: difference_type __check_count = __n; williamr@2: bool __do_check[_MAXNAMES]; williamr@4: const string* __matching_name[_MAX_NAME_LENGTH]; williamr@2: williamr@2: for (__i = 0; __i < _MAXNAMES; ++__i) williamr@2: __do_check[__i] = true; williamr@2: williamr@2: for (__i = 0; __i < _MAX_NAME_LENGTH; ++__i) williamr@2: __matching_name[__i] = __name_end; williamr@2: williamr@2: while (__first != __last) { williamr@4: for (__i = 0; __i < __n; ++__i) { williamr@4: if (__do_check[__i]) { williamr@4: if (*__first == __ct.widen(__name[__i][__pos])) { williamr@4: if (__pos == (__name[__i].size() - 1)) { williamr@2: __do_check[__i] = 0; williamr@4: __matching_name[__pos + 1] = __name + __i; williamr@2: --__check_count; williamr@2: if (__check_count == 0) { williamr@4: ++__first; williamr@4: return __name + __i; williamr@4: } williamr@2: } williamr@2: } williamr@2: else { williamr@2: __do_check[__i] = 0; williamr@2: --__check_count; williamr@4: if (__check_count == 0) williamr@2: return __matching_name[__pos]; williamr@2: } williamr@4: } williamr@4: } williamr@2: williamr@2: ++__first; ++__pos; williamr@2: } williamr@2: williamr@2: return __matching_name[__pos]; williamr@2: } williamr@2: williamr@2: // __get_formatted_time reads input that is assumed to be formatted williamr@2: // according to the rules for the C strftime function (C standard, williamr@2: // 7.12.3.5). This function is used to implement the do_get_time williamr@2: // and do_get_date virtual functions, which depend on the locale williamr@2: // specifications for the time and day formats respectively. williamr@2: // Note the catchall default case, intended mainly for the '%Z' williamr@2: // format designator, which does not make sense here since the williamr@2: // representation of timezones is not part of the locale. williamr@2: // williamr@2: // The case branches are implemented either by doing a match using williamr@2: // the appopriate name table or by doing a __get_integer_nogroup. williamr@2: // williamr@2: // 'y' format is assumed to mean that the input represents years williamr@2: // since 1900. That is, 2002 should be represented as 102. There williamr@2: // is no century-guessing. williamr@2: // williamr@2: // The match is successful if and only if the second component of the williamr@2: // return value is format_end. williamr@2: williamr@2: // Note that the antepenultimate parameter is being used only to determine williamr@2: // the correct overloading for the calls to __get_integer_nogroup. williamr@4: template williamr@4: string::const_iterator _STLP_CALL williamr@2: __get_formatted_time _STLP_WEAK (_InIt1 __first, _InIt1 __last, williamr@4: string::const_iterator __format, string::const_iterator __format_end, williamr@4: _Ch*, const _Time_Info& __table, williamr@4: const ios_base& __s, ios_base::iostate& __err, tm* __t) { williamr@4: const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __s._M_ctype_facet()); williamr@4: while (__first != __last && __format != __format_end) { williamr@2: if (*__format == '%') { williamr@2: ++__format; williamr@2: char __c = *__format; williamr@4: if (__c == '#') { //MS extension williamr@4: ++__format; williamr@4: __c = *__format; williamr@4: } williamr@4: williamr@2: switch (__c) { williamr@2: case 'a': { williamr@4: const string* __pr = __match(__first, __last, williamr@4: __table._M_dayname + 0, __table._M_dayname + 7, williamr@4: __ct); williamr@4: if (__pr == __table._M_dayname + 7) williamr@4: return __format; williamr@4: __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname); williamr@4: break; williamr@2: } williamr@2: williamr@2: case 'A': { williamr@4: const string* __pr = __match(__first, __last, williamr@4: __table._M_dayname + 7, __table._M_dayname + 14, williamr@4: __ct); williamr@4: if (__pr == __table._M_dayname + 14) williamr@4: return __format; williamr@4: __t->tm_wday = __STATIC_CAST(int, __pr - __table._M_dayname - 7); williamr@4: break; williamr@2: } williamr@2: williamr@2: case 'b': { williamr@4: const string* __pr = __match(__first, __last, williamr@4: __table._M_monthname + 0, __table._M_monthname + 12, williamr@4: __ct); williamr@4: if (__pr == __table._M_monthname + 12) williamr@4: return __format; williamr@4: __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname); williamr@4: break; williamr@2: } williamr@2: williamr@2: case 'B': { williamr@4: const string* __pr = __match(__first, __last, williamr@4: __table._M_monthname + 12, __table._M_monthname + 24, williamr@4: __ct); williamr@4: if (__pr == __table._M_monthname + 24) williamr@4: return __format; williamr@4: __t->tm_mon = __STATIC_CAST(int, __pr - __table._M_monthname - 12); williamr@4: break; williamr@2: } williamr@2: williamr@2: case 'd': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_mday, __STATIC_CAST(_Ch*, 0)); williamr@2: if (!__pr || __t->tm_mday < 1 || __t->tm_mday > 31) { williamr@4: __err |= ios_base::failbit; williamr@2: return __format; williamr@4: } williamr@2: break; williamr@2: } williamr@4: williamr@2: case 'H': case 'I': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_hour, __STATIC_CAST(_Ch*, 0)); williamr@4: if (!__pr) williamr@4: return __format; williamr@4: break; williamr@2: } williamr@2: williamr@2: case 'j': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_yday, __STATIC_CAST(_Ch*, 0)); williamr@2: if (!__pr) williamr@2: return __format; williamr@2: break; williamr@2: } williamr@2: williamr@2: case 'm': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_mon, __STATIC_CAST(_Ch*, 0)); williamr@4: --__t->tm_mon; williamr@2: if (!__pr || __t->tm_mon < 0 || __t->tm_mon > 11) { williamr@4: __err |= ios_base::failbit; williamr@2: return __format; williamr@4: } williamr@2: break; williamr@2: } williamr@2: williamr@2: case 'M': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_min, __STATIC_CAST(_Ch*, 0)); williamr@2: if (!__pr) williamr@2: return __format; williamr@2: break; williamr@2: } williamr@2: williamr@2: case 'p': { williamr@4: const string* __pr = __match(__first, __last, williamr@4: __table._M_am_pm + 0, __table._M_am_pm + 2, __ct); williamr@4: if (__pr == __table._M_am_pm + 2) williamr@2: return __format; williamr@4: // 12:00 PM <=> 12:00, 12:00 AM <=> 00:00 williamr@4: if (__pr == __table._M_am_pm + 1 && __t->tm_hour != 12 ) williamr@2: __t->tm_hour += 12; williamr@4: if (__pr == __table._M_am_pm && __t->tm_hour == 12 ) williamr@4: __t->tm_hour = 0; williamr@2: break; williamr@2: } williamr@2: williamr@2: case 'S': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_sec, __STATIC_CAST(_Ch*, 0)); williamr@2: if (!__pr) williamr@2: return __format; williamr@2: break; williamr@2: } williamr@2: williamr@4: case 'y': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0)); williamr@4: if (!__pr) williamr@4: return __format; williamr@4: break; williamr@2: } williamr@2: williamr@2: case 'Y': { williamr@4: bool __pr = __get_decimal_integer(__first, __last, __t->tm_year, __STATIC_CAST(_Ch*, 0)); williamr@2: __t->tm_year -= 1900; williamr@2: if (!__pr) williamr@2: return __format; williamr@2: break; williamr@2: } williamr@2: williamr@2: default: williamr@2: break; williamr@2: } williamr@2: } williamr@2: else { williamr@4: if (*__first++ != __ct.widen(*__format)) break; williamr@2: } williamr@4: williamr@2: ++__format; williamr@2: } williamr@2: williamr@2: return __format; williamr@2: } williamr@2: williamr@4: template williamr@2: bool _STLP_CALL williamr@4: __get_short_or_long_dayname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct, williamr@2: const _Time_Info& __table, tm* __t) { williamr@2: const string* __pr = williamr@4: __match(__first, __last, __table._M_dayname + 0, __table._M_dayname + 14, __ct); williamr@4: __t->tm_wday = __STATIC_CAST(int, (__pr - __table._M_dayname) % 7); williamr@2: return __pr != __table._M_dayname + 14; williamr@2: } williamr@2: williamr@4: template williamr@2: bool _STLP_CALL williamr@4: __get_short_or_long_monthname(_InIt& __first, _InIt& __last, const ctype<_CharT>& __ct, williamr@2: const _Time_Info& __table, tm* __t) { williamr@2: const string* __pr = williamr@4: __match(__first, __last, __table._M_monthname + 0, __table._M_monthname + 24, __ct); williamr@4: __t->tm_mon = __STATIC_CAST(int, (__pr - __table._M_monthname) % 12); williamr@2: return __pr != __table._M_monthname + 24; williamr@2: } williamr@2: williamr@4: #if !defined (_STLP_NO_WCHAR_T) williamr@2: template williamr@2: _OuIt _STLP_CALL williamr@4: __put_time(char * __first, char * __last, _OuIt __out_ite, williamr@2: const ios_base& __s, wchar_t) { williamr@4: const ctype& __ct = *__STATIC_CAST(const ctype*, __s._M_ctype_facet()); williamr@2: wchar_t __wbuf[64]; williamr@2: __ct.widen(__first, __last, __wbuf); williamr@2: ptrdiff_t __len = __last - __first; williamr@2: wchar_t * __eend = __wbuf + __len; williamr@4: return copy((wchar_t*)__wbuf, __eend, __out_ite); williamr@2: } williamr@4: #endif williamr@2: williamr@4: _STLP_MOVE_TO_STD_NAMESPACE williamr@2: williamr@2: template williamr@2: _InIt williamr@2: time_get<_Ch, _InIt>::do_get_date(_InIt __s, _InIt __end, williamr@4: ios_base& __str, ios_base::iostate& __err, williamr@4: tm* __t) const { williamr@2: typedef string::const_iterator string_iterator; williamr@2: williamr@4: string_iterator __format = _M_timeinfo._M_date_format.begin(); williamr@4: string_iterator __format_end = _M_timeinfo._M_date_format.end(); williamr@4: williamr@2: string_iterator __result williamr@4: = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end, williamr@4: __STATIC_CAST(_Ch*, 0), _M_timeinfo, williamr@4: __str, __err, __t); williamr@2: if (__result == __format_end) williamr@2: __err = ios_base::goodbit; williamr@2: else { williamr@2: __err = ios_base::failbit; williamr@2: if (__s == __end) williamr@2: __err |= ios_base::eofbit; williamr@2: } williamr@2: return __s; williamr@2: } williamr@2: williamr@2: template williamr@2: _InIt williamr@2: time_get<_Ch, _InIt>::do_get_time(_InIt __s, _InIt __end, williamr@4: ios_base& __str, ios_base::iostate& __err, williamr@4: tm* __t) const { williamr@2: typedef string::const_iterator string_iterator; williamr@4: string_iterator __format = _M_timeinfo._M_time_format.begin(); williamr@4: string_iterator __format_end = _M_timeinfo._M_time_format.end(); williamr@4: williamr@2: string_iterator __result williamr@4: = _STLP_PRIV __get_formatted_time(__s, __end, __format, __format_end, williamr@4: __STATIC_CAST(_Ch*, 0), _M_timeinfo, williamr@4: __str, __err, __t); williamr@4: __err = __result == __format_end ? ios_base::goodbit williamr@4: : ios_base::failbit; williamr@2: if (__s == __end) williamr@2: __err |= ios_base::eofbit; williamr@2: return __s; williamr@2: } williamr@2: williamr@2: template williamr@2: _InIt williamr@2: time_get<_Ch, _InIt>::do_get_year(_InIt __s, _InIt __end, williamr@4: ios_base&, ios_base::iostate& __err, williamr@4: tm* __t) const { williamr@2: if (__s == __end) { williamr@2: __err = ios_base::failbit | ios_base::eofbit; williamr@2: return __s; williamr@2: } williamr@4: williamr@4: bool __pr = _STLP_PRIV __get_decimal_integer(__s, __end, __t->tm_year, __STATIC_CAST(_Ch*, 0)); williamr@2: __t->tm_year -= 1900; williamr@2: __err = __pr ? ios_base::goodbit : ios_base::failbit; williamr@2: if (__s == __end) williamr@2: __err |= ios_base::eofbit; williamr@4: williamr@2: return __s; williamr@2: } williamr@2: williamr@2: template williamr@2: _InIt williamr@2: time_get<_Ch, _InIt>::do_get_weekday(_InIt __s, _InIt __end, williamr@4: ios_base &__str, ios_base::iostate &__err, williamr@4: tm *__t) const { williamr@4: const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet()); williamr@4: bool __result = williamr@4: _STLP_PRIV __get_short_or_long_dayname(__s, __end, __ct, _M_timeinfo, __t); williamr@4: if (__result) williamr@4: __err = ios_base::goodbit; williamr@4: else { williamr@4: __err = ios_base::failbit; williamr@4: if (__s == __end) williamr@4: __err |= ios_base::eofbit; williamr@4: } williamr@4: return __s; williamr@2: } williamr@2: williamr@2: template williamr@2: _InIt williamr@2: time_get<_Ch, _InIt>::do_get_monthname(_InIt __s, _InIt __end, williamr@4: ios_base &__str, ios_base::iostate &__err, williamr@4: tm *__t) const { williamr@4: const ctype<_Ch>& __ct = *__STATIC_CAST(const ctype<_Ch>*, __str._M_ctype_facet()); williamr@2: bool __result = williamr@4: _STLP_PRIV __get_short_or_long_monthname(__s, __end, __ct, _M_timeinfo, __t); williamr@2: if (__result) williamr@2: __err = ios_base::goodbit; williamr@2: else { williamr@2: __err = ios_base::failbit; williamr@2: if (__s == __end) williamr@2: __err |= ios_base::eofbit; williamr@2: } williamr@2: return __s; williamr@2: } williamr@2: williamr@2: template williamr@2: _OutputIter williamr@2: time_put<_Ch,_OutputIter>::put(_OutputIter __s, ios_base& __f, _Ch __fill, williamr@4: const tm* __tmb, const _Ch* __pat, williamr@4: const _Ch* __pat_end) const { williamr@4: // locale __loc = __f.getloc(); williamr@4: // const ctype<_Ch>& _Ct = use_facet >(__loc); williamr@4: const ctype<_Ch>& _Ct = *__STATIC_CAST(const ctype<_Ch>*, __f._M_ctype_facet()); williamr@2: while (__pat != __pat_end) { williamr@2: char __c = _Ct.narrow(*__pat, 0); williamr@2: if (__c == '%') { williamr@2: char __mod = 0; williamr@2: ++__pat; williamr@2: __c = _Ct.narrow(*__pat++, 0); williamr@4: if (__c == '#') { // MS extension williamr@2: __mod = __c; williamr@2: __c = _Ct.narrow(*__pat++, 0); williamr@2: } williamr@2: __s = do_put(__s, __f, __fill, __tmb, __c, __mod); williamr@2: } williamr@2: else williamr@2: *__s++ = *__pat++; williamr@2: } williamr@2: return __s; williamr@2: } williamr@2: williamr@2: template williamr@2: _OutputIter williamr@4: time_put<_Ch,_OutputIter>::do_put(_OutputIter __s, ios_base& __f, _Ch /* __fill */, williamr@4: const tm* __tmb, char __format, williamr@4: char __modifier ) const { williamr@2: char __buf[64]; williamr@4: char * __iend = _STLP_PRIV __write_formatted_time(_STLP_ARRAY_AND_SIZE(__buf), williamr@4: __format, __modifier, _M_timeinfo, __tmb); williamr@2: // locale __loc = __f.getloc(); williamr@4: return _STLP_PRIV __put_time(__buf, __iend, __s, __f, _Ch()); williamr@2: } williamr@2: williamr@2: _STLP_END_NAMESPACE williamr@2: williamr@2: #endif /* _STLP_TIME_FACETS_C */ williamr@2: williamr@2: // Local Variables: williamr@2: // mode:C++ williamr@2: // End: