Qore Programming Language - C/C++ Library  0.8.12.3
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
DLLLOCAL DateTime(qore_date_private *n_priv)
this constructor is not exported in the library
DLLEXPORT DateTime * unaryMinus() const
returns the negative time from the current time
static DLLEXPORT DateTime * makeRelativeFromSeconds(int64 n_second, int n_us=0)
static "constructor" to create a relative time, including microseconds
DLLLOCAL DateTime & operator=(const DateTime &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
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
DLLEXPORT int getMinute() const
returns the minute portion of the date-time value (in local time according to the time zone for absol...
DLLEXPORT int64 getRelativeMicroseconds() const
returns the difference as the number of microseconds between the date/time value and the local time a...
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
DLLEXPORT void setDate(int64 seconds)
sets the absolute date value based on the number of seconds from January 1, 1970
DLLEXPORT void setZone(const AbstractQoreZoneInfo *n_zone)
changes the time zone for the time without updating the epoch offset
DLLEXPORT int64 getRelativeSeconds() const
returns the difference as the number of seconds between the date/time value and the local time at the...
DLLEXPORT bool hasValue() const
returns true if the object has a value, false if not (zero value = 1970-01-01Z for absolute times...
static DLLEXPORT bool isLeapYear(int year)
returns true if the year passed is a leap year according to a proleptic gregorian calendar ...
DLLEXPORT int secsEast() const
returns seconds east of UTC for zone
DLLEXPORT int64 getEpochMicrosecondsUTC() const
gets the number of microseconds since January 1, 1970Z for the current date
DLLEXPORT int getMicrosecond() const
returns the microsecond portion of the date-time value
static DLLEXPORT DateTime * getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink)
returns a DateTime value from ISO-8601 week and day offsets
DLLEXPORT int getMonth() const
returns the month portion of the date-time value (in local time according to the time zone for absolu...
DLLEXPORT ~DateTime()
destroys the object and frees all memory
DLLEXPORT int getMillisecond() const
returns the microsecond portion of the date-time value divided by 1000
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:82
DLLEXPORT int getDay() const
returns the day portion of the date-time value (in local time according to the time zone for absolute...
DLLEXPORT int getDayOfWeek() const
returns the day of week for the current date (0-6, Sun-Sat)
DLLEXPORT bool isRelative() const
returns true if the value is a relative date-time value
DLLEXPORT void setRelativeDate(const char *str)
sets a relative date from a string in the format YYYYMMDDHHmmSS
DLLEXPORT void addSecondsTo(int64 secs, int us=0)
adds the given number of seconds (and microseconds) to the date/time value
DLLEXPORT int64 getEpochSeconds() const
gets the number of seconds since January 1, 1970 for the current date offset in local time ...
DLLEXPORT int64 getEpochMillisecondsUTC() const
gets the number of milliseconds since January 1, 1970Z for the current date
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only...
Definition: DateTimeNode.h:44
static DLLEXPORT int getLastDayOfMonth(int month, int year)
returns the number of days in the month given according to a proleptic gregorian calendar ...
DLLEXPORT int getSecond() const
returns the second portion of the date-time value (in local time according to the time zone for absol...
static DLLEXPORT int compareDates(const DateTime *left, const DateTime *right)
returns -1, 0, or 1 if the left date is less than, equal, or greater than the right date ...
DLLEXPORT int64 getEpochSecondsUTC() const
gets the number of seconds since January 1, 1970Z for the current date
static DLLEXPORT 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)
static "constructor" to create an absolute time, including microseconds
DLLEXPORT int64 getRelativeMilliseconds() const
returns the difference as the number of milliseconds between the date/time value and the local time a...
DLLEXPORT void getInfo(const AbstractQoreZoneInfo *n_zone, qore_tm &info) const
returns the broken-down time in the given time zone (n_zone = 0 means UTC)
DLLEXPORT const AbstractQoreZoneInfo * getZone() const
gets the time zone for the object; returns 0 for relative date/time values
DLLEXPORT void getTM(struct tm *tms) const
sets a "struct tm" from the current date/time value for the time zone for the object; use DateTime::g...
DLLEXPORT short getYear() const
returns the year portion of the date-time value (in local time according to the time zone for absolut...
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
DLLEXPORT void getISOWeek(int &year, int &week, int &day) const
returns the ISO-8601 week information
DLLEXPORT bool isAbsolute() const
returns true if the value is an absolute date-time value
DLLEXPORT int getHour() const
returns the hour portion of the date-time value (in local time according to the time zone for absolut...
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
DLLEXPORT void format(QoreString &str, const char *fmt) const
formats the date/time value to a QoreString
DLLEXPORT void unaryMinusInPlace()
converts the current value to the negative of itself
DLLEXPORT void setNow()
sets the current date and time in the current time zone; current contents are overwritten ...
static DLLEXPORT DateTime * makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us=0)
static "constructor" to create an absolute time as an offset from the given time zone's epoch...
DLLEXPORT double getRelativeSecondsDouble() const
returns the difference as the number of seconds between the date/time value and the local time at the...
DLLEXPORT void setLocalDate(const AbstractQoreZoneInfo *zone, int64 seconds, int us)
sets the absolute date value based on the number of seconds from January 1, 1970 in the given time zo...
DLLEXPORT int getDayNumber() const
returns the ordinal number of the day in the year for absolute dates, sometimes (mistakenly) referred...
DLLEXPORT void setTime(int h, int m, int s, short ms=0)
sets the time from hours, minutes, seconds, and milliseconds
for returning broken-down time information
Definition: DateTime.h:41
static DLLEXPORT 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)
static "constructor" to create a relative time, including microseconds