// xloctime internal header (from <locale>)
#ifndef _XLOCTIME_
#define _XLOCTIME_
#include <ctime>
#include <xlocnum>

_STD_BEGIN
		// STRUCT time_base
struct 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};

	time_base(size_t _Refs = 0)
		: locale::facet(_Refs)
		{	// default constructor
		}

	~time_base()
		{	// destroy the object
		}
	};

		// TEMPLATE CLASS time_get
export
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;

	static locale::id id;	// unique facet id

	dateorder date_order() const
		{	// return date order code
		return (do_date_order());
		}

	_InIt 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 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 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 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 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 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 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_facet< _Ctype >(_Iosbase.getloc());

		_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 time_get(size_t _Refs = 0)
		: time_base(_Refs)
		{	// construct from current locale
		_BEGIN_LOCINFO(_Lobj)
			_Init(_Lobj);
		_END_LOCINFO()
		}

	time_get(const _Locinfo& _Lobj, size_t _Refs = 0)
		: time_base(_Refs)
		{	// construct from specified locale
		_Init(_Lobj);
		}

	static size_t _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 ~time_get()
		{	// destroy the object
		_Tidy();
		}

	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 _Init(const _Locinfo& _Lobj);

	virtual dateorder do_date_order() const
		{	// return date order code
		return (_Dateorder);
		}

	virtual _InIt do_get_time(
		_InIt _First, _InIt _Last,
		ios_base&, ios_base::iostate& _State, tm *_Pt) const;

	virtual _InIt do_get_date(
		_InIt _First, _InIt _Last,
		ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt) const;

	virtual _InIt do_get_weekday(
		_InIt _First, _InIt _Last,
		ios_base&, ios_base::iostate& _State, tm *_Pt) const;

	virtual _InIt do_get_monthname(
		_InIt _First, _InIt _Last,
		ios_base&, ios_base::iostate& _State, tm *_Pt) const;

	virtual _InIt do_get_year(
		_InIt _First, _InIt _Last,
		ios_base&, ios_base::iostate& _State, tm *_Pt) const;

 #if _HAS_CPP0X
	virtual _InIt do_get(_InIt _First, _InIt _Last,
		ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt,
		char _Specifier, char = 0) const
		{	// get formatted time for _Specifier (_Modifier ignored)
		int _Ans = 0;

		switch (_Specifier)
			{	// process format specifier
		case 'a':
		case 'A':
			_First = get_weekday(_First, _Last, _Iosbase, _State, _Pt);
			break;

		case 'b':
		case 'B':
		case 'h':
			_First = get_monthname(_First, _Last, _Iosbase, _State, _Pt);
			break;

		case 'c':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				"%b %d %H : %M : %S %Y");
			break;

		case 'C':
			_State |= _Getint(_First, _Last, 0, 99, _Ans);
			if (!(_State & ios_base::failbit))
				_Pt->tm_year = _Ans * 100 - 1900;	// convert to century
			break;

		case 'd':
		case 'e':
			_State |= _Getint(_First, _Last, 1, 31, _Pt->tm_mday);
			break;

		case 'D':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				"%m / %d / %y");
			break;

		case 'H':
			_State |= _Getint(_First, _Last, 0, 23, _Pt->tm_hour);
			break;

		case 'I':
			_State |= _Getint(_First, _Last, 0, 11, _Pt->tm_hour);
			break;

		case 'j':
			_State |= _Getint(_First, _Last, 1, 366, _Pt->tm_yday);
			break;

		case 'm':
			_State |= _Getint(_First, _Last, 1, 12, _Ans);
			if (!(_State & ios_base::failbit))
				_Pt->tm_mon = _Ans - 1;
			break;

		case 'M':
			_State |= _Getint(_First, _Last, 0, 59, _Pt->tm_min);
			break;

		case 'n':
		case 't':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				" ");
			break;

		case 'p':
			_Ans = _Getloctxt(_First, _Last, (size_t)0, ":AM:am:PM:pm");
			if (_Ans < 0)
				_State |= ios_base::failbit;
			else
				_Pt->tm_hour += _Ans * 12;
			break;

		case 'r':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				"%I : %M : %S %p");
			break;

		case 'R':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				"%H : %M");
			break;

		case 'S':
			_State |= _Getint(_First, _Last, 0, 59, _Pt->tm_sec);
			break;

		case 'T':
		case 'X':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				"%H : %M : S");
			break;

		case 'U':
			_State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday);
			break;

		case 'w':
			_State |= _Getint(_First, _Last, 0, 6, _Pt->tm_wday);
			break;

		case 'W':
			_State |= _Getint(_First, _Last, 0, 53, _Pt->tm_yday);
			break;

		case 'x':
			_First = _Getfmt(_First, _Last, _Iosbase, _State, _Pt,
				"%d / %m / %y");
			break;

		case 'y':
			_State |= _Getint(_First, _Last, 0, 99, _Ans);
			if (!(_State & ios_base::failbit))
				_Pt->tm_year = _Ans < 69 ? _Ans + 100 : _Ans;
			break;

		case 'Y':
			_First = get_year(_First, _Last, _Iosbase, _State, _Pt);
			break;

		default:
			_State |= ios_base::failbit;	// unknown specifier
			}

		if (_First == _Last)
			_State |= ios_base::eofbit;
		return (_First);
		}

	_InIt _Getfmt(_InIt _First, _InIt _Last,
		ios_base& _Iosbase, ios_base::iostate& _State, tm *_Pt,
			const char *_Fmtfirst) const
		{	// get formatted time for format NTBS
		typedef ctype<_Elem> _Ctype;
		const _Ctype& _Ctype_fac = use_facet< _Ctype >(_Iosbase.getloc());

		for (; *_Fmtfirst != '\0'; ++_Fmtfirst)
			if (*_Fmtfirst == '%')
				_First = do_get(_First, _Last, _Iosbase, _State, _Pt,
					*++_Fmtfirst);	// convert a single field
			else if (*_Fmtfirst == ' ')
				for (; _First != _Last
					&& _Ctype_fac.is(_Ctype::space, *_First); ++_First)
					;
			else if (*_First != _MAKLOCCHR(_Elem, *_Fmtfirst, _Cvt))
				{	// bad literal match
				_State |= ios_base::failbit;
				break;
				}
			else
				++_First;
		if (_First == _Last)
			_State |= ios_base::eofbit;
		return (_First);
		}
 #endif /* _HAS_CPP0X */

