#ifndef DATE_HEADER #define DATE_HEADER 1 ////////////////////////////////////////////////////////////// //! userlevel=Normal //! author="Charles Galambos" //! lib=Msys //! docentry="Basic Types.Misc;Utilities.File System" //! rcsid="$Id: Date.hh,v 1.19 2000/12/13 12:13:26 ees1cg Exp $" //! file="amma/StdType/System/Generic/Date.hh" //! date="07/05/98" #include "amma/Boolean.hh" #include "amma/StdType.hh" class StringC; class BinIStreamC; class BinOStreamC; //: Date & Time information. // SMALL OBJECT. // NB. The virtual timer uses the clock() system call. This // means virtual timing will only make sense for the first // 36 minutes. After that the timer will wrap. class DateC { public: inline DateC(); //: Default constructor. // Sets time to 0. inline DateC(BooleanT setval,BooleanT useVirt = FALSE); //: Constructor. // If 'setval' is FALSE an invalid date is created. // otherwise the date is set to now. // if useVirt is TRUE a virtual time is used. DateC(RealT val); //: Construct from a real in seconds. DateC(IntT year,IntT month,IntT day,IntT hour = 0,IntT min = 0,IntT sec = 0,IntT usec = 0); //: Constructer. DateC(istream &in); //: Construct from a stream inline DateC(long xsec,long xusec); //: Construct from two longs.. inline DateC(const DateC &val); //: Copy constructor DateC(const StringC &); //: String constructor // Expects data in sec:usec format // as produced by Text() method. static BooleanT IsLeapYear(int year); //: Is give year a leap year ? static int YearToDaysSince1970(int year); //: Convert year to days since 1970 inline BooleanT IsValid() const; //: Is a valid date ? inline BooleanT IsZero() const; //: Is time zero ? inline void SetInvalid(); //: Make this date invalid. void SetToNow(BooleanT useVirt = FALSE); //: Set value of variable to now! inline long MaxUSeconds() const { return 1000000; } //: Maximum mircro seconds. inline long Resolution() const; //: Get resolution of timer in microseconds. // NB. This assumes 100Hz by default. inline void NormalisePos(); //: Normalise time representation after addition inline void NormaliseNeg(); //: Normalise time representation after subtraction inline BooleanT operator==(const DateC &oth) const; //: Compair times. inline BooleanT operator!=(const DateC &oth) const; //: Compair times. inline BooleanT operator>(const DateC &oth) const; //: Compair times. inline BooleanT operator<(const DateC &oth) const; //: Compair times. inline BooleanT operator>=(const DateC &oth) const; //: Compair times. inline BooleanT operator<=(const DateC &oth) const; //: Compair times. inline DateC operator+(const DateC &oth) const; //: Add times. inline DateC operator-(const DateC &oth) const; //: Subtract times. inline const DateC &operator-=(const DateC &val); //: Inplace subtraction. inline const DateC &operator-=(double val); //: Inplace subtraction. inline const DateC &operator+=(const DateC &val); //: Inplace addition. inline const DateC &operator+=(double val); //: Inplace addition. StringC Text() const; //: Get the time in string form. // This currently prints the time in the form // sec:usec. StringC CTime() const; //: Returns results equivelent to calling ctime(). StringC CTimeShort() const; //: Returns a short string containing date/time. inline long USeconds() const; //: Get micro seconds. inline long TotalSeconds() const; //: Get total seconds. inline double Double() const; //: Get time in double form. IntT Seconds() const; //: Return number of seconds after minuite. 0,61 (61 for leap seconds.) IntT Minute() const; //: Get minute. IntT Hour() const; //: Hours since midnight. 0-23 IntT Month() const; //: Get month 1-31 IntT Year() const; //: Get year. IntT DayInMonth() const; //: Get day in month. 1,31 IntT DayInYear() const; //: Get day of year. 0-365 IntT DayInWeek() const; //: Get day of week. Since sunday, 0-6 BooleanT DaylightSaving() const; //: Are we daylight saveing ? // True = yes BooleanT Wait() const; //: Wait until this time. static BooleanT Sleep(RealT delay); //: Pause execution for 'delay' seconds. void Save(ostream &out) const; //: Write to a stream. private: long sec; // Seconds since 12:00 January 1, 1970. long usec; // microseconds. }; ostream &operator <<(ostream &out,const DateC &date); //: Stream operator. istream &operator >>(istream &in,DateC &date); //: Stream operator. BinOStreamC &operator <<(BinOStreamC &out,const DateC &date); //: Stream operator. BinIStreamC &operator >>(BinIStreamC &in,DateC &date); //: Stream operator. ////////////////////////////////////////////////////////// inline DateC::DateC(BooleanT setval,BooleanT useVirt) { if(setval) SetToNow(useVirt); else { sec = 0; usec = -1; } } inline DateC::DateC() : sec(0), usec(0) {} inline DateC::DateC(const DateC &val) : sec(val.sec), usec(val.usec) {} inline DateC::DateC(long xsec,long xusec) : sec(xsec), usec(xusec) {} inline long DateC::Resolution() const { return 10000; } inline BooleanT DateC::operator==(const DateC &oth) const { if(sec != oth.sec) return FALSE; return (usec == oth.usec); } inline BooleanT DateC::operator!=(const DateC &oth) const { return !operator==(oth); } inline BooleanT DateC::operator>(const DateC &oth) const { if(sec > oth.sec) return TRUE; if(sec < oth.sec) return FALSE; return (usec > oth.usec); } inline BooleanT DateC::operator<(const DateC &oth) const { if(sec < oth.sec) return TRUE; if(sec > oth.sec) return FALSE; return (usec < oth.usec); } inline BooleanT DateC::operator>=(const DateC &oth) const { if(sec > oth.sec) return TRUE; if(sec < oth.sec) return FALSE; return (usec >= oth.usec); } inline BooleanT DateC::operator<=(const DateC &oth) const { if(sec < oth.sec) return TRUE; if(sec > oth.sec) return FALSE; return (usec <= oth.usec); } inline void DateC::NormalisePos() { while(usec >= MaxUSeconds()) { usec -= MaxUSeconds(); sec++; } } inline void DateC::NormaliseNeg() { while(usec < 0) { usec += MaxUSeconds(); sec--; } } inline DateC DateC::operator+(const DateC &oth) const { DateC ret; ret.sec = sec + oth.sec; ret.usec = usec + oth.usec; ret.NormalisePos(); return ret; } inline DateC DateC::operator-(const DateC &oth) const { DateC ret; ret.sec = sec - oth.sec; ret.usec = usec - oth.usec; ret.NormaliseNeg(); return ret; } inline const DateC & DateC::operator-=(const DateC &val) { sec -= val.sec; usec -= val.usec; NormaliseNeg(); return *this; } inline const DateC & DateC::operator+=(const DateC &val) { sec += val.sec; usec += val.usec; NormalisePos(); return *this; } inline const DateC & DateC::operator+=(double val) { sec += (long) val; usec += ((long) ((RealT) val * 1000000) % 1000000); NormalisePos(); return *this; } inline const DateC & DateC::operator-=(double val) { sec -= (long) val; usec -= ((long) ((RealT) val * 1000000) % 1000000); NormaliseNeg(); return *this; } inline long DateC::USeconds() const { return usec; } inline long DateC::TotalSeconds() const { return sec; } inline double DateC::Double() const { return (double) sec + (((double)usec) / ((double) MaxUSeconds())); } inline BooleanT DateC::IsValid() const { return usec >= 0; } //: Is time zero ? inline BooleanT DateC::IsZero() const { return sec == 0 && usec == 0;} inline void DateC::SetInvalid() { usec = -1; } #endif