CopperSpice API
1.9.2
|
Stores date and time information. More...
Public Methods | |
QDateTime () | |
QDateTime (const QDate &date) | |
QDateTime (const QDate &date, const QTime &time, std::optional< QTimeZone > timeZone=std::nullopt) | |
QDateTime (const QDateTime &other) | |
QDateTime (QDateTime &&other) | |
~QDateTime () | |
QDateTime | addDays (qint64 days) const |
QDateTime | addDuration (std::chrono::milliseconds msecs) const |
QDateTime | addMonths (qint64 months) const |
QDateTime | addMSecs (qint64 msecs) const |
QDateTime | addSecs (qint64 seconds) const |
QDateTime | addYears (qint64 years) const |
QDate | date () const |
qint64 | daysTo (const QDateTime &value) const |
bool | isDaylightTime () const |
bool | isNull () const |
bool | isValid () const |
qint64 | msecsTo (const QDateTime &value) const |
int | offsetFromUtc () const |
bool | operator!= (const QDateTime &value) const |
bool | operator< (const QDateTime &value) const |
bool | operator<= (const QDateTime &value) const |
QDateTime & | operator= (const QDateTime &other) |
QDateTime & | operator= (QDateTime &&other) |
bool | operator== (const QDateTime &value) const |
bool | operator> (const QDateTime &value) const |
bool | operator>= (const QDateTime &value) const |
qint64 | secsTo (const QDateTime &value) const |
void | setDate (const QDate &date) |
void | setMSecsSinceEpoch (qint64 msecs) |
void | setSecsSinceEpoch (qint64 seconds) |
void | setTime (const QTime &time) |
void | setTime_t (quint64 seconds) |
void | setTimeZone (const QTimeZone &toZone) |
void | swap (QDateTime &other) |
QTime | time () const |
QTimeZone | timeRepresentation () const |
QTimeZone | timeZone () const |
QString | timeZoneAbbreviation () const |
CFDateRef | toCFDate () const |
QDateTime | toLocalTime () const |
qint64 | toMSecsSinceEpoch () const |
NSDate * | toNSDate () const |
QDateTime | toOffsetFromUtc (qint64 offsetSeconds) const |
qint64 | toSecsSinceEpoch () const |
std::chrono::time_point< std::chrono::system_clock, std::chrono::milliseconds > | toStdSysMilliseconds () const |
std::chrono::time_point< std::chrono::system_clock, std::chrono::seconds > | toStdSysSeconds () const |
QString | toString (const QString &format) const |
QString | toString (Qt::DateFormat format=Qt::TextDate) const |
quint64 | toTime_t () const |
QDateTime | toTimeZone (const QTimeZone &timeZone) const |
QDateTime | toUTC () const |
Static Public Methods | |
static QDateTime | currentDateTime (const QTimeZone &zone=QDate::default_tz ()) |
static QDateTime | currentDateTimeUtc () |
static qint64 | currentMSecsSinceEpoch () |
static qint64 | currentSecsSinceEpoch () |
static QDateTime | fromCFDate (CFDateRef date) |
static QDateTime | fromMSecsSinceEpoch (qint64 msecs, const QTimeZone &timeZone=QDate::default_tz ()) |
static QDateTime | fromNSDate (const NSDate *date) |
static QDateTime | fromSecsSinceEpoch (qint64 seconds, const QTimeZone &timeZone=QDate::default_tz ()) |
static QDateTime | fromStdLocalTime (const std::chrono::local_time< std::chrono::milliseconds > &msecs) |
static QDateTime | fromString (const QString &str, const QString &format) |
static QDateTime | fromString (const QString &str, Qt::DateFormat format=Qt::TextDate) |
static QDateTime | fromTime_t (qint64 seconds, const QTimeZone &timeZone=QDate::default_tz ()) |
Friends | |
QDataStream & | operator<< (QDataStream &stream, const QDateTime &dateTime) |
QDataStream & | operator>> (QDataStream &stream, QDateTime &dateTime) |
The QDateTime class stores date and time information. The Julian calendar is used for any date prior to October 15 1582. The Gregorian calendar is used for dates after 15 October 1582. This class retrieves the current date and time from the operating system internal clock. Many of the methods in this class are used to calculate a new date or time based on a given offset or to compare two existing dates or times.
QDateTime does not account for leap seconds.
A QDateTime is typically created by specifying a QDate and a QTime or using the static method currentDateTime() which returns a new QDateTime based on the system clock. The date() and time() methods return a QDate and QTime. Calling toString() returns the date and time in a text format.
The date and time for a given QDateTime can be modified by calling setDate() and setTime(). The fromString() method returns a new QDateTime based on a quoted string and a date/time format.
There are a full set of operators to compare two QDateTime values where a smaller date/time one means earlier and a larger date/time is later.
The switch from the Julian calendar to the Gregorian calendar happened between October 4 1582 to October 15 1582. This 10 day gap is part of Roman history and the actually skipped these days to adjust to the new calendar. Other regions of the world made the calendar transition at different times, although many areas moved within a year. Notable exceptions are the British Empire which changed over in 1752 and Greece with moved to the Gregorian calendar in 1923.
CopperSpice follows the common practice of using the calendar corresponding to the Roman locale. QDateTime uses the Julian calendar for dates prior to October 4 1582 and the Gregorian calendar starting on October 15 1582. All dates within the gap are invalid.
If you use QDateTime for a calendar date in this range, the day after 4 October 1582 will be 15 October 1582.
There is no year 0 and attempting to construct a date for that year results in an invalid QDateTime.
The year -1 refers to the year "1 before Christ" or BCE which stands for "before common era". The day before January 1 0001 is December 31 0001 BCE. The notation CE stands for "common era" and refers to dates "after Christ".
Before Common Era
Common Era
The QDate class is implemented using a qint64 which stores the number of days since November 24 4714 BCE in the Gregorian calendar. QDateTime is also implemented using a qint64 which stores the number of milliseconds since Jan 1 1970 at midnight UTC.
For most use cases QDate and QDateTime will store the same date value. You should use QDate to prevent an overflow when the calendar date might be extremely far in the past or the future.
QDateTime uses the operating system time zone information to determine the offset of local time from UTC. If the system is not configured correctly QDateTime may return incorrect values.
QDateTime::currentDateTime() returns a QDateTime expressed as local time. Use the method toUTC() to convert local time to UTC time. Operations such as addSecs() and secsTo() are aware of daylight saving time (DST).
QDateTime uses the operating system time zone database to determine when Daylight Saving Time starts and ends for a given locale.
QDateTime::QDateTime | ( | ) |
Constructs an invalid QDateTime.
|
explicit |
Constructs a QDateTime with the given date and a valid QTime of 00:00:00.000.
QDateTime::QDateTime | ( | const QDateTime & | other | ) |
Copy constructs a new QDateTime from other.
QDateTime::QDateTime | ( | QDateTime && | other | ) |
Move constructs a new QDateTime from other.
QDateTime::~QDateTime | ( | ) |
Destroys this QDateTime.
|
nodiscard |
Adds the number of days to the current QDateTime and returns the result. If days is negative the value for days is subtracted from this QDateTime.
QDateTime QDateTime::addDuration | ( | std::chrono::milliseconds | msecs | ) | const |
Adds the number of msecs to the current QDateTime and returns the result. If msecs is negative the value for msecs is subtracted from this QDateTime.
|
nodiscard |
Adds the given number of months to the current QDateTime and returns the result. If months is negative the value for months is subtracted from this QDateTime.
|
nodiscard |
|
nodiscard |
Adds the given number of seconds to the current QDateTime and returns the result. If seconds is negative the value for seconds is subtracted from this QDateTime.
|
nodiscard |
Adds the given number of years to the current QDateTime and returns the result. If years is negative the value for years is subtracted from this QDateTime.
|
static |
Returns the current QDateTime based on the system clock. If no value for zone is passed the local time zone will be used.
|
static |
Returns a QDateTime based on the system clock, in UTC.
|
static |
Returns the number of milliseconds since Jan 1 1970 at midnight UTC.
|
static |
Returns the number of seconds since Jan 1 1970 at midnight UTC.
QDate QDateTime::date | ( | ) | const |
qint64 QDateTime::daysTo | ( | const QDateTime & | value | ) | const |
|
static |
Constructs a new QDateTime containing a copy of the CFDate date.
|
static |
Returns a QDateTime where the date and time correspond to the given number of msecs since Jan 1 1970 at midnight UTC, with the specified timeZone.
|
static |
Constructs a new QDateTime containing a copy of the NSDate date.
|
static |
Returns a QDateTime containing the date and time equal to the number of seconds after Jan 1 1970 at midnight UTC. If a valid timeZone is specified the returned QDateTime will be adjusted accordingly. If no timeZone is passed the local system time zone will be used.
There are values for seconds which will result in a QDateTime that is invalid. For these values the behavior of this method is undefined.
|
inlinestatic |
Returns a QDateTime where the date and time is calculated by adding msecs to Jan 1 1970 at midnight local time.
Returns the QDateTime represented by str using the given format. Returns an invalid QDateTime if str does not match the specified format.
The day and month names must be given in the user locale. You can only use the English names if the current locale is English.
These expressions may be used for the date part of the format string.
Expression | Output |
---|---|
d | the day as number without a leading zero (1 to 31) |
dd | the day as number with a leading zero (01 to 31) |
ddd | the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). |
dddd | the long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName(). |
M | the month as number without a leading zero (1-12) |
MM | the month as number with a leading zero (01-12) |
MMM | the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). |
MMMM | the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). |
yy | the year as two digit number (00-99) |
yyyy | the year as four digit number |
These expressions may be used for the time part of the format string.
Expression | Output |
---|---|
h | hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
H | hour without a leading zero (0 to 23, even with AM/PM display) |
HH | hour with a leading zero (00 to 23, even with AM/PM display) |
m | minute without a leading zero (0 to 59) |
mm | minute with a leading zero (00 to 59) |
s | second without a leading zero (0 to 59) |
ss | second with a leading zero (00 to 59) |
z | milliseconds without leading zeroes (0 to 999) |
zzz | milliseconds with leading zeroes (000 to 999) |
AP or A | interpret as an AM/PM time, AP must be either "AM" or "PM". |
ap or a | Interpret as an AM/PM time, ap must be either "am" or "pm". |
All other input characters will be treated as text. Any sequence of characters that are enclosed in singlequotes will also be treated as text and not be used as an expression.
If the format is not satisfied an invalid QDateTime is returned.
The expressions which do not expect leading zeroes (d, M) will be greedy. This means they will use two digits even if this will put them outside the accepted range of values and leaves too few digits for other sections. For example, the following format string does not mean January 30 because the M will grab two digits, resulting in an invalid date.
For any field that is not represented in the format the following defaults are used:
Field | Default value |
---|---|
Year | 1900 |
Month | 1 (January) |
Day | 1 |
Hour | 0 |
Minute | 0 |
Second | 0 |
|
static |
Returns the QDateTime represented by str using the given format. Returns an invalid QDateTime if str does not match the specified format.
|
static |
Returns a QDateTime where the date and time are the number of seconds since Jan 1 1970 at midnight UTC with the given timeZone.
bool QDateTime::isDaylightTime | ( | ) | const |
Returns true if this QDateTime falls in the region of Daylight Saving Time.
bool QDateTime::isNull | ( | ) | const |
Returns true if both the date and the time are null, otherwise returns false. A null QDateTime is invalid.
bool QDateTime::isValid | ( | ) | const |
Returns true if both the date and the time are valid, otherwise returns false.
qint64 QDateTime::msecsTo | ( | const QDateTime & | value | ) | const |
Returns the number of milliseconds between the current QDateTime and the given value. If the new QDateTime is earlier than the original QDateTime, the value returned is negative.
This method will convert both QDateTime values to UTC. This will ensure the result is correct if one of the values uses daylight saving time (DST) and the other does not.
int QDateTime::offsetFromUtc | ( | ) | const |
Returns the number of seconds between the time zone for this QDateTime and the UTC clock.
|
inline |
Returns true if this QDateTime is different from the given value, otherwise returns false. Two datetimes are different if the date, the time, or the time zone components are different.
bool QDateTime::operator< | ( | const QDateTime & | value | ) | const |
Returns true if this QDateTime is earlier than the given value, otherwise returns false.
|
inline |
Returns true if this QDateTime is earlier than or equal to the given value, otherwise returns false.
QDateTime & QDateTime::operator= | ( | const QDateTime & | other | ) |
Copy assigns from other and returns a reference to this object.
|
inline |
Move assigns from other and returns a reference to this object.
bool QDateTime::operator== | ( | const QDateTime & | value | ) | const |
Returns true if this QDateTime is equal to the given value, otherwise returns false.
|
inline |
Returns true if this QDateTime is later than the given value, otherwise returns false.
|
inline |
Returns true if this QDateTime is later than or equal to the given value, otherwise returns false.
qint64 QDateTime::secsTo | ( | const QDateTime & | value | ) | const |
Returns the number of seconds between the current QDateTime and the given value. If the new QDateTime is earlier than the original QDateTime, the value returned is negative.
This method will convert both QDateTime values to UTC. This will ensure the result is correct if one of the values uses daylight saving time (DST) and the other does not.
void QDateTime::setDate | ( | const QDate & | date | ) |
void QDateTime::setMSecsSinceEpoch | ( | qint64 | msecs | ) |
Sets the date and time for this QDateTime which corresponds to the given number of msecs since Jan 1 1970 at midnight UTC.
There are values for msecs which will result in a QDateTime that is invalid. For these values the behavior of this method is undefined.
void QDateTime::setSecsSinceEpoch | ( | qint64 | seconds | ) |
Sets the date and time for this QDateTime which corresponds to the given number of seconds since Jan 1 1970 at midnight UTC.
void QDateTime::setTime | ( | const QTime & | time | ) |
void QDateTime::setTime_t | ( | quint64 | seconds | ) |
Sets the date and time which correspond to the given number of seconds since Jan 1 1970 at midnight UTC.
void QDateTime::setTimeZone | ( | const QTimeZone & | toZone | ) |
Sets the time zone for this QDateTime to toZone. If the specified time zone is invalid then the QDateTime will be invalid.
|
inline |
Swaps other with this QDateTime.
QTimeZone QDateTime::timeRepresentation | ( | ) | const |
Returns the time zone for the current QDateTime. Equivalent to calling QDateTime::timeZone().
QTimeZone QDateTime::timeZone | ( | ) | const |
Returns the time zone for the current QDateTime. Equivalent to calling QDateTime::timeRepresentation().
QString QDateTime::timeZoneAbbreviation | ( | ) | const |
Returns the Time Zone Abbreviation for this QDateTime. The abbreviation is not guaranteed to be unique to a particular time zone and should not be used in place of the ID or display name.
An example abbreviation for Chicago or New Orleans would be CST or CDT for "central standard time" and "central daylight time". In Amsterdam or Berlin the abbreviation will be CET or CEST for "central european time" and "central european summer time".
CFDateRef QDateTime::toCFDate | ( | ) | const |
Creates a CFDate from this QDateTime. The caller owns the CFDate object and is responsible for releasing it.
QDateTime QDateTime::toLocalTime | ( | ) | const |
Returns the current QDateTime converted to the local time zone.
qint64 QDateTime::toMSecsSinceEpoch | ( | ) | const |
Returns this QDateTime as the number of milliseconds since Jan 1 1970 at midnight UTC. This method returns a unique value for all valid dates. If the QDateTime is invalid the result is undefined.
NSDate * QDateTime::toNSDate | ( | ) | const |
Creates an NSDate from a QDateTime. The NSDate object is autoreleased.
QDateTime QDateTime::toOffsetFromUtc | ( | qint64 | offsetSeconds | ) | const |
Returns a copy of the current QDateTime which is converted to a time zone corresponding to the offsetSeconds relative to UTC. If the offsetSeconds equals 0 then the date time will be in UTC.
qint64 QDateTime::toSecsSinceEpoch | ( | ) | const |
Converts this QDateTime to the number of seconds since Jan 1 1970 at midnight UTC. For all valid dates this method returns a unique value.
On systems which do not support time zones this method will behave as if local time were UTC. The behavior for this method is undefined if the QDateTime is not valid.
|
inline |
Converts this QDateTime to a value in milliseconds using the std::chrono::system_clock. The value is the duration from Jan 1 1970 at midnight UTC to this QDateTime.
|
inline |
Converts this QDateTime to a value in seconds using the std::chrono::system_clock. The value is the duration from Jan 1 1970 at midnight UTC to this QDateTime.
Returns this QDateTime as a string. The format parameter determines the format of the result string. If the QDateTime is invalid an empty string will be returned.
These expressions are used for the date.
Expression | Output |
---|---|
d | the day as number without a leading zero (1 to 31) |
dd | the day as number with a leading zero (01 to 31) |
ddd | the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). |
dddd | the long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName(). |
M | the month as number without a leading zero (1-12) |
MM | the month as number with a leading zero (01-12) |
MMM | the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). |
MMMM | the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). |
yy | the year as two digit number (00-99) |
yyyy | the year as four digit number |
These expressions are used for the time.
Expression | Output |
---|---|
h | hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
m | minute without a leading zero (0 to 59) |
mm | minute with a leading zero (00 to 59) |
s | second without a leading zero (0 to 59) |
ss | second with a leading zero (00 to 59) |
z | milliseconds without leading zeroes (0 to 999) |
zzz | milliseconds with leading zeroes (000 to 999) |
AP | AM/PM display. AP will be replaced by either "AM" or "PM". |
ap | am/pm display. ap will be replaced by either "am" or "pm". |
All other input characters will be ignored. Any sequence of characters which are enclosed in single quotes will be treated as text and not used as an expression. Two consecutive singlequotes are replaced by a single quote in the output.
The following examples assume the QDateTime is "21 May 2001 14:13:09".
Format | Result |
---|---|
dd.MM.yyyy | 21.05.2001 |
ddd MMMM d yy | Tue May 21 01 |
hh:mm:ss.zzz | 14:13:09.042 |
h:m:s ap | 2:13:9 pm |
QString QDateTime::toString | ( | Qt::DateFormat | format = Qt::TextDate | ) | const |
Returns this QDateTime as a string in the given format. If this QDateTime is invalid an empty string will be returned.
quint64 QDateTime::toTime_t | ( | ) | const |
Converts this QDateTime to the number of seconds which have elapsed since Jan 1, 1970 at midnight UTC.
QDateTime QDateTime::toTimeZone | ( | const QTimeZone & | timeZone | ) | const |
Returns a copy of this QDateTime converted to the given timeZone.
QDateTime QDateTime::toUTC | ( | ) | const |
Converts the current QDateTime to UTC.
Refer to UTC Defined for additional information.
|
friend |
Writes the given dateTime to the stream. Returns a reference to the stream.
Refer to Serializing Data Types for additional information.
|
friend |
Reads from the stream into the given dateTime. Returns a reference to the stream.
Refer to Serializing Data Types for additional information.