Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdatetime.cpp File Reference

(020ed1c435dce07b7f1894b89954d3b228b3f822)

#include "qdatetime.h"
#include "qcalendar.h"
#include "qdatastream.h"
#include "qdebug.h"
#include "qlocale.h"
#include "qset.h"
#include "private/qcalendarmath_p.h"
#include "private/qdatetime_p.h"
#include "private/qgregoriancalendar_p.h"
#include "private/qlocale_tools_p.h"
#include "private/qlocaltime_p.h"
#include "private/qnumeric_p.h"
#include "private/qstringconverter_p.h"
#include "private/qstringiterator_p.h"
#include <cmath>
#include <private/qtools_p.h>
+ Include dependency graph for qdatetime.cpp:

Go to the source code of this file.

Typedefs

typedef QDateTimePrivate::QDateTimeShortData ShortData
 
typedef QDateTimePrivate::QDateTimeData QDateTimeData
 

Enumerations

enum class  DaySide { Start , End }
 

Functions

static QDate fixedDate (QCalendar::YearMonthDay parts, QCalendar cal)
 
static QDate fixedDate (QCalendar::YearMonthDay parts)
 
static QString toOffsetString (Qt::DateFormat format, int offset)
 
static bool inDateTimeRange (qint64 jd, DaySide side)
 
static QDateTime toEarliest (QDate day, const QTimeZone &zone)
 
static QDateTime toLatest (QDate day, const QTimeZone &zone)
 
static qint64 msecsToJulianDay (qint64 msecs)
 
static QDate msecsToDate (qint64 msecs)
 
static QTime msecsToTime (qint64 msecs)
 
static bool daysAndMillisOverflow (qint64 days, qint64 millisInDay, qint64 *sumMillis)
 
static qint64 timeToMSecs (QDate date, QTime time)
 
static bool millisInSystemRange (qint64 millis, qint64 slack=0)
 
static int systemTimeYearMatching (int year)
 
static auto millisToWithinRange (qint64 millis)
 
static constexpr QDateTimePrivate::TransitionOptions toTransitionOptions (QDateTime::TransitionResolution res)
 
static constexpr QDateTimePrivate::TransitionOptions toTransitionOptions (QDateTimePrivate::DaylightStatus dst)
 
static QDateTimePrivate::ZoneState stateAtMillis (const QTimeZone &zone, qint64 millis, QDateTimePrivate::TransitionOptions resolve)
 
static bool specCanBeSmall (Qt::TimeSpec spec)
 
static bool msecsCanBeSmall (qint64 msecs)
 
static constexpr QDateTimePrivate::StatusFlags mergeSpec (QDateTimePrivate::StatusFlags status, Qt::TimeSpec spec)
 
static constexpr Qt::TimeSpec extractSpec (QDateTimePrivate::StatusFlags status)
 
static constexpr QDateTimePrivate::StatusFlags mergeDaylightStatus (QDateTimePrivate::StatusFlags sf, QDateTimePrivate::DaylightStatus status)
 
static constexpr QDateTimePrivate::DaylightStatus extractDaylightStatus (QDateTimePrivate::StatusFlags status)
 
static qint64 getMSecs (const QDateTimeData &d)
 
static QDateTimePrivate::StatusFlags getStatus (const QDateTimeData &d)
 
static Qt::TimeSpec getSpec (const QDateTimeData &d)
 
static bool usesSameOffset (const QDateTimeData &a, const QDateTimeData &b)
 
static void refreshZonedDateTime (QDateTimeData &d, const QTimeZone &zone, QDateTimePrivate::TransitionOptions resolve)
 
static void refreshSimpleDateTime (QDateTimeData &d)
 
static void checkValidDateTime (QDateTimeData &d, QDateTime::TransitionResolution resolve)
 
static void reviseTimeZone (QDateTimeData &d, const QTimeZone &zone, QDateTime::TransitionResolution resolve)
 
static void setDateTime (QDateTimeData &d, QDate date, QTime time)
 
static std::pair< QDate, QTimegetDateTime (const QDateTimeData &d)
 
static void massageAdjustedDateTime (QDateTimeData &d, QDate date, QTime time, bool forward)
 
Qt::weak_ordering compareThreeWay (const QDateTime &lhs, const QDateTime &rhs)
 
size_t qHash (const QDateTime &key, size_t seed)
 
size_t qHash (QDate key, size_t seed) noexcept
 
size_t qHash (QTime key, size_t seed) noexcept
 

Typedef Documentation

◆ QDateTimeData

Definition at line 2708 of file qdatetime.cpp.

