// chrono C++11 header
#pragma once
#ifndef _CHRONO_
#define _CHRONO_
#ifndef RC_INVOKED
#include <limits>
#include <ratio>
#include <utility>
#include <time.h>
#include <thr/xtimec.h>

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)
 #pragma push_macro("new")
 #undef new

 #pragma warning(disable: 6326)

_STD_BEGIN
namespace chrono {
	// CLASS TEMPLATE treat_as_floating_point
template<class _Rep>
	struct treat_as_floating_point
		: is_floating_point<_Rep>
	{	// tests for floating-point type
	};

	// CLASS TEMPLATE duration_values
template<class _Rep>
	struct duration_values
	{	// gets arithmetic properties of a type
	static _Rep zero()
		{	// get zero value
		return (_Rep(0));
		}

	static _Rep (min)()
		{	// get smallest value
		return ((numeric_limits<_Rep>::lowest)());
		}

	static _Rep (max)()
		{	// get largest value
		return ((numeric_limits<_Rep>::max)());
		}
	};

	// CLASS TEMPLATE _Is_ratio
template<class _Ty>
	struct _Is_ratio
	{	// test for ratio type
	static const bool value = false;
	};

template<intmax_t _R1,
	intmax_t _R2>
	struct _Is_ratio<ratio<_R1, _R2> >
	{	// test for ratio type
	static const bool value = true;
	};

	// CLASS TEMPLATE duration
template<class _Rep,
	class _Period = ratio<1> >
	class duration;

template<class _Ty>
	struct _Is_duration
	{	// tests for duration
	static const bool value = false;
	};

template<class _To,
	class _Rep,
	class _Period> inline
	typename enable_if<_Is_duration<_To>::value,
		_To>::type
		duration_cast(const duration<_Rep, _Period>&);

template<class _Rep,
	class _Period>
	class duration
	{	// represents a time duration
public:
	typedef duration<_Rep, _Period> _Myt;
	typedef _Rep rep;
	typedef _Period period;

	duration()
		: _MyRep()
		{	// check asserts
		static_assert(_Is_ratio<_Period>::value,
			"period must be an instance of std::ratio");
		static_assert(0 < _Period::num,
			"duration negative or zero");
		}

	template<class _Rep2,
		class = typename enable_if<is_convertible<_Rep2, _Rep>::value
			&& (treat_as_floating_point<_Rep>::value
				|| !treat_as_floating_point<_Rep2>::value),
			void>::type>
		explicit duration(const _Rep2& _Val)
			: _MyRep(static_cast<_Rep>(_Val))
		{	// construct from representation
		static_assert(_Is_ratio<_Period>::value,
			"period not an instance of std::ratio");
		static_assert(0 < _Period::num,
			"duration negative or zero");
		}

	template<class _Rep2,
		class _Period2,
		class = typename enable_if<treat_as_floating_point<_Rep>::value
			|| (ratio_divide<_Period2, _Period>::den == 1
				&& !treat_as_floating_point<_Rep2>::value),
			void>::type>
		duration(const duration<_Rep2, _Period2>& _Dur)
		: _MyRep(duration_cast<_Myt>(_Dur).count())
		{	// construct from a duration
		typedef ratio_divide<_Period2, _Period> _Checked_type;

		static_assert(_Is_ratio<_Period>::value,
			"period not an instance of std::ratio");
		static_assert(0 < _Period::num,
			"duration negative or zero");
		}

	_Rep count() const
		{	// get stored rep
		return (_MyRep);
		}

	_Myt operator+() const
		{	// get value
		return (*this);
		}

	_Myt operator-() const
		{	// get negated value
		return (_Myt(0 - _MyRep));
		}

	_Myt& operator++()
		{	// increment rep
		++_MyRep;
		return (*this);
		}

	_Myt operator++(int)
		{	// postincrement rep
		return (_Myt(_MyRep++));
		}

	_Myt& operator--()
		{	// decrement rep
		--_MyRep;
		return (*this);
		}

	_Myt operator--(int)
		{	// postdecrement rep
		return (_Myt(_MyRep--));
		}

	_Myt& operator+=(const _Myt& _Right)
		{	// add _Right to rep
		_MyRep += _Right._MyRep;
		return (*this);
		}

