📄 xloctime
字号:
// xloctime internal header (from <locale>)
#pragma once
#ifndef _XLOCTIME_
#define _XLOCTIME_
#ifndef RC_INVOKED
#include <ctime>
#include <xlocnum>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new
#pragma warning(disable: 4275)
_STD_BEGIN
// STRUCT time_base
struct _CRTIMP2_PURE time_base
: public locale::facet
{ // base class for time_get
enum dateorder
{ // constants for different orders of date components
no_order, dmy, mdy, ymd, ydm};
__CLR_OR_THIS_CALL time_base(size_t _Refs = 0)
: locale::facet(_Refs)
{ // default constructor
}
__CLR_OR_THIS_CALL ~time_base()
{ // destroy the object
}
};
// TEMPLATE CLASS time_get
template<class _Elem,
class _InIt = istreambuf_iterator<_Elem, char_traits<_Elem> > >
class time_get
: public time_base
{ // facet for converting text to encoded times
public:
typedef _Elem char_type;
typedef _InIt iter_type;
__PURE_APPDOMAIN_GLOBAL static locale::id id; // unique facet id
dateorder __CLR_OR_THIS_CALL date_order() const
{ // return date order code
return (do_date_order());
}
_InIt __CLR_OR_THIS_CALL get_time(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const
{ // get time of day from [_First, _Last) into _Pt
return (do_get_time(_First, _Last, _Iosbase, _State, _Pt));
}
_InIt __CLR_OR_THIS_CALL get_date(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const
{ // get date from [_First, _Last) into _Pt
return (do_get_date(_First, _Last, _Iosbase, _State, _Pt));
}
_InIt __CLR_OR_THIS_CALL get_weekday(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const
{ // get weekday from [_First, _Last) into _Pt
return (do_get_weekday(_First, _Last, _Iosbase, _State, _Pt));
}
_InIt __CLR_OR_THIS_CALL get_monthname(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const
{ // get month from [_First, _Last) into _Pt
return (do_get_monthname(_First, _Last, _Iosbase, _State, _Pt));
}
_InIt __CLR_OR_THIS_CALL get_year(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const
{ // get year from [_First, _Last) into _Pt
return (do_get_year(_First, _Last, _Iosbase, _State, _Pt));
}
#if _HAS_CPP0X
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt,
char _Specifier, char _Modifier = 0) const
{ // get formatted time for _Specifier/_Modifier
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
return (do_get(_First, _Last, _Iosbase, _State, _Pt,
_Specifier, _Modifier));
}
_InIt __CLR_OR_THIS_CALL get(_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt,
const _Elem *_Fmtfirst, const _Elem *_Fmtlast) const
{ // get formatted time for format string
typedef ctype<_Elem> _Ctype;
const _Ctype& _Ctype_fac = _USE(_Iosbase.getloc(), _Ctype);
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
_DEBUG_RANGE(_Fmtfirst, _Fmtlast);
const _Elem _Percent = _MAKLOCCHR(_Elem, '%', _Cvt);
for (; _Fmtfirst != _Fmtlast; ++_Fmtfirst)
if (*_Fmtfirst != _Percent)
{ // match literal element
if (_Ctype_fac.is(_Ctype::space, *_Fmtfirst))
for (; _First != _Last
&& _Ctype_fac.is(_Ctype::space, *_First); ++_First)
;
else if (*_First != *_Fmtfirst)
{ // bad literal match
_State |= ios_base::failbit;
break;
}
else
++_First;
}
else if (++_Fmtfirst == _Fmtlast)
{ // treat trailing % as literal match
if (*_First != _Percent)
_State |= ios_base::failbit;
else
++_First;
break;
}
else
{ // get specifier after %
char _Specifier = _MAKLOCBYTE(_Elem, *_Fmtfirst, _Cvt);
char _Qualifier = '\0';
if (_Specifier != 'E' && _Specifier != 'O'
&& _Specifier != 'Q' && _Specifier != '#')
; // not [E0Q#] qualifier, treat as specifier
else if (++_Fmtfirst == _Fmtlast)
{ // no specifier, treat %[E0Q#] as literal match
if (*_First != _Percent || ++_First == _Last
|| *_First != _Specifier)
_State |= ios_base::failbit;
else
++_First;
break;
}
else
{ // save both qualifier and specifier
_Qualifier = _Specifier;
_Specifier = _MAKLOCBYTE(_Elem, *_Fmtfirst, _Cvt);
}
_First = do_get(_First, _Last, _Iosbase, _State, _Pt,
_Specifier, _Qualifier); // convert a single field
}
if (_First == _Last)
_State |= ios_base::eofbit;
return (_First);
}
#endif /* _HAS_CPP0X */
explicit __CLR_OR_THIS_CALL time_get(size_t _Refs = 0)
: time_base(_Refs)
{ // construct from current locale
_BEGIN_LOCINFO(_Lobj)
_Init(_Lobj);
_END_LOCINFO()
}
__CLR_OR_THIS_CALL time_get(const _Locinfo& _Lobj, size_t _Refs = 0)
: time_base(_Refs)
{ // construct from specified locale
_Init(_Lobj);
}
static size_t __CLRCALL_OR_CDECL _Getcat(const locale::facet **_Ppf = 0,
const locale *_Ploc = 0)
{ // return locale category mask and construct standard facet
if (_Ppf != 0 && *_Ppf == 0)
*_Ppf = _NEW_CRT time_get<_Elem, _InIt>(
_Locinfo(_Ploc->c_str()));
return (_X_TIME);
}
protected:
virtual __CLR_OR_THIS_CALL ~time_get()
{ // destroy the object
_Tidy();
}
__CLR_OR_THIS_CALL time_get(const char *_Locname, size_t _Refs = 0)
: time_base(_Refs)
{ // construct from specified locale
_BEGIN_LOCINFO(_Lobj(_Locname))
_Init(_Lobj);
_END_LOCINFO()
}
void __CLR_OR_THIS_CALL _Init(const _Locinfo& _Lobj)
{ // initialize from _Lobj
_Cvt = _Lobj._Getcvt();
_Days = 0;
_Months = 0;
_TRY_BEGIN
_Days = _MAKLOCSTR(_Elem, _Lobj._Getdays(), _Cvt);
_Months = _MAKLOCSTR(_Elem, _Lobj._Getmonths(), _Cvt);
_Dateorder = (dateorder)_Lobj._Getdateorder();
_CATCH_ALL
_Tidy();
_RERAISE;
_CATCH_END
}
virtual dateorder __CLR_OR_THIS_CALL do_date_order() const
{ // return date order code
return (_Dateorder);
}
virtual _InIt __CLR_OR_THIS_CALL do_get_time(
_InIt _First, _InIt _Last,
ios_base&, ios_base::iostate& _State, tm *_Pt) const
{ // get time of day from [_First, _Last) into _Pt
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
const _Elem _Colon = _MAKLOCCHR(_Elem, ':', _Cvt);
_State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour);
if (_State != ios_base::goodbit || *_First != _Colon)
_State |= ios_base::failbit; // hour field is bad
else
_State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_min);
if (_State != ios_base::goodbit || *_First != _Colon)
_State |= ios_base::failbit; // min field is bad
else
_State |= _Getint(++_First, _Last, 0, 59, _Pt->tm_sec);
return (_First);
}
virtual _InIt __CLR_OR_THIS_CALL do_get_date(
_InIt _First, _InIt _Last,
ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const
{ // get date from [_First, _Last) into _Pt
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
const _Elem _E0 = _MAKLOCCHR(_Elem, '0', _Cvt);
const _Elem _Colon = _MAKLOCCHR(_Elem, ':', _Cvt);
const _Elem _Comma = _MAKLOCCHR(_Elem, ',', _Cvt);
const _Elem _Slash = _MAKLOCCHR(_Elem, '/', _Cvt);
const _Elem _Space = _MAKLOCCHR(_Elem, ' ', _Cvt);
dateorder _Dorder = date_order();
if (_Dorder == no_order)
_Dorder = mdy;
if (_First == _Last)
;
else if (*_First < _E0 || _E0 + 9 < *_First)
{ // begins with month name, assume mdy
_First = get_monthname(_First, _Last, _Iosbase, _State, _Pt);
_Dorder = mdy;
}
else if (_Dorder == mdy)
{ // get month number
_State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon);
--_Pt->tm_mon;
}
else if (_Dorder == dmy)
_State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday);
else // ymd or ydm
_First = get_year(_First, _Last, _Iosbase, _State, _Pt);
while (_First != _Last && *_First == _Space)
++_First; // skip spaces
if (_First != _Last
&& (*_First == _Colon
|| *_First == _Comma
|| *_First == _Slash))
++_First; // skip [:,/]
while (_First != _Last && *_First == _Space)
++_First; // skip spaces
if (_First == _Last)
;
else if (*_First < _E0 || _E0 + 9 < *_First)
if (_Dorder == mdy)
_State |= ios_base::failbit; // error, month already seen
else
{ // month name is second, like it or not
_First = get_monthname(_First, _Last, _Iosbase, _State, _Pt);
if (_Dorder == ydm)
_Dorder = ymd;
}
else if (_Dorder == dmy || _Dorder == ymd)
{ // get month number
_State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon);
--_Pt->tm_mon;
}
else
_State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday);
while (_First != _Last && *_First == _Space)
++_First; // skip spaces
if (_First != _Last
&& (*_First == _Colon
|| *_First == _Comma
|| *_First == _Slash))
++_First; // skip [:,/]
while (_First != _Last && *_First == _Space)
++_First; // skip spaces
if (_First == _Last)
_State |= ios_base::failbit; // error, missing component(s)
else if (*_First < _E0 || _E0 + 9 < *_First)
if (_Dorder != ydm)
_State |= ios_base::failbit; // error, month out of place
else
_First = get_monthname(_First, _Last, _Iosbase, _State, _Pt);
else if (_Dorder == ydm)
{ // get month number
_State |= _Getint(_First, _Last, 1, 12, _Pt->tm_mon);
--_Pt->tm_mon;
}
else if (_Dorder == ymd)
_State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday);
else // mdy or dmy
_First = get_year(_First, _Last, _Iosbase, _State, _Pt);
if (_First == _Last)
_State |= ios_base::eofbit;
return (_First);
}
virtual _InIt __CLR_OR_THIS_CALL do_get_weekday(
_InIt _First, _InIt _Last,
ios_base&, ios_base::iostate& _State, tm *_Pt) const
{ // get weekday from [_First, _Last) into _Pt
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
int _Num = _Getloctxt(_First, _Last, (size_t)0, _Days);
if (_Num < 0)
_State |= ios_base::failbit;
else
_Pt->tm_wday = _Num >> 1; // set wday
return (_First);
}
virtual _InIt __CLR_OR_THIS_CALL do_get_monthname(
_InIt _First, _InIt _Last,
ios_base&, ios_base::iostate& _State, tm *_Pt) const
{ // get month from [_First, _Last) into _Pt
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
int _Num = _Getloctxt(_First, _Last, (size_t)0, _Months);
if (_Num < 0)
_State |= ios_base::failbit;
else
_Pt->tm_mon = _Num >> 1; // set mon
return (_First);
}
virtual _InIt __CLR_OR_THIS_CALL do_get_year(
_InIt _First, _InIt _Last,
ios_base&, ios_base::iostate& _State, tm *_Pt) const
{ // get year from [_First, _Last) into _Pt
_DEBUG_RANGE(_First, _Last);
_DEBUG_POINTER(_Pt);
int _Ans = 0;
_State |= _Getint(_First, _Last, 0, 2035, _Ans);
if (!(_State & ios_base::failbit))
{ // valid input, convert to year
if (1900 <= _Ans)
_Ans -= 1900; // [1900, ...) as is
else if (2035 - 1900 < _Ans)
_State |= ios_base::failbit;
_Pt->tm_year = _Ans; // set year
}
return (_First);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -