]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.h
Move non-oneliner functions out of RefPtr class declaration
[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;
27         unsigned char month;
28         unsigned char mday;
29         unsigned char hour;
30         unsigned char minute;
31         unsigned char second;
32         unsigned usec;
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 private:
42         void init(const TimeStamp &);
43         void init(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
44
45 public:
46         static DateTime parse_rfc3339(const std::string &);
47
48         int get_year() const { return year; }
49         unsigned char get_month() const { return month; }
50         unsigned char get_mday() const { return mday; }
51         unsigned char get_hour() const { return hour; }
52         unsigned char get_minute() const { return minute; }
53         unsigned char get_second() const { return second; }
54         unsigned get_usec() const { return usec; }
55
56         void add_days(int);
57         void set_timezone(const TimeZone &);
58         void convert_timezone(const TimeZone &);
59
60         DateTime operator+(const TimeDelta &) const;
61         DateTime &operator+=(const TimeDelta &);
62         DateTime operator-(const TimeDelta &) const;
63         DateTime &operator-=(const TimeDelta &);
64
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         bool operator<=(const DateTime &d) const { return cmp(d)<=0; }
69         bool operator>(const DateTime &d) const { return cmp(d)>0; }
70         bool operator>=(const DateTime &d) const { return cmp(d)>=0; }
71
72         int cmp(const DateTime &) const;
73         TimeStamp get_timestamp() const;
74         std::string format(const std::string &) const;
75         std::string format_rfc3339() const;
76
77 private:
78         void add_raw(RawTime);
79         void normalize();
80 };
81
82 inline void operator<<(LexicalConverter &c, const DateTime &d)
83 { c.result(d.format_rfc3339()); }
84
85 inline void operator>>(const LexicalConverter &c, DateTime &d)
86 { d = DateTime::parse_rfc3339(c.get()); }
87
88 } // namespace Time
89 } // namespace Msp
90
91 #endif