QDateTime: introduce {to,from,set,current}SecsSinceEpoch

These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.

The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.

[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.

[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.

Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2016-06-21 14:08:22 -07:00
parent 90eee08b3e
commit bff15c547e
5 changed files with 207 additions and 88 deletions

View File

@ -276,9 +276,9 @@ inline QDateTime QFileSystemMetaData::fileTime(QAbstractFileEngine::FileTime tim
#endif #endif
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
inline QDateTime QFileSystemMetaData::creationTime() const { return QDateTime::fromTime_t(creationTime_); } inline QDateTime QFileSystemMetaData::creationTime() const { return QDateTime::fromSecsSinceEpoch(creationTime_); }
inline QDateTime QFileSystemMetaData::modificationTime() const { return QDateTime::fromTime_t(modificationTime_); } inline QDateTime QFileSystemMetaData::modificationTime() const { return QDateTime::fromSecsSinceEpoch(modificationTime_); }
inline QDateTime QFileSystemMetaData::accessTime() const { return QDateTime::fromTime_t(accessTime_); } inline QDateTime QFileSystemMetaData::accessTime() const { return QDateTime::fromSecsSinceEpoch(accessTime_); }
inline uint QFileSystemMetaData::userId() const { return userId_; } inline uint QFileSystemMetaData::userId() const { return userId_; }
inline uint QFileSystemMetaData::groupId() const { return groupId_; } inline uint QFileSystemMetaData::groupId() const { return groupId_; }

View File

@ -977,7 +977,7 @@ QUuid QUuid::createUuid()
#else #else
static bool seeded = false; static bool seeded = false;
if (!seeded) if (!seeded)
qsrand(QDateTime::currentDateTimeUtc().toTime_t() qsrand(QDateTime::currentDateTimeUtc().toSecsSinceEpoch()
+ quintptr(&seeded)); + quintptr(&seeded));
#endif #endif

View File

@ -3475,7 +3475,7 @@ void QDateTime::setTimeZone(const QTimeZone &toZone)
this object is not valid. However, for all valid dates, this function this object is not valid. However, for all valid dates, this function
returns a unique value. returns a unique value.
\sa toTime_t(), setMSecsSinceEpoch() \sa toSecsSinceEpoch(), setMSecsSinceEpoch()
*/ */
qint64 QDateTime::toMSecsSinceEpoch() const qint64 QDateTime::toMSecsSinceEpoch() const
{ {
@ -3504,25 +3504,44 @@ qint64 QDateTime::toMSecsSinceEpoch() const
} }
/*! /*!
\since 5.8
Returns the datetime as the number of seconds that have passed since
1970-01-01T00:00:00.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 toMSecsSinceEpoch(), setSecsSinceEpoch()
*/
qint64 QDateTime::toSecsSinceEpoch() const
{
return toMSecsSinceEpoch() / 1000;
}
#if QT_DEPRECATED_SINCE(5, 8)
/*!
\deprecated
Returns the datetime as the number of seconds that have passed Returns the datetime as the number of seconds that have passed
since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
On systems that do not support time zones, this function will On systems that do not support time zones, this function will
behave as if local time were Qt::UTC. behave as if local time were Qt::UTC.
\note This function returns a 32-bit unsigned integer, so it does not \note This function returns a 32-bit unsigned integer and is deprecated.
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 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 2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer
(i.e., 0xFFFFFFFF). (i.e., 0xFFFFFFFF).
To get an extended range, use toMSecsSinceEpoch(). To get an extended range, use toMSecsSinceEpoch() or toSecsSinceEpoch().
\sa toMSecsSinceEpoch(), setTime_t() \sa toSecsSinceEpoch(), toMSecsSinceEpoch(), setTime_t()
*/ */
uint QDateTime::toTime_t() const uint QDateTime::toTime_t() const
@ -3534,6 +3553,7 @@ uint QDateTime::toTime_t() const
return uint(-1); return uint(-1);
return uint(retval); return uint(retval);
} }
#endif
/*! /*!
\since 4.7 \since 4.7
@ -3547,7 +3567,7 @@ uint QDateTime::toTime_t() const
(\c{std::numeric_limits<qint64>::min()}) to \a msecs will result in (\c{std::numeric_limits<qint64>::min()}) to \a msecs will result in
undefined behavior. undefined behavior.
\sa toMSecsSinceEpoch(), setTime_t() \sa toMSecsSinceEpoch(), setSecsSinceEpoch()
*/ */
void QDateTime::setMSecsSinceEpoch(qint64 msecs) void QDateTime::setMSecsSinceEpoch(qint64 msecs)
{ {
@ -3614,14 +3634,33 @@ void QDateTime::setMSecsSinceEpoch(qint64 msecs)
refreshDateTime(d); refreshDateTime(d);
} }
/*!
\since 5.8
Sets the date and time given the number of seconds \a secs that have
passed since 1970-01-01T00:00:00.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.
\sa toSecsSinceEpoch(), setMSecsSinceEpoch()
*/
void QDateTime::setSecsSinceEpoch(qint64 secs)
{
setMSecsSinceEpoch(secs * 1000);
}
#if QT_DEPRECATED_SINCE(5, 8)
/*! /*!
\fn void QDateTime::setTime_t(uint seconds) \fn void QDateTime::setTime_t(uint seconds)
\deprecated
Sets the date and time given the number of \a seconds that have Sets the date and time given the number of \a seconds that have
passed since 1970-01-01T00:00:00, Coordinated Universal Time passed since 1970-01-01T00:00:00, Coordinated Universal Time
(Qt::UTC). On systems that do not support time zones this function (Qt::UTC). On systems that do not support time zones this function
will behave as if local time were Qt::UTC. will behave as if local time were Qt::UTC.
\note This function is deprecated. For new code, use setSecsSinceEpoch().
\sa toTime_t() \sa toTime_t()
*/ */
@ -3629,6 +3668,7 @@ void QDateTime::setTime_t(uint secsSince1Jan1970UTC)
{ {
setMSecsSinceEpoch((qint64)secsSince1Jan1970UTC * 1000); setMSecsSinceEpoch((qint64)secsSince1Jan1970UTC * 1000);
} }
#endif
#ifndef QT_NO_DATESTRING #ifndef QT_NO_DATESTRING
/*! /*!
@ -4284,6 +4324,17 @@ qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW
- julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400000); - julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400000);
} }
qint64 QDateTime::currentSecsSinceEpoch() Q_DECL_NOTHROW
{
SYSTEMTIME st;
memset(&st, 0, sizeof(SYSTEMTIME));
GetSystemTime(&st);
return st.wHour * SECS_PER_HOUR + st.wMinute * SECS_PER_MIN + st.wSecond +
qint64(julianDayFromDate(st.wYear, st.wMonth, st.wDay)
- julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400);
}
#elif defined(Q_OS_UNIX) #elif defined(Q_OS_UNIX)
QDate QDate::currentDate() QDate QDate::currentDate()
{ {
@ -4314,18 +4365,29 @@ qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW
return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000; return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000;
} }
qint64 QDateTime::currentSecsSinceEpoch() Q_DECL_NOTHROW
{
struct timeval tv;
gettimeofday(&tv, 0);
return qint64(tv.tv_sec);
}
#else #else
#error "What system is this?" #error "What system is this?"
#endif #endif
#if QT_DEPRECATED_SINCE(5, 8)
/*! /*!
\since 4.2 \since 4.2
\deprecated
Returns a datetime whose date and time are the number of \a seconds Returns a datetime whose date and time are the number of \a seconds
that have passed since 1970-01-01T00:00:00, Coordinated Universal that have passed since 1970-01-01T00:00:00, Coordinated Universal
Time (Qt::UTC) and converted to Qt::LocalTime. On systems that do not Time (Qt::UTC) and converted to Qt::LocalTime. On systems that do not
support time zones, the time will be set as if local time were Qt::UTC. support time zones, the time will be set as if local time were Qt::UTC.
\note This function is deprecated. Please use fromSecsSinceEpoch() in new
code.
\sa toTime_t(), setTime_t() \sa toTime_t(), setTime_t()
*/ */
QDateTime QDateTime::fromTime_t(uint seconds) QDateTime QDateTime::fromTime_t(uint seconds)
@ -4335,6 +4397,7 @@ QDateTime QDateTime::fromTime_t(uint seconds)
/*! /*!
\since 5.2 \since 5.2
\deprecated
Returns a datetime whose date and time are the number of \a seconds Returns a datetime whose date and time are the number of \a seconds
that have passed since 1970-01-01T00:00:00, Coordinated Universal that have passed since 1970-01-01T00:00:00, Coordinated Universal
@ -4344,6 +4407,9 @@ QDateTime QDateTime::fromTime_t(uint seconds)
ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0 ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0
then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds. then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds.
\note This function is deprecated. Please use fromSecsSinceEpoch() in new
code.
\sa toTime_t(), setTime_t() \sa toTime_t(), setTime_t()
*/ */
QDateTime QDateTime::fromTime_t(uint seconds, Qt::TimeSpec spec, int offsetSeconds) QDateTime QDateTime::fromTime_t(uint seconds, Qt::TimeSpec spec, int offsetSeconds)
@ -4354,11 +4420,15 @@ QDateTime QDateTime::fromTime_t(uint seconds, Qt::TimeSpec spec, int offsetSecon
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
/*! /*!
\since 5.2 \since 5.2
\deprecated
Returns a datetime whose date and time are the number of \a seconds Returns a datetime whose date and time are the number of \a seconds
that have passed since 1970-01-01T00:00:00, Coordinated Universal that have passed since 1970-01-01T00:00:00, Coordinated Universal
Time (Qt::UTC) and with the given \a timeZone. Time (Qt::UTC) and with the given \a timeZone.
\note This function is deprecated. Please use fromSecsSinceEpoch() in new
code.
\sa toTime_t(), setTime_t() \sa toTime_t(), setTime_t()
*/ */
QDateTime QDateTime::fromTime_t(uint seconds, const QTimeZone &timeZone) QDateTime QDateTime::fromTime_t(uint seconds, const QTimeZone &timeZone)
@ -4366,6 +4436,7 @@ QDateTime QDateTime::fromTime_t(uint seconds, const QTimeZone &timeZone)
return fromMSecsSinceEpoch((qint64)seconds * 1000, timeZone); return fromMSecsSinceEpoch((qint64)seconds * 1000, timeZone);
} }
#endif #endif
#endif // QT_DEPRECATED_SINCE(5, 8)
/*! /*!
\since 4.7 \since 4.7
@ -4379,7 +4450,7 @@ QDateTime QDateTime::fromTime_t(uint seconds, const QTimeZone &timeZone)
range of QDateTime, both negative and positive. The behavior of this range of QDateTime, both negative and positive. The behavior of this
function is undefined for those values. function is undefined for those values.
\sa toTime_t(), setTime_t() \sa toMSecsSinceEpoch(), setMSecsSinceEpoch()
*/ */
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs) QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs)
{ {
@ -4404,7 +4475,7 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs)
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
i.e. the current system time zone. i.e. the current system time zone.
\sa fromTime_t() \sa toMSecsSinceEpoch(), setMSecsSinceEpoch()
*/ */
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds) QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds)
{ {
@ -4414,6 +4485,31 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int of
return dt; return dt;
} }
/*!
\since 5.8
Returns a datetime whose date and time are the number of seconds \a secs
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
Time (Qt::UTC) and converted to the given \a spec.
Note that there are possible values for \a secs that lie outside the valid
range of QDateTime, both negative and positive. The behavior of this
function is undefined for those values.
If the \a spec is not Qt::OffsetFromUTC then the \a offsetSeconds will be
ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0
then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds.
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
i.e. the current system time zone.
\sa toSecsSinceEpoch(), setSecsSinceEpoch()
*/
QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds)
{
return fromMSecsSinceEpoch(secs * 1000, spec, offsetSeconds);
}
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
/*! /*!
\since 5.2 \since 5.2
@ -4422,7 +4518,7 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int of
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
Time (Qt::UTC) and with the given \a timeZone. Time (Qt::UTC) and with the given \a timeZone.
\sa fromTime_t() \sa fromSecsSinceEpoch()
*/ */
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone) QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone)
{ {
@ -4431,6 +4527,20 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone
dt.setMSecsSinceEpoch(msecs); dt.setMSecsSinceEpoch(msecs);
return dt; return dt;
} }
/*!
\since 5.8
Returns a datetime whose date and time are the number of seconds \a secs
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
Time (Qt::UTC) and with the given \a timeZone.
\sa fromMSecsSinceEpoch()
*/
QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone)
{
return fromMSecsSinceEpoch(secs * 1000, timeZone);
}
#endif #endif
#if QT_DEPRECATED_SINCE(5, 2) #if QT_DEPRECATED_SINCE(5, 2)