	_Myt& operator-=(const _Myt& _Right)
		{	// subtract _Right from rep
		_MyRep -= _Right._MyRep;
		return (*this);
		}

	_Myt& operator*=(const _Rep& _Right)
		{	// multiply rep by _Right
		_MyRep *= _Right;
		return (*this);
		}

	_Myt& operator/=(const _Rep& _Right)
		{	// divide rep by _Right
		_MyRep /= _Right;
		return (*this);
		}

	_Myt& operator%=(const _Rep& _Right)
		{	// modulus rep by _Right
		_MyRep %= _Right;
		return (*this);
		}

	_Myt& operator%=(const _Myt& _Right)
		{	// modulus rep by _Right
		_MyRep %= _Right.count();
		return (*this);
		}

	static _Myt zero()
		{	// get zero value
		return (_Myt(duration_values<_Rep>::zero()));
		}

	static _Myt (min)()
		{	// get minimum value
		return (_Myt((duration_values<_Rep>::min)()));
		}
	static _Myt (max)()
		{	// get maximum value
		return (_Myt((duration_values<_Rep>::max)()));
		}

private:
	_Rep _MyRep;	// the stored rep
	};

template<class _Rep1,
	class _Period1,
	class _Period2>
	class duration<duration<_Rep1, _Period1>, _Period2>
	{	// undefined: duration type as first template argument
	duration()
		{	// check asserts
		static_assert(_Always_false<_Rep1>::value,
			"duration can't have duration as first template argument");
		}
	};

	// CLASS TEMPLATE _Is_duration
template<class _Rep,
	class _Period>
	struct _Is_duration<duration<_Rep, _Period> >
	{	// tests for duration
	static const bool value = true;
	};

template<class _Clock,
	class _Duration = typename _Clock::duration>
	class time_point
	{	// represents a point in time
public:
	typedef _Clock clock;
	typedef _Duration duration;
	typedef typename _Duration::rep rep;
	typedef typename _Duration::period period;

	time_point()
		: _MyDur(_Duration::zero())
		{	// check asserts
		static_assert(_Is_duration<_Duration>::value,
			"duration must be an instance of std::duration");
		}

	explicit time_point(const _Duration& _Other)
		: _MyDur(_Other)
		{	// construct from a duration
		}

	template<class _Duration2,
		class = typename enable_if<is_convertible<_Duration2,
			_Duration>::value,
			void>::type>
		time_point(const time_point<_Clock, _Duration2>& _Tp)
		: _MyDur(_Tp.time_since_epoch())
		{	// construct from another duration
		}

	_Duration time_since_epoch() const
		{	// get duration from epoch
		return (_MyDur);
		}

	time_point& operator+=(const _Duration& _Dur)
		{	// increment by duration
		_MyDur += _Dur;
		return (*this);
		}

	time_point& operator-=(const _Duration& _Dur)
		{	// decrement by duration
		_MyDur -= _Dur;
		return (*this);
		}

	static time_point (min)()
		{	// get minimum time point
		return (time_point((_Duration::min)()));
		}
	static time_point (max)()
		{	// get maximum time point
		return (time_point((_Duration::max)()));
		}

private:
	_Duration _MyDur;	// duration since the epoch
	};
}	// namespace chrono

	// CLASS TEMPLATE _Lcm (LEAST COMMON MULTIPLE)
template<intmax_t _Ax,
	intmax_t _Bx>
	struct _Lcm
	{   /* compute least common multiple of _Ax and _Bx */
	static const intmax_t _Gx = _Gcd<_Ax, _Bx>::value;
	static const intmax_t value = (_Ax / _Gx) * _Bx;
	};

	// CLASS TEMPLATE common_type SPECIALIZATIONS
template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2>
	struct common_type<
		chrono::duration<_Rep1, _Period1>,
		chrono::duration<_Rep2, _Period2> >
	{	// common type of two durations
	typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
		ratio<_Gcd<_Period1::num, _Period2::num>::value,
			_Lcm<_Period1::den, _Period2::den>::value> > type;
	};

template<class _Clock,
	class _Duration1,
	class _Duration2>
	struct common_type<
		chrono::time_point<_Clock, _Duration1>,
		chrono::time_point<_Clock, _Duration2> >
	{	// common type of two time points
	typedef chrono::time_point<
		_Clock, typename common_type<_Duration1, _Duration2>::type> type;
	};

