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