6#include "qplatformdefs.h"
8#include "private/qcalendarmath_p.h"
9#if QT_CONFIG(datetimeparser)
10#include "private/qdatetimeparser_p.h"
12#include "private/qgregoriancalendar_p.h"
13#include "private/qnumeric_p.h"
14#include "private/qtenvironmentvariables_p.h"
15#if QT_CONFIG(timezone)
16#include "private/qtimezoneprivate_p.h"
21# include <qt_windows.h>
31using namespace QtPrivate::DateTimeConstants;
34
35
36
37
38
39constexpr int tmYearFromQYear(
int year) {
return year - (year < 0 ? 1899 : 1900); }
40constexpr int qYearFromTmYear(
int year) {
return year + (year < -1899 ? 1899 : 1900); }
42constexpr inline qint64 tmSecsWithinDay(
const struct tm &when)
44 return (when.tm_hour * MINS_PER_HOUR + when.tm_min) * SECS_PER_MIN + when.tm_sec;
48
49
50
51
52
53
57 static constexpr time_t maybeError = -1;
58 inline bool meansEnd1969();
59 bool changed(
const struct tm &prior)
const;
63 time_t utcSecs = maybeError;
66 MkTimeResult() { local.tm_isdst = -1; }
69 explicit MkTimeResult(
const struct tm &prior)
70 : local(prior), utcSecs(qMkTime(&local)),
71 good(utcSecs != maybeError || meansEnd1969()),
72 adjusted(changed(prior))
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96inline bool MkTimeResult::meansEnd1969()
101 if (local.tm_year < 69 || local.tm_year > 70
102# ifdef HAVE_TM_GMTOFF
106 || (tmSecsWithinDay(local) - local.tm_gmtoff + 1) % SECS_PER_DAY
108 || (local.tm_year == 69
109 ? local.tm_mon < 11 || local.tm_mday < 31
110 : local.tm_mon > 0 || local.tm_mday > 1)) {
113 struct tm copy = local;
115 if (qMkTime(©) != -2)
126bool MkTimeResult::changed(
const struct tm &prior)
const
133 return !(prior.tm_year == local.tm_year && prior.tm_mon == local.tm_mon
134 && prior.tm_mday == local.tm_mday && prior.tm_hour == local.tm_hour
135 && prior.tm_min == local.tm_min && prior.tm_sec == local.tm_sec
136 && (prior.tm_isdst == -1
137 ? local.tm_isdst >= 0 : prior.tm_isdst == local.tm_isdst));
140struct tm timeToTm(qint64 localDay,
int secs)
142 Q_ASSERT(0 <= secs && secs < SECS_PER_DAY);
143 const auto ymd = QGregorianCalendar::partsFromJulian(JULIAN_DAY_FOR_EPOCH + localDay);
144 struct tm local = {};
145 local.tm_year = tmYearFromQYear(ymd.year);
146 local.tm_mon = ymd.month - 1;
147 local.tm_mday = ymd.day;
148 local.tm_hour = secs / 3600;
149 local.tm_min = (secs % 3600) / 60;
150 local.tm_sec = (secs % 60);
158struct tm matchYearMonth(
struct tm when,
const struct tm &base)
165 while (when.tm_year > base.tm_year) {
169 while (when.tm_year < base.tm_year) {
173 Q_ASSERT(when.tm_year == base.tm_year);
174 while (when.tm_mon > base.tm_mon) {
175 const auto yearMon = QRoundingDown::qDivMod<12>(when.tm_mon);
176 int year = yearMon.quotient;
178 int month = yearMon.remainder;
183 year += when.tm_year;
184 when.tm_mday += QGregorianCalendar::monthLength(month, qYearFromTmYear(year));
187 while (when.tm_mon < base.tm_mon) {
188 const auto yearMon = QRoundingDown::qDivMod<12>(when.tm_mon);
190 when.tm_mday -= QGregorianCalendar::monthLength(
191 yearMon.remainder + 1, qYearFromTmYear(yearMon.quotient + when.tm_year));
194 Q_ASSERT(when.tm_mon == base.tm_mon);
199struct tm adjacentDay(
struct tm when,
int dayStep)
202 Q_ASSERT(dayStep * dayStep == 1);
203 when.tm_mday += dayStep;
208 if (when.tm_mday <= 0) {
212 int daysInMonth = when.tm_mon
213 ? QGregorianCalendar::monthLength(when.tm_mon, qYearFromTmYear(when.tm_year))
214 : QGregorianCalendar::monthLength(12, qYearFromTmYear(when.tm_year - 1));
215 when.tm_mday += daysInMonth;
216 if (--when.tm_mon < 0) {
220 Q_ASSERT(when.tm_mday >= 1);
222 }
else if (when.tm_mday > 28) {
224 int daysInMonth = QGregorianCalendar::monthLength(
225 when.tm_mon + 1, qYearFromTmYear(when.tm_year));
226 if (when.tm_mday > daysInMonth) {
227 when.tm_mday -= daysInMonth;
228 if (++when.tm_mon > 11) {
232 Q_ASSERT(when.tm_mday <= QGregorianCalendar::monthLength(
233 when.tm_mon + 1, qYearFromTmYear(when.tm_year)));
240qint64 secondsBetween(
const struct tm &start,
const struct tm &stop)
245 struct tm from = matchYearMonth(start, stop);
246 qint64 diff = stop.tm_mday - from.tm_mday;
247 diff = diff * 24 + stop.tm_hour - from.tm_hour;
248 diff = diff * 60 + stop.tm_min - from.tm_min;
249 return diff * 60 + stop.tm_sec - from.tm_sec;
253MkTimeResult hopAcrossGap(
const MkTimeResult &outside,
const struct tm &base)
257 const qint64 shift = secondsBetween(outside.local, base);
261 if (qLocalTime(outside.utcSecs + shift, &across)) {
262 const qint64 wider = secondsBetween(outside.local, across);
265 if (shift > 0 ? wider > shift : wider < shift) {
266 MkTimeResult result(across);
267 if (result.good && !result.adjusted)
277MkTimeResult resolveRejected(
struct tm base, MkTimeResult result,
278 QDateTimePrivate::TransitionOptions resolve)
286 if (!resolve.testAnyFlags(QDateTimePrivate::GapMask))
289 constexpr time_t twoDaysInSeconds = 2 * 24 * 60 * 60;
291 MkTimeResult early(adjacentDay(base, -1));
292 MkTimeResult later(adjacentDay(base, +1));
293 if (!early.good || !later.good)
297 Q_ASSERT(twoDaysInSeconds + early.utcSecs > later.utcSecs);
298 result.adjusted =
true;
301 QDateTimePrivate::TransitionOption beforeLater = QDateTimePrivate::GapUseBefore;
302 if (resolve.testFlag(QDateTimePrivate::FlipForReverseDst)) {
304 if (early.local.tm_isdst == 1 && !later.local.tm_isdst)
305 beforeLater = QDateTimePrivate::GapUseAfter;
307 if (resolve.testFlag(beforeLater))
308 result.utcSecs = later.utcSecs - secondsBetween(base, later.local);
310 result.utcSecs = early.utcSecs + secondsBetween(early.local, base);
312 if (!qLocalTime(result.utcSecs, &result.local))
319bool preferAlternative(QDateTimePrivate::TransitionOptions resolve,
321 int gotDst,
int altDst,
328 QDateTimePrivate::TransitionOption preferLater = inGap ? QDateTimePrivate::GapUseAfter
329 : QDateTimePrivate::FoldUseAfter;
330 if (resolve.testFlag(QDateTimePrivate::FlipForReverseDst)) {
333 if ((altDst ^ gotDst) == 1) {
337 const bool isReversed = (altDst == 1) != (altIsLater == inGap);
340 if (altIsLater == inGap)
341 isReversed = altDst != 1;
343 isReversed = altDst == 1;
346 preferLater = inGap ? QDateTimePrivate::GapUseBefore
347 : QDateTimePrivate::FoldUseBefore;
351 return resolve.testFlag(preferLater) == altIsLater;
355
356
357
358
359
360
361
362
363
364MkTimeResult resolveLocalTime(qint64 local, QDateTimePrivate::TransitionOptions resolve)
366 const auto localDaySecs = QRoundingDown::qDivMod<SECS_PER_DAY>(local);
367 struct tm base = timeToTm(localDaySecs.quotient, localDaySecs.remainder);
370 MkTimeResult result(base);
379 return resolveRejected(base, result, resolve);
380 }
else if (result.local.tm_isdst < 0) {
384 }
else if (result.adjusted) {
386 if (!resolve.testAnyFlags(QDateTimePrivate::GapMask)) {
392 const MkTimeResult flipped = hopAcrossGap(result, base);
395 if (preferAlternative(resolve, result.local.tm_isdst, flipped.local.tm_isdst,
396 flipped.utcSecs > result.utcSecs,
true)) {
398 if (!flipped.good || flipped.adjusted)
403 result.adjusted =
true;
405 }
else if (resolve.testFlag(QDateTimePrivate::FlipForReverseDst)
408 && resolve.testFlag(result.local.tm_isdst ? QDateTimePrivate::FoldUseBefore
409 : QDateTimePrivate::FoldUseAfter)) {
419 struct tm copy = base;
420 copy.tm_isdst = !result.local.tm_isdst;
421 const MkTimeResult flipped(copy);
422 if (flipped.good && !flipped.adjusted) {
424 if (!resolve.testAnyFlags(QDateTimePrivate::FoldMask)) {
430 if (preferAlternative(resolve, result.local.tm_isdst, flipped.local.tm_isdst,
431 flipped.utcSecs > result.utcSecs,
false)) {
439inline std::optional<qint64> tmToJd(
const struct tm &date)
441 return QGregorianCalendar::julianFromParts(qYearFromTmYear(date.tm_year),
442 date.tm_mon + 1, date.tm_mday);
445#define IC(N) std::integral_constant<qint64, N>()
448inline bool daysAndSecondsOverflow(qint64 julianDay, qint64 daySeconds, qint64 *epochSeconds)
450 return qMulOverflow(julianDay - JULIAN_DAY_FOR_EPOCH,
IC(SECS_PER_DAY), epochSeconds)
451 || qAddOverflow(*epochSeconds, daySeconds, epochSeconds);
455inline bool secondsAndMillisOverflow(qint64 epochSeconds, qint64 millis, qint64 *epochMillis)
457 return qMulOverflow(epochSeconds,
IC(MSECS_PER_SEC), epochMillis)
458 || qAddOverflow(*epochMillis, millis, epochMillis);
467#ifndef QT_BOOTSTRAPPED
473 TIME_ZONE_INFORMATION tzInfo;
474 if (GetTimeZoneInformation(&tzInfo) != TIME_ZONE_ID_INVALID) {
475 int bias = tzInfo.Bias;
477 if (tzInfo.StandardDate.wMonth)
478 bias += tzInfo.StandardBias;
480 return -bias * SECS_PER_MIN;
484 const time_t curr = time(
nullptr);
487
488
489
490
491
492
493
494
495
496
497
498# if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
500 if (gmtime_r(&curr, &t)) {
501 time_t mkt = qMkTime(&t);
502 int offset =
int(curr - mkt);
503 Q_ASSERT(std::abs(offset) <= SECS_PER_DAY);
507 if (
struct tm *tp = gmtime(&curr)) {
509 time_t mkt = qMkTime(&t);
510 int offset =
int(curr - mkt);
511 Q_ASSERT(std::abs(offset) <= SECS_PER_DAY);
517 qDebug(
"Unable to determine current standard time offset from UTC");
526 return QDateTimePrivate::expressUtcAsLocal(atMSecsSinceEpoch).offset;
534 const auto epoch = QRoundingDown::qDivMod<MSECS_PER_SEC>(utcMillis);
535 const time_t epochSeconds = epoch.quotient;
536 const int msec = epoch.remainder;
537 Q_ASSERT(msec >= 0 && msec < MSECS_PER_SEC);
538 if (qint64(epochSeconds) * MSECS_PER_SEC + msec != utcMillis)
542 if (!qLocalTime(epochSeconds, &local))
545 auto jd = tmToJd(local);
549 const qint64 daySeconds = tmSecsWithinDay(local);
550 Q_ASSERT(0 <= daySeconds && daySeconds < SECS_PER_DAY);
551 qint64 localSeconds, localMillis;
552 if (Q_UNLIKELY(daysAndSecondsOverflow(*jd, daySeconds, &localSeconds)
553 || secondsAndMillisOverflow(localSeconds, qint64(msec), &localMillis))) {
557 = local.tm_isdst ? QDateTimePrivate::DaylightTime : QDateTimePrivate::StandardTime;
558 return { localMillis,
int(localSeconds - epochSeconds), dst };
563 auto use = resolveLocalTime(QRoundingDown::qDiv<MSECS_PER_SEC>(local), resolve);
567 if (use.local.tm_zone)
568 return QString::fromLocal8Bit(use.local.tm_zone);
570 return qTzName(use.local.tm_isdst > 0 ? 1 : 0);
576 qint64 localSecs = local / MSECS_PER_SEC;
577 auto use = resolveLocalTime(localSecs, resolve);
581 qint64 millis = local - localSecs * MSECS_PER_SEC;
583 Q_ASSERT(local < 0 ? (millis <= 0 && millis > -MSECS_PER_SEC)
584 : (millis >= 0 && millis < MSECS_PER_SEC));
586 QDateTimePrivate::DaylightStatus dst =
587 use.local.tm_isdst > 0 ? QDateTimePrivate::DaylightTime : QDateTimePrivate::StandardTime;
590 const int offset = use.local.tm_gmtoff;
591 localSecs = offset + use.utcSecs;
594 int offset = localSecs - use.utcSecs;
595 auto jd = tmToJd(use.local);
597 return {local, offset, dst,
false};
599 qint64 daySecs = tmSecsWithinDay(use.local);
600 Q_ASSERT(0 <= daySecs && daySecs < SECS_PER_DAY);
601 if (daySecs > 0 && *jd < JULIAN_DAY_FOR_EPOCH) {
603 daySecs -= SECS_PER_DAY;
605 if (Q_UNLIKELY(daysAndSecondsOverflow(*jd, daySecs, &localSecs)))
606 return {local, offset, dst,
false};
609 offset = localSecs - use.utcSecs;
617 if (secondsAndMillisOverflow(localSecs, millis, &revised))
618 return {local, offset, QDateTimePrivate::UnknownDaylightTime,
false};
619 return {revised, offset, dst,
true};
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
653 Q_ASSERT(QGregorianCalendar::julianFromParts(1970, 1, 1) == JULIAN_DAY_FOR_EPOCH);
655 constexpr qint64 TIME_T_MAX = std::numeric_limits<time_t>::max();
656 using Bounds = std::numeric_limits<qint64>;
657 constexpr bool isNarrow = Bounds::max() / MSECS_PER_SEC > TIME_T_MAX;
658 if constexpr (isNarrow) {
659 const qint64 msecsMax = quint64(TIME_T_MAX) * MSECS_PER_SEC - 1 + MSECS_PER_SEC;
660 const qint64 msecsMin = -1 - msecsMax;
662 struct tm local = {};
663 local.tm_year = tmYearFromQYear(1901);
667 return {qMkTime(&local) == -1 ? 0 : msecsMin, msecsMax,
false,
false};
669 const struct {
int year; qint64 millis; } starts[] = {
670 {
int(QDateTime::YearRange::First) + 1, Bounds::min() },
672 { 1, -Q_INT64_C(62135596800000) },
674 { 1582, -Q_INT64_C(12244089600000) },
676 { 1752, -Q_INT64_C(6879427200000) },
678 { 1900, -Q_INT64_C(2208988800000) },
680 {
int(QDateTime::YearRange::Last) - 1, Bounds::max() },
682 { 3000, Q_INT64_C(32535215999999) },
687 quint64(std::numeric_limits<qint32>::max()) * MSECS_PER_SEC - 1 + MSECS_PER_SEC;
690 for (
const auto c : ends) {
691 struct tm local = {};
692 local.tm_year = tmYearFromQYear(c.year);
696 local.tm_min = local.tm_sec = 59;
698 if (qMkTime(&local) != -1) {
704 bool startMin =
true;
705 for (
const auto c : starts) {
707 local.tm_year = tmYearFromQYear(c.year);
711 if (qMkTime(&local) != -1)
712 return {c.millis, stop, startMin, stopMax};
715 return {0, stop,
false, stopMax};
QString localTimeAbbbreviationAt(qint64 local, QDateTimePrivate::TransitionOptions resolve)
SystemMillisRange computeSystemMillisRange()
int getUtcOffset(qint64 atMSecsSinceEpoch)
int getCurrentStandardUtcOffset()
QDateTimePrivate::ZoneState utcToLocal(qint64 utcMillis)
QDateTimePrivate::ZoneState mapLocalTime(qint64 local, QDateTimePrivate::TransitionOptions resolve)