xref: /haiku/headers/os/support/DateTime.h (revision bf57c148f7787f0df15980976997c6dfb70ee067)
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 enum time_type {
18 	B_GMT_TIME,
19 	B_LOCAL_TIME
20 };
21 
22 
23 enum diff_type {
24 	B_HOURS_DIFF,
25 	B_MINUTES_DIFF,
26 	B_SECONDS_DIFF,
27 	B_MILLISECONDS_DIFF,
28 	B_MICROSECONDS_DIFF
29 };
30 
31 
32 class BTime {
33 public:
34 								BTime();
35 								BTime(const BTime& other);
36 								BTime(int32 hour, int32 minute, int32 second,
37 									int32 microsecond = 0);
38 								BTime(const BMessage* archive);
39 								~BTime();
40 
41 			status_t			Archive(BMessage* into) const;
42 
43 			bool				IsValid() const;
44 	static	bool				IsValid(const BTime& time);
45 	static	bool				IsValid(int32 hour, int32 minute, int32 second,
46 									int32 microsecond = 0);
47 
48 	static	BTime				CurrentTime(time_type type);
49 
50 			BTime				Time() const;
51 			bool				SetTime(const BTime& time);
52 			bool				SetTime(int32 hour, int32 minute, int32 second,
53 									int32 microsecond = 0);
54 
55 			BTime&				AddHours(int32 hours);
56 			BTime&				AddMinutes(int32 minutes);
57 			BTime&				AddSeconds(int32 seconds);
58 			BTime&				AddMilliseconds(int32 milliseconds);
59 			BTime&				AddMicroseconds(int32 microseconds);
60 
61 			int32				Hour() const;
62 			int32				Minute() const;
63 			int32				Second() const;
64 			int32				Millisecond() const;
65 			int32				Microsecond() const;
66 			bigtime_t			Difference(const BTime& time,
67 									diff_type type) const;
68 
69 			bool				operator!=(const BTime& time) const;
70 			bool				operator==(const BTime& time) const;
71 
72 			bool				operator<(const BTime& time) const;
73 			bool				operator<=(const BTime& time) const;
74 
75 			bool				operator>(const BTime& time) const;
76 			bool				operator>=(const BTime& time) const;
77 
78 private:
79 			bigtime_t			_Microseconds() const;
80 			BTime&				_AddMicroseconds(bigtime_t microseconds);
81 			bool				_SetTime(bigtime_t hour, bigtime_t minute,
82 									bigtime_t second, bigtime_t microsecond);
83 
84 private:
85 			bigtime_t			fMicroseconds;
86 };
87 
88 
89 class BDate {
90 public:
91 								BDate();
92 								BDate(const BDate& other);
93 								BDate(int32 year, int32 month, int32 day);
94 								BDate(const BMessage* archive);
95 								~BDate();
96 
97 			status_t			Archive(BMessage* into) const;
98 
99 			bool				IsValid() const;
100 	static	bool				IsValid(const BDate& date);
101 	static	bool				IsValid(int32 year, int32 month,
102 									int32 day);
103 
104 	static	BDate				CurrentDate(time_type type);
105 
106 			BDate				Date() const;
107 			bool				SetDate(const BDate& date);
108 
109 			bool				SetDate(int32 year, int32 month, int32 day);
110 			void				GetDate(int32* year, int32* month,
111 									int32* day) const;
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 			void				SetDay(int32 day);
123 			void				SetMonth(int32 month);
124 			void				SetYear(int32 year);
125 
126 			int32				DayOfWeek() const;
127 			int32				DayOfYear() const;
128 
129 			int32				WeekNumber() const;
130 			bool				IsLeapYear() const;
131 	static	bool				IsLeapYear(int32 year);
132 
133 			int32				DaysInYear() const;
134 			int32				DaysInMonth() const;
135 
136 			BString				ShortDayName() const;
137 	static	BString				ShortDayName(int32 day);
138 
139 			BString				ShortMonthName() const;
140 	static	BString				ShortMonthName(int32 month);
141 
142 			BString				LongDayName() const;
143 	static	BString				LongDayName(int32 day);
144 
145 			BString				LongMonthName() const;
146 	static	BString				LongMonthName(int32 month);
147 
148 			int32				DateToJulianDay() const;
149 	static	BDate				JulianDayToDate(int32 julianDay);
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 			bool				operator>(const BDate& date) const;
158 			bool				operator>=(const BDate& date) const;
159 
160 private:
161 	static	int32				_DaysInMonth(int32 year, int32 month);
162 			bool				_SetDate(int32 year, int32 month, int32 day);
163 	static	int32				_DateToJulianDay(int32 year, int32 month,
164 									int32 day);
165 
166 private:
167 			int32				fDay;
168 			int32				fYear;
169 			int32				fMonth;
170 };
171 
172 
173 class BDateTime {
174 public:
175 								BDateTime();
176 								BDateTime(const BDate &date, const BTime &time);
177 								BDateTime(const BMessage* archive);
178 								~BDateTime();
179 
180 			status_t			Archive(BMessage* into) const;
181 
182 			bool				IsValid() const;
183 
184 	static	BDateTime			CurrentDateTime(time_type type);
185 			void				SetDateTime(const BDate &date, const BTime &time);
186 
187 			BDate&				Date();
188 			const BDate&		Date() const;
189 			void				SetDate(const BDate &date);
190 
191 			BTime&				Time();
192 			const BTime&		Time() const;
193 			void				SetTime(const BTime &time);
194 
195 			time_t				Time_t() const;
196 			void				SetTime_t(time_t seconds);
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 			bool				operator>(const BDateTime& dateTime) const;
205 			bool				operator>=(const BDateTime& dateTime) const;
206 
207 private:
208 			BDate				fDate;
209 			BTime				fTime;
210 };
211 
212 }	// namespace BPrivate
213 
214 using BPrivate::time_type;
215 using BPrivate::B_GMT_TIME;
216 using BPrivate::B_LOCAL_TIME;
217 using BPrivate::diff_type;
218 using BPrivate::B_HOURS_DIFF;
219 using BPrivate::B_MINUTES_DIFF;
220 using BPrivate::B_SECONDS_DIFF;
221 using BPrivate::B_MILLISECONDS_DIFF;
222 using BPrivate::B_MICROSECONDS_DIFF;
223 using BPrivate::BTime;
224 using BPrivate::BDate;
225 using BPrivate::BDateTime;
226 
227 
228 #endif	// _DATE_TIME_H
229