Remove usages of deprecated APIs from QDateTime

- Replaced the usages of:
  * QDateTime::toTime_t() -> QDateTime::toSecsSinceEpoch().
  * QDateTime::fromTime_t() -> QDateTime::fromSecsSinceEpoch().
  * QDate::shortDayName() -> QLocale::system().dayName().
  * QTime by QElapsedTimer, where the deprecated methods of QTime
    were used.

- Modified the tests for the deprecated methods to be enabled only
  when the corresponding methods are enabled: when the deprecated
  APIs are disabled, the tests will be also disabled, and the
  compilation won't be broken.

Task-number: QTBUG-76491
Change-Id: I4d565db2329e580c567aae511696eb1efe120843
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Sona Kurazyan 2019-06-26 12:48:21 +02:00
parent c34242c679
commit 8c8eae279d
7 changed files with 40 additions and 10 deletions

View File

@ -114,7 +114,7 @@ void MainWindow::insertCalendar()
int year = date.year(), month = date.month();
for (int weekDay = 1; weekDay <= 7; ++weekDay) {
cursor.insertText(QString("%1 ").arg(QDate::shortDayName(weekDay), 3),
cursor.insertText(QString("%1 ").arg(QLocale::system().dayName(weekDay), 3),
boldFormat);
}

View File

@ -117,7 +117,7 @@ void MainWindow::insertCalendar()
int year = date.year(), month = date.month();
for (int weekDay = 1; weekDay <= 7; ++weekDay) {
cursor.insertText(QString("%1 ").arg(QDate::shortDayName(weekDay), 3),
cursor.insertText(QString("%1 ").arg(QLocale::system().dayName(weekDay), 3),
boldFormat);
}

View File

@ -1261,7 +1261,7 @@ void tst_QFileInfo::fakeFileTimes_data()
QTest::newRow("early") << QDateTime(QDate(1901, 12, 14), QTime(12, 0));
// QTBUG-12006 claims XP handled this (2010-Mar-26 8:46:10) wrong due to an MS API bug:
QTest::newRow("XP-bug") << QDateTime::fromTime_t(1269593170);
QTest::newRow("XP-bug") << QDateTime::fromSecsSinceEpoch(1269593170);
}
void tst_QFileInfo::fakeFileTimes()

View File

@ -91,7 +91,7 @@ private slots:
void negativeYear() const;
void printNegativeYear() const;
void roundtripGermanLocale() const;
#if QT_CONFIG(textdate)
#if QT_CONFIG(textdate) && QT_DEPRECATED_SINCE(5, 10)
void shortDayName() const;
void standaloneShortDayName() const;
void longDayName() const;
@ -1473,7 +1473,7 @@ void tst_QDate::roundtripGermanLocale() const
theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
}
#if QT_CONFIG(textdate)
#if QT_CONFIG(textdate) && QT_DEPRECATED_SINCE(5, 10)
QT_WARNING_PUSH // the methods tested here are all deprecated
QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")

View File

@ -61,6 +61,10 @@ private slots:
void timeSpec();
void toSecsSinceEpoch_data();
void toSecsSinceEpoch();
#if QT_DEPRECATED_SINCE(5, 8)
void toTime_t_data();
void toTime_t();
#endif
void daylightSavingsTimeChange_data();
void daylightSavingsTimeChange();
void springForward_data();
@ -1690,6 +1694,34 @@ void tst_QDateTime::toSecsSinceEpoch()
QDateTime datetime = dt( dateTimeStr );
qint64 asSecsSinceEpoch = datetime.toSecsSinceEpoch();
QCOMPARE(asSecsSinceEpoch, datetime.toMSecsSinceEpoch() / 1000);
QDateTime datetime2 = QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch);
QCOMPARE(datetime, datetime2);
}
#if QT_DEPRECATED_SINCE(5, 8)
void tst_QDateTime::toTime_t_data()
{
QTest::addColumn<QString>("dateTimeStr");
QTest::addColumn<bool>("res");
QTest::newRow( "data1" ) << str( 1800, 1, 1, 12, 0, 0 ) << false;
QTest::newRow( "data2" ) << str( 1969, 1, 1, 12, 0, 0 ) << false;
QTest::newRow( "data3" ) << str( 2002, 1, 1, 12, 0, 0 ) << true;
QTest::newRow( "data4" ) << str( 2002, 6, 1, 12, 0, 0 ) << true;
QTest::newRow( "data5" ) << QString("INVALID") << false;
QTest::newRow( "data6" ) << str( 2038, 1, 1, 12, 0, 0 ) << true;
QTest::newRow( "data7" ) << str( 2063, 4, 5, 12, 0, 0 ) << true; // the day of First Contact
QTest::newRow( "data8" ) << str( 2107, 1, 1, 12, 0, 0 )
<< bool( sizeof(uint) > 32 && sizeof(time_t) > 32 );
}
void tst_QDateTime::toTime_t()
{
QFETCH( QString, dateTimeStr );
QDateTime datetime = dt( dateTimeStr );
uint asTime_t = datetime.toTime_t();
QFETCH( bool, res );
if (res) {
@ -1697,15 +1729,13 @@ void tst_QDateTime::toSecsSinceEpoch()
} else {
QVERIFY( asTime_t == (uint)-1 );
}
QCOMPARE(asSecsSinceEpoch, datetime.toMSecsSinceEpoch() / 1000);
if ( asTime_t != (uint) -1 ) {
QDateTime datetime2 = QDateTime::fromTime_t( asTime_t );
QCOMPARE(datetime, datetime2);
}
QDateTime datetime2 = QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch);
QCOMPARE(datetime, datetime2);
}
#endif
void tst_QDateTime::daylightSavingsTimeChange_data()
{

View File

@ -172,7 +172,7 @@ ResultSet testRun(ContainerType &container, Algorithm &algorithm, int millisecs)
{
TestInt::lessThanRefCount = 0;
int count = 0;
QTime t;
QElapsedTimer t;
t.start();
while(t.elapsed() < millisecs) {
++count;

View File

@ -3454,7 +3454,7 @@ void tst_QDateTimeEdit::timeSpec()
}
QVERIFY(edit.minimumTime() != min.time());
QVERIFY(edit.minimumDateTime().timeSpec() != min.timeSpec());
QCOMPARE(edit.minimumDateTime().toTime_t(), min.toTime_t());
QCOMPARE(edit.minimumDateTime().toSecsSinceEpoch(), min.toSecsSinceEpoch());
} else {
QSKIP("Not tested in the GMT timezone");
}