Qt 4.8
Public Functions | Static Public Functions | Private Functions | Properties | Friends | List of all members
QDateTime Class Reference

The QDateTime class provides date and time functions. More...

#include <qdatetime.h>

Public Functions

QDateTime addDays (int days) const
 Returns a QDateTime object containing a datetime ndays days later than the datetime of this object (or earlier if ndays is negative). More...
 
QDateTime addMonths (int months) const
 Returns a QDateTime object containing a datetime nmonths months later than the datetime of this object (or earlier if nmonths is negative). More...
 
QDateTime addMSecs (qint64 msecs) const
 Returns a QDateTime object containing a datetime msecs miliseconds later than the datetime of this object (or earlier if msecs is negative). More...
 
QDateTime addSecs (int secs) const
 Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or earlier if s is negative). More...
 
QDateTime addYears (int years) const
 Returns a QDateTime object containing a datetime nyears years later than the datetime of this object (or earlier if nyears is negative). More...
 
QDate date () const
 Returns the date part of the datetime. More...
 
int daysTo (const QDateTime &) const
 Returns the number of days from this datetime to the other datetime. More...
 
bool isNull () const
 Returns true if both the date and the time are null; otherwise returns false. More...
 
bool isValid () const
 Returns true if both the date and the time are valid; otherwise returns false. More...
 
qint64 msecsTo (const QDateTime &) const
 Returns the number of milliseconds from this datetime to the other datetime. More...
 
bool operator!= (const QDateTime &other) const
 Returns true if this datetime is different from the other datetime; otherwise returns false. More...
 
bool operator< (const QDateTime &other) const
 Returns true if this datetime is earlier than the other datetime; otherwise returns false. More...
 
bool operator<= (const QDateTime &other) const
 Returns true if this datetime is earlier than or equal to the other datetime; otherwise returns false. More...
 
QDateTimeoperator= (const QDateTime &other)
 Makes a copy of the other datetime and returns a reference to the copy. More...
 
bool operator== (const QDateTime &other) const
 Returns true if this datetime is equal to the other datetime; otherwise returns false. More...
 
bool operator> (const QDateTime &other) const
 Returns true if this datetime is later than the other datetime; otherwise returns false. More...
 
bool operator>= (const QDateTime &other) const
 Returns true if this datetime is later than or equal to the other datetime; otherwise returns false. More...
 
 QDateTime ()
 Constructs a null datetime (i.e. More...
 
 QDateTime (const QDate &)
 Constructs a datetime with the given date, a valid time(00:00:00.000), and sets the timeSpec() to Qt::LocalTime. More...
 
 QDateTime (const QDate &, const QTime &, Qt::TimeSpec spec=Qt::LocalTime)
 Constructs a datetime with the given date and time, using the time specification defined by spec. More...
 
 QDateTime (const QDateTime &other)
 Constructs a copy of the other datetime. More...
 
int secsTo (const QDateTime &) const
 Returns the number of seconds from this datetime to the other datetime. More...
 
void setDate (const QDate &date)
 Sets the date part of this datetime to date. More...
 
void setMSecsSinceEpoch (qint64 msecs)
 Sets the date and time given the number of milliseconds,msecs, that have passed since 1970-01-01T00:00:00. More...
 
void setTime (const QTime &time)
 Sets the time part of this datetime to time. More...
 
void setTime_t (uint secsSince1Jan1970UTC)
 Sets the date and time given the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). More...
 
void setTimeSpec (Qt::TimeSpec spec)
 Sets the time specification used in this datetime to spec. More...
 
void setUtcOffset (int seconds)
 
QTime time () const
 Returns the time part of the datetime. More...
 
Qt::TimeSpec timeSpec () const
 Returns the time specification of the datetime. More...
 
QDateTime toLocalTime () const
 Returns a datetime containing the date and time information in this datetime, but specified using the Qt::LocalTime definition. More...
 
qint64 toMSecsSinceEpoch () const
 Returns the datetime as the number of milliseconds that have passed since 1970-01-01T00:00:00. More...
 
QString toString (Qt::DateFormat f=Qt::TextDate) const
 Returns the datetime as a string in the format given. More...
 
QString toString (const QString &format) const
 Returns the datetime as a string. More...
 
uint toTime_t () const
 Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). More...
 
QDateTime toTimeSpec (Qt::TimeSpec spec) const
 Returns a copy of this datetime configured to use the given time specification. More...
 
QDateTime toUTC () const
 Returns a datetime containing the date and time information in this datetime, but specified using the Qt::UTC definition. More...
 
int utcOffset () const
 
 ~QDateTime ()
 Destroys the datetime. More...
 

Static Public Functions

static QDateTime currentDateTime ()
 Returns the current datetime, as reported by the system clock, in the local time zone. More...
 
static QDateTime currentDateTimeUtc ()
 Returns the current datetime, as reported by the system clock, in UTC. More...
 
static qint64 currentMSecsSinceEpoch ()
 Returns the number of milliseconds since 1970-01-01T00:00:00 Universal Coordinated Time. More...
 
static QDateTime fromMSecsSinceEpoch (qint64 msecs)
 Returns a datetime whose date and time are the number of milliseconds, msecs, that have passed since 1970-01-01T00:00:00. More...
 
static QDateTime fromString (const QString &s, Qt::DateFormat f=Qt::TextDate)
 Returns the QDateTime represented by the string, using the format given, or an invalid datetime if this is not possible. More...
 
static QDateTime fromString (const QString &s, const QString &format)
 Returns the QDateTime represented by the string, using the format given, or an invalid datetime if the string cannot be parsed. More...
 
static QDateTime fromTime_t (uint secsSince1Jan1970UTC)
 Returns a datetime whose date and time are the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). More...
 

Private Functions

void detach ()
 

Properties

QExplicitlySharedDataPointer< QDateTimePrivated
 

Friends

Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, const QDateTime &)
 Writes dateTime to the out stream. More...
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QDateTime &)
 Reads a datetime from the stream in into dateTime. More...
 
class QDateTimePrivate
 

Detailed Description

The QDateTime class provides date and time functions.

Note
This class or function is reentrant.

A QDateTime object contains a calendar date and a clock time (a "datetime"). It is a combination 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 local time or as UTC. QDateTime::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.

Definition at line 216 of file qdatetime.h.

Constructors and Destructors

◆ QDateTime() [1/4]

QDateTime::QDateTime ( )

Constructs a null datetime (i.e.

null date and null time). A null datetime is invalid, since the date is invalid.

See also
isValid()

Definition at line 2267 of file qdatetime.cpp.

Referenced by addDays(), addMonths(), QDateTimePrivate::addMSecs(), addYears(), currentDateTime(), currentDateTimeUtc(), currentMSecsSinceEpoch(), fromString(), QDateTimeParser::getMaximum(), QDateTimeParser::getMinimum(), localToUtc(), QDateTimeParser::parse(), and QDateTimeParser::setDigit().

2268  : d(new QDateTimePrivate)
2269 {
2270 }
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ QDateTime() [2/4]

QDateTime::QDateTime ( const QDate date)
explicit

Constructs a datetime with the given date, a valid time(00:00:00.000), and sets the timeSpec() to Qt::LocalTime.

Definition at line 2278 of file qdatetime.cpp.

2279  : d(new QDateTimePrivate)
2280 {
2281  d->date = date;
2282  d->time = QTime(0, 0, 0);
2283 }
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ QDateTime() [3/4]