◆ ShortData

Enumeration Type Documentation

◆ DaySide

enum class DaySide
strong
Enumerator
Start 
End 

Definition at line 837 of file qdatetime.cpp.

Function Documentation

◆ checkValidDateTime()

static void checkValidDateTime ( QDateTimeData & d,
QDateTime::TransitionResolution resolve )
static

Definition at line 3241 of file qdatetime.cpp.

References d, extractSpec(), getStatus(), Qt::LocalTime, Qt::OffsetFromUTC, refreshSimpleDateTime(), refreshZonedDateTime(), resolve(), Qt::TimeZone, toTransitionOptions(), and Qt::UTC.

Referenced by QDateTime::setDate(), and QDateTime::setTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compareThreeWay()

Qt::weak_ordering compareThreeWay ( const QDateTime & lhs,
const QDateTime & rhs )

Definition at line 5233 of file qdatetime.cpp.

◆ daysAndMillisOverflow()

static bool daysAndMillisOverflow ( qint64 days,
qint64 millisInDay,
qint64 * sumMillis )
inlinestatic

Definition at line 2728 of file qdatetime.cpp.

References qAddOverflow(), and qMulOverflow().

Referenced by millisToWithinRange(), setDateTime(), and timeToMSecs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ extractDaylightStatus()

static constexpr QDateTimePrivate::DaylightStatus extractDaylightStatus ( QDateTimePrivate::StatusFlags status)
inlinestaticconstexpr

Definition at line 3102 of file qdatetime.cpp.

References QDateTimePrivate::DaylightTime, QDateTimePrivate::SetToDaylightTime, QDateTimePrivate::SetToStandardTime, QDateTimePrivate::StandardTime, and QDateTimePrivate::UnknownDaylightTime.

Referenced by QDateTime::isDaylightTime(), QDateTime::offsetFromUtc(), QDateTime::timeZoneAbbreviation(), and QDateTime::toMSecsSinceEpoch().

+ Here is the caller graph for this function:

◆ extractSpec()

static constexpr Qt::TimeSpec extractSpec ( QDateTimePrivate::StatusFlags status)
inlinestaticconstexpr

Definition at line 3082 of file qdatetime.cpp.

References QDateTimePrivate::TimeSpecMask, QDateTimePrivate::TimeSpecShift, and toInt().

Referenced by checkValidDateTime(), getSpec(), massageAdjustedDateTime(), QDateTime::offsetFromUtc(), refreshSimpleDateTime(), refreshZonedDateTime(), QDateTime::setMSecsSinceEpoch(), QDateTime::toMSecsSinceEpoch(), and usesSameOffset().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fixedDate() [1/2]

static QDate fixedDate ( QCalendar::YearMonthDay parts)
inlinestatic

Definition at line 62 of file qdatetime.cpp.

References QCalendar::YearMonthDay::day, QDate::fromJulianDay(), QGregorianCalendar::julianFromParts(), QCalendar::YearMonthDay::month, QGregorianCalendar::monthLength(), qMin(), and QCalendar::YearMonthDay::year.

+ Here is the call graph for this function:

◆ fixedDate() [2/2]

static QDate fixedDate ( QCalendar::YearMonthDay parts,
QCalendar cal )
inlinestatic

Definition at line 53 of file qdatetime.cpp.

References QCalendar::YearMonthDay::day, QCalendar::YearMonthDay::month, qMin(), and QCalendar::YearMonthDay::year.

Referenced by QDate::addMonths(), QDate::addMonths(), QDate::addYears(), and QDate::addYears().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getDateTime()

static std::pair< QDate, QTime > getDateTime ( const QDateTimeData & d)
static

Definition at line 3357 of file qdatetime.cpp.

References d, QDate::fromJulianDay(), QTime::fromMSecsSinceStartOfDay(), getMSecs(), getStatus(), QtPrivate::DateTimeConstants::JULIAN_DAY_FOR_EPOCH, QDateTimePrivate::ValidDate, and QDateTimePrivate::ValidTime.

Referenced by QDateTime::addDays(), QDateTime::addMonths(), and QDateTime::addYears().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMSecs()

static qint64 getMSecs ( const QDateTimeData & d)
inlinestatic

Definition at line 3111 of file qdatetime.cpp.

References d.

Referenced by QDateTime::addMSecs(), QDateTime::date(), getDateTime(), QDateTime::isDaylightTime(), QDateTime::offsetFromUtc(), refreshZonedDateTime(), QDateTime::time(), QDateTime::timeZoneAbbreviation(), and QDateTime::toMSecsSinceEpoch().

