Qore Programming Language  0.9.4.6
DateTime.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  DateTime.h
4 
5  Qore programming language
6 
7  Copyright (C) 2003 - 2018 Qore Technologies, s.r.o.
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 <ctime>
37 
38 class AbstractQoreZoneInfo;
39 
41 hashdecl 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 
170  DLLEXPORT DateTime(const char* date, ExceptionSink* xsink);
171 
173 
176  DLLEXPORT DateTime(const AbstractQoreZoneInfo* zone, const char* date);
177 
179 
182  DLLEXPORT DateTime(const struct tm *tms);
183 
185  DLLEXPORT DateTime(const DateTime &dt);
186 
188  DLLEXPORT ~DateTime();
189 
191  DLLEXPORT void getTM(struct tm *tms) const;
192 
194 
196  DLLEXPORT void setNow();
197 
199 
202  DLLEXPORT void setNow(const AbstractQoreZoneInfo* zone);
203 
205 
208  DLLEXPORT void setDate(int64 seconds);
209 
211 
215  DLLEXPORT void setDate(int64 seconds, int ms);
216 
218 
222  DLLEXPORT void setDate(const AbstractQoreZoneInfo* zone, int64 seconds, int us);
223 
225 
229  DLLEXPORT void setLocalDate(const AbstractQoreZoneInfo* zone, int64 seconds, int us);
230 
232  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);
233 
235 
238  DLLEXPORT void setDate(const char* str);
239 
241 
247  DLLEXPORT void setDate(const char* str, ExceptionSink* xsink);
248 
250 
254  DLLEXPORT void setDate(const AbstractQoreZoneInfo* zone, const char* str);
255 
257  DLLEXPORT void setRelativeDate(const char* str);
258 
260  DLLEXPORT void setDate(const struct tm *tms, short ms = 0);
261 
263  DLLEXPORT void setDate(const DateTime &date);
264 
266 
272  DLLEXPORT void setTime(int h, int m, int s, short ms = 0);
273 
275  DLLEXPORT void setRelativeDateSeconds(int64 s, int us = 0);
276 
277  DLLEXPORT bool checkValidity() const;
278  DLLEXPORT bool isEqual(const DateTime* dt) const;
279  DLLEXPORT bool isEqual(const DateTime& dt) const;
280  DLLEXPORT DateTime* add(const DateTime* dt) const;
281  DLLEXPORT DateTime* add(const DateTime& dt) const;
282  DLLEXPORT DateTime* subtractBy(const DateTime* dt) const;
283  DLLEXPORT DateTime* subtractBy(const DateTime& dt) const;
284 
286 
293  DLLEXPORT void addSecondsTo(int64 secs, int us = 0);
294 
296 
299  DLLEXPORT int64 getEpochSeconds() const;
300 
302 
305  DLLEXPORT int64 getEpochSecondsUTC() const;
306 
308 
311  DLLEXPORT int64 getEpochMicrosecondsUTC() const;
312 
314 
317  DLLEXPORT int64 getEpochMillisecondsUTC() const;
318 
320 
323  DLLEXPORT int getDayNumber() const;
324 
326 
329  DLLEXPORT int getDayOfWeek() const;
330 
332 
337  DLLEXPORT void getISOWeek(int& year, int& week, int& day) const;
338 
340 
379  DLLEXPORT void format(QoreString& str, const char* fmt) const;
380 
382 
385  DLLEXPORT bool isRelative() const;
386 
388 
391  DLLEXPORT bool isAbsolute() const;
392 
394 
397  DLLEXPORT short getYear() const;
398 
400 
403  DLLEXPORT int getMonth() const;
404 
406 
409  DLLEXPORT int getDay() const;
410 
412 
415  DLLEXPORT int getHour() const;
416 
418 
421  DLLEXPORT int getMinute() const;
422 
424 
427  DLLEXPORT int getSecond() const;
428 
430 
433  DLLEXPORT int getMillisecond() const;
434 
436 
438  DLLEXPORT int getMicrosecond() const;
439 
441 
444  DLLEXPORT int64 getRelativeSeconds() const;
445 
447 
450  DLLEXPORT int64 getRelativeMilliseconds() const;
451 
453 
456  DLLEXPORT int64 getRelativeMicroseconds() const;
457 
459 
462  DLLEXPORT double getRelativeSecondsDouble() const;
463 
465  DLLEXPORT bool hasValue() const;
466 
468  DLLEXPORT DateTime* unaryMinus() const;
469 
471  DLLEXPORT void unaryMinusInPlace();
472 
474  DLLEXPORT void getInfo(const AbstractQoreZoneInfo* n_zone, qore_tm &info) const;
475 
477  DLLEXPORT void getInfo(qore_tm &info) const;
478 
480  DLLEXPORT void setZone(const AbstractQoreZoneInfo* n_zone);
481 
483  DLLEXPORT const AbstractQoreZoneInfo* getZone() const;
484 
485  // static methods
487  DLLEXPORT static bool isLeapYear(int year);
488 
490  DLLEXPORT static int getLastDayOfMonth(int month, int year);
491 
493 
501  DLLEXPORT static DateTime* getDateFromISOWeek(int year, int week, int day, ExceptionSink* xsink);
502 
504  DLLEXPORT static int compareDates(const DateTime* left, const DateTime* right);
505 
507  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);
508 
510 
515  DLLEXPORT static DateTime* makeAbsolute(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
516 
518 
523  DLLEXPORT static DateTime* makeAbsoluteLocal(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
524 
526  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);
527 
529  DLLEXPORT static DateTime* makeRelativeFromSeconds(int64 n_second, int n_us = 0);
530 };
531 
532 
533 
534 #endif
DLLEXPORT const char * regionName() const
returns the region name of the zone
class qore_date_private * priv
private date data - most are ints so relative dates can hold a lot of data
Definition: DateTime.h:100
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:81
Qore&#39;s parse tree/value type for date-time values, reference-counted, dynamically-allocated only...
Definition: DateTimeNode.h:45
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
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:260
DLLEXPORT int secsEast() const
returns seconds east of UTC for zone
for returning broken-down time information
Definition: DateTime.h:41