DateTime.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
DateTime.h
Qore programming language
Copyright (C) 2003 - 2018 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#ifndef QORE_DATETIME_H
#define QORE_DATETIME_H
#include <ctime>
class AbstractQoreZoneInfo;
struct qore_tm {
int year, month, day, hour, minute, second, us, utc_secs_east;
bool dst;
const char* zone_name;
const AbstractQoreZoneInfo* zone;
DLLLOCAL void clear() {
year = 0;
month = 0;
day = 0;
hour = 0;
minute = 0;
second = 0;
us = 0;
utc_secs_east = 0;
dst = false;
zone_name = 0;
zone = 0;
}
DLLLOCAL bool isTimeNull() const {
return !hour && !minute && !second && !us;
}
DLLEXPORT int secsEast() const;
DLLEXPORT const char* regionName() const;
};
class DateTime {
friend class DateTimeNode;
friend class qore_relative_time;
friend class qore_absolute_time;
protected:
class qore_date_private *priv;
DLLLOCAL void setDateLiteral(int64 date);
DLLLOCAL void setRelativeDateLiteral(int64 date);
DLLLOCAL DateTime& operator=(const DateTime&);
DLLLOCAL DateTime(qore_date_private *n_priv);
public:
DLLEXPORT DateTime(bool r = false);
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);
DLLEXPORT DateTime(int64 seconds);
DLLEXPORT DateTime(int64 seconds, int ms);
DLLEXPORT explicit DateTime(const AbstractQoreZoneInfo* zone, const QoreValue v);
DLLEXPORT explicit DateTime(const QoreValue v);
DLLEXPORT DateTime(const char* date);
DLLEXPORT DateTime(const char* date, ExceptionSink* xsink);
DLLEXPORT DateTime(const AbstractQoreZoneInfo* zone, const char* date);
DLLEXPORT DateTime(const struct tm *tms);
DLLEXPORT DateTime(const DateTime &dt);
DLLEXPORT ~DateTime();
DLLEXPORT void getTM(struct tm *tms) const;
DLLEXPORT void setNow();
DLLEXPORT void setNow(const AbstractQoreZoneInfo* zone);
DLLEXPORT void setDate(int64 seconds);
DLLEXPORT void setDate(int64 seconds, int ms);
DLLEXPORT void setDate(const AbstractQoreZoneInfo* zone, int64 seconds, int us);
DLLEXPORT void setLocalDate(const AbstractQoreZoneInfo* zone, int64 seconds, int us);
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);
DLLEXPORT void setDate(const char* str);
DLLEXPORT void setDate(const char* str, ExceptionSink* xsink);
DLLEXPORT void setDate(const AbstractQoreZoneInfo* zone, const char* str);
DLLEXPORT void setRelativeDate(const char* str);
DLLEXPORT void setDate(const struct tm *tms, short ms = 0);
DLLEXPORT void setDate(const DateTime &date);
DLLEXPORT void setTime(int h, int m, int s, short ms = 0);
DLLEXPORT void setRelativeDateSeconds(int64 s, int us = 0);
DLLEXPORT bool checkValidity() const;
DLLEXPORT bool isEqual(const DateTime* dt) const;
DLLEXPORT bool isEqual(const DateTime& dt) const;
DLLEXPORT DateTime* add(const DateTime* dt) const;
DLLEXPORT DateTime* add(const DateTime& dt) const;
DLLEXPORT DateTime* subtractBy(const DateTime* dt) const;
DLLEXPORT DateTime* subtractBy(const DateTime& dt) const;
DLLEXPORT void addSecondsTo(int64 secs, int us = 0);
DLLEXPORT int64 getEpochSeconds() const;
DLLEXPORT int64 getEpochSecondsUTC() const;
DLLEXPORT int64 getEpochMicrosecondsUTC() const;
DLLEXPORT int64 getEpochMillisecondsUTC() const;
DLLEXPORT int getDayNumber() const;
DLLEXPORT int getDayOfWeek() const;
DLLEXPORT void getISOWeek(int& year, int& week, int& day) const;
DLLEXPORT void format(QoreString& str, const char* fmt) const;
DLLEXPORT bool isRelative() const;
DLLEXPORT bool isAbsolute() const;
DLLEXPORT short getYear() const;
DLLEXPORT int getMonth() const;
DLLEXPORT int getDay() const;
DLLEXPORT int getHour() const;
DLLEXPORT int getMinute() const;
DLLEXPORT int getSecond() const;
DLLEXPORT int getMillisecond() const;
DLLEXPORT int getMicrosecond() const;
DLLEXPORT int64 getRelativeSeconds() const;
DLLEXPORT int64 getRelativeMilliseconds() const;
DLLEXPORT int64 getRelativeMicroseconds() const;
DLLEXPORT double getRelativeSecondsDouble() const;
DLLEXPORT bool hasValue() const;
DLLEXPORT DateTime* unaryMinus() const;
DLLEXPORT void unaryMinusInPlace();
DLLEXPORT void getInfo(const AbstractQoreZoneInfo* n_zone, qore_tm &info) const;
DLLEXPORT void getInfo(qore_tm &info) const;
DLLEXPORT void setZone(const AbstractQoreZoneInfo* n_zone);
DLLEXPORT const AbstractQoreZoneInfo* getZone() const;
// static methods
DLLEXPORT static bool isLeapYear(int year);
DLLEXPORT static int getLastDayOfMonth(int month, int year);
DLLEXPORT static DateTime* getDateFromISOWeek(int year, int week, int day, ExceptionSink* xsink);
DLLEXPORT static int compareDates(const DateTime* left, const DateTime* right);
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);
DLLEXPORT static DateTime* makeAbsolute(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
DLLEXPORT static DateTime* makeAbsoluteLocal(const AbstractQoreZoneInfo* zone, int64 seconds, int us = 0);
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);
DLLEXPORT static DateTime* makeRelativeFromSeconds(int64 n_second, int n_us = 0);
};
#endif
DateTime::unaryMinus
DLLEXPORT DateTime * unaryMinus() const
returns the negative time from the current time
DateTime::getDateFromISOWeek
static DLLEXPORT DateTime * getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink)
returns a DateTime value from ISO-8601 week and day offsets
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
DateTime::makeAbsoluteLocal
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,...
DateTime::getDayOfWeek
DLLEXPORT int getDayOfWeek() const
returns the day of week for the current date (0-6, Sun-Sat)
DateTime::compareDates
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
qore_tm::secsEast
DLLEXPORT int secsEast() const
returns seconds east of UTC for zone
DateTime::getTM
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...
DateTime::isAbsolute
DLLEXPORT bool isAbsolute() const
returns true if the value is an absolute date-time value
DateTime::getEpochMicrosecondsUTC
DLLEXPORT int64 getEpochMicrosecondsUTC() const
gets the number of microseconds since January 1, 1970Z for the current date
DateTime::addSecondsTo
DLLEXPORT void addSecondsTo(int64 secs, int us=0)
adds the given number of seconds (and microseconds) to the date/time value
DateTime::getMonth
DLLEXPORT int getMonth() const
returns the month portion of the date-time value (in local time according to the time zone for absolu...
DateTime::getEpochSecondsUTC
DLLEXPORT int64 getEpochSecondsUTC() const
gets the number of seconds since January 1, 1970Z for the current date
DateTime::getLastDayOfMonth
static DLLEXPORT int getLastDayOfMonth(int month, int year)
returns the number of days in the month given according to a proleptic gregorian calendar
DateTime::getDay
DLLEXPORT int getDay() const
returns the day portion of the date-time value (in local time according to the time zone for absolute...
DateTime::makeRelative
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
DateTime::getRelativeMilliseconds
DLLEXPORT int64 getRelativeMilliseconds() const
returns the difference as the number of milliseconds between the date/time value and the local time a...
DateTime::DateTime
DLLLOCAL DateTime(qore_date_private *n_priv)
this constructor is not exported in the library
DateTime::getISOWeek
DLLEXPORT void getISOWeek(int &year, int &week, int &day) const
returns the ISO-8601 week information
DateTime::makeAbsolute
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
DateTime::makeRelativeFromSeconds
static DLLEXPORT DateTime * makeRelativeFromSeconds(int64 n_second, int n_us=0)
static "constructor" to create a relative time, including microseconds
int64
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
DateTime::isRelative
DLLEXPORT bool isRelative() const
returns true if the value is a relative date-time value
DateTime::operator=
DLLLOCAL DateTime & operator=(const DateTime &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DateTime::unaryMinusInPlace
DLLEXPORT void unaryMinusInPlace()
converts the current value to the negative of itself
DateTime::getSecond
DLLEXPORT int getSecond() const
returns the second portion of the date-time value (in local time according to the time zone for absol...
DateTime::getMicrosecond
DLLEXPORT int getMicrosecond() const
returns the microsecond portion of the date-time value
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
DateTimeNode
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only.
Definition: DateTimeNode.h:45
DateTime::getRelativeSeconds
DLLEXPORT int64 getRelativeSeconds() const
returns the difference as the number of seconds between the date/time value and the local time at the...
DateTime::getMinute
DLLEXPORT int getMinute() const
returns the minute portion of the date-time value (in local time according to the time zone for absol...
DateTime::getMillisecond
DLLEXPORT int getMillisecond() const
returns the microsecond portion of the date-time value divided by 1000
DateTime::isLeapYear
static DLLEXPORT bool isLeapYear(int year)
returns true if the year passed is a leap year according to a proleptic gregorian calendar
DateTime::setTime
DLLEXPORT void setTime(int h, int m, int s, short ms=0)
sets the time from hours, minutes, seconds, and milliseconds
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DateTime::setLocalDate
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...
qore_tm::regionName
DLLEXPORT const char * regionName() const
returns the region name of the zone
DateTime::getHour
DLLEXPORT int getHour() const
returns the hour portion of the date-time value (in local time according to the time zone for absolut...
DateTime::getYear
DLLEXPORT short getYear() const
returns the year portion of the date-time value (in local time according to the time zone for absolut...
DateTime::getEpochMillisecondsUTC
DLLEXPORT int64 getEpochMillisecondsUTC() const
gets the number of milliseconds since January 1, 1970Z for the current date
DateTime::setRelativeDate
DLLEXPORT void setRelativeDate(const char *str)
sets a relative date from a string in the format YYYYMMDDHHmmSS
DateTime::priv
class qore_date_private * priv
private date data - most are ints so relative dates can hold a lot of data
Definition: DateTime.h:100
DateTime::setRelativeDateSeconds
DLLEXPORT void setRelativeDateSeconds(int64 s, int us=0)
set the object to a relative date/time value as given in seconds and optionally microseconds
DateTime
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
DateTime::~DateTime
DLLEXPORT ~DateTime()
destroys the object and frees all memory
DateTime::getEpochSeconds
DLLEXPORT int64 getEpochSeconds() const
gets the number of seconds since January 1, 1970 for the current date offset in local time
DateTime::format
DLLEXPORT void format(QoreString &str, const char *fmt) const
formats the date/time value to a QoreString
DateTime::getDayNumber
DLLEXPORT int getDayNumber() const
returns the ordinal number of the day in the year for absolute dates, sometimes (mistakenly) referred...
DateTime::getZone
DLLEXPORT const AbstractQoreZoneInfo * getZone() const
gets the time zone for the object; returns 0 for relative date/time values
DateTime::setDate
DLLEXPORT void setDate(int64 seconds)
sets the absolute date value based on the number of seconds from January 1, 1970
DateTime::getRelativeSecondsDouble
DLLEXPORT double getRelativeSecondsDouble() const
returns the difference as the number of seconds between the date/time value and the local time at the...
qore_tm
for returning broken-down time information
Definition: DateTime.h:41
DateTime::hasValue
DLLEXPORT bool hasValue() const
returns true if the object has a value, false if not (zero value = 1970-01-01Z for absolute times,...
DateTime::getRelativeMicroseconds
DLLEXPORT int64 getRelativeMicroseconds() const
returns the difference as the number of microseconds between the date/time value and the local time a...
DateTime::getInfo
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)
DateTime::setNow
DLLEXPORT void setNow()
sets the current date and time in the current time zone; current contents are overwritten
DateTime::setZone
DLLEXPORT void setZone(const AbstractQoreZoneInfo *n_zone)
changes the time zone for the time without updating the epoch offset