///////////////////////////////////////////////// // Forward class TILx; class TOLx; class TTm; ///////////////////////////////////////////////// // Time-Units (round time to one of the buckets) typedef enum { tmuUndef, tmu1Sec, tmu1Min, tmu10Min, tmu15Min, tmu30Min, tmu1Hour, tmu2Hour, tmu4Hour, tmu6Hour, tmu12Hour, tmuDay, tmuWeek, // Sunday is the start of the week tmuMonth, tmuYear, tmuNodes, tmuEdges // wrap around time // tmuWrapHour, tmuWrapDay, tmuWrapWeek, tmuWrapMonth, tmuWrapYear } TTmUnit; ///////////////////////////////////////////////// // Time-Info class TTmInfo{ private: static bool InitP; static TStrV UsMonthNmV; static TStrV SiMonthNmV; static TStrV UsDayOfWeekNmV; static TStrV SiDayOfWeekNmV; static void InitMonthNmV(); static void InitDayOfWeekNmV(); static void EnsureInit(){ if (!InitP){InitMonthNmV(); InitDayOfWeekNmV(); InitP=true;}} public: static int GetMonthN(const TStr& MonthNm, const TLoc& Loc=lUs); static bool IsMonthNm(const TStr& MonthNm, const TLoc& Loc=lUs){ return GetMonthN(MonthNm, Loc)!=-1;} static TStr GetMonthNm(const int& MonthN, const TLoc& Loc=lUs); static int GetDayOfWeekN(const TStr& DayOfWeekNm, const TLoc& Loc=lUs); static bool IsDayOfWeekNm(const TStr& DayOfWeekNm, const TLoc& Loc=lUs){ return GetDayOfWeekN(DayOfWeekNm, Loc)!=-1;} static TStr GetDayOfWeekNm(const int& DayOfWeekN, const TLoc& Loc=lUs); static TStr GetHmFromMins(const int& Mins); static int GetTmUnitSecs(const TTmUnit& TmUnit); static TStr GetTmUnitStr(const TTmUnit& TmUnit); static TStr GetTmZoneDiffStr(const TStr& TmZoneStr); // day-of-week numbers static const int SunN; static const int MonN; static const int TueN; static const int WedN; static const int ThuN; static const int FriN; static const int SatN; // month numbers static const int JanN; static const int FebN; static const int MarN; static const int AprN; static const int MayN; static const int JunN; static const int JulN; static const int AugN; static const int SepN; static const int OctN; static const int NovN; static const int DecN; // time duration (msecs) static uint64 GetMinMSecs(){return 60*1000;} static uint64 GetHourMSecs(){return 60*60*1000;} static uint64 GetDayMSecs(){return 24*60*60*1000;} static uint64 GetWeekMSecs(){return 7*24*60*60*1000;} }; ///////////////////////////////////////////////// // Julian-Dates class TJulianDate{ public: static int LastJulianDate; /* last day to use Julian calendar */ static int LastJulianDateN; /* jdn of same */ static int GetJulianDateN(int d, int m, int y); static void GetCalendarDate(int jdn, int& dd, int& mm, int& yy); }; ///////////////////////////////////////////////// // Seconds-Time // NOTE: Time origin is relative to time zone. Regardless of computer time // zone second 0 maps to "Jan 1 1970 00:00" (so time zone information is lost) class TSecTm { private: TUInt AbsSecs; // pretend that local time zone is UTC private: // functions that interact with C time functions (time.h) static bool GetTmSec(const int& YearN, const int& MonthN, const int& DayN, const int& HourN, const int& MinN, const int& SecN, uint& AbsSec); static bool GetTmSec(struct tm& Tm, uint& AbsSec); static bool GetTmStruct(const uint& AbsSec, struct tm& Tm); static time_t MkGmTime(struct tm *t); // implementation of _mkgmtime public: TSecTm(): AbsSecs(TUInt::Mx){} explicit TSecTm(const uint& _AbsSecs): AbsSecs(_AbsSecs){} operator uint() const {return AbsSecs.Val;} TSecTm(const TSecTm& SecTm): AbsSecs(SecTm.AbsSecs){} TSecTm(const int& YearN, const int& MonthN, const int& DayN, const int& HourN=0, const int& MinN=0, const int& SecN=0); TSecTm(const TTm& Tm); explicit TSecTm(const PXmlTok& XmlTok); PXmlTok GetXmlTok() const; TSecTm(TSIn& SIn): AbsSecs(SIn){} void Load(TSIn& SIn){AbsSecs.Load(SIn);} void Save(TSOut& SOut) const {AbsSecs.Save(SOut);} TSecTm& operator=(const TSecTm& SecTm){ AbsSecs=SecTm.AbsSecs; return *this;} TSecTm& operator=(const uint& _AbsSecs){ AbsSecs=_AbsSecs; return *this;} TSecTm& operator+=(const uint& Secs){ IAssert(IsDef()); AbsSecs()+=Secs; return *this;} TSecTm& operator-=(const uint& Secs){ IAssert(IsDef()); AbsSecs()-=Secs; return *this;} bool operator==(const TSecTm& SecTm) const { return AbsSecs==SecTm.AbsSecs;} bool operator<(const TSecTm& SecTm) const { IAssert(IsDef()&&SecTm.IsDef()); return AbsSecs TSecTmV; typedef TKeyDat TSecTmStrKd; typedef TVec TSecTmStrKdV; ///////////////////////////////////////////////// // Time class TTm{ private: typedef struct { unsigned int Min:6; unsigned int Hour:5; unsigned int Day:5; unsigned int Month:4; unsigned int Year:12; } TTmDateTimeBits; typedef union { TTmDateTimeBits Bits; int Int; } TTmDateTime; private: TInt Year, Month, Day, DayOfWeek; TInt Hour, Min, Sec, MSec; public: TTm(): Year(-1), Month(-1), Day(-1), DayOfWeek(-1), Hour(-1), Min(-1), Sec(-1), MSec(-1){} TTm(const TTm& Tm): Year(Tm.Year), Month(Tm.Month), Day(Tm.Day), DayOfWeek(Tm.DayOfWeek), Hour(Tm.Hour), Min(Tm.Min), Sec(Tm.Sec), MSec(Tm.MSec){} TTm( const int& _Year, const int& _Month, const int& _Day, const int& _DayOfWeek=-1, const int& _Hour=0, const int& _Min=0, const int& _Sec=0, const int& _MSec=0): Year(_Year), Month(_Month), Day(_Day), DayOfWeek(_DayOfWeek), Hour(_Hour), Min(_Min), Sec(_Sec), MSec(_MSec){} TTm(const TSecTm& SecTm): Year(SecTm.GetYearN()), Month(SecTm.GetMonthN()), Day(SecTm.GetDayN()), DayOfWeek(SecTm.GetDayOfWeekN()), Hour(SecTm.GetHourN()), Min(SecTm.GetMinN()), Sec(SecTm.GetSecN()), MSec(0){} ~TTm(){} TTm(TSIn& SIn): Year(SIn), Month(SIn), Day(SIn), DayOfWeek(SIn), Hour(SIn), Min(SIn), Sec(SIn), MSec(SIn){} void Save(TSOut& SOut) const { Year.Save(SOut); Month.Save(SOut); Day.Save(SOut); DayOfWeek.Save(SOut); Hour.Save(SOut); Min.Save(SOut); Sec.Save(SOut); MSec.Save(SOut);} TTm& operator=(const TTm& Tm){ Year=Tm.Year; Month=Tm.Month; Day=Tm.Day; DayOfWeek=Tm.DayOfWeek; Hour=Tm.Hour; Min=Tm.Min; Sec=Tm.Sec; MSec=Tm.MSec; return *this;} bool operator==(const TTm& Tm) const { return (Year==Tm.Year)&&(Month==Tm.Month)&&(Day==Tm.Day)&& (Hour==Tm.Hour)&&(Min==Tm.Min)&&(Sec==Tm.Sec)&&(MSec==Tm.MSec);} bool operator<(const TTm& Tm) const { return (Year TTmV; typedef TPair TTmStrPr; typedef TVec TTmStrPrV; ///////////////////////////////////////////////// // Execution-Time class TExeTm{ private: clock_t LastTick; public: TExeTm(): LastTick(0) { Tick(); } TExeTm(const TExeTm& Tm): LastTick(Tm.LastTick) { } TExeTm& operator=(const TExeTm& Tm){ LastTick=Tm.LastTick; return *this;} void Tick(){LastTick=clock();} int GetTime(){return clock()-LastTick;} double GetSecs() const {return double(clock()-LastTick)/double(CLOCKS_PER_SEC);} const char* GetStr() const {return GetTmStr();} TStr GetStr2() const {return GetTmStr();} const char* GetTmStr() const { static char TmStr[32]; if (GetSecs() < 60) { sprintf(TmStr, "%.2fs", GetSecs()); } else if (GetSecs() < 3600) { sprintf(TmStr, "%02dm%02ds", int(GetSecs())/60, int(GetSecs())%60); } else { sprintf(TmStr, "%02dh%02dm", int(GetSecs())/3600, (int(GetSecs())%3600)/60); } return TmStr; } static char* GetCurTm(){ static TStr TmStr; TmStr=TSecTm::GetCurTm().GetTmStr(); return TmStr.CStr(); } }; ///////////////////////////////////////////////// // Time-Stop-Watch class TTmStopWatch { private: clock_t TmSoFar; bool RunningP; TExeTm ExeTm; UndefCopyAssign(TTmStopWatch); public: TTmStopWatch(const bool& Start = false): TmSoFar(0), RunningP(Start) { } void Start() { if (!RunningP) { RunningP = true; ExeTm.Tick(); } } void Stop() { if (RunningP) { RunningP = false; TmSoFar += ExeTm.GetTime(); } } void Reset(const bool& Start) { TmSoFar = 0; RunningP = Start; ExeTm.Tick(); } clock_t GetTime() { return TmSoFar + (RunningP ? ExeTm.GetTime() : 0); } double GetSec() { return double(GetTime()) / double(CLOCKS_PER_SEC); } int GetSecInt() { return TFlt::Round(GetSec()); } };