QDateTime::QDateTime ( const QDate date,
const QTime time,
Qt::TimeSpec  spec = Qt::LocalTime 
)

Constructs a datetime with the given date and time, using the time specification defined by spec.

If date is valid and time is not, the time will be set to midnight.

Definition at line 2292 of file qdatetime.cpp.

2293  : d(new QDateTimePrivate)
2294 {
2295  d->date = date;
2296  d->time = date.isValid() && !time.isValid() ? QTime(0, 0, 0) : time;
2298 }
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566

◆ QDateTime() [4/4]

QDateTime::QDateTime ( const QDateTime other)

Constructs a copy of the other datetime.

Definition at line 2304 of file qdatetime.cpp.

2305  : d(other.d)
2306 {
2307 }
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ ~QDateTime()

QDateTime::~QDateTime ( )

Destroys the datetime.

Definition at line 2312 of file qdatetime.cpp.

2313 {
2314 }

Functions

◆ addDays()

QDateTime QDateTime::addDays ( int  ndays) const

Returns a QDateTime object containing a datetime ndays days later than the datetime of this object (or earlier if ndays is negative).

See also
daysTo(), addMonths(), addYears(), addSecs()

Definition at line 2783 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDurationComparator::addDurationToDateTime(), operator+(), operator-(), and QDateTimeEditPrivate::stepBy().

2784 {
2785  return QDateTime(d->date.addDays(ndays), d->time, timeSpec());
2786 }
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
QDate addDays(int days) const
Returns a QDate object containing a date ndays later than the date of this object (or earlier if nday...
Definition: qdatetime.cpp:989
Qt::TimeSpec timeSpec() const
Returns the time specification of the datetime.
Definition: qdatetime.cpp:2379
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ addMonths()

QDateTime QDateTime::addMonths ( int  nmonths) const

Returns a QDateTime object containing a datetime nmonths months later than the datetime of this object (or earlier if nmonths is negative).

See also
daysTo(), addDays(), addYears(), addSecs()

Definition at line 2796 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDurationComparator::addDurationToDateTime(), and QDateTimeParser::parse().

2797 {
2798  return QDateTime(d->date.addMonths(nmonths), d->time, timeSpec());
2799 }
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
QDate addMonths(int months) const
Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nm...
Definition: qdatetime.cpp:1016
Qt::TimeSpec timeSpec() const
Returns the time specification of the datetime.
Definition: qdatetime.cpp:2379
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ addMSecs()

QDateTime QDateTime::addMSecs ( qint64  msecs) const

Returns a QDateTime object containing a datetime msecs miliseconds later than the datetime of this object (or earlier if msecs is negative).

See also
addSecs(), msecsTo(), addDays(), addMonths(), addYears()

Definition at line 2881 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDurationComparator::addDurationToDateTime(), QDateTimePrivate::addMSecs(), QDateTimePrivate::getUTC(), and localToUtc().

2882 {
2883  return d->addMSecs(*this, msecs);
2884 }
static QDateTime addMSecs(const QDateTime &dt, qint64 msecs)
Definition: qdatetime.cpp:2814
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ addSecs()

QDateTime QDateTime::addSecs ( int  s) const

Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or earlier if s is negative).

See also
addMSecs(), secsTo(), addDays(), addMonths(), addYears()

Definition at line 2869 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDurationComparator::addDurationToDateTime(), QNetworkAccessHttpBackend::fetchCacheMetaData(), QNetworkAccessCache::linkEntry(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), operator-(), QDateTimeParser::parse(), parseDateString(), QNetworkCookiePrivate::parseSetCookieHeaderLine(), q_getTimeFromASN1(), and qtValue().

2870 {
2871  return d->addMSecs(*this, qint64(s) * 1000);
2872 }
__int64 qint64
Definition: qglobal.h:942
static QDateTime addMSecs(const QDateTime &dt, qint64 msecs)
Definition: qdatetime.cpp:2814
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ addYears()

QDateTime QDateTime::addYears ( int  nyears) const

Returns a QDateTime object containing a datetime nyears years later than the datetime of this object (or earlier if nyears is negative).

See also
daysTo(), addDays(), addMonths(), addSecs()

Definition at line 2809 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDurationComparator::addDurationToDateTime().

2810 {
2811  return QDateTime(d->date.addYears(nyears), d->time, timeSpec());
2812 }
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
QDate addYears(int years) const
Returns a QDate object containing a date nyears later than the date of this object (or earlier if nye...
Definition: qdatetime.cpp:1081
Qt::TimeSpec timeSpec() const
Returns the time specification of the datetime.
Definition: qdatetime.cpp:2379
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ currentDateTime()

QDateTime QDateTime::currentDateTime ( )
static

Returns the current datetime, as reported by the system clock, in the local time zone.

Use the currentDateTime() overload that takes no parameters instead.

See also
currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()

Definition at line 3138 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), QZipWriterPrivate::addEntry(), QTransportAuth::authFromMessage(), QNetworkCookieJar::cookiesForUrl(), QUuid::createUuid(), currentMSecsSinceEpoch(), DumpIDL(), QPSPrintEnginePrivate::emitHeader(), QNetworkAccessHttpBackend::fetchCacheMetaData(), QNetworkSessionManagerPrivate::forceSessionClose(), QScriptDebuggerLocalsModelPrivate::get(), QSslCertificate::isValid(), QNetworkAccessCache::linkEntry(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), QScriptErrorLogWidget::message(), QNetworkCookiePrivate::parseSetCookieHeaderLine(), QScriptScriptData::QScriptScriptData(), qt_CoCreateGuid(), qt_x11_apply_settings_in_all_apps(), QNetworkCookieJar::setCookiesFromUrl(), QConnmanEngine::startTime(), QNetworkSessionPrivateImpl::stateChange(), QNetworkAccessCache::timerEvent(), QNetworkAccessCache::updateTimer(), and QPdfEnginePrivate::writeInfo().

3139 {
3140  QDate d;
3141  QTime t;
3142  SYSTEMTIME st;
3143  memset(&st, 0, sizeof(SYSTEMTIME));
3144  GetLocalTime(&st);
3145  d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
3146  t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
3147  return QDateTime(d, t);
3148 }
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
The QDate class provides date functions.
Definition: qdatetime.h:55
static uint msecsFromDecomposed(int hour, int minute, int sec, int msec=0)
Definition: qdatetime.cpp:3109
static uint julianDayFromDate(int year, int month, int day)
Definition: qdatetime.cpp:113
The QTime class provides clock time functions.
Definition: qdatetime.h:148
int mds
Definition: qdatetime.h:200
#define st(var, type, card)
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
uint jd
Definition: qdatetime.h:137

◆ currentDateTimeUtc()

QDateTime QDateTime::currentDateTimeUtc ( )
static

Returns the current datetime, as reported by the system clock, in UTC.

Since
4.7
See also
currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()

Definition at line 3150 of file qdatetime.cpp.

Referenced by currentMSecsSinceEpoch().

3151 {
3152  QDate d;
3153  QTime t;
3154  SYSTEMTIME st;
3155  memset(&st, 0, sizeof(SYSTEMTIME));
3156  GetSystemTime(&st);
3157  d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
3158  t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
3159  return QDateTime(d, t, Qt::UTC);
3160 }
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
The QDate class provides date functions.
Definition: qdatetime.h:55
static uint msecsFromDecomposed(int hour, int minute, int sec, int msec=0)
Definition: qdatetime.cpp:3109
static uint julianDayFromDate(int year, int month, int day)
Definition: qdatetime.cpp:113
The QTime class provides clock time functions.
Definition: qdatetime.h:148
int mds
Definition: qdatetime.h:200
#define st(var, type, card)
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
uint jd
Definition: qdatetime.h:137

