X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Fdatetime.h;h=a5d91cf3451d4b547e93698cabd1f191983b4b0f;hp=57ca89d8280535eea94efa301f750308688f9196;hb=HEAD;hpb=cd89068b8ebafdc0f888c1aa21498bb93b55d814 diff --git a/source/time/datetime.h b/source/time/datetime.h index 57ca89d..2336374 100644 --- a/source/time/datetime.h +++ b/source/time/datetime.h @@ -1,9 +1,10 @@ -/* $Id$ */ #ifndef MSP_TIME_DATETIME_H_ #define MSP_TIME_DATETIME_H_ #include -#include "types.h" +#include +#include "timezone.h" +#include "rawtime.h" namespace Msp { namespace Time { @@ -20,26 +21,43 @@ Due to the complex internal representation, arithmetic operations on a DateTime are relatively slow. For purposes of internal scheduling in a program, a TimeStamp is a better choice. */ -class DateTime +class MSPCORE_API DateTime { +private: + int year = 1970; + unsigned char month = 1; + unsigned char mday = 1; + unsigned char hour = 0; + unsigned char minute = 0; + unsigned char second = 0; + unsigned usec = 0; + TimeZone zone; + public: DateTime(const TimeStamp &); + DateTime(const TimeStamp &, const TimeZone &); DateTime(int, unsigned char, unsigned char); DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char); DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned); - - int get_year() const { return year; } - unsigned char get_month() const { return month; } - unsigned char get_mday() const { return mday; } - unsigned char get_hour() const { return hour; } + + static DateTime parse_rfc3339(const std::string &); + + int get_year() const { return year; } + unsigned char get_month() const { return month; } + unsigned char get_mday() const { return mday; } + unsigned char get_hour() const { return hour; } unsigned char get_minute() const { return minute; } unsigned char get_second() const { return second; } - unsigned get_usec() const { return usec; } + unsigned get_usec() const { return usec; } void add_days(int); + void set_timezone(const TimeZone &); + void convert_timezone(const TimeZone &); DateTime operator+(const TimeDelta &) const; DateTime &operator+=(const TimeDelta &); + DateTime operator-(const TimeDelta &) const; + DateTime &operator-=(const TimeDelta &); bool operator==(const DateTime &d) const { return cmp(d)==0; } bool operator!=(const DateTime &d) const { return cmp(d)!=0; } @@ -51,20 +69,19 @@ public: int cmp(const DateTime &) const; TimeStamp get_timestamp() const; std::string format(const std::string &) const; -private: - int year; - unsigned char month; - unsigned char mday; - unsigned char hour; - unsigned char minute; - unsigned char second; - unsigned usec; + std::string format_rfc3339() const; +private: void add_raw(RawTime); void normalize(); - void validate() const; }; +inline void operator<<(LexicalConverter &c, const DateTime &d) +{ c.result(d.format_rfc3339()); } + +inline void operator>>(const LexicalConverter &c, DateTime &d) +{ d = DateTime::parse_rfc3339(c.get()); } + } // namespace Time } // namespace Msp