X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ftime%2Fdatetime.h;fp=source%2Ftime%2Fdatetime.h;h=4f439e6be7acde84557bf136ee8f05e2c5823239;hp=0000000000000000000000000000000000000000;hb=80bbee2f401b4af71cb1b80508bdb0d2bb61fa40;hpb=7ff2cc6e945c770c64d39089a2c27b33305495dd diff --git a/source/time/datetime.h b/source/time/datetime.h new file mode 100644 index 0000000..4f439e6 --- /dev/null +++ b/source/time/datetime.h @@ -0,0 +1,70 @@ +/* $Id$ */ +#ifndef MSP_TIME_DATETIME_H_ +#define MSP_TIME_DATETIME_H_ + +#include +#include + +namespace Msp { +namespace Time { + +class TimeDelta; +class TimeStamp; + +/** +Provides handling of arbitary dates and times. Can represent a moment of time +in the range of about ±2.1×10⁹ years. It can also be formatted into a string +for presentation to the user. + +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 +{ +public: + DateTime(const TimeStamp &); + DateTime(int32_t, uint8_t, uint8_t); + DateTime(int32_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t); + DateTime(int32_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint32_t); + + int32_t get_year() const { return year; } + uint8_t get_month() const { return month; } + uint8_t get_mday() const { return mday; } + uint8_t get_hour() const { return hour; } + uint8_t get_minute() const { return minute; } + uint8_t get_second() const { return second; } + uint32_t get_usec() const { return usec; } + + void add_days(int32_t); + + 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; } + bool operator<(const DateTime &d) const { return cmp(d)<0; } + bool operator<=(const DateTime &d) const { return cmp(d)<=0; } + bool operator>(const DateTime &d) const { return cmp(d)>0; } + bool operator>=(const DateTime &d) const { return cmp(d)>=0; } + + int cmp(const DateTime &) const; + TimeStamp get_timestamp() const; + std::string format(const std::string &) const; +private: + int32_t year; + uint8_t month; + uint8_t mday; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint32_t usec; + + void add_raw(int64_t); + void normalize(); +}; + +} // namespace Time +} // namespace Msp + +#endif