#ifndef _utl_UTCDate_h_ #define _utl_UTCDate_h_ #include #include #include namespace utl { /** \class UTCDate UTCDate.h utl/UTCDate.h \author Darko Veberic \date 22 Jul 2009 \version $Id$ \ingroup time */ class UTCDate : public SafeBoolCast { public: enum Month { eJan = 1, eFeb = 2, eMar = 3, eApr = 4, eMay = 5, eJun = 6, eJul = 7, eAug = 8, eSep = 9, eOct = 10, eNov = 11, eDec = 12 }; UTCDate() : fYear(0), fMonth(0), fDay(0) { } /// Jan = 1, Dec = 12 UTCDate(const int year, const int month, const int day) { Set(year, month, day); } void Set(const int year, const int month, const int day); int GetYear() const { return fYear; } int GetMonth() const { return fMonth; } int GetDay() const { return fDay; } std::string GetInAugerFormat() const; std::string GetInXMLFormat() const { return GetInXMLFormatZone("Z"); } bool BoolCast() const { return fYear && fMonth && fDay; } bool operator==(const UTCDate& d) const { return fYear == d.fYear && fMonth == d.fMonth && fDay == d.fDay; } bool operator!=(const UTCDate& d) const { return !operator==(d); } static UTCDate GetGPSEpoch() { return UTCDate(1980, eJan, 6); } static UTCDate GetUnixEpoch() { return UTCDate(1970, eJan, 1); } /// Relative to Unix epoch (1 Jan 1970 00:00:00 UTC) without leap corrections std::time_t GetUnixSecond() const { return GetUnixSecond(fYear, fMonth, fDay, 0, 0, 0); } protected: static const char* const fgMonthNames[]; static std::time_t GetUnixSecond(const int year, const int month, const int day, const int hour, const int minute, const int second); std::string GetInXMLFormatZone(const char* const zone) const; std::istream& Parse(std::istream& is, const bool zone = true); private: static bool IsLeapYear(const int year) { return (!(year%4) && (year%100)) || !(year%400); } static int NumberOfDaysInMonth(const int year, const int month) { switch (month) { case eJan: case eMar: case eMay: case eJul: case eAug: case eOct: case eDec: return 31; case eFeb: return IsLeapYear(year) ? 29 : 28; case eApr: case eJun: case eSep: case eNov: return 30; default: return 0; } } int fYear; int fMonth; int fDay; friend std::istream& operator>>(std::istream&, UTCDate&); }; /// output in XML schema type "Date" (UTC) compliant format inline std::ostream& operator<<(std::ostream& os, const UTCDate& date) { return os << date.GetInXMLFormat(); } /// read from XML schema type "Date" (any timezone) compliant format inline std::istream& operator>>(std::istream& is, UTCDate& date) { return date.Parse(is); } } #endif