View File

@ -280,8 +280,7 @@ public:
bool isDaylightTime() const; bool isDaylightTime() const;
qint64 toMSecsSinceEpoch() const; qint64 toMSecsSinceEpoch() const;
// ### Qt 6: use quint64 instead of uint qint64 toSecsSinceEpoch() const;
uint toTime_t() const;
void setDate(const QDate &date); void setDate(const QDate &date);
void setTime(const QTime &time); void setTime(const QTime &time);
@ -291,8 +290,7 @@ public:
void setTimeZone(const QTimeZone &toZone); void setTimeZone(const QTimeZone &toZone);
#endif // QT_BOOTSTRAPPED #endif // QT_BOOTSTRAPPED
void setMSecsSinceEpoch(qint64 msecs); void setMSecsSinceEpoch(qint64 msecs);
// ### Qt 6: use quint64 instead of uint void setSecsSinceEpoch(qint64 secs);
void setTime_t(uint secsSince1Jan1970UTC);
#ifndef QT_NO_DATESTRING #ifndef QT_NO_DATESTRING
QString toString(Qt::DateFormat f = Qt::TextDate) const; QString toString(Qt::DateFormat f = Qt::TextDate) const;
@ -334,21 +332,28 @@ public:
static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate); static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
static QDateTime fromString(const QString &s, const QString &format); static QDateTime fromString(const QString &s, const QString &format);
#endif #endif
// ### Qt 6: use quint64 instead of uint
#if QT_DEPRECATED_SINCE(5, 8)
uint toTime_t() const;
void setTime_t(uint secsSince1Jan1970UTC);
static QDateTime fromTime_t(uint secsSince1Jan1970UTC); static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
// ### Qt 6: Merge with above with default spec = Qt::LocalTime
static QDateTime fromTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec, static QDateTime fromTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec,
int offsetFromUtc = 0); int offsetFromUtc = 0);
#ifndef QT_BOOTSTRAPPED
static QDateTime fromTime_t(uint secsSince1Jan1970UTC, const QTimeZone &timeZone); static QDateTime fromTime_t(uint secsSince1Jan1970UTC, const QTimeZone &timeZone);
#endif #endif
static QDateTime fromMSecsSinceEpoch(qint64 msecs); static QDateTime fromMSecsSinceEpoch(qint64 msecs);
// ### Qt 6: Merge with above with default spec = Qt::LocalTime // ### Qt 6: Merge with above with default spec = Qt::LocalTime
static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0); static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0);
static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spe = Qt::LocalTime, int offsetFromUtc = 0);
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone); static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone);
static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone);
#endif #endif
static qint64 currentMSecsSinceEpoch() Q_DECL_NOTHROW; static qint64 currentMSecsSinceEpoch() Q_DECL_NOTHROW;
static qint64 currentSecsSinceEpoch() Q_DECL_NOTHROW;
#if defined(Q_OS_DARWIN) || defined(Q_QDOC) #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
static QDateTime fromCFDate(CFDateRef date); static QDateTime fromCFDate(CFDateRef date);

