/src/poco/Foundation/include/Poco/LocalDateTime.h
Line | Count | Source |
1 | | // |
2 | | // LocalDateTime.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: DateTime |
6 | | // Module: LocalDateTime |
7 | | // |
8 | | // Definition of the LocalDateTime class. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_LocalDateTime_INCLUDED |
18 | | #define Foundation_LocalDateTime_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include "Poco/DateTime.h" |
23 | | |
24 | | |
25 | | namespace Poco { |
26 | | |
27 | | |
28 | | class Foundation_API LocalDateTime |
29 | | /// This class represents an instant in local time |
30 | | /// (as opposed to UTC), expressed in years, months, days, |
31 | | /// hours, minutes, seconds and milliseconds based on the |
32 | | /// Gregorian calendar. |
33 | | /// |
34 | | /// In addition to the date and time, the class also |
35 | | /// maintains a time zone differential, which denotes |
36 | | /// the difference in seconds from UTC to local time, |
37 | | /// i.e. UTC = local time - time zone differential. |
38 | | /// |
39 | | /// Although LocalDateTime supports relational and arithmetic |
40 | | /// operators, all date/time comparisons and date/time arithmetic |
41 | | /// should be done in UTC, using the DateTime or Timestamp |
42 | | /// class for better performance. The relational operators |
43 | | /// normalize the dates/times involved to UTC before carrying out |
44 | | /// the comparison. |
45 | | /// |
46 | | /// The time zone differential is based on the input date and |
47 | | /// time and current time zone. A number of constructors accept |
48 | | /// an explicit time zone differential parameter. These should |
49 | | /// not be used since daylight savings time processing is impossible |
50 | | /// since the time zone is unknown. Each of the constructors |
51 | | /// accepting a tzd parameter have been marked as deprecated and |
52 | | /// may be removed in a future revision. |
53 | | { |
54 | | public: |
55 | | LocalDateTime(); |
56 | | /// Creates a LocalDateTime with the current date/time |
57 | | /// for the current time zone. |
58 | | |
59 | | LocalDateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0); |
60 | | /// Creates a LocalDateTime for the given Gregorian local date and time. |
61 | | /// * year is from 0 to 9999. |
62 | | /// * month is from 1 to 12. |
63 | | /// * day is from 1 to 31. |
64 | | /// * hour is from 0 to 23. |
65 | | /// * minute is from 0 to 59. |
66 | | /// * second is from 0 to 60 (allowing leap seconds). |
67 | | /// * millisecond is from 0 to 999. |
68 | | /// * microsecond is from 0 to 999. |
69 | | |
70 | | LocalDateTime(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond); |
71 | | /// Creates a LocalDateTime for the given Gregorian date and time in the |
72 | | /// time zone denoted by the time zone differential in tzd. |
73 | | /// * tzd is in seconds. |
74 | | /// * year is from 0 to 9999. |
75 | | /// * month is from 1 to 12. |
76 | | /// * day is from 1 to 31. |
77 | | /// * hour is from 0 to 23. |
78 | | /// * minute is from 0 to 59. |
79 | | /// * second is from 0 to 60 (allowing leap seconds). |
80 | | /// * millisecond is from 0 to 999. |
81 | | /// * microsecond is from 0 to 999. |
82 | | |
83 | | LocalDateTime(const DateTime& dateTime); |
84 | | /// Creates a LocalDateTime from the UTC time given in dateTime, |
85 | | /// using the time zone differential of the current time zone. |
86 | | |
87 | | LocalDateTime(int tzd, const DateTime& dateTime); |
88 | | /// Creates a LocalDateTime from the UTC time given in dateTime, |
89 | | /// using the given time zone differential. Adjusts dateTime |
90 | | /// for the given time zone differential. |
91 | | |
92 | | LocalDateTime(int tzd, const DateTime& dateTime, bool adjust); |
93 | | /// Creates a LocalDateTime from the UTC time given in dateTime, |
94 | | /// using the given time zone differential. If adjust is true, |
95 | | /// adjusts dateTime for the given time zone differential. |
96 | | |
97 | | LocalDateTime(double julianDay); |
98 | | /// Creates a LocalDateTime for the given Julian day in the local time zone. |
99 | | |
100 | | LocalDateTime(int tzd, double julianDay); |
101 | | /// Creates a LocalDateTime for the given Julian day in the time zone |
102 | | /// denoted by the time zone differential in tzd. |
103 | | |
104 | | LocalDateTime(const LocalDateTime& dateTime); |
105 | | /// Copy constructor. Creates the LocalDateTime from another one. |
106 | | |
107 | | ~LocalDateTime(); |
108 | | /// Destroys the LocalDateTime. |
109 | | |
110 | | LocalDateTime& operator = (const LocalDateTime& dateTime); |
111 | | /// Assigns another LocalDateTime. |
112 | | |
113 | | LocalDateTime& operator = (const Timestamp& timestamp); |
114 | | /// Assigns a timestamp |
115 | | |
116 | | LocalDateTime& operator = (double julianDay); |
117 | | /// Assigns a Julian day in the local time zone. |
118 | | |
119 | | LocalDateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0); |
120 | | /// Assigns a Gregorian local date and time. |
121 | | /// * year is from 0 to 9999. |
122 | | /// * month is from 1 to 12. |
123 | | /// * day is from 1 to 31. |
124 | | /// * hour is from 0 to 23. |
125 | | /// * minute is from 0 to 59. |
126 | | /// * second is from 0 to 60 (allowing leap seconds). |
127 | | /// * millisecond is from 0 to 999. |
128 | | /// * microsecond is from 0 to 999. |
129 | | |
130 | | LocalDateTime& assign(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microseconds); |
131 | | /// Assigns a Gregorian local date and time in the time zone denoted by |
132 | | /// the time zone differential in tzd. |
133 | | /// * tzd is in seconds. |
134 | | /// * year is from 0 to 9999. |
135 | | /// * month is from 1 to 12. |
136 | | /// * day is from 1 to 31. |
137 | | /// * hour is from 0 to 23. |
138 | | /// * minute is from 0 to 59. |
139 | | /// * second is from 0 to 60 (allowing leap seconds). |
140 | | /// * millisecond is from 0 to 999. |
141 | | /// * microsecond is from 0 to 999. |
142 | | |
143 | | LocalDateTime& assign(int tzd, double julianDay); |
144 | | /// Assigns a Julian day in the time zone denoted by the |
145 | | /// time zone differential in tzd. |
146 | | |
147 | | void swap(LocalDateTime& dateTime); |
148 | | /// Swaps the LocalDateTime with another one. |
149 | | |
150 | | int year() const; |
151 | | /// Returns the year. |
152 | | |
153 | | int month() const; |
154 | | /// Returns the month (1 to 12). |
155 | | |
156 | | int week(int firstDayOfWeek = DateTime::MONDAY) const; |
157 | | /// Returns the week number within the year. |
158 | | /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1). |
159 | | /// The returned week number will be from 0 to 53. Week number 1 is the week |
160 | | /// containing January 4. This is in accordance to ISO 8601. |
161 | | /// |
162 | | /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started |
163 | | /// on a Saturday, week 1 will be the week starting on Monday, January 3. |
164 | | /// January 1 and 2 will fall within week 0 (or the last week of the previous year). |
165 | | /// |
166 | | /// For 2007, which starts on a Monday, week 1 will be the week starting on Monday, January 1. |
167 | | /// There will be no week 0 in 2007. |
168 | | |
169 | | int day() const; |
170 | | /// Returns the day within the month (1 to 31). |
171 | | |
172 | | int dayOfWeek() const; |
173 | | /// Returns the weekday (0 to 6, where |
174 | | /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday). |
175 | | |
176 | | int dayOfYear() const; |
177 | | /// Returns the number of the day in the year. |
178 | | /// January 1 is 1, February 1 is 32, etc. |
179 | | |
180 | | int hour() const; |
181 | | /// Returns the hour (0 to 23). |
182 | | |
183 | | int hourAMPM() const; |
184 | | /// Returns the hour (0 to 12). |
185 | | |
186 | | bool isAM() const; |
187 | | /// Returns true if hour < 12; |
188 | | |
189 | | bool isPM() const; |
190 | | /// Returns true if hour >= 12. |
191 | | |
192 | | int minute() const; |
193 | | /// Returns the minute (0 to 59). |
194 | | |
195 | | int second() const; |
196 | | /// Returns the second (0 to 59). |
197 | | |
198 | | int millisecond() const; |
199 | | /// Returns the millisecond (0 to 999) |
200 | | |
201 | | int microsecond() const; |
202 | | /// Returns the microsecond (0 to 999) |
203 | | |
204 | | double julianDay() const; |
205 | | /// Returns the Julian day for the date. |
206 | | |
207 | | int tzd() const; |
208 | | /// Returns the time zone differential. |
209 | | |
210 | | DateTime utc() const; |
211 | | /// Returns the UTC equivalent for the local date and time. |
212 | | |
213 | | Timestamp timestamp() const; |
214 | | /// Returns the date and time expressed as a Timestamp. |
215 | | |
216 | | Timestamp::UtcTimeVal utcTime() const; |
217 | | /// Returns the UTC equivalent for the local date and time. |
218 | | |
219 | | bool operator == (const LocalDateTime& dateTime) const; |
220 | | bool operator != (const LocalDateTime& dateTime) const; |
221 | | bool operator < (const LocalDateTime& dateTime) const; |
222 | | bool operator <= (const LocalDateTime& dateTime) const; |
223 | | bool operator > (const LocalDateTime& dateTime) const; |
224 | | bool operator >= (const LocalDateTime& dateTime) const; |
225 | | |
226 | | LocalDateTime operator + (const Timespan& span) const; |
227 | | LocalDateTime operator - (const Timespan& span) const; |
228 | | Timespan operator - (const LocalDateTime& dateTime) const; |
229 | | LocalDateTime& operator += (const Timespan& span); |
230 | | LocalDateTime& operator -= (const Timespan& span); |
231 | | |
232 | | protected: |
233 | | LocalDateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff, int tzd); |
234 | | |
235 | | void determineTzd(bool adjust = false); |
236 | | /// Recalculate the tzd based on the _dateTime member based |
237 | | /// on the current timezone using the Standard C runtime functions. |
238 | | /// If adjust is true, then adjustForTzd() is called after the |
239 | | /// differential is calculated. |
240 | | |
241 | | void adjustForTzd(); |
242 | | /// Adjust the _dateTime member based on the _tzd member. |
243 | | |
244 | | std::time_t dstOffset(int& dstOffset) const; |
245 | | /// Determine the DST offset for the current date/time. |
246 | | |
247 | | private: |
248 | | DateTime _dateTime; |
249 | | int _tzd; |
250 | | |
251 | | friend class DateTimeFormatter; |
252 | | friend class DateTimeParser; |
253 | | }; |
254 | | |
255 | | |
256 | | // |
257 | | // inlines |
258 | | // |
259 | | inline int LocalDateTime::year() const |
260 | 0 | { |
261 | 0 | return _dateTime.year(); |
262 | 0 | } |
263 | | |
264 | | |
265 | | inline int LocalDateTime::month() const |
266 | 0 | { |
267 | 0 | return _dateTime.month(); |
268 | 0 | } |
269 | | |
270 | | |
271 | | inline int LocalDateTime::week(int firstDayOfWeek) const |
272 | 0 | { |
273 | 0 | return _dateTime.week(firstDayOfWeek); |
274 | 0 | } |
275 | | |
276 | | |
277 | | inline int LocalDateTime::day() const |
278 | 0 | { |
279 | 0 | return _dateTime.day(); |
280 | 0 | } |
281 | | |
282 | | |
283 | | inline int LocalDateTime::dayOfWeek() const |
284 | 0 | { |
285 | 0 | return _dateTime.dayOfWeek(); |
286 | 0 | } |
287 | | |
288 | | |
289 | | inline int LocalDateTime::dayOfYear() const |
290 | 0 | { |
291 | 0 | return _dateTime.dayOfYear(); |
292 | 0 | } |
293 | | |
294 | | |
295 | | inline int LocalDateTime::hour() const |
296 | 0 | { |
297 | 0 | return _dateTime.hour(); |
298 | 0 | } |
299 | | |
300 | | |
301 | | inline int LocalDateTime::hourAMPM() const |
302 | 0 | { |
303 | 0 | return _dateTime.hourAMPM(); |
304 | 0 | } |
305 | | |
306 | | |
307 | | inline bool LocalDateTime::isAM() const |
308 | 0 | { |
309 | 0 | return _dateTime.isAM(); |
310 | 0 | } |
311 | | |
312 | | |
313 | | inline bool LocalDateTime::isPM() const |
314 | 0 | { |
315 | 0 | return _dateTime.isPM(); |
316 | 0 | } |
317 | | |
318 | | |
319 | | inline int LocalDateTime::minute() const |
320 | 0 | { |
321 | 0 | return _dateTime.minute(); |
322 | 0 | } |
323 | | |
324 | | |
325 | | inline int LocalDateTime::second() const |
326 | 0 | { |
327 | 0 | return _dateTime.second(); |
328 | 0 | } |
329 | | |
330 | | |
331 | | inline int LocalDateTime::millisecond() const |
332 | 0 | { |
333 | 0 | return _dateTime.millisecond(); |
334 | 0 | } |
335 | | |
336 | | |
337 | | inline int LocalDateTime::microsecond() const |
338 | 0 | { |
339 | 0 | return _dateTime.microsecond(); |
340 | 0 | } |
341 | | |
342 | | |
343 | | inline double LocalDateTime::julianDay() const |
344 | 0 | { |
345 | 0 | return _dateTime.julianDay(); |
346 | 0 | } |
347 | | |
348 | | |
349 | | inline int LocalDateTime::tzd() const |
350 | 0 | { |
351 | 0 | return _tzd; |
352 | 0 | } |
353 | | |
354 | | |
355 | | inline Timestamp LocalDateTime::timestamp() const |
356 | 0 | { |
357 | 0 | return Timestamp::fromUtcTime(_dateTime.utcTime()); |
358 | 0 | } |
359 | | |
360 | | |
361 | | inline Timestamp::UtcTimeVal LocalDateTime::utcTime() const |
362 | 0 | { |
363 | 0 | return _dateTime.utcTime() - ((Timestamp::TimeDiff) _tzd)*10000000; |
364 | 0 | } |
365 | | |
366 | | |
367 | | inline void LocalDateTime::adjustForTzd() |
368 | 0 | { |
369 | 0 | _dateTime += Timespan(((Timestamp::TimeDiff) _tzd)*Timespan::SECONDS); |
370 | 0 | } |
371 | | |
372 | | |
373 | | inline void swap(LocalDateTime& d1, LocalDateTime& d2) noexcept |
374 | 0 | { |
375 | 0 | d1.swap(d2); |
376 | 0 | } |
377 | | |
378 | | |
379 | | } // namespace Poco |
380 | | |
381 | | |
382 | | #endif // Foundation_LocalDateTime_INCLUDED |