From 7b07c3ff781c9fcdaf7a60aabed9b7aa57e25006 Mon Sep 17 00:00:00 2001 From: John Layt Date: Mon, 19 Aug 2013 16:50:54 +0200 Subject: QDateTime - Fix Daylight Transition for "missing" hour When Daylight Time transtion goes from Standard Time to Daylight Time there is a "missing" hour, i.e. at 2am CET the clock goes forward to 3am. Currently QDateTime ignores this gap and considers the time to be valid and able to be manipulated. This change respects the transition, so any time set in the missing hour is considered invalid, and any date maths returns valid results. The validity in the current time zone needs to be checked every time isValid() is called in case the system time zone has changed since the last time it was checked. This is done by calling mktime to check the returned result matches the expected result. This could be very inefficient, but the returned offset value is cached each time so mktime is not required to be called again within each method call, effectively meaning mktime is called the same number of times by each method. Note that this means any new methods added must be careful to ensure either isValid() or refreshLocalTime() is called first by any method needing to use the UTC value. [ChangeLog][QtCore][QDateTime] The Standard Time to Daylight Time transition for Qt::LocalTime is now handled correctly. Any date set in the "missing" hour is now considered invalid. All date math results that fall into the missing hour will be automatically adjusted to a valid time in the following hour. Change-Id: Ia652c8511b45df15f4917acf12403ec01a7f08e7 Reviewed-by: Thiago Macieira --- src/corelib/tools/qdatetime_p.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/corelib/tools/qdatetime_p.h') diff --git a/src/corelib/tools/qdatetime_p.h b/src/corelib/tools/qdatetime_p.h index 124bd98a31..7fd36bc4e0 100644 --- a/src/corelib/tools/qdatetime_p.h +++ b/src/corelib/tools/qdatetime_p.h @@ -86,6 +86,7 @@ public: NullTime = 0x02, ValidDate = 0x04, ValidTime = 0x08, + ValidDateTime = 0x10 }; Q_DECLARE_FLAGS(StatusFlags, StatusFlag) @@ -114,11 +115,20 @@ public: void setDateTime(const QDate &date, const QTime &time); void getDateTime(QDate *date, QTime *time) const; + // Returns msecs since epoch, assumes offset value is current + inline qint64 toMSecsSinceEpoch() const { return (m_msecs - (m_offsetFromUtc * 1000)); } + + void checkValidDateTime(); + void refreshDateTime(); + // Get/set date and time status inline bool isNullDate() const { return (m_status & NullDate) == NullDate; } inline bool isNullTime() const { return (m_status & NullTime) == NullTime; } inline bool isValidDate() const { return (m_status & ValidDate) == ValidDate; } inline bool isValidTime() const { return (m_status & ValidTime) == ValidTime; } + inline bool isValidDateTime() const { return (m_status & ValidDateTime) == ValidDateTime; } + inline void setValidDateTime() { m_status = m_status | ValidDateTime; } + inline void clearValidDateTime() { m_status = m_status & ~ValidDateTime; } static inline qint64 minJd() { return QDate::minJd(); } static inline qint64 maxJd() { return QDate::maxJd(); } -- cgit v1.2.3