View File

@ -59,8 +59,8 @@ private slots:
void date(); void date();
void time(); void time();
void timeSpec(); void timeSpec();
void toTime_t_data(); void toSecsSinceEpoch_data();
void toTime_t(); void toSecsSinceEpoch();
void daylightSavingsTimeChange_data(); void daylightSavingsTimeChange_data();
void daylightSavingsTimeChange(); void daylightSavingsTimeChange();
void springForward_data(); void springForward_data();
@ -70,7 +70,7 @@ private slots:
void setTime(); void setTime();
void setTimeSpec_data(); void setTimeSpec_data();
void setTimeSpec(); void setTimeSpec();
void setTime_t(); void setSecsSinceEpoch();
void setMSecsSinceEpoch_data(); void setMSecsSinceEpoch_data();
void setMSecsSinceEpoch(); void setMSecsSinceEpoch();
void fromMSecsSinceEpoch_data(); void fromMSecsSinceEpoch_data();
@ -175,22 +175,22 @@ tst_QDateTime::tst_QDateTime()
differently, so don't probe them here. differently, so don't probe them here.
*/ */
const uint day = 24 * 3600; // in seconds const uint day = 24 * 3600; // in seconds
zoneIsCET = (QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7)).toTime_t() == 0x7fffffff zoneIsCET = (QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7)).toSecsSinceEpoch() == 0x7fffffff
// Entries a year apart robustly differ by multiples of day. // Entries a year apart robustly differ by multiples of day.
&& QDateTime(QDate(2015, 7, 1), QTime()).toTime_t() == 1435701600 && QDateTime(QDate(2015, 7, 1), QTime()).toSecsSinceEpoch() == 1435701600
&& QDateTime(QDate(2015, 1, 1), QTime()).toTime_t() == 1420066800 && QDateTime(QDate(2015, 1, 1), QTime()).toSecsSinceEpoch() == 1420066800
&& QDateTime(QDate(2013, 7, 1), QTime()).toTime_t() == 1372629600 && QDateTime(QDate(2013, 7, 1), QTime()).toSecsSinceEpoch() == 1372629600
&& QDateTime(QDate(2013, 1, 1), QTime()).toTime_t() == 1356994800 && QDateTime(QDate(2013, 1, 1), QTime()).toSecsSinceEpoch() == 1356994800
&& QDateTime(QDate(2012, 7, 1), QTime()).toTime_t() == 1341093600 && QDateTime(QDate(2012, 7, 1), QTime()).toSecsSinceEpoch() == 1341093600
&& QDateTime(QDate(2012, 1, 1), QTime()).toTime_t() == 1325372400 && QDateTime(QDate(2012, 1, 1), QTime()).toSecsSinceEpoch() == 1325372400
&& QDateTime(QDate(2008, 7, 1), QTime()).toTime_t() == 1214863200 && QDateTime(QDate(2008, 7, 1), QTime()).toSecsSinceEpoch() == 1214863200
&& QDateTime(QDate(2004, 1, 1), QTime()).toTime_t() == 1072911600 && QDateTime(QDate(2004, 1, 1), QTime()).toSecsSinceEpoch() == 1072911600
&& QDateTime(QDate(2000, 1, 1), QTime()).toTime_t() == 946681200 && QDateTime(QDate(2000, 1, 1), QTime()).toSecsSinceEpoch() == 946681200
&& QDateTime(QDate(1990, 7, 1), QTime()).toTime_t() == 646783200 && QDateTime(QDate(1990, 7, 1), QTime()).toSecsSinceEpoch() == 646783200
&& QDateTime(QDate(1990, 1, 1), QTime()).toTime_t() == 631148400 && QDateTime(QDate(1990, 1, 1), QTime()).toSecsSinceEpoch() == 631148400
&& QDateTime(QDate(1979, 1, 1), QTime()).toTime_t() == 283993200 && QDateTime(QDate(1979, 1, 1), QTime()).toSecsSinceEpoch() == 283993200
// .toTime_t() returns -1 for everything before this: // .toSecsSinceEpoch() returns -1 for everything before this:
&& QDateTime(QDate(1970, 1, 1), QTime(1, 0, 0)).toTime_t() == 0); && QDateTime(QDate(1970, 1, 1), QTime(1, 0, 0)).toSecsSinceEpoch() == 0);
// Use .toMSecsSinceEpoch() if you really need to test anything earlier. // Use .toMSecsSinceEpoch() if you really need to test anything earlier.
/* /*
@ -202,12 +202,12 @@ tst_QDateTime::tst_QDateTime()
*/ */
const int sampled = 3; const int sampled = 3;
// UTC starts of months in 2004, 2038 and 1970: // UTC starts of months in 2004, 2038 and 1970:
uint jans[sampled] = { 12418 * day, 24837 * day, 0 }; qint64 jans[sampled] = { 12418 * day, 24837 * day, 0 };
uint juls[sampled] = { 12600 * day, 25018 * day, 181 * day }; qint64 juls[sampled] = { 12600 * day, 25018 * day, 181 * day };
localTimeType = LocalTimeIsUtc; localTimeType = LocalTimeIsUtc;
for (int i = sampled; i-- > 0; ) { for (int i = sampled; i-- > 0; ) {
QDateTime jan = QDateTime::fromTime_t(jans[i]); QDateTime jan = QDateTime::fromSecsSinceEpoch(jans[i]);
QDateTime jul = QDateTime::fromTime_t(juls[i]); QDateTime jul = QDateTime::fromSecsSinceEpoch(juls[i]);
if (jan.date().year() < 1970 || jul.date().month() < 7) { if (jan.date().year() < 1970 || jul.date().month() < 7) {
localTimeType = LocalTimeBehindUtc; localTimeType = LocalTimeBehindUtc;
break; break;
@ -516,52 +516,52 @@ void tst_QDateTime::setTimeSpec()
QCOMPARE(dateTime.timeSpec(), newTimeSpec); QCOMPARE(dateTime.timeSpec(), newTimeSpec);
} }
void tst_QDateTime::setTime_t() void tst_QDateTime::setSecsSinceEpoch()
{ {
QDateTime dt1; QDateTime dt1;
dt1.setTime_t(0); dt1.setSecsSinceEpoch(0);
QCOMPARE(dt1.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC)); QCOMPARE(dt1.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC));
QCOMPARE(dt1.timeSpec(), Qt::LocalTime); QCOMPARE(dt1.timeSpec(), Qt::LocalTime);
dt1.setTimeSpec(Qt::UTC); dt1.setTimeSpec(Qt::UTC);
dt1.setTime_t(0); dt1.setSecsSinceEpoch(0);
QCOMPARE(dt1, QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC)); QCOMPARE(dt1, QDateTime(QDate(1970, 1, 1), QTime(), Qt::UTC));
QCOMPARE(dt1.timeSpec(), Qt::UTC); QCOMPARE(dt1.timeSpec(), Qt::UTC);
dt1.setTime_t(123456); dt1.setSecsSinceEpoch(123456);
QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC)); QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC));
if (zoneIsCET) { if (zoneIsCET) {
QDateTime dt2; QDateTime dt2;
dt2.setTime_t(123456); dt2.setSecsSinceEpoch(123456);
QCOMPARE(dt2, QDateTime(QDate(1970, 1, 2), QTime(11, 17, 36), Qt::LocalTime)); QCOMPARE(dt2, QDateTime(QDate(1970, 1, 2), QTime(11, 17, 36), Qt::LocalTime));
} }
dt1.setTime_t((uint)(quint32)-123456); dt1.setSecsSinceEpoch((uint)(quint32)-123456);
QCOMPARE(dt1, QDateTime(QDate(2106, 2, 5), QTime(20, 10, 40), Qt::UTC)); QCOMPARE(dt1, QDateTime(QDate(2106, 2, 5), QTime(20, 10, 40), Qt::UTC));
if (zoneIsCET) { if (zoneIsCET) {
QDateTime dt2; QDateTime dt2;
dt2.setTime_t((uint)(quint32)-123456); dt2.setSecsSinceEpoch((uint)(quint32)-123456);
QCOMPARE(dt2, QDateTime(QDate(2106, 2, 5), QTime(21, 10, 40), Qt::LocalTime)); QCOMPARE(dt2, QDateTime(QDate(2106, 2, 5), QTime(21, 10, 40), Qt::LocalTime));
} }
dt1.setTime_t(1214567890); dt1.setSecsSinceEpoch(1214567890);
QCOMPARE(dt1, QDateTime(QDate(2008, 6, 27), QTime(11, 58, 10), Qt::UTC)); QCOMPARE(dt1, QDateTime(QDate(2008, 6, 27), QTime(11, 58, 10), Qt::UTC));
if (zoneIsCET) { if (zoneIsCET) {
QDateTime dt2; QDateTime dt2;
dt2.setTime_t(1214567890); dt2.setSecsSinceEpoch(1214567890);
QCOMPARE(dt2, QDateTime(QDate(2008, 6, 27), QTime(13, 58, 10), Qt::LocalTime)); QCOMPARE(dt2, QDateTime(QDate(2008, 6, 27), QTime(13, 58, 10), Qt::LocalTime));
} }
dt1.setTime_t(0x7FFFFFFF); dt1.setSecsSinceEpoch(0x7FFFFFFF);
QCOMPARE(dt1, QDateTime(QDate(2038, 1, 19), QTime(3, 14, 7), Qt::UTC)); QCOMPARE(dt1, QDateTime(QDate(2038, 1, 19), QTime(3, 14, 7), Qt::UTC));
if (zoneIsCET) { if (zoneIsCET) {
QDateTime dt2; QDateTime dt2;
dt2.setTime_t(0x7FFFFFFF); dt2.setSecsSinceEpoch(0x7FFFFFFF);
QCOMPARE(dt2, QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7), Qt::LocalTime)); QCOMPARE(dt2, QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7), Qt::LocalTime));
} }
dt1 = QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 60 * 60); dt1 = QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 60 * 60);
dt1.setTime_t(123456); dt1.setSecsSinceEpoch(123456);
QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC)); QCOMPARE(dt1, QDateTime(QDate(1970, 1, 2), QTime(10, 17, 36), Qt::UTC));
QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC); QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC);
QCOMPARE(dt1.offsetFromUtc(), 60 * 60); QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
@ -665,7 +665,7 @@ void tst_QDateTime::setMSecsSinceEpoch()
QCOMPARE(dt.toMSecsSinceEpoch(), msecs); QCOMPARE(dt.toMSecsSinceEpoch(), msecs);
if (quint64(msecs / 1000) < 0xFFFFFFFF) { if (quint64(msecs / 1000) < 0xFFFFFFFF) {
QCOMPARE(qint64(dt.toTime_t()), msecs / 1000); QCOMPARE(qint64(dt.toSecsSinceEpoch()), msecs / 1000);
} }
QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC); QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC);
@ -716,9 +716,9 @@ void tst_QDateTime::fromMSecsSinceEpoch()
QCOMPARE(dtOffset.toMSecsSinceEpoch(), msecs); QCOMPARE(dtOffset.toMSecsSinceEpoch(), msecs);
if (quint64(msecs / 1000) < 0xFFFFFFFF) { if (quint64(msecs / 1000) < 0xFFFFFFFF) {
QCOMPARE(qint64(dtLocal.toTime_t()), msecs / 1000); QCOMPARE(qint64(dtLocal.toSecsSinceEpoch()), msecs / 1000);
QCOMPARE(qint64(dtUtc.toTime_t()), msecs / 1000); QCOMPARE(qint64(dtUtc.toSecsSinceEpoch()), msecs / 1000);
QCOMPARE(qint64(dtOffset.toTime_t()), msecs / 1000); QCOMPARE(qint64(dtOffset.toSecsSinceEpoch()), msecs / 1000);
} }
QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC); QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC);
@ -1425,7 +1425,7 @@ void tst_QDateTime::currentDateTime()
time_t buf1, buf2; time_t buf1, buf2;
::time(&buf1); ::time(&buf1);
QDateTime lowerBound; QDateTime lowerBound;
lowerBound.setTime_t(buf1); lowerBound.setSecsSinceEpoch(buf1);
QDateTime dt1 = QDateTime::currentDateTime(); QDateTime dt1 = QDateTime::currentDateTime();
QDateTime dt2 = QDateTime::currentDateTime().toLocalTime(); QDateTime dt2 = QDateTime::currentDateTime().toLocalTime();
@ -1434,7 +1434,7 @@ void tst_QDateTime::currentDateTime()
::time(&buf2); ::time(&buf2);
QDateTime upperBound; QDateTime upperBound;
upperBound.setTime_t(buf2); upperBound.setSecsSinceEpoch(buf2);
// Note we must add 2 seconds here because time() may return up to // Note we must add 2 seconds here because time() may return up to
// 1 second difference from the more accurate method used by QDateTime::currentDateTime() // 1 second difference from the more accurate method used by QDateTime::currentDateTime()
upperBound = upperBound.addSecs(2); upperBound = upperBound.addSecs(2);
@ -1445,11 +1445,11 @@ void tst_QDateTime::currentDateTime()
"dt2: %3\n" "dt2: %3\n"
"dt3: %4\n" "dt3: %4\n"
"upperBound: %5\n") "upperBound: %5\n")
.arg(lowerBound.toTime_t()) .arg(lowerBound.toSecsSinceEpoch())
.arg(dt1.toTime_t()) .arg(dt1.toSecsSinceEpoch())
.arg(dt2.toTime_t()) .arg(dt2.toSecsSinceEpoch())
.arg(dt3.toTime_t()) .arg(dt3.toSecsSinceEpoch())
.arg(upperBound.toTime_t()); .arg(upperBound.toSecsSinceEpoch());
QVERIFY2(lowerBound < upperBound, qPrintable(details)); QVERIFY2(lowerBound < upperBound, qPrintable(details));
@ -1471,7 +1471,7 @@ void tst_QDateTime::currentDateTimeUtc()
::time(&buf1); ::time(&buf1);
QDateTime lowerBound; QDateTime lowerBound;
lowerBound.setTime_t(buf1); lowerBound.setSecsSinceEpoch(buf1);
QDateTime dt1 = QDateTime::currentDateTimeUtc(); QDateTime dt1 = QDateTime::currentDateTimeUtc();
QDateTime dt2 = QDateTime::currentDateTimeUtc().toLocalTime(); QDateTime dt2 = QDateTime::currentDateTimeUtc().toLocalTime();
@ -1480,7 +1480,7 @@ void tst_QDateTime::currentDateTimeUtc()
::time(&buf2); ::time(&buf2);
QDateTime upperBound; QDateTime upperBound;
upperBound.setTime_t(buf2); upperBound.setSecsSinceEpoch(buf2);
// Note we must add 2 seconds here because time() may return up to // Note we must add 2 seconds here because time() may return up to
// 1 second difference from the more accurate method used by QDateTime::currentDateTime() // 1 second difference from the more accurate method used by QDateTime::currentDateTime()
upperBound = upperBound.addSecs(2); upperBound = upperBound.addSecs(2);
@ -1491,11 +1491,11 @@ void tst_QDateTime::currentDateTimeUtc()
"dt2: %3\n" "dt2: %3\n"
"dt3: %4\n" "dt3: %4\n"
"upperBound: %5\n") "upperBound: %5\n")
.arg(lowerBound.toTime_t()) .arg(lowerBound.toSecsSinceEpoch())
.arg(dt1.toTime_t()) .arg(dt1.toSecsSinceEpoch())
.arg(dt2.toTime_t()) .arg(dt2.toSecsSinceEpoch())
.arg(dt3.toTime_t()) .arg(dt3.toSecsSinceEpoch())
.arg(upperBound.toTime_t()); .arg(upperBound.toSecsSinceEpoch());
QVERIFY2(lowerBound < upperBound, qPrintable(details)); QVERIFY2(lowerBound < upperBound, qPrintable(details));
@ -1540,14 +1540,14 @@ void tst_QDateTime::currentDateTimeUtc2()
QCOMPARE(local.toUTC(), utc); QCOMPARE(local.toUTC(), utc);
QCOMPARE(utc.toLocalTime(), local); QCOMPARE(utc.toLocalTime(), local);
// and finally, the time_t should equal our number // and finally, the SecsSinceEpoch should equal our number
QCOMPARE(qint64(utc.toTime_t()), msec / 1000); QCOMPARE(qint64(utc.toSecsSinceEpoch()), msec / 1000);
QCOMPARE(qint64(local.toTime_t()), msec / 1000); QCOMPARE(qint64(local.toSecsSinceEpoch()), msec / 1000);
QCOMPARE(utc.toMSecsSinceEpoch(), msec); QCOMPARE(utc.toMSecsSinceEpoch(), msec);
QCOMPARE(local.toMSecsSinceEpoch(), msec); QCOMPARE(local.toMSecsSinceEpoch(), msec);
} }
void tst_QDateTime::toTime_t_data() void tst_QDateTime::toSecsSinceEpoch_data()
{ {
QTest::addColumn<QString>("dateTimeStr"); QTest::addColumn<QString>("dateTimeStr");
QTest::addColumn<bool>("res"); QTest::addColumn<bool>("res");
@ -1563,11 +1563,12 @@ void tst_QDateTime::toTime_t_data()
<< bool( sizeof(uint) > 32 && sizeof(time_t) > 32 ); << bool( sizeof(uint) > 32 && sizeof(time_t) > 32 );
} }
void tst_QDateTime::toTime_t() void tst_QDateTime::toSecsSinceEpoch()
{ {
QFETCH( QString, dateTimeStr ); QFETCH( QString, dateTimeStr );
QDateTime datetime = dt( dateTimeStr ); QDateTime datetime = dt( dateTimeStr );
qint64 asSecsSinceEpoch = datetime.toSecsSinceEpoch();
uint asTime_t = datetime.toTime_t(); uint asTime_t = datetime.toTime_t();
QFETCH( bool, res ); QFETCH( bool, res );
if (res) { if (res) {
@ -1575,11 +1576,14 @@ void tst_QDateTime::toTime_t()
} else { } else {
QVERIFY( asTime_t == (uint)-1 ); QVERIFY( asTime_t == (uint)-1 );
} }
QCOMPARE(asSecsSinceEpoch, datetime.toMSecsSinceEpoch() / 1000);
if ( asTime_t != (uint) -1 ) { if ( asTime_t != (uint) -1 ) {
QDateTime datetime2 = QDateTime::fromTime_t( asTime_t ); QDateTime datetime2 = QDateTime::fromTime_t( asTime_t );
QCOMPARE(datetime, datetime2); QCOMPARE(datetime, datetime2);
} }
QDateTime datetime2 = QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch);
QCOMPARE(datetime, datetime2);
} }
void tst_QDateTime::daylightSavingsTimeChange_data() void tst_QDateTime::daylightSavingsTimeChange_data()
@ -1618,7 +1622,7 @@ void tst_QDateTime::daylightSavingsTimeChange()
// First with simple construction // First with simple construction
QDateTime dt = QDateTime(outDST, QTime(0, 0, 0), Qt::LocalTime); QDateTime dt = QDateTime(outDST, QTime(0, 0, 0), Qt::LocalTime);
int outDSTsecs = dt.toTime_t(); int outDSTsecs = dt.toSecsSinceEpoch();
dt.setDate(inDST); dt.setDate(inDST);
dt = dt.addSecs(1); dt = dt.addSecs(1);
@ -1640,8 +1644,8 @@ void tst_QDateTime::daylightSavingsTimeChange()
dt = dt.addMonths(-months).addSecs(1); dt = dt.addMonths(-months).addSecs(1);
QCOMPARE(dt, QDateTime(inDST, QTime(0, 0, 5))); QCOMPARE(dt, QDateTime(inDST, QTime(0, 0, 5)));
// now using fromTime_t // now using fromSecsSinceEpoch
dt = QDateTime::fromTime_t(outDSTsecs); dt = QDateTime::fromSecsSinceEpoch(outDSTsecs);
QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 0))); QCOMPARE(dt, QDateTime(outDST, QTime(0, 0, 0)));
dt.setDate(inDST); dt.setDate(inDST);
@ -1695,7 +1699,7 @@ void tst_QDateTime::springForward_data()
QTest::addColumn<int>("adjust"); // minutes ahead of UTC on day stepped from QTest::addColumn<int>("adjust"); // minutes ahead of UTC on day stepped from
/* /*
Zone tests compare a summer and winter moment's time_t to known values. Zone tests compare a summer and winter moment's SecsSinceEpoch to known values.
This could in principle be flawed (two DST-using zones in the same This could in principle be flawed (two DST-using zones in the same
hemisphere with the same DST and standard times but different transition hemisphere with the same DST and standard times but different transition
times) but no actual example is known where this is a problem. Please times) but no actual example is known where this is a problem. Please
@ -1705,8 +1709,8 @@ void tst_QDateTime::springForward_data()
test. test.
*/ */
uint winter = QDateTime(QDate(2015, 1, 1), QTime()).toTime_t(); uint winter = QDateTime(QDate(2015, 1, 1), QTime()).toSecsSinceEpoch();
uint summer = QDateTime(QDate(2015, 7, 1), QTime()).toTime_t(); uint summer = QDateTime(QDate(2015, 7, 1), QTime()).toSecsSinceEpoch();
if (winter == 1420066800 && summer == 1435701600) { if (winter == 1420066800 && summer == 1435701600) {
QTest::newRow("CET from day before") << QDate(2015, 3, 29) << QTime(2, 30, 0) << 1 << 60; QTest::newRow("CET from day before") << QDate(2015, 3, 29) << QTime(2, 30, 0) << 1 << 60;
@ -2418,7 +2422,7 @@ void tst_QDateTime::setOffsetFromUtc()
dt1.setMSecsSinceEpoch(123456789); dt1.setMSecsSinceEpoch(123456789);
QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC); QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC);
QCOMPARE(dt1.offsetFromUtc(), 60 * 60); QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
dt1.setTime_t(123456789); dt1.setSecsSinceEpoch(123456789);
QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC); QCOMPARE(dt1.timeSpec(), Qt::OffsetFromUTC);
QCOMPARE(dt1.offsetFromUtc(), 60 * 60); QCOMPARE(dt1.offsetFromUtc(), 60 * 60);
@ -3036,7 +3040,7 @@ void tst_QDateTime::timeZones() const
QCOMPARE(dt1.time(), QTime(0, 0, 0)); QCOMPARE(dt1.time(), QTime(0, 0, 0));
QCOMPARE(dt1.timeZone(), nzTz); QCOMPARE(dt1.timeZone(), nzTz);
QDateTime dt2 = QDateTime::fromTime_t(1338465600, nzTz); QDateTime dt2 = QDateTime::fromSecsSinceEpoch(1338465600, nzTz);
QCOMPARE(dt2.date(), dt1.date()); QCOMPARE(dt2.date(), dt1.date());
QCOMPARE(dt2.time(), dt1.time()); QCOMPARE(dt2.time(), dt1.time());
QCOMPARE(dt2.timeSpec(), dt1.timeSpec()); QCOMPARE(dt2.timeSpec(), dt1.timeSpec());