xref: /haiku/headers/os/support/DateTime.h (revision 45b01eb84189a623981270a02d21c7a8cb93a9e1)
1 /*
2  * Copyright 2007-2010, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _DATE_TIME_H_
6 #define _DATE_TIME_H_
7 
8 
9 #include <String.h>
10 
11 
12 class BMessage;
13 
14 
15 namespace BPrivate {
16 
17 
18 enum time_type {
19 	B_GMT_TIME,
20 	B_LOCAL_TIME
21 };
22 
23 
24 enum diff_type {
25 	B_HOURS_DIFF,
26 	B_MINUTES_DIFF,
27 	B_SECONDS_DIFF,
28 	B_MILLISECONDS_DIFF,
29 	B_MICROSECONDS_DIFF
30 };
31 
32 
33 class BTime {
34 public:
35 								BTime();
36 								BTime(const BTime& other);
37 								BTime(int32 hour, int32 minute, int32 second,
38 									int32 microsecond = 0);
39 								BTime(const BMessage* archive);
40 								~BTime();
41 
42 			status_t			Archive(BMessage* into) const;
43 
44 			bool				IsValid() const;
45 			bool				IsValid(const BTime& time) const;
46 			bool				IsValid(int32 hour, int32 minute, int32 second,
47 									int32 microsecond = 0) const;
48 
49 	static	BTime				CurrentTime(time_type type);
50 
51 			BTime				Time() const;
52 			bool				SetTime(const BTime& time);
53 			bool				SetTime(int32 hour, int32 minute, int32 second,
54 									int32 microsecond = 0);
55 
56 			BTime&				AddHours(int32 hours);
57 			BTime&				AddMinutes(int32 minutes);
58 			BTime&				AddSeconds(int32 seconds);
59 			BTime&				AddMilliseconds(int32 milliseconds);
60 			BTime&				AddMicroseconds(int32 microseconds);
61 
62 			int32				Hour() const;
63 			int32				Minute() const;
64 			int32				Second() const;
65 			int32				Millisecond() const;
66 			int32				Microsecond() const;
67 			bigtime_t			Difference(const BTime& time,
68 									diff_type type) const;
69 
70 			bool				operator!=(const BTime& time) const;
71 			bool				operator==(const BTime& time) const;
72 
73 			bool				operator<(const BTime& time) const;
74 			bool				operator<=(const BTime& time) const;
75 
76 			bool				operator>(const BTime& time) const;
77 			bool				operator>=(const BTime& time) const;
78 
79 private:
80 			bigtime_t			_Microseconds() const;
81 			BTime&				_AddMicroseconds(bigtime_t microseconds);
82 			bool				_SetTime(bigtime_t hour, bigtime_t minute,
83 									bigtime_t second, bigtime_t microsecond);
84 
85 private:
86 			bigtime_t			fMicroseconds;
87 };
88 
89 
90 class BDate {
91 public:
92 								BDate();
93 								BDate(const BDate& other);
94 								BDate(int32 year, int32 month, int32 day);
95 								BDate(const BMessage* archive);
96 								~BDate();
97 
98 			status_t			Archive(BMessage* into) const;
99 
100 			bool				IsValid() const;
101 			bool				IsValid(const BDate& date) const;
102 			bool				IsValid(int32 year, int32 month,
103 									int32 day) const;
104 
105 	static	BDate				CurrentDate(time_type type);
106 
107 			BDate				Date() const;
108 			bool				SetDate(const BDate& date);
109 
110 			bool				SetDate(int32 year, int32 month, int32 day);
111 			void				GetDate(int32* year, int32* month, int32* day);
112 
113 			void				AddDays(int32 days);
114 			void				AddYears(int32 years);
115 			void				AddMonths(int32 months);
116 
117 			int32				Day() const;
118 			int32				Year() const;
119 			int32				Month() const;
120 			int32				Difference(const BDate& date) const;
121 
122 			int32				DayOfWeek() const;
123 			int32				DayOfYear() const;
124 
125 			int32				WeekNumber() const;
126 			bool				IsLeapYear(int32 year) const;
127 
128 			int32				DaysInYear() const;
129 			int32				DaysInMonth() const;
130 
131 			BString				ShortDayName() const;
132 	static	BString				ShortDayName(int32 day);
133 
134 			BString				ShortMonthName() const;
135 	static	BString				ShortMonthName(int32 month);
136 
137 			BString				LongDayName() const;
138 	static	BString				LongDayName(int32 day);
139 
140 			BString				LongMonthName() const;
141 	static	BString				LongMonthName(int32 month);
142 
143 			int32				DateToJulianDay() const;
144 	static	BDate				JulianDayToDate(int32 julianDay);
145 
146 			bool				operator!=(const BDate& date) const;
147 			bool				operator==(const BDate& date) const;
148 
149 			bool				operator<(const BDate& date) const;
150 			bool				operator<=(const BDate& date) const;
151 
152 			bool				operator>(const BDate& date) const;
153 			bool				operator>=(const BDate& date) const;
154 
155 private:
156 			int32				_DaysInMonth(int32 year, int32 month) const;
157 			bool				_SetDate(int32 year, int32 month, int32 day);
158 			int32				_DateToJulianDay(int32 year, int32 month,
159 									int32 day) const;
160 
161 private:
162 			int32				fDay;
163 			int32				fYear;
164 			int32				fMonth;
165 };
166 
167 
168 class BDateTime {
169 public:
170 								BDateTime();
171 								BDateTime(const BDate &date, const BTime &time);
172 								BDateTime(const BMessage* archive);
173 								~BDateTime();
174 
175 			status_t			Archive(BMessage* into) const;
176 
177 			bool				IsValid() const;
178 
179 	static	BDateTime			CurrentDateTime(time_type type);
180 			void				SetDateTime(const BDate &date, const BTime &time);
181 
182 			BDate&				Date();
183 			const BDate&		Date() const;
184 			void				SetDate(const BDate &date);
185 
186 			BTime&				Time();
187 			const BTime&		Time() const;
188 			void				SetTime(const BTime &time);
189 
190 			int32				Time_t() const;
191 			void				SetTime_t(uint32 seconds);
192 
193 			bool				operator!=(const BDateTime& dateTime) const;
194 			bool				operator==(const BDateTime& dateTime) const;
195 
196 			bool				operator<(const BDateTime& dateTime) const;
197 			bool				operator<=(const BDateTime& dateTime) const;
198 
199 			bool				operator>(const BDateTime& dateTime) const;
200 			bool				operator>=(const BDateTime& dateTime) const;
201 
202 private:
203 			BDate				fDate;
204 			BTime				fTime;
205 };
206 
207 
208 }	// namespace BPrivate
209 
210 
211 using BPrivate::time_type;
212 using BPrivate::B_GMT_TIME;
213 using BPrivate::B_LOCAL_TIME;
214 using BPrivate::diff_type;
215 using BPrivate::B_HOURS_DIFF;
216 using BPrivate::B_MINUTES_DIFF;
217 using BPrivate::B_SECONDS_DIFF;
218 using BPrivate::B_MILLISECONDS_DIFF;
219 using BPrivate::B_MICROSECONDS_DIFF;
220 using BPrivate::BTime;
221 using BPrivate::BDate;
222 using BPrivate::BDateTime;
223 
224 
225 #endif	// _DATE_TIME_H_
226