+ Here is the caller graph for this function:

◆ getSpec()

static Qt::TimeSpec getSpec ( const QDateTimeData & d)
inlinestatic

Definition at line 3131 of file qdatetime.cpp.

References d, extractSpec(), and getStatus().

Referenced by QDateTime::addMSecs(), QDateTime::isDaylightTime(), QDateTime::timeSpec(), and QDateTime::timeZoneAbbreviation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getStatus()

static QDateTimePrivate::StatusFlags getStatus ( const QDateTimeData & d)
inlinestatic

◆ inDateTimeRange()

static bool inDateTimeRange ( qint64 jd,
DaySide side )
static

Definition at line 839 of file qdatetime.cpp.

References End, QtPrivate::DateTimeConstants::JULIAN_DAY_FOR_EPOCH, QtPrivate::DateTimeConstants::MSECS_PER_DAY, and Start.

Referenced by QDate::endOfDay(), and QDate::startOfDay().

+ Here is the caller graph for this function:

◆ massageAdjustedDateTime()

static void massageAdjustedDateTime ( QDateTimeData & d,
QDate date,
QTime time,
bool forward )
inlinestatic

Definition at line 4745 of file qdatetime.cpp.

References d, date, extractSpec(), getStatus(), QTimeZone::isUtcOrFixedOffset(), mergeDaylightStatus(), Q_ASSERT, refreshSimpleDateTime(), QDateTime::RelativeToAfter, QDateTime::RelativeToBefore, resolve(), setDateTime(), QDateTimePrivate::ShortData, state, stateAtMillis(), time, timeToMSecs(), toTransitionOptions(), QDateTimePrivate::UnknownDaylightTime, QDateTimePrivate::ValidDate, QDateTimePrivate::ValidDateTime, and QDateTimePrivate::ValidTime.

Referenced by QDateTime::addDays(), QDateTime::addMonths(), and QDateTime::addYears().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mergeDaylightStatus()

static constexpr QDateTimePrivate::StatusFlags mergeDaylightStatus ( QDateTimePrivate::StatusFlags sf,
QDateTimePrivate::DaylightStatus status )
inlinestaticconstexpr

Definition at line 3089 of file qdatetime.cpp.

References QDateTimePrivate::DaylightTime, QDateTimePrivate::SetToDaylightTime, QDateTimePrivate::SetToStandardTime, and QDateTimePrivate::StandardTime.

Referenced by massageAdjustedDateTime(), refreshZonedDateTime(), and QDateTime::setMSecsSinceEpoch().

+ Here is the caller graph for this function:

◆ mergeSpec()

static constexpr QDateTimePrivate::StatusFlags mergeSpec ( QDateTimePrivate::StatusFlags status,
Qt::TimeSpec spec )
inlinestaticconstexpr

Definition at line 3075 of file qdatetime.cpp.

References QDateTimePrivate::TimeSpecShift.

Referenced by reviseTimeZone().

+ Here is the caller graph for this function:

◆ millisInSystemRange()

static bool millisInSystemRange ( qint64 millis,
qint64 slack = 0 )
inlinestatic

Tests whether system functions can handle a given time.

The range of milliseconds for which the time_t-based functions work depends somewhat on platform (see computeSystemMillisRange() for details). This function tests whether the UTC time millis milliseconds from the epoch is in the supported range.

To test a local time, pass an upper bound on the magnitude of time-zone correction potentially needed as slack: in this case the range is extended by this many milliseconds at each end (where applicable). The function then returns true precisely if millis is within this (possibly) widened range. This doesn't guarantee that the time_t functions can handle the time, so check their returns to be sure. Values for which the function returns false should be assumed unrepresentable.

Definition at line 2767 of file qdatetime.cpp.

References QLocalTime::computeSystemMillisRange().

Referenced by QDateTimePrivate::expressUtcAsLocal(), QDateTimePrivate::localNameAtMillis(), and QDateTimePrivate::localStateAtMillis().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ millisToWithinRange()

static auto millisToWithinRange ( qint64 millis)
static

Definition at line 2861 of file qdatetime.cpp.

References daysAndMillisOverflow(), QGregorianCalendar::julianFromParts(), msecsToJulianDay(), QGregorianCalendar::partsFromJulian(), R, and systemTimeYearMatching().

Referenced by QDateTimePrivate::localNameAtMillis(), and QDateTimePrivate::localStateAtMillis().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ msecsCanBeSmall()

static bool msecsCanBeSmall ( qint64 msecs)
inlinestatic

Definition at line 3064 of file qdatetime.cpp.

