Skip to content

Commit 8c8d6ff

Browse files
committed
Deprecate QDateTime methods using plain Qt::TimeSpec
As foreshadowed when QDateTime adapted to route all QTimeSpec use through QTimeZone, this commit deprecates the old API in favor of the newly more capable QTimeZone-based API. Fixes: QTBUG-108199 Change-Id: I9a3f9f94d4a5d8cc229db72b3e4731a9e318a076 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
1 parent 074a4e1 commit 8c8d6ff

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎src/corelib/time/qdatetime.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ int QDate::weekNumber(int *yearNumber) const
755755
return (thursday.dayOfYear() + 6) / 7;
756756
}
757757

758+
#if QT_DEPRECATED_SINCE(6, 9)
759+
// Only called by deprecated methods (so bootstrap builds warn unused without this #if).
758760
static QTimeZone asTimeZone(Qt::TimeSpec spec, int offset, const char *warner)
759761
{
760762
if (warner) {
@@ -783,6 +785,7 @@ static QTimeZone asTimeZone(Qt::TimeSpec spec, int offset, const char *warner)
783785
? QTimeZone::fromSecondsAheadOfUtc(offset)
784786
: QTimeZone(QTimeZone::LocalTime);
785787
}
788+
#endif // Helper for 6.9 deprecation
786789

787790
static bool inDateTimeRange(qint64 jd, bool start)
788791
{
@@ -893,9 +896,11 @@ QDateTime QDate::startOfDay() const
893896
return startOfDay(QTimeZone::LocalTime);
894897
}
895898

899+
#if QT_DEPRECATED_SINCE(6, 9)
896900
/*!
897901
\overload
898902
\since 5.14
903+
\deprecated [6.9] Use \c{startOfDay(const QTimeZone &)} instead.
899904
900905
Returns the start-moment of the day.
901906
@@ -926,6 +931,7 @@ QDateTime QDate::startOfDay(Qt::TimeSpec spec, int offsetSeconds) const
926931
// If spec was Qt::TimeZone, zone's is Qt::LocalTime.
927932
return zone.timeSpec() == spec ? startOfDay(zone) : QDateTime();
928933
}
934+
#endif // 6.9 deprecation
929935

930936
static QDateTime toLatest(QDate day, const QTimeZone &zone)
931937
{
@@ -1022,9 +1028,11 @@ QDateTime QDate::endOfDay() const
10221028
return endOfDay(QTimeZone::LocalTime);
10231029
}
10241030

1031+
#if QT_DEPRECATED_SINCE(6, 9)
10251032
/*!
10261033
\overload
10271034
\since 5.14
1035+
\deprecated [6.9] Use \c{endOfDay(const QTimeZone &) instead.
10281036
10291037
Returns the end-moment of the day.
10301038
@@ -1055,6 +1063,7 @@ QDateTime QDate::endOfDay(Qt::TimeSpec spec, int offsetSeconds) const
10551063
// If spec was Qt::TimeZone, zone's is Qt::LocalTime.
10561064
return endOfDay(zone);
10571065
}
1066+
#endif // 6.9 deprecation
10581067

10591068
#if QT_CONFIG(datestring) // depends on, so implies, textdate
10601069

@@ -3410,7 +3419,10 @@ QDateTime::QDateTime() noexcept
34103419
static_assert(sizeof(ShortData) >= sizeof(void*), "oops, Data::swap() is broken!");
34113420
}
34123421

3422+
#if QT_DEPRECATED_SINCE(6, 9)
34133423
/*!
3424+
\deprecated [6.9] Use \c{QDateTime(date, time)} or \c{QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offsetSeconds))}.
3425+
34143426
Constructs a datetime with the given \a date and \a time, using the time
34153427
representation implied by \a spec and \a offsetSeconds seconds.
34163428
@@ -3432,6 +3444,7 @@ QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSecond
34323444
: d(QDateTimePrivate::create(date, time, asTimeZone(spec, offsetSeconds, "QDateTime")))
34333445
{
34343446
}
3447+
#endif // 6.9 deprecation
34353448

34363449
/*!
34373450
\since 5.2
@@ -3772,7 +3785,10 @@ void QDateTime::setTime(QTime time)
37723785
checkValidDateTime(d);
37733786
}
37743787

3788+
#if QT_DEPRECATED_SINCE(6, 9)
37753789
/*!
3790+
\deprecated [6.9] Use setTimeZone() instead
3791+
37763792
Sets the time specification used in this datetime to \a spec.
37773793
The datetime may refer to a different point in time.
37783794
@@ -3795,6 +3811,7 @@ void QDateTime::setTimeSpec(Qt::TimeSpec spec)
37953811

37963812
/*!
37973813
\since 5.2
3814+
\deprecated [6.9] Use setTimeZone(QTimeZone::fromSecondsAheadOfUtc(offsetSeconds)) instead
37983815
37993816
Sets the timeSpec() to Qt::OffsetFromUTC and the offset to \a offsetSeconds.
38003817
The datetime may refer to a different point in time.
@@ -3812,6 +3829,7 @@ void QDateTime::setOffsetFromUtc(int offsetSeconds)
38123829
{
38133830
reviseTimeZone(d, QTimeZone::fromSecondsAheadOfUtc(offsetSeconds));
38143831
}
3832+
#endif // 6.9 deprecations
38153833

38163834
/*!
38173835
\since 5.2
@@ -4480,7 +4498,10 @@ qint64 QDateTime::msecsTo(const QDateTime &other) const
44804498
\sa addMSecs
44814499
*/
44824500

4501+
#if QT_DEPRECATED_SINCE(6, 9)
44834502
/*!
4503+
\deprecated [6.9] Use \l toTimeZone() instead.
4504+
44844505
Returns a copy of this datetime converted to the given time \a spec.
44854506
44864507
The result represents the same moment in time as, and is equal to, this datetime.
@@ -4501,6 +4522,7 @@ QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
45014522
{
45024523
return toTimeZone(asTimeZone(spec, 0, "toTimeSpec"));
45034524
}
4525+
#endif // 6.9 deprecation
45044526

45054527
/*!
45064528
\since 5.2
@@ -4919,8 +4941,11 @@ qint64 QDateTime::currentSecsSinceEpoch() noexcept
49194941
#error "What system is this?"
49204942
#endif
49214943

4944+
#if QT_DEPRECATED_SINCE(6, 9)
49224945
/*!
4946+
\since 5.2
49234947
\overload
4948+
\deprecated [6.9] Pass a \l QTimeZone instead, or omit \a spec and \a offsetSeconds.
49244949
49254950
Returns a datetime representing a moment the given number \a msecs of
49264951
milliseconds after the start, in UTC, of the year 1970, described as
@@ -4949,6 +4974,7 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int of
49494974
/*!
49504975
\since 5.8
49514976
\overload
4977+
\deprecated [6.9] Pass a \l QTimeZone instead, or omit \a spec and \a offsetSeconds.
49524978
49534979
Returns a datetime representing a moment the given number \a secs of seconds
49544980
after the start, in UTC, of the year 1970, described as specified by \a spec
@@ -4973,6 +4999,7 @@ QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offs
49734999
return fromSecsSinceEpoch(secs,
49745000
asTimeZone(spec, offsetSeconds, "QDateTime::fromSecsSinceEpoch"));
49755001
}
5002+
#endif // 6.9 deprecations
49765003

49775004
/*!
49785005
\since 5.2

‎src/corelib/time/qdatetime.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ class Q_CORE_EXPORT QDate
103103
int daysInMonth(QCalendar cal) const;
104104
int daysInYear(QCalendar cal) const;
105105

106+
#if QT_DEPRECATED_SINCE(6, 9)
107+
QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
106108
QDateTime startOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const;
109+
QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
107110
QDateTime endOfDay(Qt::TimeSpec spec, int offsetSeconds = 0) const;
111+
#endif
112+
108113
QDateTime startOfDay(const QTimeZone &zone) const;
109114
QDateTime endOfDay(const QTimeZone &zone) const;
110115
QDateTime startOfDay() const;
@@ -307,7 +312,10 @@ class Q_CORE_EXPORT QDateTime
307312

308313
public:
309314
QDateTime() noexcept;
315+
#if QT_DEPRECATED_SINCE(6, 9)
316+
QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead")
310317
QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0);
318+
#endif
311319
QDateTime(QDate date, QTime time, const QTimeZone &timeZone);
312320
QDateTime(QDate date, QTime time);
313321
QDateTime(const QDateTime &other) noexcept;
@@ -338,8 +346,12 @@ class Q_CORE_EXPORT QDateTime
338346

339347
void setDate(QDate date);
340348
void setTime(QTime time);
349+
#if QT_DEPRECATED_SINCE(6, 9)
350+
QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead")
341351
void setTimeSpec(Qt::TimeSpec spec);
352+
QT_DEPRECATED_VERSION_X_6_9("Use setTimeZone() instead")
342353
void setOffsetFromUtc(int offsetSeconds);
354+
#endif
343355
void setTimeZone(const QTimeZone &toZone);
344356
void setMSecsSinceEpoch(qint64 msecs);
345357
void setSecsSinceEpoch(qint64 secs);
@@ -360,7 +372,10 @@ class Q_CORE_EXPORT QDateTime
360372
return addMSecs(msecs.count());
361373
}
362374

375+
#if QT_DEPRECATED_SINCE(6, 9)
376+
QT_DEPRECATED_VERSION_X_6_9("Use toTimeZone instead")
363377
QDateTime toTimeSpec(Qt::TimeSpec spec) const;
378+
#endif
364379
QDateTime toLocalTime() const;
365380
QDateTime toUTC() const;
366381
QDateTime toOffsetFromUtc(int offsetSeconds) const;
@@ -387,8 +402,12 @@ class Q_CORE_EXPORT QDateTime
387402
{ return fromString(string, qToStringViewIgnoringNull(format), cal); }
388403
#endif
389404

405+
#if QT_DEPRECATED_SINCE(6, 9)
406+
QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset")
390407
static QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetFromUtc = 0);
408+
QT_DEPRECATED_VERSION_X_6_9("Pass QTimeZone instead of time-spec, offset")
391409
static QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetFromUtc = 0);
410+
#endif
392411

393412
static QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone);
394413
static QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone);

‎tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ private Q_SLOTS:
3232
void isValid();
3333
void date();
3434
void time();
35+
#if QT_DEPRECATED_SINCE(6, 9)
3536
void timeSpec();
37+
#endif
3638
void toSecsSinceEpoch_data();
3739
void toSecsSinceEpoch();
3840
void daylightSavingsTimeChange_data();
@@ -44,8 +46,10 @@ private Q_SLOTS:
4446
void setTime();
4547
void setTimeZone_data();
4648
void setTimeZone();
49+
#if QT_DEPRECATED_SINCE(6, 9)
4750
void setTimeSpec_data();
4851
void setTimeSpec();
52+
#endif
4953
void setSecsSinceEpoch();
5054
void setMSecsSinceEpoch_data();
5155
void setMSecsSinceEpoch();
@@ -74,13 +78,15 @@ private Q_SLOTS:
7478
void addSecs();
7579
void addMSecs_data();
7680
void addMSecs();
81+
#if QT_DEPRECATED_SINCE(6, 9)
7782
void toTimeSpec_data();
7883
void toTimeSpec();
7984
void toLocalTime_data() { toTimeSpec_data(); }
8085
void toLocalTime();
8186
void toUTC_data() { toTimeSpec_data(); }
8287
void toUTC();
8388
void toUTC_extra();
89+
#endif
8490
void daysTo();
8591
void secsTo_data();
8692
void secsTo();
@@ -105,8 +111,10 @@ private Q_SLOTS:
105111
#endif
106112

107113
void offsetFromUtc();
114+
#if QT_DEPRECATED_SINCE(6, 9)
108115
void setOffsetFromUtc();
109116
void toOffsetFromUtc();
117+
#endif
110118

111119
void zoneAtTime_data();
112120
void zoneAtTime();
@@ -428,6 +436,9 @@ void tst_QDateTime::time()
428436
QCOMPARE(dt4.time(), QTime(0, 45, 57));
429437
}
430438

439+
#if QT_DEPRECATED_SINCE(6, 9)
440+
QT_WARNING_PUSH
441+
QT_WARNING_DISABLE_DEPRECATED
431442
void tst_QDateTime::timeSpec()
432443
{
433444
QDateTime dt1(QDate(2004, 1, 24), QTime(23, 45, 57));
@@ -450,6 +461,8 @@ void tst_QDateTime::timeSpec()
450461
QDateTime dt4 = QDateTime::currentDateTime();
451462
QCOMPARE(dt4.timeSpec(), Qt::LocalTime);
452463
}
464+
QT_WARNING_POP
465+
#endif
453466

454467
void tst_QDateTime::setDate()
455468
{
@@ -577,6 +590,7 @@ void tst_QDateTime::setTimeZone()
577590
QCOMPARE(dateTime.timeRepresentation(), zone);
578591
}
579592

593+
#if QT_DEPRECATED_SINCE(6, 9)
580594
void tst_QDateTime::setTimeSpec_data()
581595
{
582596
QTest::addColumn<QDateTime>("dateTime");
@@ -590,6 +604,8 @@ void tst_QDateTime::setTimeSpec_data()
590604
<< QDateTime(QDate(2004, 3, 25), QTime(0, 45, 57), UTC) << Qt::OffsetFromUTC;
591605
}
592606

607+
QT_WARNING_PUSH
608+
QT_WARNING_DISABLE_DEPRECATED
593609
void tst_QDateTime::setTimeSpec()
594610
{
595611
QFETCH(QDateTime, dateTime);
@@ -607,6 +623,8 @@ void tst_QDateTime::setTimeSpec()
607623
else
608624
QCOMPARE(dateTime.timeSpec(), newTimeSpec);
609625
}
626+
QT_WARNING_POP
627+
#endif
610628

611629
void tst_QDateTime::setSecsSinceEpoch()
612630
{
@@ -1597,6 +1615,7 @@ void tst_QDateTime::addMSecs()
15971615
verify(dt.addDuration(std::chrono::milliseconds(nsecs * 1000)));
15981616
}
15991617

1618+
#if QT_DEPRECATED_SINCE(6, 9)
16001619
void tst_QDateTime::toTimeSpec_data()
16011620
{
16021621
if (!zoneIsCET)
@@ -1665,6 +1684,8 @@ void tst_QDateTime::toTimeSpec_data()
16651684
<< QDateTime(QDate(4000, 6, 30), localDaylightTime.addMSecs(1));
16661685
}
16671686

1687+
QT_WARNING_PUSH
1688+
QT_WARNING_DISABLE_DEPRECATED
16681689
void tst_QDateTime::toTimeSpec()
16691690
{
16701691
QFETCH(QDateTime, fromUtc);
@@ -1752,6 +1773,8 @@ void tst_QDateTime::toUTC_extra()
17521773
QString t = dt.toUTC().toString("zzz");
17531774
QCOMPARE(s, t);
17541775
}
1776+
QT_WARNING_POP
1777+
#endif // 6.9 deprecation
17551778

17561779
void tst_QDateTime::daysTo()
17571780
{
@@ -3232,6 +3255,9 @@ void tst_QDateTime::offsetFromUtc()
32323255
#endif
32333256
}
32343257

3258+
#if QT_DEPRECATED_SINCE(6, 9)
3259+
QT_WARNING_PUSH
3260+
QT_WARNING_DISABLE_DEPRECATED
32353261
void tst_QDateTime::setOffsetFromUtc()
32363262
{
32373263
/* Basic tests. */
@@ -3327,6 +3353,8 @@ void tst_QDateTime::toOffsetFromUtc()
33273353
QCOMPARE(dt2.date(), QDate(2013, 1, 1));
33283354
QCOMPARE(dt2.time(), QTime(0, 0));
33293355
}
3356+
QT_WARNING_POP
3357+
#endif // 6.9 deprecation
33303358

33313359
void tst_QDateTime::zoneAtTime_data()
33323360
{

0 commit comments

Comments
 (0)