private:
	ios_base::iostate _Getint(
		_InIt& _First, _InIt& _Last,
		int _Lo, int _Hi, int& _Val) const;

	void _Tidy();

	const _Elem *_Days;	// ":Sun:Sunday:Mon:Monday..." for example
	const _Elem *_Months;	// "Jan:January:Feb:February..." for example
	dateorder _Dateorder;
	_Locinfo::_Cvtvec _Cvt;		// conversion information
	};

		// STATIC time_get::id OBJECT
template<class _Elem,
	class _InIt>
	locale::id time_get<_Elem, _InIt>::id;

		// TEMPLATE CLASS time_get_byname
template<class _Elem,
	class _InIt = istreambuf_iterator<_Elem, char_traits<_Elem> > >
	class time_get_byname
		: public time_get<_Elem, _InIt>
	{	// time_get for named locale
public:
	explicit time_get_byname(const char *_Locname, size_t _Refs = 0)
		: time_get<_Elem, _InIt>(_Locname, _Refs)
		{	// construct for named locale
		}

 #if _HAS_CPP0X
	explicit time_get_byname(const string& _Str, size_t _Refs = 0)
		: time_get<_Elem, _InIt>(_Locinfo(_Str.c_str()), _Refs)
		{	// construct for named locale
		}
 #endif /* _HAS_CPP0X */

protected:
	virtual ~time_get_byname()
		{	// destroy the object
		}
	};

		// TEMPLATE CLASS time_put
export
template<class _Elem,
	class _OutIt = ostreambuf_iterator<_Elem, char_traits<_Elem> > >
	class time_put
		: public locale::facet
	{	// facet for converting encoded times to text
public:
	typedef _Elem char_type;
	typedef _OutIt iter_type;

	_OutIt put(_OutIt _Dest,
		ios_base& _Iosbase, _Elem _Fill, const tm *_Pt,
			const _Elem *_Fmtfirst, const _Elem *_Fmtlast) const;

	_OutIt put(_OutIt _Dest,
		ios_base& _Iosbase, _Elem _Fill, const tm *_Pt,
		char _Specifier, char _Modifier = 0) const
		{	// put formatted time from _Pt to _Dest for _Specifier/_Modifier
		return (do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier));
		}

	static locale::id id;	// unique facet id

	explicit time_put(size_t _Refs = 0)
		: locale::facet(_Refs)
		{	// construct from current locale
		_BEGIN_LOCINFO(_Lobj)
			_Init(_Lobj);
		_END_LOCINFO()
		}

	time_put(const _Locinfo& _Lobj, size_t _Refs = 0)
		: locale::facet(_Refs)
		{	// construct from specified locale
		_Init(_Lobj);
		}

	static size_t _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_put<_Elem, _OutIt>(
				_Locinfo(_Ploc->c_str()));
		return (_X_TIME);
		}