namespace chrono {
	// duration ARITHMETIC
template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	typename common_type<
		duration<_Rep1, _Period1>,
		duration<_Rep2, _Period2> >::type
		operator+(
			const duration<_Rep1, _Period1>& _Left,
			const duration<_Rep2, _Period2>& _Right)
	{	// add two durations
	typedef typename common_type<
		duration<_Rep1, _Period1>,
		duration<_Rep2, _Period2> >::type _CD;
		return (_CD(_Left) += _Right);
		}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	typename common_type<
		duration<_Rep1, _Period1>,
		duration<_Rep2, _Period2> >::type
		operator-(
			const duration<_Rep1, _Period1>& _Left,
			const duration<_Rep2, _Period2>& _Right)
	{	// subtract two durations
	typedef typename common_type<
		duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _CD;
	return (_CD(_Left) -= _Right);
	}

template<class _Rep1,
	class _Period1,
	class _Rep2> inline
	typename enable_if<is_convertible<_Rep2,
		typename common_type<_Rep1, _Rep2>::type>::value,
		duration<typename common_type<_Rep1, _Rep2>::type, _Period1> >::type
		operator*(
			const duration<_Rep1, _Period1>& _Left,
			const _Rep2& _Right)
	{	// multiply duration by rep
	typedef typename common_type<_Rep1, _Rep2>::type _CR;
	return (duration<_CR, _Period1>(_Left) *= _Right);
	}

template<class _Rep1,
	class _Rep2,
	class _Period2> inline
	typename enable_if<is_convertible<_Rep1,
		typename common_type<_Rep1, _Rep2>::type>::value,
		duration<typename common_type<_Rep1, _Rep2>::type, _Period2> >::type
		operator*(
			const _Rep1& _Left,
			const duration<_Rep2, _Period2>& _Right)
	{	// multiply rep by duration
	return (_Right * _Left);
	}

template<class _Rep1,
	class _Period1,
	class _Rep2> inline
	typename enable_if<is_convertible<_Rep2,
		typename common_type<_Rep1, _Rep2>::type>::value
		&& !_Is_duration<_Rep2>::value,
		duration<typename common_type<_Rep1, _Rep2>::type, _Period1> >::type
		operator/(
			const duration<_Rep1, _Period1>& _Left,
			const _Rep2& _Right)
	{	// divide duration by rep
	typedef typename common_type<_Rep1, _Rep2>::type _CR;
	return (duration<_CR, _Period1>(_Left) /= _Right);
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	typename common_type<_Rep1, _Rep2>::type
		operator/(
			const duration<_Rep1, _Period1>& _Left,
			const duration<_Rep2, _Period2>& _Right)
	{	// divide duration by duration
	typedef typename common_type<
		duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _CD;
	return (_CD(_Left).count() / _CD(_Right).count());
	}

template<class _Rep1,
	class _Period1,
	class _Rep2> inline
	typename enable_if<is_convertible<_Rep2,
		typename common_type<_Rep1, _Rep2>::type>::value
		&& !_Is_duration<_Rep2>::value,
		duration<typename common_type<_Rep1, _Rep2>::type, _Period1> >::type
		operator%(
			const duration<_Rep1, _Period1>& _Left,
			const _Rep2& _Right)
	{	// divide duration by rep
	typedef typename common_type<_Rep1, _Rep2>::type _CR;
	return (duration<_CR, _Period1>(_Left) %= _Right);
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	typename common_type<_Rep1, _Rep2>::type
		operator%(
			const duration<_Rep1, _Period1>& _Left,
			const duration<_Rep2, _Period2>& _Right)
	{	// divide duration by duration
	typedef typename common_type<
		duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _CD;
	return (_CD(_Left).count() % _CD(_Right).count());
	}

	// duration COMPARISONS
template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	bool operator==(
		const duration<_Rep1, _Period1>& _Left,
		const duration<_Rep2, _Period2>& _Right)
	{	// test if duration == duration
	typedef typename common_type<
		duration<_Rep1, _Period1>,
		duration<_Rep2, _Period2> >::type _CT;
	return (_CT(_Left).count() == _CT(_Right).count());
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	bool operator!=(
		const duration<_Rep1, _Period1>& _Left,
		const duration<_Rep2, _Period2>& _Right)
	{	// test if duration != duration
	return (!(_Left == _Right));
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	bool operator<(
		const duration<_Rep1, _Period1>& _Left,
		const duration<_Rep2, _Period2>& _Right)
	{	// test if duration < duration
	typedef typename common_type<
		duration<_Rep1, _Period1>,
		duration<_Rep2, _Period2> >::type _CT;
	return (_CT(_Left).count() < _CT(_Right).count());
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	bool operator<=(
		const duration<_Rep1, _Period1>& _Left,
		const duration<_Rep2, _Period2>& _Right)
	{	// test if duration <= duration
	return (!(_Right < _Left));
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	bool operator>(
		const duration<_Rep1, _Period1>& _Left,
		const duration<_Rep2, _Period2>& _Right)
	{	// test if duration > duration
	return (_Right < _Left);
	}

template<class _Rep1,
	class _Period1,
	class _Rep2,
	class _Period2> inline
	bool operator>=(
		const duration<_Rep1, _Period1>& _Left,
		const duration<_Rep2, _Period2>& _Right)
	{	// test if duration >= duration
	return (!(_Left < _Right));
	}

	// duration_cast
template<class _To,
	class _Rep,
	class _Period> inline
	typename enable_if<_Is_duration<_To>::value,
		_To>::type
		duration_cast(const duration<_Rep, _Period>& _Dur)
	{	// convert duration to another duration
	typedef ratio_divide<_Period, typename _To::period> _CF;
	typedef typename _To::rep _ToRep;
	typedef typename common_type<_ToRep, _Rep, intmax_t>::type _CR;
	if (_CF::num == 1 && _CF::den == 1)
		return (_To(static_cast<_ToRep>(_Dur.count())));
	else if (_CF::num != 1 && _CF::den == 1)
		return (_To(static_cast<_ToRep>(
			static_cast<_CR>(_Dur.count()) * static_cast<_CR>(_CF::num))));
	else if (_CF::num == 1 && _CF::den != 1)
		return (_To(static_cast<_ToRep>(
			static_cast<_CR>(_Dur.count()) / static_cast<_CR>(_CF::den))));
	else
		return (_To(static_cast<_ToRep>(
			static_cast<_CR>(_Dur.count()) * static_cast<_CR>(_CF::num)
			/ static_cast<_CR>(_CF::den))));
	}

	// duration TYPEDEFS
typedef duration<long long, nano> nanoseconds;
typedef duration<long long, micro> microseconds;
typedef duration<long long, milli> milliseconds;
typedef duration<long long> seconds;
typedef duration<int, ratio<60> > minutes;
typedef duration<int, ratio<3600> > hours;

	// time_point ARITHMETIC
template<class _Clock,
	class _Duration,
	class _Rep,
	class _Period> inline
	time_point<_Clock,
		typename common_type<_Duration, duration<_Rep, _Period> >::type>
		operator+(
			const time_point<_Clock, _Duration>& _Left,
			const duration<_Rep, _Period>& _Right)
	{	// add duration to time_point
	typedef time_point<_Clock, typename common_type<
		_Duration, duration<_Rep, _Period> >::type> _RT;
	return (_RT(_Left) += _Right);
	}

template<class _Rep,
	class _Period,
	class _Clock,
	class _Duration> inline
	time_point<_Clock,
		typename common_type<duration<_Rep, _Period>, _Duration>::type>
		operator+(
			const duration<_Rep, _Period>& _Left,
			const time_point<_Clock, _Duration>& _Right)
	{	// add time_point to duration
	return (_Right + _Left);
	}

template<class _Clock,
	class _Duration,
	class _Rep,
	class _Period> inline
	time_point<_Clock,
		typename common_type<_Duration, duration<_Rep, _Period> >::type>
		operator-(
			const time_point<_Clock, _Duration>& _Left,
			const duration<_Rep, _Period>& _Right)
	{	// subtract duration from time_point
	typedef time_point<_Clock, typename common_type<
		_Duration, duration<_Rep, _Period> >::type> _RT;
	return (_RT(_Left) -= _Right);
	}

template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	typename common_type<_Duration1, _Duration2>::type
		operator-(
			const time_point<_Clock, _Duration1>& _Left,
			const time_point<_Clock, _Duration2>& _Right)
	{	// add time_point to time_point
	return (_Left.time_since_epoch() - _Right.time_since_epoch());
	}

	// time_point COMPARISONS
template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	bool operator==(
		const time_point<_Clock, _Duration1>& _Left,
		const time_point<_Clock, _Duration2>& _Right)
	{	// test for time_point == time_point
	return (_Left.time_since_epoch() == _Right.time_since_epoch());
	}

template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	bool operator!=(
		const time_point<_Clock, _Duration1>& _Left,
		const time_point<_Clock, _Duration2>& _Right)
	{	// test for time_point != time_point
	return (!(_Left == _Right));
	}

template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	bool operator<(
		const time_point<_Clock, _Duration1>& _Left,
		const time_point<_Clock, _Duration2>& _Right)
	{	// test for time_point < time_point
	return (_Left.time_since_epoch() < _Right.time_since_epoch());
	}

template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	bool operator<=(
		const time_point<_Clock, _Duration1>& _Left,
		const time_point<_Clock, _Duration2>& _Right)
	{	// test for time_point <= time_point
	return (!(_Right < _Left));
	}

template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	bool operator>(
		const time_point<_Clock, _Duration1>& _Left,
		const time_point<_Clock, _Duration2>& _Right)
	{	// test for time_point > time_point
	return (_Right < _Left);
	}

template<class _Clock,
	class _Duration1,
	class _Duration2> inline
	bool operator>=(
		const time_point<_Clock, _Duration1>& _Left,
		const time_point<_Clock, _Duration2>& _Right)
	{	// test for time_point >= time_point
	return !(_Left < _Right);
	}

	// time_point_cast
template<class _To,
	class _Clock,
	class _Duration> inline
		typename enable_if<_Is_duration<_To>::value,
			time_point<_Clock, _To> >::type
		time_point_cast(const time_point<_Clock, _Duration>& _Time)
	{	// convert time_point to duration
	static_assert(_Is_duration<_To>::value,
		"time_point_cast can only convert to a duration");

	return (time_point<_Clock, _To>(
		duration_cast<_To>(_Time.time_since_epoch())));
	}

	// CLOCKS
struct system_clock
	{	// wraps system clock
	typedef _LONGLONG rep;
	typedef ratio_multiply<ratio<_XTIME_NSECS_PER_TICK, 1>, nano> period;
	typedef chrono::duration<rep, period> duration;
	typedef chrono::time_point<system_clock> time_point;
	static const bool is_monotonic = false;	// retained
	static const bool is_steady = false;

	static time_point now() _NOEXCEPT
		{	// get current time
		return (time_point(duration(_Xtime_get_ticks())));
		}

// C conversions
	static time_t to_time_t(const time_point& _Time) _NOEXCEPT
		{	// convert to time_t
		return ((time_t)(_Time.time_since_epoch().count()
			/ _XTIME_TICKS_PER_TIME_T));
		}

	static time_point from_time_t(time_t _Tm) _NOEXCEPT
		{	// convert from time_t
		return (time_point(duration(_Tm * _XTIME_TICKS_PER_TIME_T)));
		}
	};

class steady_clock
	: public system_clock
	{	// wraps monotonic clock
public:
	static const bool is_monotonic = true;	// retained
	static const bool is_steady = true;
	};

typedef steady_clock monotonic_clock;	// retained
typedef system_clock high_resolution_clock;
}	// namespace chrono

	// HELPERS
template<class _Rep,
	class _Period> inline
	xtime _To_xtime(const chrono::duration<_Rep, _Period>& _Rel_time)
	{	// convert duration to xtime
	xtime _Xt;
	if (_Rel_time <= chrono::duration<_Rep, _Period>::zero())
		{	// negative or zero relative time, return zero
		_Xt.sec = 0;
		_Xt.nsec = 0;
		}
	else
		{	// positive relative time, convert
		chrono::nanoseconds _T0 =
			chrono::system_clock::now().time_since_epoch();
		_T0 += _Rel_time;
		_Xt.sec = chrono::duration_cast<chrono::seconds>(_T0).count();
		_T0 -= chrono::seconds(_Xt.sec);
		_Xt.nsec = (long)_T0.count();
		}
	return (_Xt);
	}
_STD_END
 #pragma pop_macro("new")
 #pragma warning(pop)
 #pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _CHRONO */

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