diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp index ce0ccc12f4..1a89d4d5aa 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qdatetime.cpp @@ -156,4 +156,39 @@ qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate); qSwap(startDate, endDate); // Make endDate before startDate. qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate); -//! [15] \ No newline at end of file +//! [15] + +//! [16] +QDateTime local(QDateTime::currentDateTime()); +QDateTime UTC(local.toTimeSpec(Qt::UTC)); +qDebug() << "Local time is:" << local; +qDebug() << "UTC time is:" << UTC; +qDebug() << "No difference between times:" << local.secsTo(UTC); +//! [16] + +//! [17] +QDateTime UTC(QDateTime::currentDateTimeUtc()); +QDateTime local(UTC.toLocalTime()); +qDebug() << "UTC time is:" << UTC; +qDebug() << "Local time is:" << local; +qDebug() << "No difference between times:" << UTC.secsTo(local); +//! [17] + +//! [18] +QDateTime local(QDateTime::currentDateTime()); +QDateTime UTC(local.toUTC()); +qDebug() << "Local time is:" << local; +qDebug() << "UTC time is:" << UTC; +qDebug() << "No difference between times:" << local.secsTo(UTC); +//! [18] + +//! [19] +QDateTime local(QDateTime::currentDateTime()); +qDebug() << "Local time is:" << local; + +QDateTime UTC(local); +UTC.setTimeSpec(Qt::UTC); +qDebug() << "UTC time is:" << UTC; + +qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes."; +//! [19] \ No newline at end of file diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 905055c0fe..8accc32930 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -2322,6 +2322,10 @@ void QDateTime::setTime(const QTime &time) /*! Sets the time specification used in this datetime to \a spec. + The datetime will refer to a different point in time. + + Example: + \snippet code/src_corelib_tools_qdatetime.cpp 19 \sa timeSpec(), setDate(), setTime(), Qt::TimeSpec */ @@ -2856,9 +2860,12 @@ qint64 QDateTime::msecsTo(const QDateTime &other) const /*! \fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec specification) const - Returns a copy of this datetime configured to use the given time + Returns a copy of this datetime converted to the given time \a specification. + Example: + \snippet code/src_corelib_tools_qdatetime.cpp 16 + \sa timeSpec(), toUTC(), toLocalTime() */ @@ -3570,6 +3577,10 @@ QDateTime QDateTime::fromString(const QString &string, const QString &format) Returns a datetime containing the date and time information in this datetime, but specified using the Qt::LocalTime definition. + Example: + + \snippet code/src_corelib_tools_qdatetime.cpp 17 + \sa toTimeSpec() */ @@ -3579,6 +3590,10 @@ QDateTime QDateTime::fromString(const QString &string, const QString &format) Returns a datetime containing the date and time information in this datetime, but specified using the Qt::UTC definition. + Example: + + \snippet code/src_corelib_tools_qdatetime.cpp 18 + \sa toTimeSpec() */