QDateTime - Add api for isDaylightTime()

Add new method to return if the current time is Daylight Time.

[ChangeLog][QtCore][QDateTime] Added new method isDaylightTime() to
return if the datetime is in Daylight Time or not.

Change-Id: Icb93fd5dd0b2f7d83d2d4643eeb12922c1137e3e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
John Layt 2013-09-02 16:35:25 +02:00 committed by The Qt Project
parent fafc2daf94
commit 8d6ee59948
3 changed files with 50 additions and 0 deletions

View File

@ -2992,6 +2992,32 @@ QString QDateTime::timeZoneAbbreviation() const
return QString(); return QString();
} }
/*!
\since 5.2
Returns if this datetime falls in Daylight Savings Time.
If the Qt::TimeSpec is not Qt::LocalTime then will always
return false.
\sa timeSpec()
*/
bool QDateTime::isDaylightTime() const
{
switch (d->m_spec) {
case Qt::UTC:
case Qt::OffsetFromUTC:
return false;
case Qt::LocalTime: {
QDateTimePrivate::DaylightStatus status;
localMSecsToEpochMSecs(d->m_msecs, 0, 0, &status, 0);
return (status == QDateTimePrivate::DaylightTime);
}
}
return false;
}
/*! /*!
Sets the date part of this datetime to \a date. Sets the date part of this datetime to \a date.
If no time is set, it is set to midnight. If no time is set, it is set to midnight.

View File

@ -223,6 +223,7 @@ public:
Qt::TimeSpec timeSpec() const; Qt::TimeSpec timeSpec() const;
int offsetFromUtc() const; int offsetFromUtc() const;
QString timeZoneAbbreviation() const; QString timeZoneAbbreviation() const;
bool isDaylightTime() const;
qint64 toMSecsSinceEpoch() const; qint64 toMSecsSinceEpoch() const;
// ### Qt 6: use quint64 instead of uint // ### Qt 6: use quint64 instead of uint

View File

@ -143,6 +143,7 @@ private slots:
void roundtripGermanLocale() const; void roundtripGermanLocale() const;
void utcOffsetLessThan() const; void utcOffsetLessThan() const;
void isDaylightTime() const;
void daylightTransitions() const; void daylightTransitions() const;
private: private:
@ -2316,6 +2317,28 @@ void tst_QDateTime::utcOffsetLessThan() const
QVERIFY(!(dt2 < dt1)); QVERIFY(!(dt2 < dt1));
} }
void tst_QDateTime::isDaylightTime() const
{
QDateTime utc1(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC);
QVERIFY(!utc1.isDaylightTime());
QDateTime utc2(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC);
QVERIFY(!utc2.isDaylightTime());
QDateTime offset1(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 1 * 60 * 60);
QVERIFY(!offset1.isDaylightTime());
QDateTime offset2(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::OffsetFromUTC, 1 * 60 * 60);
QVERIFY(!offset2.isDaylightTime());
if (europeanTimeZone) {
QDateTime cet1(QDate(2012, 1, 1), QTime(0, 0, 0));
QVERIFY(!cet1.isDaylightTime());
QDateTime cet2(QDate(2012, 6, 1), QTime(0, 0, 0));
QVERIFY(cet2.isDaylightTime());
} else {
QSKIP("You must test using Central European (CET/CEST) time zone, e.g. TZ=Europe/Oslo");
}
}
void tst_QDateTime::daylightTransitions() const void tst_QDateTime::daylightTransitions() const
{ {
if (europeanTimeZone) { if (europeanTimeZone) {