Qore Programming Language  0.8.11.1
DateTime.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  DateTime.h
4 
5  Qore programming language
6 
7  Copyright (C) 2003 - 2014 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 
96 protected:
98  class qore_date_private *priv;
99 
100  DLLLOCAL void setDateLiteral(int64 date);
101  DLLLOCAL void setRelativeDateLiteral(int64 date);
102 
104  DLLLOCAL DateTime& operator=(const DateTime&);
105 
107  DLLLOCAL DateTime(qore_date_private *n_priv);
108 
109 public:
111 
114  DLLEXPORT DateTime(bool r = false);
115 
117 
127  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);
128 
130 
133  DLLEXPORT DateTime(int64 seconds);
134 
136 
140  DLLEXPORT DateTime(int64 seconds, int ms);
141 
143 
145  DLLEXPORT DateTime(const char *date);
146 
148 
151  DLLEXPORT DateTime(const AbstractQoreZoneInfo *zone, const char *date);
152 
154 
157  DLLEXPORT DateTime(const struct tm *tms);
158 
160  DLLEXPORT DateTime(const DateTime &dt);
161 
163  DLLEXPORT ~DateTime();
164 
166  DLLEXPORT void getTM(struct tm *tms) const;
167 
169 
171  DLLEXPORT void setNow();
172 
174 
177  DLLEXPORT void setNow(const AbstractQoreZoneInfo *zone);
178 
180 
183  DLLEXPORT void setDate(int64 seconds);
184 
186 
190  DLLEXPORT void setDate(int64 seconds, int ms);
191 
193 
197  DLLEXPORT void setDate(const AbstractQoreZoneInfo *zone, int64 seconds, int us);
198 
200 
204  DLLEXPORT void setLocalDate(const AbstractQoreZoneInfo *zone, int64 seconds, int us);
205 
207  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);
208 
210 
213  DLLEXPORT void setDate(const char *str);
214 
216 
220  DLLEXPORT void setDate(const AbstractQoreZoneInfo *zone, const char *str);
221 
223  DLLEXPORT void setRelativeDate(const char *str);
224 
226  DLLEXPORT void setDate(const struct tm *tms, short ms = 0);
227 
229  DLLEXPORT void setDate(const DateTime &date);
230 
232 
238  DLLEXPORT void setTime(int h, int m, int s, short ms = 0);
239 
240  DLLEXPORT bool checkValidity() const;
241  DLLEXPORT bool isEqual(const DateTime *dt) const;
242  DLLEXPORT DateTime *add(const DateTime *dt) const;
243  DLLEXPORT DateTime *subtractBy(const DateTime *dt) const;
244 
246 
249  DLLEXPORT int64 getEpochSeconds() const;
250 
252 
255  DLLEXPORT int64 getEpochSecondsUTC() const;
256 
258 
261  DLLEXPORT int64 getEpochMicrosecondsUTC() const;
262 
264 
267  DLLEXPORT int64 getEpochMillisecondsUTC() const;
268 
270 
273  DLLEXPORT int getDayNumber() const;
274 
276 
279  DLLEXPORT int getDayOfWeek() const;
280 
282 
287  DLLEXPORT void getISOWeek(int &year, int &week, int &day) const;
288 
290 
329  DLLEXPORT void format(QoreString &str, const char *fmt) const;
330 
332 
335  DLLEXPORT bool isRelative() const;
336 
338 
341  DLLEXPORT bool isAbsolute() const;
342 
344 
347  DLLEXPORT short getYear() const;
348 
350 
353  DLLEXPORT int getMonth() const;
354 
356 
359  DLLEXPORT int getDay() const;
360 
362 
365  DLLEXPORT int getHour() const;
366 
368 
371  DLLEXPORT int getMinute() const;
372 
374 
377  DLLEXPORT int getSecond() const;
378 
380 
383  DLLEXPORT int getMillisecond() const;
384 
386 
388  DLLEXPORT int getMicrosecond() const;
389 
391 
394  DLLEXPORT int64 getRelativeSeconds() const;
395 
397 
400  DLLEXPORT int64 getRelativeMilliseconds() const;
401 
403 
406  DLLEXPORT int64 getRelativeMicroseconds() const;
407 
409  DLLEXPORT bool hasValue() const;
410 
412  DLLEXPORT DateTime *unaryMinus() const;
413 
415  DLLEXPORT void unaryMinusInPlace();
416 
418  DLLEXPORT void getInfo(const AbstractQoreZoneInfo *n_zone, qore_tm &info) const;
419 
421  DLLEXPORT void getInfo(qore_tm &info) const;
422 
424  DLLEXPORT void setZone(const AbstractQoreZoneInfo *n_zone);
425 
427  DLLEXPORT const AbstractQoreZoneInfo* getZone() const;
428 
429  // static methods
431  DLLEXPORT static bool isLeapYear(int year);
432 
434  DLLEXPORT static int getLastDayOfMonth(int month, int year);
435 
437 
445  DLLEXPORT static DateTime *getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink);
446 
448  DLLEXPORT static int compareDates(const DateTime *left, const DateTime *right);
449 
451  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);
452 
454 
459  DLLEXPORT static DateTime *makeAbsolute(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
460 
462 
467  DLLEXPORT static DateTime *makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0);
468 
470  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);
471 };
472 
473 #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
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:98
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:50
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 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...
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 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