Referenced by QDateTime::addMSecs(), refreshZonedDateTime(), setDateTime(), and QDateTime::setMSecsSinceEpoch().

+ Here is the caller graph for this function:

◆ msecsToDate()

static QDate msecsToDate ( qint64 msecs)
static

Definition at line 2716 of file qdatetime.cpp.

References QDate::fromJulianDay(), and msecsToJulianDay().

Referenced by QDateTime::date().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ msecsToJulianDay()

static qint64 msecsToJulianDay ( qint64 msecs)
static

Definition at line 2711 of file qdatetime.cpp.

References QtPrivate::DateTimeConstants::JULIAN_DAY_FOR_EPOCH.

Referenced by QDateTimePrivate::expressUtcAsLocal(), millisToWithinRange(), and msecsToDate().

+ Here is the caller graph for this function:

◆ msecsToTime()

static QTime msecsToTime ( qint64 msecs)
static

Definition at line 2721 of file qdatetime.cpp.

References QTime::fromMSecsSinceStartOfDay().

Referenced by QDateTime::time().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ qHash() [1/3]

size_t qHash ( const QDateTime & key,
size_t seed )
related

Definition at line 6252 of file qdatetime.cpp.

Referenced by QHash< Key, T >::qHash(), QHash< Key, T >::qHash(), and QHash< Key, T >::qHash().

+ Here is the caller graph for this function:

◆ qHash() [2/3]

size_t qHash ( QDate key,
size_t seed )
related

Definition at line 6267 of file qdatetime.cpp.

◆ qHash() [3/3]

size_t qHash ( QTime key,
size_t seed )
related

Definition at line 6278 of file qdatetime.cpp.

◆ refreshSimpleDateTime()

static void refreshSimpleDateTime ( QDateTimeData & d)
static

Definition at line 3227 of file qdatetime.cpp.

References d, extractSpec(), getStatus(), QTimeZone::isUtcOrFixedOffset(), Q_ASSERT, QDateTimePrivate::ShortData, QDateTimePrivate::ValidDate, QDateTimePrivate::ValidDateTime, and QDateTimePrivate::ValidTime.

Referenced by checkValidDateTime(), QDateTimePrivate::create(), massageAdjustedDateTime(), and reviseTimeZone().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ refreshZonedDateTime()

static void refreshZonedDateTime ( QDateTimeData & d,
const QTimeZone & zone,
QDateTimePrivate::TransitionOptions resolve )
static

Definition at line 3167 of file qdatetime.cpp.

References d, extractSpec(), getMSecs(), getStatus(), Qt::LocalTime, mergeDaylightStatus(), msecsCanBeSmall(), Q_ASSERT, Q_UNLIKELY, resolve(), QtPrivate::DateTimeConstants::SECS_PER_DAY, QDateTimePrivate::ShortData, state, stateAtMillis(), Qt::TimeZone, QDateTimePrivate::UnknownDaylightTime, QDateTimePrivate::ValidDate, QDateTimePrivate::ValidDateTime, and QDateTimePrivate::ValidTime.

Referenced by checkValidDateTime(), QDateTimePrivate::create(), and reviseTimeZone().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reviseTimeZone()

static void reviseTimeZone ( QDateTimeData & d,
const QTimeZone & zone,
QDateTime::TransitionResolution resolve )
static

Definition at line 3259 of file qdatetime.cpp.

References d, QDateTimePrivate::DaylightMask, getStatus(), QTimeZone::isUtcOrFixedOffset(), Qt::LocalTime, mergeSpec(), Qt::OffsetFromUTC, Q_ASSERT, refreshSimpleDateTime(), refreshZonedDateTime(), resolve(), Qt::TimeZone, toTransitionOptions(), Qt::UTC, and QDateTimePrivate::ValidDateTime.

Referenced by QDateTime::fromMSecsSinceEpoch(), QDateTime::fromSecsSinceEpoch(), and QDateTime::setTimeZone().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setDateTime()

static void setDateTime ( QDateTimeData & d,
QDate date,
QTime time )
static

Definition at line 3302 of file qdatetime.cpp.

References d, date, QDateTimePrivate::DaylightMask, daysAndMillisOverflow(), QTime::fromMSecsSinceStartOfDay(), QDate::isValid(), QTime::isValid(), QtPrivate::DateTimeConstants::JULIAN_DAY_FOR_EPOCH, QtPrivate::DateTimeConstants::MSECS_PER_DAY, msecsCanBeSmall(), QTime::msecsSinceStartOfDay(), Q_ASSERT, time, toInt(), QDate::toJulianDay(), QDateTimePrivate::ValidDate, QDateTimePrivate::ValidityMask, and QDateTimePrivate::ValidTime.

