QDateTime¶
- PyQt5.QtCore.QDateTime
Description¶
The QDateTime class provides date and time functions.
A QDateTime object encodes a calendar date and a clock time (a “datetime”). It combines features of the QDate and QTime classes. It can read the current datetime from the system clock. It provides functions for comparing datetimes and for manipulating a datetime by adding a number of seconds, days, months, or years.
A QDateTime object is typically created either by giving a date and time explicitly in the constructor, or by using the static function currentDateTime() that returns a QDateTime object set to the system clock’s time. The date and time can be changed with setDate() and setTime(). A datetime can also be set using the setTime_t() function that takes a POSIX-standard “number of seconds since 00:00:00 on January 1, 1970” value. The fromString() function returns a QDateTime, given a string and a date format used to interpret the date within the string.
The date() and time() functions provide access to the date and time parts of the datetime. The same information is provided in textual format by the toString() function.
QDateTime provides a full set of operators to compare two QDateTime objects, where smaller means earlier and larger means later.
You can increment (or decrement) a datetime by a given number of milliseconds using addMSecs(), seconds using addSecs(), or days using addDays(). Similarly, you can use addMonths() and addYears(). The daysTo() function returns the number of days between two datetimes, secsTo() returns the number of seconds between two datetimes, and msecsTo() returns the number of milliseconds between two datetimes.
QDateTime can store datetimes as LocalTime or as UTC. currentDateTime() returns a QDateTime expressed as local time; use toUTC() to convert it to UTC. You can also use timeSpec() to find out if a QDateTime object stores a UTC time or a local time. Operations such as addSecs() and secsTo() are aware of daylight-saving time (DST).
Note: QDateTime does not account for leap seconds.
Remarks¶
No Year 0¶
There is no year 0. Dates in that year are considered invalid. The year -1 is the year “1 before Christ” or “1 before current era.” The day before 1 January 1 CE is 31 December 1 BCE.
Range of Valid Dates¶
The range of valid values able to be stored in QDateTime is dependent on the internal storage implementation. QDateTime is currently stored in a qint64 as a serial msecs value encoding the date and time. This restricts the date range to about /- 292 million years, compared to the QDate range of /- 2 billion years. Care must be taken when creating a QDateTime with extreme values that you do not overflow the storage. The exact range of supported values varies depending on the TimeSpec and time zone.
Use of System Timezone¶
QDateTime uses the system’s time zone information to determine the offset of local time from UTC. If the system is not configured correctly or not up-to-date, QDateTime will give wrong results as well.
Daylight-Saving Time (DST)¶
QDateTime takes into account the system’s time zone information when dealing with DST. On modern Unix systems, this means it applies the correct historical DST data whenever possible. On Windows, where the system doesn’t support historical DST data, historical accuracy is not maintained with respect to DST.
The range of valid dates taking DST into account is 1970-01-01 to the present, and rules are in place for handling DST correctly until 2037-12-31, but these could change. For dates falling outside that range, QDateTime makes a best guess using the rules for year 1970 or 2037, but we can’t guarantee accuracy. This means QDateTime doesn’t take into account changes in a locale’s time zone before 1970, even if the system’s time zone database supports that information.
QDateTime takes into consideration the Standard Time to Daylight-Saving Time transition. For example if the transition is at 2am and the clock goes forward to 3am, then there is a “missing” hour from 02:00:00 to 02:59:59.999 which QDateTime considers to be invalid. Any date maths performed will take this missing hour into account and return a valid result.
Offset From UTC¶
A TimeSpec of OffsetFromUTC is also supported. This allows you to define a QDateTime relative to UTC at a fixed offset of a given number of seconds from UTC. For example, an offset of 3600 seconds is one hour ahead of UTC and is usually written in ISO standard notation as “UTC 01:00”. Daylight-Saving Time never applies with this TimeSpec.
There is no explicit size restriction to the offset seconds, but there is an implicit limit imposed when using the toString() and fromString() methods which use a format of [ |-]hh:mm, effectively limiting the range to /- 99 hours and 59 minutes and whole minutes only. Note that currently no time zone lies outside the range of /- 14 hours.
Time Zone Support¶
A TimeSpec of Qt::TimeZone is also supported in conjunction with the QTimeZone class. This allows you to define a datetime in a named time zone adhering to a consistent set of daylight-saving transition rules. For example a time zone of “Europe/Berlin” will apply the daylight-saving rules as used in Germany since 1970. Note that the transition rules applied depend on the platform support. See the QTimeZone documentation for more details.
Enums¶
- YearRange
TODO
Member
Value
Description
First TODO
TODO
Last TODO
TODO
Methods¶
- __init__()
TODO
- __init__(Union[QDateTime, datetime.datetime])
TODO
- __init__(Union[QDate, datetime.date])
TODO
- __init__(Union[QDate, datetime.date], Union[QTime, datetime.time], timeSpec: TimeSpec = LocalTime)
TODO
- __init__(int, int, int, int, int, second: int = 0, msec: int = 0, timeSpec: int = 0)
TODO
- addDays(int) QDateTime
TODO
- addMonths(int) QDateTime
TODO
- addMSecs(int) QDateTime
TODO
- addSecs(int) QDateTime
TODO
- addYears(int) QDateTime
TODO
- __bool__() int
TODO
-
@staticmethod
currentDateTime() QDateTime TODO
-
@staticmethod
currentDateTimeUtc() QDateTime TODO
-
@staticmethod
currentMSecsSinceEpoch() int TODO
-
@staticmethod
currentSecsSinceEpoch() int TODO
- daysTo(Union[QDateTime, datetime.datetime]) int
TODO
- __eq__(Union[QDateTime, datetime.datetime]) bool
TODO
-
@staticmethod
fromMSecsSinceEpoch(int) QDateTime TODO
-
@staticmethod
fromSecsSinceEpoch(int, spec: TimeSpec = LocalTime, offsetSeconds: int = 0) QDateTime TODO
-
@staticmethod
fromString(str, format: DateFormat = TextDate) QDateTime TODO
-
@staticmethod
fromString(str, str) QDateTime TODO
-
@staticmethod
fromTime_t(int) QDateTime TODO
- __ge__(Union[QDateTime, datetime.datetime]) bool
TODO
- __gt__(Union[QDateTime, datetime.datetime]) bool
TODO
- __hash__() int
TODO
- isDaylightTime() bool
TODO
- isNull() bool
TODO
- isValid() bool
TODO
- __le__(Union[QDateTime, datetime.datetime]) bool
TODO
- __lt__(Union[QDateTime, datetime.datetime]) bool
TODO
- msecsTo(Union[QDateTime, datetime.datetime]) int
TODO
- __ne__(Union[QDateTime, datetime.datetime]) bool
TODO
- offsetFromUtc() int
TODO
- __repr__() str
TODO
- secsTo(Union[QDateTime, datetime.datetime]) int
TODO
- setMSecsSinceEpoch(int)
TODO
- setOffsetFromUtc(int)
TODO
- setSecsSinceEpoch(int)
TODO
- setTime_t(int)
TODO
- setTimeSpec(TimeSpec)
See also
- setTimeZone(QTimeZone)
TODO
- swap(QDateTime)
TODO
- timeSpec() TimeSpec
See also
- timeZone() QTimeZone
TODO
- timeZoneAbbreviation() str
TODO
- toLocalTime() QDateTime
TODO
- toMSecsSinceEpoch() int
TODO
- toOffsetFromUtc(int) QDateTime
TODO
- toPyDateTime() datetime.datetime
TODO
- toSecsSinceEpoch() int
TODO
- toString(format: DateFormat = TextDate) str
TODO
- toString(str) str
TODO
- toString(str, QCalendar) str
TODO
- toTime_t() int
TODO
- toUTC() QDateTime
TODO