◆ currentMSecsSinceEpoch()

qint64 QDateTime::currentMSecsSinceEpoch ( )
static

Returns the number of milliseconds since 1970-01-01T00:00:00 Universal Coordinated Time.

Since
4.7

This number is like the POSIX time_t variable, but expressed in milliseconds instead.

See also
currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec()

Definition at line 3162 of file qdatetime.cpp.

Referenced by QElapsedTimer::elapsed(), and QElapsedTimer::restart().

3163 {
3164  QDate d;
3165  QTime t;
3166  SYSTEMTIME st;
3167  memset(&st, 0, sizeof(SYSTEMTIME));
3168  GetSystemTime(&st);
3169 
3170  return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
3171  qint64(julianDayFromGregorianDate(st.wYear, st.wMonth, st.wDay)
3172  - julianDayFromGregorianDate(1970, 1, 1)) * Q_INT64_C(86400000);
3173 }
The QDate class provides date functions.
Definition: qdatetime.h:55
static uint msecsFromDecomposed(int hour, int minute, int sec, int msec=0)
Definition: qdatetime.cpp:3109
The QTime class provides clock time functions.
Definition: qdatetime.h:148
static uint julianDayFromGregorianDate(int year, int month, int day)
Definition: qdatetime.cpp:103
__int64 qint64
Definition: qglobal.h:942
#define Q_INT64_C(c)
Definition: qglobal.h:940
#define st(var, type, card)
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ date()

QDate QDateTime::date ( ) const

Returns the date part of the datetime.

See also
setDate(), time(), timeSpec()

