Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdatetime_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QDATETIME_P_H
7#define QDATETIME_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtCore/private/qglobal_p.h>
21#include "qplatformdefs.h"
22#include "QtCore/qatomic.h"
23#include "QtCore/qdatetime.h"
24#include "QtCore/qshareddata.h"
25#include "QtCore/qtimezone.h"
26
27#if QT_CONFIG(timezone)
28#include "qtimezone.h"
29#endif
30
31#include <chrono>
32
33QT_BEGIN_NAMESPACE
34
35class QDateTimePrivate : public QSharedData
36{
37public:
38 // forward the declarations from QDateTime (this makes them public)
39 typedef QDateTime::ShortData QDateTimeShortData;
40 typedef QDateTime::Data QDateTimeData;
41
42 // Never change or delete this enum, it is required for backwards compatible
43 // serialization of QDateTime before 5.2, so is essentially public API
44 enum Spec {
45 LocalUnknown = -1,
46 LocalStandard = 0,
47 LocalDST = 1,
48 UTC = 2,
49 OffsetFromUTC = 3,
50 TimeZone = 4
51 };
52
53 // Daylight Time Status
54 enum DaylightStatus {
55 UnknownDaylightTime = -1,
56 StandardTime = 0,
57 DaylightTime = 1
58 };
59
60 // Status of date/time
61 enum StatusFlag {
62 ShortData = 0x01,
63
64 ValidDate = 0x02,
65 ValidTime = 0x04,
66 ValidDateTime = 0x08,
67
68 TimeSpecMask = 0x30,
69
70 SetToStandardTime = 0x40,
71 SetToDaylightTime = 0x80,
72 ValidityMask = ValidDate | ValidTime | ValidDateTime,
73 DaylightMask = SetToStandardTime | SetToDaylightTime,
74 };
75 Q_DECLARE_FLAGS(StatusFlags, StatusFlag)
76
77
78 enum TransitionOption {
79 // Handling of a spring-forward (or other gap):
80 GapUseBefore = 2,
81 GapUseAfter = 4,
82 // Handling of a fall-back (or other repeated period):
83 FoldUseBefore = 0x20,
84 FoldUseAfter = 0x40,
85 // Quirk for negative DST:
86 FlipForReverseDst = 0x400,
87
88 GapMask = GapUseBefore | GapUseAfter,
89 FoldMask = FoldUseBefore | FoldUseAfter,
90 };
91 Q_DECLARE_FLAGS(TransitionOptions, TransitionOption)
92
93 enum {
94 TimeSpecShift = 4,
95 };
96
97 struct ZoneState {
98 qint64 when; // ms after zone/local 1970 start; may be revised from the input time.
99 int offset = 0; // seconds
100 DaylightStatus dst = UnknownDaylightTime;
101 // Other fields are set, if possible, even when valid is false due to spring-forward.
102 bool valid = false;
103
104 ZoneState(qint64 local) : when(local) {}
105 ZoneState(qint64 w, int o, DaylightStatus d, bool v = true)
106 : when(w), offset(o), dst(d), valid(v) {}
107 };
108
109 static QDateTime::Data create(QDate toDate, QTime toTime, const QTimeZone &timeZone,
110 QDateTime::TransitionResolution resolve);
111#if QT_CONFIG(timezone)
112 static ZoneState zoneStateAtMillis(const QTimeZone &zone, qint64 millis,
113 TransitionOptions resolve);
114#endif // timezone
115
116 static ZoneState expressUtcAsLocal(qint64 utcMSecs);
117
118 static ZoneState localStateAtMillis(qint64 millis, TransitionOptions resolve);
119 static QString localNameAtMillis(qint64 millis, DaylightStatus dst); // empty if unknown
120
121 StatusFlags m_status = StatusFlag(Qt::LocalTime << TimeSpecShift);
122 qint64 m_msecs = 0;
123 int m_offsetFromUtc = 0;
124 QTimeZone m_timeZone;
125};
126
127Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimePrivate::StatusFlags)
128Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimePrivate::TransitionOptions)
129
130namespace QtPrivate {
132using namespace std::chrono;
133inline
135inline
137inline
138constexpr qint64 SECS_PER_DAY = SECS_PER_HOUR * 24; // std::chrono::days is C++20
139
140inline
142
143inline
145inline
146constexpr qint64 MSECS_PER_MIN = SECS_PER_MIN * MSECS_PER_SEC;
147inline
148constexpr qint64 MSECS_PER_HOUR = SECS_PER_HOUR * MSECS_PER_SEC;
149inline
150constexpr qint64 MSECS_PER_DAY = SECS_PER_DAY * MSECS_PER_SEC;
151
152inline
153constexpr qint64 JULIAN_DAY_FOR_EPOCH = 2440588; // result of QDate(1970, 1, 1).toJulianDay()
154
155inline
156constexpr qint64 JulianDayMax = Q_INT64_C( 784354017364);
157inline
158constexpr qint64 JulianDayMin = Q_INT64_C(-784350574879);
159}
160}
161
162QT_END_NAMESPACE
163
164#endif // QDATETIME_P_H
constexpr qint64 JulianDayMax
constexpr qint64 SECS_PER_HOUR
constexpr qint64 MSECS_PER_MIN
constexpr qint64 SECS_PER_MIN
constexpr qint64 SECS_PER_DAY
constexpr qint64 JulianDayMin
constexpr qint64 MSECS_PER_DAY
constexpr qint64 JULIAN_DAY_FOR_EPOCH
constexpr qint64 MINS_PER_HOUR
constexpr qint64 MSECS_PER_SEC
constexpr qint64 MSECS_PER_HOUR