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