]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.h
Drop copyright and license notices from source files
[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         int           get_year() const   { return year; }
47         unsigned char get_month() const  { return month; }
48         unsigned char get_mday() const   { return mday; }
49         unsigned char get_hour() const   { return hour; }
50         unsigned char get_minute() const { return minute; }
51         unsigned char get_second() const { return second; }
52         unsigned      get_usec() const   { return usec; }
53
54         void add_days(int);
55         void set_timezone(const TimeZone &);
56         void convert_timezone(const TimeZone &);
57
58         DateTime operator+(const TimeDelta &) const;
59         DateTime &operator+=(const TimeDelta &);
60         DateTime operator-(const TimeDelta &) const;
61         DateTime &operator-=(const TimeDelta &);
62
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         bool operator>=(const DateTime &d) const { return cmp(d)>=0; }
69
70         int cmp(const DateTime &) const;
71         TimeStamp get_timestamp() const;
72         std::string format(const std::string &) const;
73         std::string format_rfc3339() const;
74
75 private:
76         void add_raw(RawTime);
77         void normalize();
78 };
79
80 } // namespace Time
81 } // namespace Msp
82
83 #endif