Qore Programming Language 1.19.5
Loading...
Searching...
No Matches
DateTime.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 DateTime.h
4
5 Qore programming language
6
7 Copyright (C) 2003 - 2023 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
38class AbstractQoreZoneInfo;
39
41hashdecl 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
93class DateTime {
94 friend class DateTimeNode;
95 friend class qore_relative_time;
96 friend class qore_absolute_time;
97
98protected:
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
111public:
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
312
314
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
451
453
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
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
static DLLEXPORT DateTime * getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink)
returns a DateTime value from ISO-8601 week and day offsets
DLLEXPORT DateTime * unaryMinus() const
returns the negative time from the current time
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 getRelativeMilliseconds() const
returns the difference as the number of milliseconds between the date/time value and the local time a...
DLLEXPORT DateTime(const char *date, ExceptionSink *xsink)
constructor for setting the date from a string with a flexible format, throws a Qore-language excepti...
DLLEXPORT void addSecondsTo(int64 secs, int us=0)
adds the given number of seconds (and microseconds) to the date/time value
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 setDate(int64 seconds, int ms)
sets the absolute date value based on the number of seconds from January 1, 1970 UTC (plus millisecon...
DLLEXPORT int64 getEpochSecondsUTC() const
gets the number of seconds since January 1, 1970Z for the current date
DLLEXPORT int getMillisecond() const
returns the microsecond portion of the date-time value divided by 1000
class qore_date_private * priv
private date data - most are ints so relative dates can hold a lot of data
Definition: DateTime.h:100
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
DLLEXPORT int getDayNumber() const
returns the ordinal number of the day in the year for absolute dates, sometimes (mistakenly) referred...
DLLEXPORT void getISOWeek(int &year, int &week, int &day) const
returns the ISO-8601 week information
DLLEXPORT DateTime(const AbstractQoreZoneInfo *zone, const QoreValue v)
constructor for creating an absolute date from a value representing a number of seconds
DLLEXPORT int getHour() const
returns the hour portion of the date-time value (in local time according to the time zone for absolut...
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 getLastDayOfMonth(int month, int year)
returns the number of days in the month given according to a proleptic gregorian calendar
DLLEXPORT void setNow(const AbstractQoreZoneInfo *zone)
sets the current date and time in the given time zone; current contents are overwritten
DLLEXPORT bool isRelative() const
returns true if the value is a relative date-time value
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 DateTime(const char *date)
constructor for setting the date from a string
DLLEXPORT bool hasValue() const
returns true if the object has a value, false if not (zero value = 1970-01-01Z for absolute times,...
DLLEXPORT void format(QoreString &str, const char *fmt) const
formats the date/time value to a QoreString
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 void setRelativeDateSeconds(int64 s, int us=0)
set the object to a relative date/time value as given in seconds and optionally microseconds
DLLEXPORT DateTime(int64 seconds)
constructor for setting an absolute date based on the number of seconds from January 1,...
DLLEXPORT DateTime(int64 seconds, int ms)
constructor for setting an absolute date based on the number of seconds from January 1,...
DLLEXPORT int64 getEpochMicrosecondsUTC() const
gets the number of microseconds since January 1, 1970Z for the current date
DLLEXPORT void setZone(const AbstractQoreZoneInfo *n_zone)
changes the time zone for the time without updating the epoch offset
DLLEXPORT DateTime(const AbstractQoreZoneInfo *zone, const char *date)
constructor for setting the date from a string with a flexible format
DLLEXPORT void setDate(const char *str)
sets an absolute date value from a string with a flexible format
DLLEXPORT void setDate(const char *str, ExceptionSink *xsink)
sets an absolute date value from a string with a flexible format
DLLEXPORT void setRelativeDate(const char *str)
sets a relative date from a string in the format YYYYMMDDHHmmSS
DLLEXPORT int getDayOfWeek() const
returns the day of week for the current date (0-6, Sun-Sat)
DLLLOCAL DateTime(qore_date_private *n_priv)
this constructor is not exported in the library
DLLEXPORT void setTime(int h, int m, int s, short ms=0)
sets the time from hours, minutes, seconds, and milliseconds
static DLLEXPORT DateTime * makeAbsolute(const AbstractQoreZoneInfo *zone, int64 seconds, int us=0)
static "constructor" to create an absolute time as an offset from the epoch, including microseconds
DLLEXPORT int64 getRelativeSeconds() const
returns the difference as the number of seconds between the date/time value and the local time at the...
DLLEXPORT void setNow()
sets the current date and time in the current time zone; current contents are overwritten
DLLEXPORT int getMinute() const
returns the minute portion of the date-time value (in local time according to the time zone for absol...
static DLLEXPORT bool isLeapYear(int year)
returns true if the year passed is a leap year according to a proleptic gregorian calendar
DLLEXPORT void setDate(int64 seconds)
sets the absolute date value based on the number of seconds from January 1, 1970
DLLEXPORT int64 getRelativeMicroseconds() const
returns the difference as the number of microseconds between the date/time value and the local time a...
DLLEXPORT void setDate(const DateTime &date)
sets the date from a DateTime reference
DLLEXPORT void setDate(const AbstractQoreZoneInfo *zone, const char *str)
sets an absolute date value from a time zone pointer and a string with a flexible format
DLLEXPORT const AbstractQoreZoneInfo * getZone() const
gets the time zone for the object; returns 0 for relative date/time values
DLLEXPORT int64 getEpochSeconds() const
gets the number of seconds since January 1, 1970 for the current date offset in local time
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(const DateTime &dt)
copy constructor
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 int getDay() const
returns the day portion of the date-time value (in local time according to the time zone for absolute...
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)
constructor for setting all parameters
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)
sets the date to an absolute date/time as given
DLLEXPORT ~DateTime()
destroys the object and frees all memory
DLLEXPORT short getYear() const
returns the year portion of the date-time value (in local time according to the time zone for absolut...
DLLEXPORT void getTM(hashdecl tm *tms) const
sets a "hashdecl tm" from the current date/time value for the time zone for the object; use DateTime::g...
DLLEXPORT bool isAbsolute() const
returns true if the value is an absolute date-time value
DLLLOCAL DateTime & operator=(const DateTime &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DLLEXPORT void unaryMinusInPlace()
converts the current value to the negative of itself
DLLEXPORT void setDate(const hashdecl tm *tms, short ms=0)
sets the absolute date from a "hashdecl tm" pointer and millisecond value
DLLEXPORT DateTime(const hashdecl tm *tms)
constructor for setting an absolute date based on a "hashdecl tm"
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 DateTime(const QoreValue v)
constructor for creating a relative date from a value representing a number of seconds
DLLEXPORT DateTime(bool r=false)
constructor for an empty object
static DLLEXPORT DateTime * makeRelativeFromSeconds(int64 n_second, int n_us=0)
static "constructor" to create a relative time, including microseconds
DLLEXPORT int64 getEpochMillisecondsUTC() const
gets the number of milliseconds since January 1, 1970Z for the current date
DLLEXPORT void getInfo(qore_tm &info) const
returns the broken-down time in the current time zone
DLLEXPORT int getMicrosecond() const
returns the microsecond portion of the date-time value
DLLEXPORT void setDate(const AbstractQoreZoneInfo *zone, int64 seconds, int us)
sets the absolute date value based on the number of seconds from January 1, 1970 UTC (plus microsecon...
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only.
Definition: DateTimeNode.h:47
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
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
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:279
for returning broken-down time information
Definition: DateTime.h:41
DLLEXPORT int secsEast() const
returns seconds east of UTC for zone
DLLEXPORT const char * regionName() const
returns the region name of the zone