Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore \reentrant More...
#include <qdatetime.h>
Public Member Functions | |
constexpr | QDate () |
Constructs a null date. | |
QDate (int y, int m, int d) | |
Constructs a date with year y, month m and day d. | |
QDate (int y, int m, int d, QCalendar cal) | |
constexpr bool | isNull () const |
Returns true if the date is null; otherwise returns false . | |
constexpr bool | isValid () const |
Returns true if this date is valid; otherwise returns false . | |
int | year () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | month () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | day () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | dayOfWeek () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | dayOfYear () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | daysInMonth () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | daysInYear () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
int | weekNumber (int *yearNum=nullptr) const |
Returns the ISO 8601 week number (1 to 53). | |
int | year (QCalendar cal) const |
Returns the year of this date. | |
int | month (QCalendar cal) const |
Returns the month-number for the date. | |
int | day (QCalendar cal) const |
Returns the day of the month for this date. | |
int | dayOfWeek (QCalendar cal) const |
Returns the weekday (1 = Monday to 7 = Sunday) for this date. | |
int | dayOfYear (QCalendar cal) const |
Returns the day of the year (1 for the first day) for this date. | |
int | daysInMonth (QCalendar cal) const |
Returns the number of days in the month for this date. | |
int | daysInYear (QCalendar cal) const |
Returns the number of days in the year for this date. | |
QDateTime | startOfDay (const QTimeZone &zone) const |
QDateTime | endOfDay (const QTimeZone &zone) const |
QDateTime | startOfDay () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QDateTime | endOfDay () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
bool | setDate (int year, int month, int day) |
bool | setDate (int year, int month, int day, QCalendar cal) |
void | getDate (int *year, int *month, int *day) const |
QDate | addDays (qint64 days) const |
Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative). | |
QDate | addMonths (int months) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QDate | addYears (int years) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QDate | addMonths (int months, QCalendar cal) const |
Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative). | |
QDate | addYears (int years, QCalendar cal) const |
Returns a QDate object containing a date nyears later than the date of this object (or earlier if nyears is negative). | |
qint64 | daysTo (QDate d) const |
Returns the number of days from this date to d (which is negative if d is earlier than this date). | |
constexpr qint64 | toJulianDay () const |
Converts the date to a Julian day. | |
Static Public Member Functions | |
static QDate | currentDate () |
Returns the system clock's current date. | |
static bool | isValid (int y, int m, int d) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the specified date (year, month, and day) is valid in the Gregorian calendar; otherwise returns false . | |
static bool | isLeapYear (int year) |
Returns true if the specified year is a leap year in the Gregorian calendar; otherwise returns false . | |
static constexpr QDate | fromJulianDay (qint64 jd_) |
Converts the Julian day jd to a QDate. | |
Friends | |
class | QDateTime |
class | QDateTimeParser |
class | QDateTimePrivate |
constexpr bool | comparesEqual (const QDate &lhs, const QDate &rhs) noexcept |
constexpr Qt::strong_ordering | compareThreeWay (const QDate &lhs, const QDate &rhs) noexcept |
Q_CORE_EXPORT QDataStream & | operator<< (QDataStream &, QDate) |
Writes the date to stream out. | |
Q_CORE_EXPORT QDataStream & | operator>> (QDataStream &, QDate &) |
Reads a date from stream in into the date. | |
\inmodule QtCore \reentrant
The QDate class provides date functions.
\compares strong \compareswith strong std::chrono::year_month_day std::chrono::year_month_day_last \ std::chrono::year_month_weekday std::chrono::year_month_weekday_last These comparison operators are only available when using C++20. \endcompareswith
A QDate object represents a particular day, regardless of calendar, locale or other settings used when creating it or supplied by the system. It can report the year, month and day of the month that represent the day with respect to the proleptic Gregorian calendar or any calendar supplied as a QCalendar object. QDate objects should be passed by value rather than by reference to const; they simply package qint64
.
A QDate object is typically created by giving the year, month, and day numbers explicitly. Note that QDate interprets year numbers less than 100 as presented, i.e., as years 1 through 99, without adding any offset. The static function currentDate() creates a QDate object containing the date read from the system clock. An explicit date can also be set using setDate(). The fromString() function returns a QDate given a string and a date format which is used to interpret the date within the string.
The year(), month(), and day() functions provide access to the year, month, and day numbers. When more than one of these values is needed, it is more efficient to call QCalendar::partsFromDate(), to save repeating (potentially expensive) calendrical calculations.
Also, dayOfWeek() and dayOfYear() functions are provided. The same information is provided in textual format by toString(). QLocale can map the day numbers to names, QCalendar can map month numbers to names.
QDate provides a full set of operators to compare two QDate objects where smaller means earlier, and larger means later.
You can increment (or decrement) a date by a given number of days using addDays(). Similarly you can use addMonths() and addYears(). The daysTo() function returns the number of days between two dates.
The daysInMonth() and daysInYear() functions return how many days there are in this date's month and year, respectively. The isLeapYear() function indicates whether a date is in a leap year. QCalendar can also supply this information, in some cases more conveniently.
Definition at line 28 of file qdatetime.h.
|
inlineconstexpr |
QDate::QDate | ( | int | y, |
int | m, | ||
int | d ) |
Constructs a date with year y, month m and day d.
The date is understood in terms of the Gregorian calendar. If the specified date is invalid, the date is not set and isValid() returns false
.
Definition at line 442 of file qdatetime.cpp.
QDate::QDate | ( | int | y, |
int | m, | ||
int | d, | ||
QCalendar | cal ) |
Definition at line 449 of file qdatetime.cpp.
Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
Returns a null date if the current date is invalid or the new date is out of range.
Definition at line 1399 of file qdatetime.cpp.
|
nodiscard |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1477 of file qdatetime.cpp.
Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).
Uses cal as calendar, if supplied, else the Gregorian calendar.
Definition at line 1445 of file qdatetime.cpp.
|
nodiscard |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1542 of file qdatetime.cpp.
Returns a QDate object containing a date nyears later than the date of this object (or earlier if nyears is negative).
Uses cal as calendar, if supplied, else the Gregorian calendar.
Definition at line 1519 of file qdatetime.cpp.
|
static |
Returns the system clock's current date.
int QDate::day | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 642 of file qdatetime.cpp.
int QDate::day | ( | QCalendar | cal | ) | const |
Returns the day of the month for this date.
Uses cal as calendar if supplied, else the Gregorian calendar (for which the return ranges from 1 to 31). Returns 0 if the date is invalid.
Definition at line 628 of file qdatetime.cpp.
int QDate::dayOfWeek | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 674 of file qdatetime.cpp.
int QDate::dayOfWeek | ( | QCalendar | cal | ) | const |
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
Uses cal as calendar if supplied, else the Gregorian calendar. Returns 0 if the date is invalid. Some calendars may give special meaning (e.g. intercallary days) to values greater than 7.
Definition at line 662 of file qdatetime.cpp.
int QDate::dayOfYear | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 702 of file qdatetime.cpp.
int QDate::dayOfYear | ( | QCalendar | cal | ) | const |
Returns the day of the year (1 for the first day) for this date.
Uses cal as calendar if supplied, else the Gregorian calendar. Returns 0 if either the date or the first day of its year is invalid.
Definition at line 688 of file qdatetime.cpp.
int QDate::daysInMonth | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 735 of file qdatetime.cpp.
int QDate::daysInMonth | ( | QCalendar | cal | ) | const |
Returns the number of days in the month for this date.
Uses cal as calendar if supplied, else the Gregorian calendar (for which the result ranges from 28 to 31). Returns 0 if the date is invalid.
Definition at line 721 of file qdatetime.cpp.
int QDate::daysInYear | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 766 of file qdatetime.cpp.
int QDate::daysInYear | ( | QCalendar | cal | ) | const |
Returns the number of days in the year for this date.
Uses cal as calendar if supplied, else the Gregorian calendar (for which the result is 365 or 366). Returns 0 if the date is invalid.
Definition at line 754 of file qdatetime.cpp.
Returns the number of days from this date to d (which is negative if d is earlier than this date).
Returns 0 if either date is invalid.
Example:
Definition at line 1573 of file qdatetime.cpp.
QDateTime QDate::endOfDay | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1127 of file qdatetime.cpp.
Returns the end-moment of the day.
When a day ends depends on a how time is described: each day starts and ends earlier for those in time-zones further west and later for those in time-zones further east. The time representation to use can be specified by an optional time zone. The default time representation is the system's local time.
Usually, the end of the day is one millisecond before the midnight, 24:00: however, if a time-zone transition causes the given date to skip over that moment (e.g. a DST spring-forward skipping over 23:00 and the following hour), the actual latest time in the day is returned. This can only arise when the time representation is a time-zone or local time.
When zone has a timeSpec() of Qt::OffsetFromUTC or Qt::UTC, the time representation has no transitions so the end of the day is QTime(23, 59, 59, 999).
In the rare case of a date that was entirely skipped (this happens when a zone east of the international date-line switches to being west of it), the return shall be invalid. Passing an invalid time-zone as zone will also produce an invalid result, as shall dates that end outside the range representable by QDateTime.
Definition at line 1097 of file qdatetime.cpp.
Converts the Julian day jd to a QDate.
Definition at line 170 of file qdatetime.h.
void QDate::getDate | ( | int * | year, |
int * | month, | ||
int * | day ) const |
Extracts the date's year, month, and day, and assigns them to year, *month, and *day. The pointers may be null.
Returns 0 if the date is invalid.
{const}.Definition at line 1374 of file qdatetime.cpp.
|
static |
Returns true
if the specified year is a leap year in the Gregorian calendar; otherwise returns false
.
Definition at line 1928 of file qdatetime.cpp.
|
inlineconstexpr |
Returns true
if the date is null; otherwise returns false
.
A null date is invalid.
Definition at line 71 of file qdatetime.h.
|
inlineconstexpr |
Returns true
if this date is valid; otherwise returns false
.
Definition at line 72 of file qdatetime.h.
|
static |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true
if the specified date (year, month, and day) is valid in the Gregorian calendar; otherwise returns false
.
Example:
Definition at line 1914 of file qdatetime.cpp.
int QDate::month | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 609 of file qdatetime.cpp.
int QDate::month | ( | QCalendar | cal | ) | const |
Returns the month-number for the date.
Numbers the months of the year starting with 1 for the first. Uses cal as calendar if supplied, else the Gregorian calendar, for which the month numbering is as follows:
\list
Returns 0 if the date is invalid. Note that some calendars may have more than 12 months in some years.
Definition at line 595 of file qdatetime.cpp.
bool QDate::setDate | ( | int | year, |
int | month, | ||
int | day ) |
Sets this to represent the date, in the Gregorian calendar, with the given year, month and day numbers. Returns true if the resulting date is valid, otherwise it sets this to represent an invalid date and returns false.
Definition at line 1338 of file qdatetime.cpp.
bool QDate::setDate | ( | int | year, |
int | month, | ||
int | day, | ||
QCalendar | cal ) |
Sets this to represent the date, in the given calendar cal, with the given year, month and day numbers. Returns true if the resulting date is valid, otherwise it sets this to represent an invalid date and returns false.
Definition at line 1356 of file qdatetime.cpp.
QDateTime QDate::startOfDay | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 972 of file qdatetime.cpp.
Returns the start-moment of the day.
When a day starts depends on a how time is described: each day starts and ends earlier for those in time-zones further west and later for those in time-zones further east. The time representation to use can be specified by an optional time zone. The default time representation is the system's local time.
Usually, the start of the day is midnight, 00:00: however, if a time-zone transition causes the given date to skip over that midnight (e.g. a DST spring-forward skipping over the first hour of the day day), the actual earliest time in the day is returned. This can only arise when the time representation is a time-zone or local time.
When zone has a timeSpec() of is Qt::OffsetFromUTC or Qt::UTC, the time representation has no transitions so the start of the day is QTime(0, 0).
In the rare case of a date that was entirely skipped (this happens when a zone east of the international date-line switches to being west of it), the return shall be invalid. Passing an invalid time-zone as zone will also produce an invalid result, as shall dates that start outside the range representable by QDateTime.
Definition at line 941 of file qdatetime.cpp.
|
inlineconstexpr |
Converts the date to a Julian day.
Definition at line 172 of file qdatetime.h.
int QDate::weekNumber | ( | int * | yearNumber = nullptr | ) | const |
Returns the ISO 8601 week number (1 to 53).
Returns 0 if the date is invalid. Otherwise, returns the week number for the date. If yearNumber is not \nullptr (its default), stores the year as {yearNumber}.
In accordance with ISO 8601, each week falls in the year to which most of its days belong, in the Gregorian calendar. As ISO 8601's week starts on Monday, this is the year in which the week's Thursday falls. Most years have 52 weeks, but some have 53.
Definition at line 790 of file qdatetime.cpp.
int QDate::year | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 557 of file qdatetime.cpp.
int QDate::year | ( | QCalendar | cal | ) | const |
Returns the year of this date.
Uses cal as calendar, if supplied, else the Gregorian calendar.
Returns 0 if the date is invalid. For some calendars, dates before their first year may all be invalid.
If using a calendar which has a year 0, check using isValid() if the return is 0. Such calendars use negative year numbers in the obvious way, with year 1 preceded by year 0, in turn preceded by year -1 and so on.
Some calendars, despite having no year 0, have a conventional numbering of the years before their first year, counting backwards from 1. For example, in the proleptic Gregorian calendar, successive years before 1 CE (the first year) are identified as 1 BCE, 2 BCE, 3 BCE and so on. For such calendars, negative year numbers are used to indicate these years before year 1, with -1 indicating the year before 1.
Definition at line 543 of file qdatetime.cpp.
Definition at line 207 of file qdatetime.h.
|
friend |
Definition at line 210 of file qdatetime.h.
|
friend |
Writes the date to stream out.
Definition at line 6005 of file qdatetime.cpp.
|
friend |
Reads a date from stream in into the date.
Definition at line 6021 of file qdatetime.cpp.
|
friend |
Definition at line 203 of file qdatetime.h.
|
friend |
Definition at line 204 of file qdatetime.h.
|
friend |
Definition at line 205 of file qdatetime.h.