Definition at line 2357 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), _q_parseDosDir(), _q_parseUnixDir(), QDateTimeParser::absoluteMax(), adjustDate(), QScript::DateTimeToMs(), QStyledItemDelegate::displayText(), QDB2Result::exec(), QODBCResult::exec(), QPatternist::YearFromAbstractDateTimeFN::extract(), QPatternist::DayFromAbstractDateTimeFN::extract(), QPatternist::MonthFromAbstractDateTimeFN::extract(), QDeclarativeEnginePrivate::formatDate(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QPatternist::GMonthDay::fromDateTime(), QPatternist::Date::fromDateTime(), QPatternist::GDay::fromDateTime(), QPatternist::GMonth::fromDateTime(), QPatternist::GYear::fromDateTime(), QPatternist::GYearMonth::fromDateTime(), QDateTimeParser::fromString(), fromString(), QDeclarativeCompiler::genLiteralAssignment(), QDateTimeParser::getDigit(), QDateTimePrivate::getLocal(), QDateTimePrivate::getUTC(), localToUtc(), operator*(), operator<<(), QDateTimeParser::parse(), QDateTimeParser::parseSection(), QDateTimeParser::potentialValue(), QDateTime(), QDateTimeToDATE(), qMakeOraDate(), setDate(), QDateTimeEdit::setDateTime(), QDateTimeParser::setDigit(), QDateTimeEdit::setMaximumDateTime(), QDateTimeEdit::setMinimumDateTime(), QDateTimeEditPrivate::stepBy(), QPatternist::AtomicValue::toQt(), QSystemLocalePrivate::toString(), QLocale::toString(), toTimeStamp(), QItemDelegatePrivate::valueToText(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().

2358 {
2359  return d->date;
2360 }
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ daysTo()

int QDateTime::daysTo ( const QDateTime other) const

Returns the number of days from this datetime to the other datetime.

If the other datetime is earlier than this datetime, the value returned is negative.

See also
addDays(), secsTo(), msecsTo()

Definition at line 2894 of file qdatetime.cpp.

Referenced by QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), operator-(), and QDateTimeParser::parse().

2895 {
2896  return d->date.daysTo(other.d->date);
2897 }
int daysTo(const QDate &) const
Returns the number of days from this date to d (which is negative if d is earlier than this date)...
Definition: qdatetime.cpp:1111
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ detach()

void QDateTime::detach ( )
private
Warning
This function is not part of the public interface.

Definition at line 3791 of file qdatetime.cpp.

Referenced by setDate(), setMSecsSinceEpoch(), setTime(), setTime_t(), setTimeSpec(), and setUtcOffset().

3792 {
3793  d.detach();
3794 }
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:148
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ fromMSecsSinceEpoch()

QDateTime QDateTime::fromMSecsSinceEpoch ( qint64  msecs)
static

Returns a datetime whose date and time are the number of milliseconds, msecs, that have passed since 1970-01-01T00:00:00.

Since
4.7

000, Coordinated Universal Time (Qt::UTC). On systems that do not support time zones, the time will be set as if local time were Qt::UTC.

Note that there are possible values for msecs that lie outside the valid range of QDateTime, both negative and positive. The behavior of this function is undefined for those values.

See also
toTime_t(), setTime_t()

Definition at line 3393 of file qdatetime.cpp.

Referenced by operator==().

3394 {
3395  QDateTime d;
3396  d.setMSecsSinceEpoch(msecs);
3397  return d;
3398 }
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
void setMSecsSinceEpoch(qint64 msecs)
Sets the date and time given the number of milliseconds,msecs, that have passed since 1970-01-01T00:0...
Definition: qdatetime.cpp:2530

◆ fromString() [1/2]

QDateTime QDateTime::fromString ( const QString string,
Qt::DateFormat  format = Qt::TextDate 
)
static

Returns the QDateTime represented by the string, using the format given, or an invalid datetime if this is not possible.

Note for Qt::TextDate: It is recommended that you use the English short month names (e.g. "Jan"). Although localized month names can also be used, they depend on the user's locale settings.

Definition at line 3487 of file qdatetime.cpp.

Referenced by convert(), QPSQLResult::data(), QDeclarativeStringConverters::dateTimeFromString(), QNetworkHeadersPrivate::fromHttpDate(), QNetworkAccessFtpBackend::ftpRawCommandReply(), and qDateTimeFromString().

3488 {
3489  if (s.isEmpty()) {
3490  return QDateTime();
3491  }
3492 
3493  switch (f) {
3494  case Qt::ISODate: {
3495  QString tmp = s;
3497  const QDate date = QDate::fromString(tmp.left(10), Qt::ISODate);
3498  if (tmp.size() == 10)
3499  return QDateTime(date);
3500 
3501  tmp = tmp.mid(11);
3502 
3503  // Recognize UTC specifications
3504  if (tmp.endsWith(QLatin1Char('Z'))) {
3505  ts = Qt::UTC;
3506  tmp.chop(1);
3507  }
3508 
3509  // Recognize timezone specifications
3510  QRegExp rx(QLatin1String("[+-]"));
3511  if (tmp.contains(rx)) {
3512  int idx = tmp.indexOf(rx);
3513  QString tmp2 = tmp.mid(idx);
3514  tmp = tmp.left(idx);
3515  bool ok = true;
3516  int ntzhour = 1;
3517  int ntzminute = 3;
3518  if ( tmp2.indexOf(QLatin1Char(':')) == 3 )
3519  ntzminute = 4;
3520  const int tzhour(tmp2.mid(ntzhour, 2).toInt(&ok));
3521  const int tzminute(tmp2.mid(ntzminute, 2).toInt(&ok));
3522  QTime tzt(tzhour, tzminute);
3523  int utcOffset = (tzt.hour() * 60 + tzt.minute()) * 60;
3524  if ( utcOffset != 0 ) {
3525  ts = Qt::OffsetFromUTC;
3526  QDateTime dt(date, QTime::fromString(tmp, Qt::ISODate), ts);
3527  dt.setUtcOffset( utcOffset * (tmp2.startsWith(QLatin1Char('-')) ? -1 : 1) );
3528  return dt;
3529  }
3530  }
3531  return QDateTime(date, QTime::fromString(tmp, Qt::ISODate), ts);
3532  }
3533  case Qt::SystemLocaleDate:
3538  case Qt::LocaleDate:
3541  return fromString(s, QLocale().dateTimeFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat
3543 #if !defined(QT_NO_TEXTDATE)
3544  case Qt::TextDate: {
3545  QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts);
3546 
3547  if ((parts.count() < 5) || (parts.count() > 6)) {
3548  return QDateTime();
3549  }
3550 
3551  // Accept "Sun Dec 1 13:02:00 1974" and "Sun 1. Dec 13:02:00 1974"
3552  int month = -1, day = -1;
3553  bool ok;
3554 
3555  month = fromShortMonthName(parts.at(1));
3556  if (month != -1) {
3557  day = parts.at(2).toInt(&ok);
3558  if (!ok)
3559  day = -1;
3560  }
3561 
3562  if (month == -1 || day == -1) {
3563  // first variant failed, lets try the other
3564  month = fromShortMonthName(parts.at(2));
3565  if (month != -1) {
3566  QString dayStr = parts.at(1);
3567  if (dayStr.endsWith(QLatin1Char('.'))) {
3568  dayStr.chop(1);
3569  day = dayStr.toInt(&ok);
3570  if (!ok)
3571  day = -1;
3572  } else {
3573  day = -1;
3574  }
3575  }
3576  }
3577 
3578  if (month == -1 || day == -1) {
3579  // both variants failed, give up
3580  return QDateTime();
3581  }
3582 
3583  int year;
3584  QStringList timeParts = parts.at(3).split(QLatin1Char(':'));
3585  if ((timeParts.count() == 3) || (timeParts.count() == 2)) {
3586  year = parts.at(4).toInt(&ok);
3587  if (!ok)
3588  return QDateTime();
3589  } else {
3590  timeParts = parts.at(4).split(QLatin1Char(':'));
3591  if ((timeParts.count() != 3) && (timeParts.count() != 2))
3592  return QDateTime();
3593  year = parts.at(3).toInt(&ok);
3594  if (!ok)
3595  return QDateTime();
3596  }
3597 
3598  int hour = timeParts.at(0).toInt(&ok);
3599  if (!ok) {
3600  return QDateTime();
3601  }
3602 
3603  int minute = timeParts.at(1).toInt(&ok);
3604  if (!ok) {
3605  return QDateTime();
3606  }
3607 
3608  int second = (timeParts.count() > 2) ? timeParts.at(2).toInt(&ok) : 0;
3609  if (!ok) {
3610  return QDateTime();
3611  }
3612 
3613  QDate date(year, month, day);
3614  QTime time(hour, minute, second);
3615 
3616  if (parts.count() == 5)
3617  return QDateTime(date, time, Qt::LocalTime);
3618 
3619  QString tz = parts.at(5);
3620  if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
3621  return QDateTime();
3622  QDateTime dt(date, time, Qt::UTC);
3623  if (tz.length() > 3) {
3624  int tzoffset = 0;
3625  QChar sign = tz.at(3);
3626  if ((sign != QLatin1Char('+'))
3627  && (sign != QLatin1Char('-'))) {
3628  return QDateTime();
3629  }
3630  int tzhour = tz.mid(4, 2).toInt(&ok);
3631  if (!ok)
3632  return QDateTime();
3633  int tzminute = tz.mid(6).toInt(&ok);
3634  if (!ok)
3635  return QDateTime();
3636  tzoffset = (tzhour*60 + tzminute) * 60;
3637  if (sign == QLatin1Char('-'))
3638  tzoffset = -tzoffset;
3639  dt.setUtcOffset(tzoffset);
3640  }
3641  return dt.toLocalTime();
3642  }
3643 #endif //QT_NO_TEXTDATE
3644  }
3645 
3646  return QDateTime();
3647 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
void chop(int n)
Removes n characters from the end of the string.
Definition: qstring.cpp:4623
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
The QDate class provides date functions.
Definition: qdatetime.h:55
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
int utcOffset() const
Definition: qdatetime.cpp:3449
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
static int sign(int x)
TimeSpec
Definition: qnamespace.h:1404
static QDateTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDateTime represented by the string, using the format given, or an invalid datetime if th...
Definition: qdatetime.cpp:3487
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static QTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the time represented in the string as a QTime using the format given, or an invalid time if t...
Definition: qdatetime.cpp:1928
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
static int fromShortMonthName(const QString &monthName)
Definition: qdatetime.cpp:3459
static QDate fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDate represented by the string, using the format given, or an invalid date if the string...
Definition: qdatetime.cpp:1203
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ fromString() [2/2]

QDateTime QDateTime::fromString ( const QString string,
const QString format 
)
static

Returns the QDateTime represented by the string, using the format given, or an invalid datetime if the string cannot be parsed.

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
Note
Unlike the other version of this function, day and month names must be given in the user's local language. It is only possible to use the English names if the user's language is English.

These expressions may be used for the time part of the format string:

Expression Output
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H the hour without a leading zero (0 to 23, even with AM/PM display)
HH the hour with a leading zero (00 to 23, even with AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the second without a leading zero (0 to 59)
ss the second with a leading zero (00 to 59)
z the milliseconds without leading zeroes (0 to 999)
zzz the 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.

QTime time1 = QTime::fromString("131", "HHh");
// time1 is 13:00:00
QTime time1 = QTime::fromString("1apA", "1amAM");
// time1 is 01:00:00
QDateTime dateTime2 = QDateTime::fromString("M1d1y9800:01:02",
"'M'M'd'd'y'yyhh:mm:ss");
// dateTime is 1 January 1998 00:01:02

If the format is not satisfied an invalid QDateTime is returned. The expressions that don't have leading zeroes (d, M, h, m, s, z) will be greedy. This means that they will use two digits even if this will put them outside the range and/or leave too few digits for other sections.

QDateTime dateTime = QDateTime::fromString("130", "Mm"); // invalid

This could have meant 1 January 00:30.00 but the M will grab two digits.

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

For example:

QDateTime dateTime = QDateTime::fromString("1.30.1", "M.d.s");
// dateTime is January 30 in 1900 at 00:00:01.
See also
QDate::fromString() QTime::fromString() QDate::toString() QDateTime::toString() QTime::toString()

Definition at line 3748 of file qdatetime.cpp.

3749 {
3750 #ifndef QT_BOOTSTRAPPED
3751  QTime time;
3752  QDate date;
3753 
3755  if (dt.parseFormat(format) && dt.fromString(string, &date, &time))
3756  return QDateTime(date, time);
3757 #else
3758  Q_UNUSED(string);
3759  Q_UNUSED(format);
3760 #endif
3761  return QDateTime(QDate(), QTime(-1, -1, -1));
3762 }
QDateTime()
Constructs a null datetime (i.e.
Definition: qdatetime.cpp:2267
The QDate class provides date functions.
Definition: qdatetime.h:55
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ fromTime_t()

QDateTime QDateTime::fromTime_t ( uint  seconds)
static

Returns a datetime whose date and time are the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).

Since
4.2

On systems that do not support time zones, the time will be set as if local time were Qt::UTC.

See also
toTime_t(), setTime_t()

Definition at line 3369 of file qdatetime.cpp.

Referenced by QFileSystemMetaData::fileTime().

3370 {
3371  QDateTime d;
3372  d.setTime_t(seconds);
3373  return d;
3374 }
void setTime_t(uint secsSince1Jan1970UTC)
Sets the date and time given the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
Definition: qdatetime.cpp:2566
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ isNull()

bool QDateTime::isNull ( ) const

Returns true if both the date and the time are null; otherwise returns false.

A null datetime is invalid.

See also
QDate::isNull(), QTime::isNull(), isValid()

Definition at line 2334 of file qdatetime.cpp.

Referenced by QDateTimeToDATE(), QConnmanEngine::startTime(), and QDateTimeEditPrivate::validateAndInterpret().

2335 {
2336  return d->date.isNull() && d->time.isNull();
2337 }
bool isNull() const
Returns true if the date is null; otherwise returns false.
Definition: qdatetime.h:66
bool isNull() const
Returns true if the time is null (i.
Definition: qdatetime.h:158
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ isValid()

bool QDateTime::isValid ( ) const

Returns true if both the date and the time are valid; otherwise returns false.

See also
QDate::isValid(), QTime::isValid()

Definition at line 2346 of file qdatetime.cpp.

Referenced by _q_parseUnixDir(), QDateTimeParser::absoluteMax(), QPatternist::AbstractDateTime::AbstractDateTime(), convert(), QPatternist::AdjustDateTimeToTimezoneFN::createValue(), QPatternist::AdjustDateToTimezoneFN::createValue(), QPatternist::AdjustTimeToTimezoneFN::createValue(), QDeclarativeStringConverters::dateTimeFromString(), QScript::DateTimeToMs(), QDB2Driver::formatValue(), QSqlDriver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QTDSDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QPatternist::Date::fromDateTime(), QPatternist::SchemaTime::fromDateTime(), QPatternist::DateTime::fromDateTime(), QNetworkHeadersPrivate::fromHttpDate(), QPatternist::Date::fromValue(), QPatternist::SchemaTime::fromValue(), QPatternist::DateTime::fromValue(), QNetworkCookie::isSessionCookie(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), QDateTimeParser::parse(), parseDateString(), parseHttpDate(), QNetworkCookiePrivate::parseSetCookieHeaderLine(), QDateTimeEdit::QDateTimeEdit(), QDateTimeToDATE(), QScriptScriptData::QScriptScriptData(), QDateTimeEdit::setDateTime(), QDateTimeEdit::setMaximumDateTime(), QDateTimeEdit::setMinimumDateTime(), QTest::toString(), toString(), QLocale::toString(), utcOffset(), and writeMSDosDate().

2347 {
2348  return d->date.isValid() && d->time.isValid();
2349 }
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566

◆ msecsTo()

qint64 QDateTime::msecsTo ( const QDateTime other) const

Returns the number of milliseconds from this datetime to the other datetime.

Since
4.7

If the other datetime is earlier than this datetime, the value returned is negative.

Before performing the comparison, the two datetimes are converted to Qt::UTC to ensure that the result is correct if one of the two datetimes has daylight saving time (DST) and the other doesn't.

See also
addMSecs(), daysTo(), QTime::msecsTo()

Definition at line 2942 of file qdatetime.cpp.

2943 {
2944  QDate selfDate;
2945  QDate otherDate;
2946  QTime selfTime;
2947  QTime otherTime;
2948 
2949  d->getUTC(selfDate, selfTime);
2950  other.d->getUTC(otherDate, otherTime);
2951 
2952  return (static_cast<qint64>(selfDate.daysTo(otherDate)) * static_cast<qint64>(MSECS_PER_DAY))
2953  + static_cast<qint64>(selfTime.msecsTo(otherTime));
2954 }
int daysTo(const QDate &) const
Returns the number of days from this date to d (which is negative if d is earlier than this date)...
Definition: qdatetime.cpp:1111
int msecsTo(const QTime &) const
Returns the number of milliseconds from this time to t.
Definition: qdatetime.cpp:1832
The QDate class provides date functions.
Definition: qdatetime.h:55
The QTime class provides clock time functions.
Definition: qdatetime.h:148
void getUTC(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4422
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ operator!=()

bool QDateTime::operator!= ( const QDateTime other) const
inline

Returns true if this datetime is different from the other datetime; otherwise returns false.

Two datetimes are different if either the date, the time, or the time zone components are different.

See also
operator==()

Definition at line 257 of file qdatetime.h.

257 { return !(*this == other); }

◆ operator<()

bool QDateTime::operator< ( const QDateTime other) const

Returns true if this datetime is earlier than the other datetime; otherwise returns false.

Definition at line 3025 of file qdatetime.cpp.

3026 {
3027  if (d->spec == other.d->spec && d->spec != QDateTimePrivate::OffsetFromUTC) {
3028  if (d->date != other.d->date)
3029  return d->date < other.d->date;
3030  return d->time < other.d->time;
3031  } else {
3032  QDate date1, date2;
3033  QTime time1, time2;
3034  d->getUTC(date1, time1);
3035  other.d->getUTC(date2, time2);
3036  if (date1 != date2)
3037  return date1 < date2;
3038  return time1 < time2;
3039  }
3040 }
The QDate class provides date functions.
Definition: qdatetime.h:55
The QTime class provides clock time functions.
Definition: qdatetime.h:148
void getUTC(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4422
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ operator<=()

bool QDateTime::operator<= ( const QDateTime other) const
inline

Returns true if this datetime is earlier than or equal to the other datetime; otherwise returns false.

Definition at line 259 of file qdatetime.h.

259 { return !(other < *this); }

◆ operator=()

QDateTime & QDateTime::operator= ( const QDateTime other)

Makes a copy of the other datetime and returns a reference to the copy.

Definition at line 2321 of file qdatetime.cpp.

2322 {
2323  d = other.d;
2324  return *this;
2325 }
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ operator==()

bool QDateTime::operator== ( const QDateTime other) const

Returns true if this datetime is equal to the other datetime; otherwise returns false.

See also
operator!=()

Definition at line 2991 of file qdatetime.cpp.

2992 {
2993  if (d->spec == other.d->spec && d->utcOffset == other.d->utcOffset)
2994  return d->time == other.d->time && d->date == other.d->date;
2995  else {
2996  QDate date1, date2;
2997  QTime time1, time2;
2998 
2999  d->getUTC(date1, time1);
3000  other.d->getUTC(date2, time2);
3001  return time1 == time2 && date1 == date2;
3002  }
3003 }
The QDate class provides date functions.
Definition: qdatetime.h:55
The QTime class provides clock time functions.
Definition: qdatetime.h:148
void getUTC(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4422
int utcOffset
The offset in seconds.
Definition: qdatetime_p.h:102
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ operator>()

bool QDateTime::operator> ( const QDateTime other) const
inline

Returns true if this datetime is later than the other datetime; otherwise returns false.

Definition at line 260 of file qdatetime.h.

260 { return other < *this; }

◆ operator>=()

bool QDateTime::operator>= ( const QDateTime other) const
inline

Returns true if this datetime is later than or equal to the other datetime; otherwise returns false.

Definition at line 261 of file qdatetime.h.

261 { return !(*this < other); }

◆ secsTo()

int QDateTime::secsTo ( const QDateTime other) const

Returns the number of seconds from this datetime to the other datetime.

If the other datetime is earlier than this datetime, the value returned is negative.

Before performing the comparison, the two datetimes are converted to Qt::UTC to ensure that the result is correct if one of the two datetimes has daylight saving time (DST) and the other doesn't.

Example:

QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0));
qDebug("There are %d seconds to Christmas", now.secsTo(xmas));
See also
addSecs(), daysTo(), QTime::secsTo()

Definition at line 2914 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), FAREnforcer::logAuthAttempt(), macValue(), QDirSortItemComparator::operator()(), operator-(), QConnmanEngine::startTime(), and QNetworkAccessCache::updateTimer().

2915 {
2916  QDate date1, date2;
2917  QTime time1, time2;
2918 
2919  d->getUTC(date1, time1);
2920  other.d->getUTC(date2, time2);
2921 
2922  return (date1.daysTo(date2) * SECS_PER_DAY) + time1.secsTo(time2);
2923 }
int daysTo(const QDate &) const
Returns the number of days from this date to d (which is negative if d is earlier than this date)...
Definition: qdatetime.cpp:1111
The QDate class provides date functions.
Definition: qdatetime.h:55
int secsTo(const QTime &) const
Returns the number of seconds from this time to t.
Definition: qdatetime.cpp:1788
The QTime class provides clock time functions.
Definition: qdatetime.h:148
void getUTC(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4422
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ setDate()

void QDateTime::setDate ( const QDate date)

Sets the date part of this datetime to date.

If no time is set, it is set to midnight.

See also
date(), setTime(), setTimeSpec()

Definition at line 2399 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), _q_parseDosDir(), _q_parseUnixDir(), fileTimeToQDateTime(), QPatternist::Date::fromDateTime(), and QPatternist::SchemaTime::fromDateTime().

2400 {
2401  detach();
2402  d->date = date;
2406  if (date.isValid() && !d->time.isValid())
2407  d->time = QTime(0, 0, 0);
2408 }
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
The QTime class provides clock time functions.
Definition: qdatetime.h:148
void detach()
Definition: qdatetime.cpp:3791
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566

◆ setMSecsSinceEpoch()

void QDateTime::setMSecsSinceEpoch ( qint64  msecs)

Sets the date and time given the number of milliseconds,msecs, that have passed since 1970-01-01T00:00:00.

Since
4.7

000, Coordinated Universal Time (Qt::UTC). On systems that do not support time zones this function will behave as if local time were Qt::UTC.

Note that there are possible values for msecs that lie outside the valid range of QDateTime, both negative and positive. The behavior of this function is undefined for those values.

See also
toMSecsSinceEpoch(), setTime_t()

Definition at line 2530 of file qdatetime.cpp.

Referenced by fromMSecsSinceEpoch().

2531 {
2532  detach();
2533 
2534  QDateTimePrivate::Spec oldSpec = d->spec;
2535 
2536  int ddays = msecs / MSECS_PER_DAY;
2537  msecs %= MSECS_PER_DAY;
2538  if (msecs < 0) {
2539  // negative
2540  --ddays;
2541  msecs += MSECS_PER_DAY;
2542  }
2543 
2544  d->date = QDate(1970, 1, 1).addDays(ddays);
2545  d->time = QTime().addMSecs(msecs);
2547 
2548  if (oldSpec != QDateTimePrivate::UTC)
2549  d->spec = d->getLocal(d->date, d->time);
2550 }
The QDate class provides date functions.
Definition: qdatetime.h:55
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QDate addDays(int days) const
Returns a QDate object containing a date ndays later than the date of this object (or earlier if nday...
Definition: qdatetime.cpp:989
void detach()
Definition: qdatetime.cpp:3791
Spec getLocal(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4413
QTime addMSecs(int ms) const
Returns a QTime object containing a time ms milliseconds later than the time of this object (or earli...
Definition: qdatetime.cpp:1803
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ setTime()

void QDateTime::setTime ( const QTime time)

Sets the time part of this datetime to time.

See also
time(), setDate(), setTimeSpec()

Definition at line 2416 of file qdatetime.cpp.

Referenced by fileTimeToQDateTime(), QPatternist::SchemaTime::fromDateTime(), operator+(), and operator-().

2417 {
2418  detach();
2422  d->time = time;
2423 }
void detach()
Definition: qdatetime.cpp:3791
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368

◆ setTime_t()

void QDateTime::setTime_t ( uint  seconds)

Sets the date and time given the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).

Use the single-argument overload of setTime_t() instead.

On systems that do not support time zones this function will behave as if local time were Qt::UTC.

See also
toTime_t()

Definition at line 2566 of file qdatetime.cpp.

Referenced by fileTimeToQDateTime(), fromTime_t(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), macValue(), and qtValue().

2567 {
2568  detach();
2569 
2570  QDateTimePrivate::Spec oldSpec = d->spec;
2571 
2572  d->date = QDate(1970, 1, 1).addDays(secsSince1Jan1970UTC / SECS_PER_DAY);
2573  d->time = QTime().addSecs(secsSince1Jan1970UTC % SECS_PER_DAY);
2575 
2576  if (oldSpec != QDateTimePrivate::UTC)
2577  d->spec = d->getLocal(d->date, d->time);
2578 }
The QDate class provides date functions.
Definition: qdatetime.h:55
QTime addSecs(int secs) const
Returns a QTime object containing a time s seconds later than the time of this object (or earlier if ...
Definition: qdatetime.cpp:1770
The QTime class provides clock time functions.
Definition: qdatetime.h:148
QDate addDays(int days) const
Returns a QDate object containing a date ndays later than the date of this object (or earlier if nday...
Definition: qdatetime.cpp:989
void detach()
Definition: qdatetime.cpp:3791
Spec getLocal(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4413
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ setTimeSpec()

void QDateTime::setTimeSpec ( Qt::TimeSpec  spec)

Sets the time specification used in this datetime to spec.

See also
timeSpec(), setDate(), setTime(), Qt::TimeSpec

Definition at line 2431 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDateTime::copyTimeSpec(), QNetworkHeadersPrivate::fromHttpDate(), and QPatternist::AbstractDateTime::setUtcOffset().

2432 {
2433  detach();
2434 
2435  switch(spec)
2436  {
2437  case Qt::UTC:
2439  break;
2440  case Qt::OffsetFromUTC:
2442  break;
2443  default:
2445  break;
2446  }
2447 }
void detach()
Definition: qdatetime.cpp:3791
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ setUtcOffset()

void QDateTime::setUtcOffset ( int  seconds)
Since
4.4
Warning
This function is not part of the public interface.

Sets the offset from UTC to seconds, and also sets timeSpec() to Qt::OffsetFromUTC.

The maximum and minimum offset is 14 positive or negative hours. If seconds is larger or smaller than that, the result is undefined.

0 as offset is identical to UTC. Therefore, if seconds is 0, the timeSpec() will be set to Qt::UTC. Hence the UTC offset always relates to UTC, and can never relate to local time.

See also
isValid(), utcOffset()

Definition at line 3416 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDateTime::copyTimeSpec(), fromString(), and QPatternist::AbstractDateTime::setUtcOffset().

3417 {
3418  detach();
3419 
3420  /* The motivation to also setting d->spec is to ensure that the QDateTime
3421  * instance stay in well-defined states all the time, instead of that
3422  * we instruct the user to ensure it. */
3423  if(seconds == 0)
3425  else
3427 
3428  /* Even if seconds is 0 we assign it to utcOffset. */
3429  d->utcOffset = seconds;
3430 }
void detach()
Definition: qdatetime.cpp:3791
int utcOffset
The offset in seconds.
Definition: qdatetime_p.h:102
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ time()

QTime QDateTime::time ( ) const

◆ timeSpec()

Qt::TimeSpec QDateTime::timeSpec ( ) const

◆ toLocalTime()

QDateTime QDateTime::toLocalTime ( ) const
inline

Returns a datetime containing the date and time information in this datetime, but specified using the Qt::LocalTime definition.

See also
toTimeSpec()

Definition at line 250 of file qdatetime.h.

Referenced by fromString(), and QScript::MsToDateTime().

250 { return toTimeSpec(Qt::LocalTime); }
QDateTime toTimeSpec(Qt::TimeSpec spec) const
Returns a copy of this datetime configured to use the given time specification.
Definition: qdatetime.cpp:2969

◆ toMSecsSinceEpoch()

qint64 QDateTime::toMSecsSinceEpoch ( ) const

Returns the datetime as the number of milliseconds that have passed since 1970-01-01T00:00:00.

Since
4.7

000, Coordinated Universal Time (Qt::UTC).

On systems that do not support time zones, this function will
behave as if local time were Qt::UTC.

The behavior for this function is undefined if the datetime stored in
this object is not valid. However, for all valid dates, this function
returns a unique value.

\sa toTime_t(), setMSecsSinceEpoch()

Definition at line 2474 of file qdatetime.cpp.

Referenced by operator==(), and toTime_t().

2475 {
2476  QDate utcDate;
2477  QTime utcTime;
2478  d->getUTC(utcDate, utcTime);
2479 
2480  return toMSecsSinceEpoch_helper(utcDate.jd, utcTime.ds());
2481 }
qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs)
Definition: qdatetime.cpp:2449
The QDate class provides date functions.
Definition: qdatetime.h:55
int ds() const
Definition: qdatetime.h:199
The QTime class provides clock time functions.
Definition: qdatetime.h:148
void getUTC(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4422
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
uint jd
Definition: qdatetime.h:137

◆ toString() [1/2]

QString QDateTime::toString ( Qt::DateFormat  format = Qt::TextDate) const

Returns the datetime as a string in the format given.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

If the format is Qt::TextDate, the string is formatted in the default way. QDate::shortDayName(), QDate::shortMonthName(), and QTime::toString() are used to generate the string, so the day and month names will be localized names. An example of this formatting is "Wed May 20 03:40:13 1998".

If the format is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form YYYY-MM-DDTHH:mm:ss[Z|[+|-]HH:mm], depending on the timeSpec() of the QDateTime. If the timeSpec() is Qt::UTC, Z will be appended to the string; if the timeSpec() is Qt::OffsetFromUTC the offset in hours and minutes from UTC will be appended to the string.

If the format is Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(datetime, QLocale::ShortFormat) or QLocale::system().toString(datetime, QLocale::LongFormat).

If the format is Qt::DefaultLocaleShortDate or Qt::DefaultLocaleLongDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(datetime, QLocale::ShortFormat) or QLocale().toString(datetime, QLocale::LongFormat).

If the datetime is invalid, an empty string will be returned.

Warning
The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to locale-aware formats as well, depending on the locale settings.
See also
QDate::toString() QTime::toString() Qt::DateFormat

Definition at line 2628 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDateTime::dateToString(), DumpIDL(), QPSPrintEnginePrivate::emitHeader(), QDeclarativeEnginePrivate::formatDateTime(), QSqlDriver::formatValue(), QTDSDriver::formatValue(), fromScriptValue(), QScriptDebuggerLocalsModelPrivate::get(), QDateTimeParser::getDigit(), QLibraryPrivate::isPlugin(), operator<<(), QDateTimeParser::parse(), QDateTimeParser::setDigit(), QPatternist::GYear::stringValue(), QPatternist::GYearMonth::stringValue(), QPatternist::GMonth::stringValue(), QPatternist::GDay::stringValue(), QPatternist::GMonthDay::stringValue(), QPatternist::AbstractDateTime::timeToString(), and QTest::toString().

2629 {
2630  QString buf;
2631  if (!isValid())
2632  return buf;
2633 
2634  if (f == Qt::ISODate) {
2635  buf = d->date.toString(Qt::ISODate);
2636  if (buf.isEmpty())
2637  return QString(); // failed to convert
2638  buf += QLatin1Char('T');
2639  buf += d->time.toString(Qt::ISODate);
2640  switch (d->spec) {
2641  case QDateTimePrivate::UTC:
2642  buf += QLatin1Char('Z');
2643  break;
2645  int sign = d->utcOffset >= 0 ? 1: -1;
2646  buf += QString::fromLatin1("%1%2:%3").
2647  arg(sign == 1 ? QLatin1Char('+') : QLatin1Char('-')).
2648  arg(d->utcOffset * sign / SECS_PER_HOUR, 2, 10, QLatin1Char('0')).
2649  arg((d->utcOffset / 60) % 60, 2, 10, QLatin1Char('0'));
2650  break;
2651  }
2652  default:
2653  break;
2654  }
2655  }
2656 #ifndef QT_NO_TEXTDATE
2657  else if (f == Qt::TextDate) {
2658 #ifndef Q_WS_WIN
2659  buf = d->date.shortDayName(d->date.dayOfWeek());
2660  buf += QLatin1Char(' ');
2661  buf += d->date.shortMonthName(d->date.month());
2662  buf += QLatin1Char(' ');
2663  buf += QString::number(d->date.day());
2664 #else
2665  wchar_t out[255];
2666  GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILDATE, out, 255);
2667  QString winstr = QString::fromWCharArray(out);
2668  switch (winstr.toInt()) {
2669  case 1:
2670  buf = d->date.shortDayName(d->date.dayOfWeek());
2671  buf += QLatin1Char(' ');
2672  buf += QString::number(d->date.day());
2673  buf += QLatin1String(". ");
2674  buf += d->date.shortMonthName(d->date.month());
2675  break;
2676  default:
2677  buf = d->date.shortDayName(d->date.dayOfWeek());
2678  buf += QLatin1Char(' ');
2679  buf += d->date.shortMonthName(d->date.month());
2680  buf += QLatin1Char(' ');
2681  buf += QString::number(d->date.day());
2682  }
2683 #endif
2684  buf += QLatin1Char(' ');
2685  buf += d->time.toString();
2686  buf += QLatin1Char(' ');
2687  buf += QString::number(d->date.year());
2688  }
2689 #endif
2690  else {
2691  buf = d->date.toString(f);
2692  if (buf.isEmpty())
2693  return QString(); // failed to convert
2694  buf += QLatin1Char(' ');
2695  buf += d->time.toString(f);
2696  }
2697 
2698  return buf;
2699 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
int dayOfWeek() const
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
Definition: qdatetime.cpp:408
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QString shortDayName(int weekday)
Returns the short version of the name of the weekday.
Definition: qdatetime.cpp:709
The QString class provides a Unicode character string.
Definition: qstring.h:83
static int sign(int x)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the time as a string.
Definition: qdatetime.cpp:1653
int utcOffset
The offset in seconds.
Definition: qdatetime_p.h:102
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QString shortMonthName(int month)
Returns the short version of the name of the month.
Definition: qdatetime.cpp:591
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the date as a string.
Definition: qdatetime.cpp:823
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ toString() [2/2]

QString QDateTime::toString ( const QString format) const

Returns the datetime as a string.

The format parameter determines the format of the result string.

These expressions may be 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 'Qt::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:

Expression Output
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the second without a leading zero (0 to 59)
ss the second with a leading zero (00 to 59)
z the milliseconds without leading zeroes (0 to 999)
zzz the milliseconds with leading zeroes (000 to 999)
AP use AM/PM display. AP will be replaced by either "AM" or "PM".
ap use am/pm display. ap will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.

Example format strings (assumed that 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

If the datetime is invalid, an empty string will be returned.

See also
QDate::toString() QTime::toString()

Definition at line 2769 of file qdatetime.cpp.

2770 {
2771  return fmtDateTime(format, &d->time, &d->date);
2772 }
static QString fmtDateTime(const QString &f, const QTime *dt=0, const QDate *dd=0)
Definition: qdatetime.cpp:4133
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ toTime_t()

uint QDateTime::toTime_t ( ) const

Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).

On systems that do not support time zones, this function will behave as if local time were Qt::UTC.

Note
This function returns a 32-bit unsigned integer, so it does not support dates before 1970, but it does support dates after 2038-01-19T03:14:06, which may not be valid time_t values. Be careful when passing those time_t values to system functions, which could interpret them as negative dates.

If the date is outside the range 1970-01-01T00:00:00 to 2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer (i.e., 0xFFFFFFFF).

To get an extended range, use toMSecsSinceEpoch().

See also
toMSecsSinceEpoch(), setTime_t()

Definition at line 2505 of file qdatetime.cpp.

Referenced by QNetworkSessionManagerPrivate::forceSessionClose(), QPixmap::load(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), qEncodeNtlmv2Response(), and qt_CoCreateGuid().

2506 {
2507  qint64 retval = toMSecsSinceEpoch() / 1000;
2508  if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF))
2509  return uint(-1);
2510  return uint(retval);
2511 }
qint64 toMSecsSinceEpoch() const
Returns the datetime as the number of milliseconds that have passed since 1970-01-01T00:00:00.
Definition: qdatetime.cpp:2474
unsigned __int64 quint64
Definition: qglobal.h:943
unsigned int uint
Definition: qglobal.h:996
__int64 qint64
Definition: qglobal.h:942
#define Q_UINT64_C(c)
Definition: qglobal.h:941

◆ toTimeSpec()

QDateTime QDateTime::toTimeSpec ( Qt::TimeSpec  specification) const

Returns a copy of this datetime configured to use the given time specification.

See also
timeSpec(), toUTC(), toLocalTime()

Definition at line 2969 of file qdatetime.cpp.

Referenced by QDateTimeParser::parse(), QDateTimeEdit::setDateTimeRange(), QDateTimeEdit::setMaximumDateTime(), QDateTimeEdit::setMinimumDateTime(), QDateTimeEditPrivate::stepBy(), and QDateTimeEditPrivate::updateTimeSpec().

2970 {
2971  if ((d->spec == QDateTimePrivate::UTC) == (spec == Qt::UTC))
2972  return *this;
2973 
2974  QDateTime ret;
2975  if (spec == Qt::UTC) {
2976  d->getUTC(ret.d->date, ret.d->time);
2977  ret.d->spec = QDateTimePrivate::UTC;
2978  } else {
2979  ret.d->spec = d->getLocal(ret.d->date, ret.d->time);
2980  }
2981  return ret;
2982 }
void getUTC(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4422
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
Spec getLocal(QDate &outDate, QTime &outTime) const
Definition: qdatetime.cpp:4413
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ toUTC()

QDateTime QDateTime::toUTC ( ) const
inline

Returns a datetime containing the date and time information in this datetime, but specified using the Qt::UTC definition.

See also
toTimeSpec()

Definition at line 251 of file qdatetime.h.

Referenced by QScript::DateTimeToMs(), QNetworkAccessHttpBackend::loadFromCacheIfAllowed(), QNetworkCookie::operator==(), operator>=(), QNetworkCookiePrivate::parseSetCookieHeaderLine(), QPatternist::AtomicValue::toQt(), QNetworkCookie::toRawForm(), and QPdfEnginePrivate::writeInfo().

251 { return toTimeSpec(Qt::UTC); }
QDateTime toTimeSpec(Qt::TimeSpec spec) const
Returns a copy of this datetime configured to use the given time specification.
Definition: qdatetime.cpp:2969

◆ utcOffset()

int QDateTime::utcOffset ( ) const
Since
4.4
Warning
This function is not part of the public interface.

Returns the UTC offset in seconds. If the timeSpec() isn't Qt::OffsetFromUTC, 0 is returned. However, since 0 is a valid UTC offset the return value of this function cannot be used to determine whether a utcOffset() is used or is valid, timeSpec() must be checked.

Likewise, if this QDateTime() is invalid or if timeSpec() isn't Qt::OffsetFromUTC, 0 is returned.

The UTC offset only applies if the timeSpec() is Qt::OffsetFromUTC.

See also
isValid(), setUtcOffset()

Definition at line 3449 of file qdatetime.cpp.

Referenced by QPatternist::AbstractDateTime::copyTimeSpec(), QPatternist::TimezoneFromAbstractDateTimeFN::extract(), fromString(), QDateTimePrivate::getUTC(), and QPatternist::AbstractDateTime::zoneOffsetToString().

3450 {
3452  return d->utcOffset;
3453  else
3454  return 0;
3455 }
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
int utcOffset
The offset in seconds.
Definition: qdatetime_p.h:102
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

Friends and Related Functions

◆ operator<<

QDataStream & operator<< ( QDataStream out,
const QDateTime dateTime 
)
friend

Writes dateTime to the out stream.

See also
{Serializing Qt Data Types}

Definition at line 3881 of file qdatetime.cpp.

3882 {
3883  out << dateTime.d->date << dateTime.d->time;
3884  if (out.version() >= 7)
3885  out << (qint8)dateTime.d->spec;
3886  return out;
3887 }
signed char qint8
Definition: qglobal.h:933
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ operator>>

QDataStream & operator>> ( QDataStream in,
QDateTime dateTime 
)
friend

Reads a datetime from the stream in into dateTime.

See also
{Serializing Qt Data Types}

Definition at line 3900 of file qdatetime.cpp.

3901 {
3902  dateTime.detach();
3903 
3905  in >> dateTime.d->date >> dateTime.d->time;
3906  if (in.version() >= 7)
3907  in >> ts;
3908  dateTime.d->spec = (QDateTimePrivate::Spec)ts;
3909  return in;
3910 }
signed char qint8
Definition: qglobal.h:933
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
void detach()
Definition: qdatetime.cpp:3791
QExplicitlySharedDataPointer< QDateTimePrivate > d
Definition: qdatetime.h:294

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 292 of file qdatetime.h.

Properties

◆ d


The documentation for this class was generated from the following files: