Qore Programming Language - C/C++ Library  0.8.12.1
DateTime.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  DateTime.h
4 
5  Qore programming language
6 
7  Copyright (C) 2003 - 2015 David Nichols
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef QORE_DATETIME_H
33 
34 #define QORE_DATETIME_H
35 
36 #include <time.h>
37 
38 class AbstractQoreZoneInfo;
39 
41 struct qore_tm {
42  int year, month, day, hour, minute, second, us, utc_secs_east;
43  bool dst;
44  const char* zone_name;
45  const AbstractQoreZoneInfo* zone;
46 
47  DLLLOCAL void clear() {
48  year = 0;
49  month = 0;
50  day = 0;
51  hour = 0;
52  minute = 0;
53  second = 0;
54  us = 0;
55  utc_secs_east = 0;
56  dst = false;
57  zone_name = 0;
58  zone = 0;
59  }
60 
61  DLLLOCAL bool isTimeNull() const {
62  return !hour && !minute && !second && !us;
63  }
64 
66  DLLEXPORT int secsEast() const;
67 
69  DLLEXPORT const char* regionName() const;
70 };
71 
73 
93 class DateTime {
94  friend class DateTimeNode;
95  friend class qore_relative_time;
96  friend class qore_absolute_time;
97 
98 protected:
100  class qore_date_private *priv;
101 
102  DLLLOCAL void setDateLiteral(int64 date);
103  DLLLOCAL void setRelativeDateLiteral(int64 date);
104 
106  DLLLOCAL DateTime& operator=(const DateTime&);
107 
109  DLLLOCAL DateTime(qore_date_private *n_priv);
110 
111 public:
113 
116  DLLEXPORT DateTime(bool r = false);
117 
119 
129  DLLEXPORT DateTime(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, short n_ms = 0, bool n_relative = false);
130 
132 
135  DLLEXPORT DateTime(int64 seconds);
136 
138 
142  DLLEXPORT DateTime(int64 seconds, int ms);
143 
145 
151  DLLEXPORT explicit DateTime(const AbstractQoreZoneInfo* zone, const QoreValue v);
152 
154 
157  DLLEXPORT explicit DateTime(const QoreValue v);
158 
160 
162  DLLEXPORT DateTime(const char* date);
163 
165 
168  DLLEXPORT DateTime(const AbstractQoreZoneInfo* zone, const char* date);
169 
171 
174  DLLEXPORT DateTime(const struct tm *tms);
175 
177  DLLEXPORT DateTime(const DateTime &dt);
178 
180  DLLEXPORT ~DateTime();
181 
183  DLLEXPORT void getTM(struct tm *tms) const;
184 
186 
188  DLLEXPORT void setNow();
189 
191 
194  DLLEXPORT void setNow(const AbstractQoreZoneInfo* zone);
195 
197 
200  DLLEXPORT void setDate(int64 seconds);
201 
203 
207  DLLEXPORT void setDate(int64 seconds, int ms);
208 
210 
214  DLLEXPORT void setDate(const AbstractQoreZoneInfo* zone, int64 seconds, int us);
215 
217 
221  DLLEXPORT void setLocalDate(const AbstractQoreZoneInfo* zone, int64 seconds, int us);
222 
224  DLLEXPORT void setDate(const AbstractQoreZoneInfo* n_zone, int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
225 
227 
230  DLLEXPORT void setDate(const char* str);
231 
233 
237  DLLEXPORT void setDate(const AbstractQoreZoneInfo* zone, const char* str);
238 
240  DLLEXPORT void setRelativeDate(const char* str);
241 
243  DLLEXPORT void setDate(const struct tm *tms, short ms = 0);
244 
246  DLLEXPORT void setDate(const DateTime &date);
247 
249 
255  DLLEXPORT void setTime(int h, int m, int s, short ms = 0);
256 
257  DLLEXPORT bool checkValidity() const;
258  DLLEXPORT bool isEqual(const DateTime* dt) const;
259  DLLEXPORT bool isEqual(const DateTime& dt) const;
260  DLLEXPORT DateTime* add(const DateTime* dt) const;
261  DLLEXPORT DateTime* add(const DateTime& dt) const;
262  DLLEXPORT DateTime* subtractBy(const DateTime* dt) const;
263  DLLEXPORT DateTime* subtractBy(const DateTime& dt) const;
264 
266 
273  DLLEXPORT void addSecondsTo(int64 secs, int us = 0);
274 
276 
279  DLLEXPORT int64 getEpochSeconds() const;
280 
282 
285  DLLEXPORT int64 getEpochSecondsUTC() const;
286 
288 
291  DLLEXPORT int64 getEpochMicrosecondsUTC() const;
292 
294 
297  DLLEXPORT int64 getEpochMillisecondsUTC() const;
298 
300 
303  DLLEXPORT int getDayNumber() const;
304 
306 
309  DLLEXPORT int getDayOfWeek() const;
310 
312 
317  DLLEXPORT void getISOWeek(int& year, int& week, int& day) const;
318 
320 
359  DLLEXPORT void format(QoreString& str, const char* fmt) const;
360 
362 
365  DLLEXPORT bool isRelative() const;
366 
368 
371  DLLEXPORT bool isAbsolute() const;
372 
374 
377  DLLEXPORT short getYear() const;
378 
380 
383  DLLEXPORT int getMonth() const;
384 
386 
389  DLLEXPORT int getDay() const;
390 
392 
395  DLLEXPORT int getHour() const;
396 
398 
401  DLLEXPORT int getMinute() const;
402 
404 
407  DLLEXPORT int getSecond() const;
408 
410 
413  DLLEXPORT int getMillisecond() const;
414 
416 
418  DLLEXPORT int getMicrosecond() const;
419 
421 
424  DLLEXPORT int64 getRelativeSeconds() const;
425 
427 
430  DLLEXPORT int64 getRelativeMilliseconds() const;
431 
433 
436  DLLEXPORT int64 getRelativeMicroseconds() const;
437 
439 
442  DLLEXPORT double getRelativeSecondsDouble() const;
443 
445  DLLEXPORT bool hasValue() const;
446 
448  DLLEXPORT DateTime* unaryMinus() const;
449 
451  DLLEXPORT void unaryMinusInPlace();
452 
454  DLLEXPORT void getInfo(const AbstractQoreZoneInfo* n_zone, qore_tm &info) const;
455 
457  DLLEXPORT void getInfo(qore_tm &info) const;
458 
460  DLLEXPORT void setZone(const AbstractQoreZoneInfo* n_zone);
461 
463  DLLEXPORT const AbstractQoreZoneInfo* getZone() const;
464 
465  // static methods
467  DLLEXPORT static bool isLeapYear(int year);
468 
470  DLLEXPORT static int getLastDayOfMonth(int month, int year);
471 
473 
481  DLLEXPORT static DateTime* getDateFromISOWeek(int year, int week, int day, ExceptionSink* xsink);
482 
484  DLLEXPORT static int compareDates(const DateTime* left, const DateTime* right);
485 
487  DLLEXPORT static DateTime* makeAbsolute(const AbstractQoreZoneInfo* n_zone, int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
488 
490 
495  DLLEXPORT static DateTime* makeAbsolute(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
496 
498 
503  DLLEXPORT static DateTime* makeAbsoluteLocal(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
504 
506  DLLEXPORT static DateTime* makeRelative(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0);
507 
509  DLLEXPORT static DateTime* makeRelativeFromSeconds(int64 n_second, int n_us = 0);
510 };
511 
512 
513 
514 #endif
class qore_date_private * priv
private date data - most are ints so relative dates can hold a lot of data
Definition: DateTime.h:100
DLLEXPORT const char * regionName() const
returns the region name of the zone
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
DLLEXPORT int secsEast() const
returns seconds east of UTC for zone
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:82
Qore&#39;s parse tree/value type for date-time values, reference-counted, dynamically-allocated only...
Definition: DateTimeNode.h:44
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:228
for returning broken-down time information
Definition: DateTime.h:41