xref: /haiku/headers/os/support/DateTime.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 	static	bool				IsValid(const BTime& time);
46 	static	bool				IsValid(int32 hour, int32 minute, int32 second,
47 									int32 microsecond = 0);
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 	static	bool				IsValid(const BDate& date);
102 	static	bool				IsValid(int32 year, int32 month,
103 									int32 day);
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,
112 									int32* day) const;
113 
114 			void				AddDays(int32 days);
115 			void				AddYears(int32 years);
116 			void				AddMonths(int32 months);
117 
118 			int32				Day() const;
119 			int32				Year() const;
120 			int32				Month() const;
121 			int32				Difference(const BDate& date) const;
122 
123 			int32				DayOfWeek() const;
124 			int32				DayOfYear() const;
125 
126 			int32				WeekNumber() const;
127 			bool				IsLeapYear() const;
128 	static	bool				IsLeapYear(int32 year);
129 
130 			int32				DaysInYear() const;
131 			int32				DaysInMonth() const;
132 
133 			BString				ShortDayName() const;
134 	static	BString				ShortDayName(int32 day);
135 
136 			BString				ShortMonthName() const;
137 	static	BString				ShortMonthName(int32 month);
138 
139 			BString				LongDayName() const;
140 	static	BString				LongDayName(int32 day);
141 
142 			BString				LongMonthName() const;
143 	static	BString				LongMonthName(int32 month);
144 
145 			int32				DateToJulianDay() const;
146 	static	BDate				JulianDayToDate(int32 julianDay);
147 
148 			bool				operator!=(const BDate& date) const;
149 			bool				operator==(const BDate& date) const;
150 
151 			bool				operator<(const BDate& date) const;
152 			bool				operator<=(const BDate& date) const;
153 
154 			bool				operator>(const BDate& date) const;
155 			bool				operator>=(const BDate& date) const;
156 
157 private:
158 	static	int32				_DaysInMonth(int32 year, int32 month);
159 			bool				_SetDate(int32 year, int32 month, int32 day);
160 	static	int32				_DateToJulianDay(int32 year, int32 month,
161 									int32 day);
162 
163 private:
164 			int32				fDay;
165 			int32				fYear;
166 			int32				fMonth;
167 };
168 
169 
170 class BDateTime {
171 public:
172 								BDateTime();
173 								BDateTime(const BDate &date, const BTime &time);
174 								BDateTime(const BMessage* archive);
175 								~BDateTime();
176 
177 			status_t			Archive(BMessage* into) const;
178 
179 			bool				IsValid() const;
180 
181 	static	BDateTime			CurrentDateTime(time_type type);
182 			void				SetDateTime(const BDate &date, const BTime &time);
183 
184 			BDate&				Date();
185 			const BDate&		Date() const;
186 			void				SetDate(const BDate &date);
187 
188 			BTime&				Time();
189 			const BTime&		Time() const;
190 			void				SetTime(const BTime &time);
191 
192 			int32				Time_t() const;
193 			void				SetTime_t(uint32 seconds);
194 
195 			bool				operator!=(const BDateTime& dateTime) const;
196 			bool				operator==(const BDateTime& dateTime) const;
197 
198 			bool				operator<(const BDateTime& dateTime) const;
199 			bool				operator<=(const BDateTime& dateTime) const;
200 
201 			bool				operator>(const BDateTime& dateTime) const;
202 			bool				operator>=(const BDateTime& dateTime) const;
203 
204 private:
205 			BDate				fDate;
206 			BTime				fTime;
207 };
208 
209 
210 }	// namespace BPrivate
211 
212 
213 using BPrivate::time_type;
214 using BPrivate::B_GMT_TIME;
215 using BPrivate::B_LOCAL_TIME;
216 using BPrivate::diff_type;
217 using BPrivate::B_HOURS_DIFF;
218 using BPrivate::B_MINUTES_DIFF;
219 using BPrivate::B_SECONDS_DIFF;
220 using BPrivate::B_MILLISECONDS_DIFF;
221 using BPrivate::B_MICROSECONDS_DIFF;
222 using BPrivate::BTime;
223 using BPrivate::BDate;
224 using BPrivate::BDateTime;
225 
226 
227 #endif	// _DATE_TIME_H_
228