protected:
	virtual ~time_put()
		{	// destroy the object
		}

	void _Init(const _Locinfo& _Lobj)
		{	// initialize from _Lobj
		_Cvt = _Lobj._Getcvt();
		_Tnames = _Lobj._Gettnames();
		}

	virtual _OutIt do_put(_OutIt _Dest,
		ios_base&, _Elem, const tm *_Pt,
			char _Specifier, char _Modifier = 0) const;

private:
	_Locinfo::_Timevec _Tnames;	// locale-specific stuff for _Strftime
	_Locinfo::_Cvtvec _Cvt;		// conversion information
	};

		// STATIC time_put::id OBJECT
template<class _Elem,
	class _OutIt>
	locale::id time_put<_Elem, _OutIt>::id;

		// CLASS time_put<wchar_t>
export
template<class _OutIt>
	class time_put<wchar_t, _OutIt>
		: public locale::facet
	{	// facet for converting encoded times to wchar_t text
public:
	typedef wchar_t _Elem;
	typedef _Elem char_type;
	typedef _OutIt iter_type;

	_OutIt put(_OutIt _Dest,
		ios_base& _Iosbase, _Elem _Fill, const tm *_Pt,
			const _Elem *_Fmtfirst, const _Elem *_Fmtlast) const;

	_OutIt put(_OutIt _Dest,
		ios_base& _Iosbase, _Elem _Fill, const tm *_Pt,
		char _Specifier, char _Modifier = 0) const
		{	// put formatted time from _Pt to _Dest for _Specifier/_Modifier
		return (do_put(_Dest, _Iosbase, _Fill, _Pt, _Specifier, _Modifier));
		}

	static locale::id id;	// unique facet id

	explicit time_put(size_t _Refs = 0)
		: locale::facet(_Refs)
		{	// construct from current locale
		_BEGIN_LOCINFO(_Lobj)
			_Init(_Lobj);
		_END_LOCINFO()
		}

	time_put(const _Locinfo& _Lobj, size_t _Refs = 0)
		: locale::facet(_Refs)
		{	// construct from specified locale
		_Init(_Lobj);
		}

	static size_t _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_put<_Elem, _OutIt>(
				_Locinfo(_Ploc->c_str()));
		return (_X_TIME);
		}

protected:
	virtual ~time_put()
		{	// destroy the object
		}

	time_put(const char *_Locname, size_t _Refs = 0)
		: locale::facet(_Refs)
		{	// construct from specified locale
		_BEGIN_LOCINFO(_Lobj(_Locname))
			_Init(_Lobj);
		_END_LOCINFO()
		}

	void _Init(const _Locinfo& _Lobj)
		{	// initialize from _Lobj
		_Cvt = _Lobj._Getcvt();
		_Tnames = _Lobj._Gettnames();
		}

	virtual _OutIt do_put(_OutIt _Dest,
		ios_base&, _Elem, const tm *_Pt,
			char _Specifier, char _Modifier = 0) const;

private:
	_Locinfo::_Timevec _Tnames;	// locale-specific stuff for _Strftime
	_Locinfo::_Cvtvec _Cvt;		// conversion information
	};

		// TEMPLATE CLASS time_put_byname
template<class _Elem,
	class _OutIt = ostreambuf_iterator<_Elem, char_traits<_Elem> > >
	class time_put_byname
		: public time_put<_Elem, _OutIt>
	{	// time_put for named locale
public:
	explicit time_put_byname(const char *_Locname, size_t _Refs = 0)
		: time_put<_Elem, _OutIt>(_Locname, _Refs)
		{	// construct for named locale
		}

 #if _HAS_CPP0X
	explicit time_put_byname(const string& _Str, size_t _Refs = 0)
		: time_put<_Elem, _OutIt>(_Str.c_str(), _Refs)
		{	// construct for named locale
		}
 #endif /* _HAS_CPP0X */

protected:
	virtual ~time_put_byname()
		{	// destroy the object
		}
	};
_STD_END
#endif /* _XLOCTIME_ */

/*
 * Copyright (c) 1992-2010 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
V5.30:0009 */
