]> git.tdb.fi Git - libs/core.git/blob - source/time/datetime.h
MSVC compatibility fixes
[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 "types.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 public:
26         DateTime(const TimeStamp &);
27         DateTime(int, unsigned char, unsigned char);
28         DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
29         DateTime(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
30         
31         int           get_year() const   { return year; }
32         unsigned char get_month() const  { return month; }
33         unsigned char get_mday() const   { return mday; }
34         unsigned char get_hour() const   { return hour; }
35         unsigned char get_minute() const { return minute; }
36         unsigned char get_second() const { return second; }
37         unsigned      get_usec() const   { return usec; }
38
39         void add_days(int);
40
41         DateTime operator+(const TimeDelta &) const;
42         DateTime &operator+=(const TimeDelta &);
43
44         bool operator==(const DateTime &d) const { return cmp(d)==0; }
45         bool operator!=(const DateTime &d) const { return cmp(d)!=0; }
46         bool operator<(const DateTime &d) const { return cmp(d)<0; }
47         bool operator<=(const DateTime &d) const { return cmp(d)<=0; }
48         bool operator>(const DateTime &d) const { return cmp(d)>0; }
49         bool operator>=(const DateTime &d) const { return cmp(d)>=0; }
50
51         int cmp(const DateTime &) const;
52         TimeStamp get_timestamp() const;
53         std::string format(const std::string &) const;
54 private:
55         int           year;
56         unsigned char month;
57         unsigned char mday;
58         unsigned char hour;
59         unsigned char minute;
60         unsigned char second;
61         unsigned      usec;
62
63         void add_raw(RawTime);
64         void normalize();
65         void validate() const;
66 };
67
68 } // namespace Time
69 } // namespace Msp
70
71 #endif