Referenced by QDateTimePrivate::create(), massageAdjustedDateTime(), QDateTime::setDate(), and QDateTime::setTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ specCanBeSmall()

static bool specCanBeSmall ( Qt::TimeSpec spec)
inlinestatic

Definition at line 3059 of file qdatetime.cpp.

References Qt::LocalTime, and Qt::UTC.

Referenced by QDateTime::setMSecsSinceEpoch().

+ Here is the caller graph for this function:

◆ stateAtMillis()

static QDateTimePrivate::ZoneState stateAtMillis ( const QTimeZone & zone,
qint64 millis,
QDateTimePrivate::TransitionOptions resolve )
inlinestatic

Definition at line 3047 of file qdatetime.cpp.

References QDateTimePrivate::localStateAtMillis(), Qt::LocalTime, resolve(), and Qt::TimeZone.

Referenced by massageAdjustedDateTime(), and refreshZonedDateTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ systemTimeYearMatching()

static int systemTimeYearMatching ( int year)
static

Returns a year, in the system range, with the same day-of-week pattern

Returns the number of a year, in the range supported by system time_t functions, that starts and ends on the same days of the week as year. This implies it is a leap year precisely if year is. If year is before the epoch, a year early in the supported range is used; otherwise, one late in that range. For a leap year, this may be as much as 26 years years from the range's relevant end; for normal years at most a decade from the end.

This ensures that any DST rules based on, e.g., the last Sunday in a particular month will select the same date in the returned year as they would if applied to year. Of course, the zone's rules may be different in year than in the selected year, but it's hard to do better.

Definition at line 2790 of file qdatetime.cpp.

References QGregorianCalendar::leapTest(), Q_ASSERT, and QGregorianCalendar::yearStartWeekDay().

Referenced by QDateTimePrivate::expressUtcAsLocal(), and millisToWithinRange().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ timeToMSecs()

static qint64 timeToMSecs ( QDate date,
QTime time )
static

Definition at line 2735 of file qdatetime.cpp.

References date, daysAndMillisOverflow(), QtPrivate::DateTimeConstants::JULIAN_DAY_FOR_EPOCH, QtPrivate::DateTimeConstants::MSECS_PER_DAY, QTime::msecsSinceStartOfDay(), time, and QDate::toJulianDay().

Referenced by massageAdjustedDateTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toEarliest()

static QDateTime toEarliest ( QDate day,
const QTimeZone & zone )
static

Definition at line 859 of file qdatetime.cpp.

References QTime::addSecs(), QDateTime::date(), QDateTime::isValid(), QTime::msecsSinceStartOfDay(), QDateTime::PreferBefore, Q_ASSERT, Q_UNLIKELY, QDateTime::Reject, time, and QDateTime::time().

Referenced by QDate::startOfDay().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toLatest()

static QDateTime toLatest ( QDate day,
const QTimeZone & zone )
static

Definition at line 1014 of file qdatetime.cpp.

References QTime::addSecs(), QDateTime::date(), QDateTime::isValid(), QTime::msecsSinceStartOfDay(), QDateTime::PreferAfter, Q_ASSERT, Q_UNLIKELY, QDateTime::Reject, time, and QDateTime::time().

Referenced by QDate::endOfDay().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toOffsetString()

static QString toOffsetString ( Qt::DateFormat format,
int offset )
static

Definition at line 284 of file qdatetime.cpp.

References QString::asprintf(), qAbs(), QtPrivate::DateTimeConstants::SECS_PER_HOUR, and Qt::TextDate.

Referenced by QDateTime::timeZoneAbbreviation().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toTransitionOptions() [1/2]

◆ toTransitionOptions() [2/2]

static constexpr QDateTimePrivate::TransitionOptions toTransitionOptions ( QDateTimePrivate::DaylightStatus dst)
staticconstexpr

Definition at line 2955 of file qdatetime.cpp.

References QDateTimePrivate::DaylightTime, QDateTime::PreferDaylightSaving, QDateTime::PreferStandard, and toTransitionOptions().

+ Here is the call graph for this function:

◆ usesSameOffset()

static bool usesSameOffset ( const QDateTimeData & a,
const QDateTimeData & b )
inlinestatic

Definition at line 3141 of file qdatetime.cpp.

References extractSpec(), getStatus(), Qt::LocalTime, Qt::OffsetFromUTC, Q_ASSERT, Qt::TimeZone, and Qt::UTC.

+ Here is the call graph for this function: