]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.h
Add support for time zones
[libs/core.git] / source / time / datetime.h
1 /* $Id$ */
2 #ifndef MSP_TIME_DATETIME_H_
3 #define MSP_TIME_DATETIME_H_
4
5 #include <string>
6 #include "timezone.h"
7 #include "types.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 DateTime
25 {
26 public:
27         DateTime(const TimeStamp &);
28         DateTime(const TimeStamp &, const TimeZone &);
29         DateTime(int, unsigned char, unsigned char);
30         DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
31         DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
32         
33         int           get_year() const   { return year; }
34         unsigned char get_month() const  { return month; }
35         unsigned char get_mday() const   { return mday; }
36         unsigned char get_hour() const   { return hour; }
37         unsigned char get_minute() const { return minute; }
38         unsigned char get_second() const { return second; }
39         unsigned      get_usec() const   { return usec; }
40
41         void add_days(int);
42         void set_timezone(const TimeZone &);
43         void convert_timezone(const TimeZone &);
44
45         DateTime operator+(const TimeDelta &) const;
46         DateTime &operator+=(const TimeDelta &);
47         DateTime operator-(const TimeDelta &) const;
48         DateTime &operator-=(const TimeDelta &);
49
50         bool operator==(const DateTime &d) const { return cmp(d)==0; }
51         bool operator!=(const DateTime &d) const { return cmp(d)!=0; }
52         bool operator<(const DateTime &d) const { return cmp(d)<0; }
53         bool operator<=(const DateTime &d) const { return cmp(d)<=0; }
54         bool operator>(const DateTime &d) const { return cmp(d)>0; }
55         bool operator>=(const DateTime &d) const { return cmp(d)>=0; }
56
57         int cmp(const DateTime &) const;
58         TimeStamp get_timestamp() const;
59         std::string format(const std::string &) const;
60         std::string format_rfc3339() const;
61 private:
62         int           year;
63         unsigned char month;
64         unsigned char mday;
65         unsigned char hour;
66         unsigned char minute;
67         unsigned char second;
68         unsigned      usec;
69         TimeZone      zone;
70
71         void init(const TimeStamp &);
72         void init(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
73         void add_raw(RawTime);
74         void normalize();
75         void validate() const;
76 };
77
78 } // namespace Time
79 } // namespace Msp
80
81 #endif