]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.h
Add move semantics to Variant
[libs/core.git] / source / time / datetime.h
1 #ifndef MSP_TIME_DATETIME_H_
2 #define MSP_TIME_DATETIME_H_
3
4 #include <string>
5 #include "timezone.h"
6 #include "rawtime.h"
7
8 namespace Msp {
9 namespace Time {
10
11 class TimeDelta;
12 class TimeStamp;
13
14 /**
15 Provides handling of arbitary dates and times.  Can represent a moment of time
16 in the range of about ±2.1×10⁹ years.  It can also be formatted into a string
17 for presentation to the user.
18
19 Due to the complex internal representation, arithmetic operations on a DateTime
20 are relatively slow.  For purposes of internal scheduling in a program, a
21 TimeStamp is a better choice.
22 */
23 class DateTime
24 {
25 private:
26         int year = 1970;
27         unsigned char month = 1;
28         unsigned char mday = 1;
29         unsigned char hour = 0;
30         unsigned char minute = 0;
31         unsigned char second = 0;
32         unsigned usec = 0;
33         TimeZone zone;
34
35 public:
36         DateTime(const TimeStamp &);
37         DateTime(const TimeStamp &, const TimeZone &);
38         DateTime(int, unsigned char, unsigned char);
39         DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
40         DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
41
42         static DateTime parse_rfc3339(const std::string &);
43
44         int get_year() const { return year; }
45         unsigned char get_month() const { return month; }
46         unsigned char get_mday() const { return mday; }
47         unsigned char get_hour() const { return hour; }
48         unsigned char get_minute() const { return minute; }
49         unsigned char get_second() const { return second; }
50         unsigned get_usec() const { return usec; }
51
52         void add_days(int);
53         void set_timezone(const TimeZone &);
54         void convert_timezone(const TimeZone &);
55
56         DateTime operator+(const TimeDelta &) const;
57         DateTime &operator+=(const TimeDelta &);
58         DateTime operator-(const TimeDelta &) const;
59         DateTime &operator-=(const TimeDelta &);
60
61         bool operator==(const DateTime &d) const { return cmp(d)==0; }
62         bool operator!=(const DateTime &d) const { return cmp(d)!=0; }
63         bool operator<(const DateTime &d) const { return cmp(d)<0; }
64         bool operator<=(const DateTime &d) const { return cmp(d)<=0; }
65         bool operator>(const DateTime &d) const { return cmp(d)>0; }
66         bool operator>=(const DateTime &d) const { return cmp(d)>=0; }
67
68         int cmp(const DateTime &) const;
69         TimeStamp get_timestamp() const;
70         std::string format(const std::string &) const;
71         std::string format_rfc3339() const;
72
73 private:
74         void add_raw(RawTime);
75         void normalize();
76 };
77
78 inline void operator<<(LexicalConverter &c, const DateTime &d)
79 { c.result(d.format_rfc3339()); }
80
81 inline void operator>>(const LexicalConverter &c, DateTime &d)
82 { d = DateTime::parse_rfc3339(c.get()); }
83
84 } // namespace Time
85 } // namespace Msp
86
87 #endif