/** * Copyright Mikael Högdahl - triyana@users.sourceforge.net * * This source is distributed under the terms of the Q Public License version 1.0, * created by Trolltech (www.trolltech.com). */ #ifndef MHDate_h #define MHDate_h #include #include "MHUtil.h" #include "MHVector.h" #include "MHString.h" /** * A class for handling date/time operations. * It can format date and times into many different * format. CreateTimeSerie can be called to create a * vector with date strings. Some of the operations will fail * if date is before 1970. */ class MHDate : public MH { public: enum DAYS { MONDAY = 1, TUESDAY, WENDSDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, WEEKDAY, WEEKEND, WEEK, MONTH, ALL, }; enum MONTHS { JANUARY = 1, FEBRUARY, MARS, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER, }; enum FORMAT { DATE = 1, DATE_, FULL_DATE, LONG_DATE, LONG_DATE_, SHORT_DATE, SHORT_DATE_, SHORT_DATE_NO_YEAR, SHORT_DATE_NO_YEAR_, TIME, SHORT_TIME, YEAR, WEEK_IN_YEAR, DAY_IN_YEAR, LOCAL_DATE_AND_TIME, LOCAL_DATE, LOCAL_TIME, NAME_OF_DAY, ABBR_OF_DAY, NAME_OF_MONTH, ABBR_OF_MONTH, }; enum SIZE { SECS_HOUR = 3600, SECSDAY = SECS_HOUR * 24, SECSWEEK = SECSDAY * 7, SECSYEAR = SECSDAY * 365, MAX_STRING_LEN = 20, }; MHDate (); MHDate (MHDate& Date); MHDate (const char* s); MHDate (int year, int month, int date, int hour = 0, int min = 0, int sec = 0); virtual ~MHDate () {;} MHDate& operator=(const MHDate&); bool operator==(MHDate& x) {return GetSecs() == x.GetSecs();} bool operator!=(MHDate& x) {return GetSecs() != x.GetSecs();} bool operator<(MHDate& x) {return GetSecs() < x.GetSecs();} bool operator<=(MHDate& x) {return GetSecs() <= x.GetSecs();} bool operator>(MHDate& x) {return GetSecs() > x.GetSecs();} bool operator>=(MHDate& x) {return GetSecs() >= x.GetSecs();} void AddDays (int nDays); const char* Class () {return "MHDate";} int Compare (MH* o, int type) {MHDate* d = (MHDate*)o; if (aSecs < d->aSecs) return -1; else if (aSecs == d->aSecs) return 0; else return 1;} static void ConvertSecsToTime (int Secs, MHString& s); static void CreateTimeSerie (const char* pStartDate, const char* pStopDate, int TimeType, MHVector* in, MHVector* out, int StringType = LONG_DATE); static int DiffDays (MHDate* d1, MHDate* d2) {return (d1->GetSecs() - d2->GetSecs()) / SECSDAY;} const char* Get (FORMAT nDateType); void Get (FORMAT nDateType, MHString& s); int GetDate (); int GetLongYear (); int GetMonth (); int GetSecs (); int GetSecsToday (); int GetWeekday (); int GetYear (); void GotoNextWeekday (int nWeekday); void GotoWeekday (int nWeekday); bool IsLeapYear (); static bool IsLeapYear (int year); static void LongToVeryLong (const char* in, char* out); static double NoYears (const char* d1, const char* d2); static MHString* ParseString (MHString* dateString); static long Reference (); void Reset (); void Set (int nSecs = 0); void Set (const char* s); void Set (int year, int month, int date, int hour = 0, int min = 0, int sec = 0); static bool ValidDate (const char* date); private: MHString aDate; struct tm aTm; time_t aSecs; }; /** * Construct a new date object with date and time set to now */ inline MHDate::MHDate () : MH(){ aDate = 0; Set(0); } /** * Construct new object with date set to string. * @param const char* - Date in the form of "YYYYMMDD" */ inline MHDate::MHDate (const char* s) : MH(){ aDate = 0; Set (s); } /** * Construct new object. * @param int - Year * @param int - Month * @param int - Day in month * @param int - Hour, can be omitted * @param int - Min, can be omitted * @param int - Sec, can be omitted */ inline MHDate::MHDate (int year, int month, int date, int hour, int min, int sec) : MH() { aDate = 0; Set (year, month, date, hour, min, sec); } /** * Add days to current date. * @param int - Number of days to add */ inline void MHDate::AddDays (int nDays) { Set ((int)aSecs + (nDays * SECSDAY)); } /** * Count day in month. * @return int - Day number */ inline int MHDate::GetDate () { return aTm.tm_mday; } /** * Return current year. * @return int - Year */ inline int MHDate::GetLongYear () { return aTm.tm_year + 1900; } /** * Get current month number, one of JANUARY - DECEMBER * @return int - Month number */ inline int MHDate::GetMonth () { return (aTm.tm_mon + 1); } /** * Count number of seconds since 1970. * @return int - Seconds */ inline int MHDate::GetSecs () { return (int) aSecs; } /** * Count number of seconds since midnight. * @return int - Seconds */ inline int MHDate::GetSecsToday () { return ((aTm.tm_hour * 3600) + (aTm.tm_min * 60) + aTm.tm_sec); } /** * Return current year. * @return int - Year */ inline int MHDate::GetYear () { return aTm.tm_year; } /** * Check if year is leap year. * @return bool - true if leap year */ inline bool MHDate::IsLeapYear () { return MHDate::IsLeapYear (GetYear()); } /** * Set date/time set to now. */ inline void MHDate::Reset () { Set (0); } #endif