summaryrefslogtreecommitdiffstats
path: root/src/corelib/time/qdatetime_p.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/time/qdatetime_p.h')
-rw-r--r--src/corelib/time/qdatetime_p.h123
1 files changed, 68 insertions, 55 deletions
diff --git a/src/corelib/time/qdatetime_p.h b/src/corelib/time/qdatetime_p.h
index c500b023c4..02b047dd73 100644
--- a/src/corelib/time/qdatetime_p.h
+++ b/src/corelib/time/qdatetime_p.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2022 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QDATETIME_P_H
#define QDATETIME_P_H
@@ -57,11 +21,14 @@
#include "QtCore/qatomic.h"
#include "QtCore/qdatetime.h"
#include "QtCore/qshareddata.h"
+#include "QtCore/qtimezone.h"
#if QT_CONFIG(timezone)
#include "qtimezone.h"
#endif
+#include <chrono>
+
QT_BEGIN_NAMESPACE
class QDateTimePrivate : public QSharedData
@@ -100,39 +67,85 @@ public:
TimeSpecMask = 0x30,
SetToStandardTime = 0x40,
- SetToDaylightTime = 0x80
+ SetToDaylightTime = 0x80,
+ ValidityMask = ValidDate | ValidTime | ValidDateTime,
+ DaylightMask = SetToStandardTime | SetToDaylightTime,
};
Q_DECLARE_FLAGS(StatusFlags, StatusFlag)
+
+ enum TransitionOption {
+ // Handling of a spring-forward (or other gap):
+ GapUseBefore = 2,
+ GapUseAfter = 4,
+ // Handling of a fall-back (or other repeated period):
+ FoldUseBefore = 0x20,
+ FoldUseAfter = 0x40,
+ // Quirk for negative DST:
+ FlipForReverseDst = 0x400,
+
+ GapMask = GapUseBefore | GapUseAfter,
+ FoldMask = FoldUseBefore | FoldUseAfter,
+ };
+ Q_DECLARE_FLAGS(TransitionOptions, TransitionOption)
+
enum {
TimeSpecShift = 4,
- ValidityMask = ValidDate | ValidTime | ValidDateTime,
- DaylightMask = SetToStandardTime | SetToDaylightTime
};
- static QDateTime::Data create(QDate toDate, QTime toTime, Qt::TimeSpec toSpec,
- int offsetSeconds);
+ struct ZoneState {
+ qint64 when; // ms after zone/local 1970 start; may be revised from the input time.
+ int offset = 0; // seconds
+ DaylightStatus dst = UnknownDaylightTime;
+ // Other fields are set, if possible, even when valid is false due to spring-forward.
+ bool valid = false;
+
+ ZoneState(qint64 local) : when(local) {}
+ ZoneState(qint64 w, int o, DaylightStatus d, bool v = true)
+ : when(w), offset(o), dst(d), valid(v) {}
+ };
+ static QDateTime::Data create(QDate toDate, QTime toTime, const QTimeZone &timeZone,
+ QDateTime::TransitionResolution resolve);
#if QT_CONFIG(timezone)
- static QDateTime::Data create(QDate toDate, QTime toTime, const QTimeZone & timeZone);
+ static ZoneState zoneStateAtMillis(const QTimeZone &zone, qint64 millis,
+ TransitionOptions resolve);
#endif // timezone
+ static ZoneState expressUtcAsLocal(qint64 utcMSecs);
+
+ static ZoneState localStateAtMillis(qint64 millis, TransitionOptions resolve);
+ static QString localNameAtMillis(qint64 millis, DaylightStatus dst); // empty if unknown
+
StatusFlags m_status = StatusFlag(Qt::LocalTime << TimeSpecShift);
qint64 m_msecs = 0;
int m_offsetFromUtc = 0;
-#if QT_CONFIG(timezone)
QTimeZone m_timeZone;
-#endif // timezone
+};
-#if QT_CONFIG(timezone)
- static qint64 zoneMSecsToEpochMSecs(qint64 msecs, const QTimeZone &zone,
- DaylightStatus hint = UnknownDaylightTime,
- QDate *localDate = nullptr, QTime *localTime = nullptr);
+Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimePrivate::StatusFlags)
+Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimePrivate::TransitionOptions)
- // Inlined for its one caller in qdatetime.cpp
- inline void setUtcOffsetByTZ(qint64 atMSecsSinceEpoch);
-#endif // timezone
-};
+namespace QtPrivate {
+namespace DateTimeConstants {
+using namespace std::chrono;
+constexpr qint64 SECS_PER_MIN = minutes::period::num;
+constexpr qint64 SECS_PER_HOUR = hours::period::num;
+constexpr qint64 SECS_PER_DAY = SECS_PER_HOUR * 24; // std::chrono::days is C++20
+
+constexpr qint64 MINS_PER_HOUR = std::ratio_divide<hours::period, minutes::period>::num;
+
+constexpr qint64 MSECS_PER_SEC = milliseconds::period::den;
+constexpr qint64 MSECS_PER_MIN = SECS_PER_MIN * MSECS_PER_SEC;
+constexpr qint64 MSECS_PER_HOUR = SECS_PER_HOUR * MSECS_PER_SEC;
+constexpr qint64 MSECS_PER_DAY = SECS_PER_DAY * MSECS_PER_SEC;
+
+constexpr qint64 JULIAN_DAY_FOR_EPOCH = 2440588; // result of QDate(1970, 1, 1).toJulianDay()
+
+constexpr qint64 JulianDayMax = Q_INT64_C( 784354017364);
+constexpr qint64 JulianDayMin = Q_INT64_C(-784350574879);
+}
+}
QT_END_NAMESPACE