5#include "qplatformdefs.h"
7#include "private/qcalendarmath_p.h"
8#if QT_CONFIG(datetimeparser)
9#include "private/qdatetimeparser_p.h"
11#include "private/qgregoriancalendar_p.h"
12#include "private/qnumeric_p.h"
13#include "private/qtenvironmentvariables_p.h"
14#if QT_CONFIG(timezone)
15#include "private/qtimezoneprivate_p.h"
20# include <qt_windows.h>
30using namespace QtPrivate::DateTimeConstants;
33
34
35
36
37
38constexpr int tmYearFromQYear(
int year) {
return year - (year < 0 ? 1899 : 1900); }
39constexpr int qYearFromTmYear(
int year) {
return year + (year < -1899 ? 1899 : 1900); }
41constexpr inline qint64 tmSecsWithinDay(
const struct tm &when)
43 return (when.tm_hour * MINS_PER_HOUR + when.tm_min) * SECS_PER_MIN + when.tm_sec;
47
48
49
50
51
52
56 static constexpr time_t maybeError = -1;
57 inline bool meansEnd1969();
58 bool changed(
const struct tm &prior)
const;
62 time_t utcSecs = maybeError;
65 MkTimeResult() { local.tm_isdst = -1; }
68 explicit MkTimeResult(
const struct tm &prior)
69 : local(prior), utcSecs(qMkTime(&local)),
70 good(utcSecs != maybeError || meansEnd1969()),
71 adjusted(changed(prior))
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95inline bool MkTimeResult::meansEnd1969()
100 if (local.tm_year < 69 || local.tm_year > 70
101# ifdef HAVE_TM_GMTOFF
105 || (tmSecsWithinDay(local) - local.tm_gmtoff + 1) % SECS_PER_DAY
107 || (local.tm_year == 69
108 ? local.tm_mon < 11 || local.tm_mday < 31
109 : local.tm_mon > 0 || local.tm_mday > 1)) {
112 struct tm copy = local;
114 if (qMkTime(©) != -2)
125bool MkTimeResult::changed(
const struct tm &prior)
const
132 return !(prior.tm_year == local.tm_year && prior.tm_mon == local.tm_mon
133 && prior.tm_mday == local.tm_mday && prior.tm_hour == local.tm_hour
134 && prior.tm_min == local.tm_min && prior.tm_sec == local.tm_sec
135 && (prior.tm_isdst == -1
136 ? local.tm_isdst >= 0 : prior.tm_isdst == local.tm_isdst));
139struct tm timeToTm(qint64 localDay,
int secs)
141 Q_ASSERT(0 <= secs && secs < SECS_PER_DAY);
142 const auto ymd = QGregorianCalendar::partsFromJulian(JULIAN_DAY_FOR_EPOCH + localDay);
143 struct tm local = {};
144 local.tm_year = tmYearFromQYear(ymd.year);
145 local.tm_mon = ymd.month - 1;
146 local.tm_mday = ymd.day;
147 local.tm_hour = secs / 3600;
148 local.tm_min = (secs % 3600) / 60;
149 local.tm_sec = (secs % 60);
157struct tm matchYearMonth(
struct tm when,
const struct tm &base)
164 while (when.tm_year > base.tm_year) {
168 while (when.tm_year < base.tm_year) {
172 Q_ASSERT(when.tm_year == base.tm_year);
173 while (when.tm_mon > base.tm_mon) {
174 const auto yearMon = QRoundingDown::qDivMod<12>(when.tm_mon);
175 int year = yearMon.quotient;
177 int month = yearMon.remainder;
182 year += when.tm_year;
183 when.tm_mday += QGregorianCalendar::monthLength(month, qYearFromTmYear(year));
186 while (when.tm_mon < base.tm_mon) {
187 const auto yearMon = QRoundingDown::qDivMod<12>(when.tm_mon);
189 when.tm_mday -= QGregorianCalendar::monthLength(
190 yearMon.remainder + 1, qYearFromTmYear(yearMon.quotient + when.tm_year));
193 Q_ASSERT(when.tm_mon == base.tm_mon);
198struct tm adjacentDay(
struct tm when,
int dayStep)
201 Q_ASSERT(dayStep * dayStep == 1);
202 when.tm_mday += dayStep;
207 if (when.tm_mday <= 0) {
211 int daysInMonth = when.tm_mon
212 ? QGregorianCalendar::monthLength(when.tm_mon, qYearFromTmYear(when.tm_year))
213 : QGregorianCalendar::monthLength(12, qYearFromTmYear(when.tm_year - 1));
214 when.tm_mday += daysInMonth;
215 if (--when.tm_mon < 0) {
219 Q_ASSERT(when.tm_mday >= 1);
221 }
else if (when.tm_mday > 28) {
223 int daysInMonth = QGregorianCalendar::monthLength(
224 when.tm_mon + 1, qYearFromTmYear(when.tm_year));
225 if (when.tm_mday > daysInMonth) {
226 when.tm_mday -= daysInMonth;
227 if (++when.tm_mon > 11) {
231 Q_ASSERT(when.tm_mday <= QGregorianCalendar::monthLength(
232 when.tm_mon + 1, qYearFromTmYear(when.tm_year)));
239qint64 secondsBetween(
const struct tm &start,
const struct tm &stop)
244 struct tm from = matchYearMonth(start, stop);
245 qint64 diff = stop.tm_mday - from.tm_mday;
246 diff = diff * 24 + stop.tm_hour - from.tm_hour;
247 diff = diff * 60 + stop.tm_min - from.tm_min;
248 return diff * 60 + stop.tm_sec - from.tm_sec;
252MkTimeResult hopAcrossGap(
const MkTimeResult &outside,
const struct tm &base)
256 const qint64 shift = secondsBetween(outside.local, base);
260 if (qLocalTime(outside.utcSecs + shift, &across)) {
261 const qint64 wider = secondsBetween(outside.local, across);
264 if (shift > 0 ? wider > shift : wider < shift) {
265 MkTimeResult result(across);
266 if (result.good && !result.adjusted)
276MkTimeResult resolveRejected(
struct tm base, MkTimeResult result,
277 QDateTimePrivate::TransitionOptions resolve)
285 if (!resolve.testAnyFlags(QDateTimePrivate::GapMask))
288 constexpr time_t twoDaysInSeconds = 2 * 24 * 60 * 60;
290 MkTimeResult early(adjacentDay(base, -1));
291 MkTimeResult later(adjacentDay(base, +1));
292 if (!early.good || !later.good)
296 Q_ASSERT(twoDaysInSeconds + early.utcSecs > later.utcSecs);
297 result.adjusted =
true;
300 QDateTimePrivate::TransitionOption beforeLater = QDateTimePrivate::GapUseBefore;
301 if (resolve.testFlag(QDateTimePrivate::FlipForReverseDst)) {
303 if (early.local.tm_isdst == 1 && !later.local.tm_isdst)
304 beforeLater = QDateTimePrivate::GapUseAfter;
306 if (resolve.testFlag(beforeLater))
307 result.utcSecs = later.utcSecs - secondsBetween(base, later.local);
309 result.utcSecs = early.utcSecs + secondsBetween(early.local, base);
311 if (!qLocalTime(result.utcSecs, &result.local))
318bool preferAlternative(QDateTimePrivate::TransitionOptions resolve,
320 int gotDst,
int altDst,
327 QDateTimePrivate::TransitionOption preferLater = inGap ? QDateTimePrivate::GapUseAfter
328 : QDateTimePrivate::FoldUseAfter;
329 if (resolve.testFlag(QDateTimePrivate::FlipForReverseDst)) {
332 if ((altDst ^ gotDst) == 1) {
336 const bool isReversed = (altDst == 1) != (altIsLater == inGap);
339 if (altIsLater == inGap)
340 isReversed = altDst != 1;
342 isReversed = altDst == 1;
345 preferLater = inGap ? QDateTimePrivate::GapUseBefore
346 : QDateTimePrivate::FoldUseBefore;
350 return resolve.testFlag(preferLater) == altIsLater;
354
355
356
357
358
359
360
361
362
363MkTimeResult resolveLocalTime(qint64 local, QDateTimePrivate::TransitionOptions resolve)
365 const auto localDaySecs = QRoundingDown::qDivMod<SECS_PER_DAY>(local);
366 struct tm base = timeToTm(localDaySecs.quotient, localDaySecs.remainder);
369 MkTimeResult result(base);
378 return resolveRejected(base, result, resolve);
379 }
else if (result.local.tm_isdst < 0) {
383 }
else if (result.adjusted) {
385 if (!resolve.testAnyFlags(QDateTimePrivate::GapMask)) {
391 const MkTimeResult flipped = hopAcrossGap(result, base);
394 if (preferAlternative(resolve, result.local.tm_isdst, flipped.local.tm_isdst,
395 flipped.utcSecs > result.utcSecs,
true)) {
397 if (!flipped.good || flipped.adjusted)
402 result.adjusted =
true;
404 }
else if (resolve.testFlag(QDateTimePrivate::FlipForReverseDst)
407 && resolve.testFlag(result.local.tm_isdst ? QDateTimePrivate::FoldUseBefore
408 : QDateTimePrivate::FoldUseAfter)) {
418 struct tm copy = base;
419 copy.tm_isdst = !result.local.tm_isdst;
420 const MkTimeResult flipped(copy);
421 if (flipped.good && !flipped.adjusted) {
423 if (!resolve.testAnyFlags(QDateTimePrivate::FoldMask)) {
429 if (preferAlternative(resolve, result.local.tm_isdst, flipped.local.tm_isdst,
430 flipped.utcSecs > result.utcSecs,
false)) {
438inline std::optional<qint64> tmToJd(
const struct tm &date)
440 return QGregorianCalendar::julianFromParts(qYearFromTmYear(date.tm_year),
441 date.tm_mon + 1, date.tm_mday);
444#define IC(N) std::integral_constant<qint64, N>()
447inline bool daysAndSecondsOverflow(qint64 julianDay, qint64 daySeconds, qint64 *epochSeconds)
449 return qMulOverflow(julianDay - JULIAN_DAY_FOR_EPOCH,
IC(SECS_PER_DAY), epochSeconds)
450 || qAddOverflow(*epochSeconds, daySeconds, epochSeconds);
454inline bool secondsAndMillisOverflow(qint64 epochSeconds, qint64 millis, qint64 *epochMillis)
456 return qMulOverflow(epochSeconds,
IC(MSECS_PER_SEC), epochMillis)
457 || qAddOverflow(*epochMillis, millis, epochMillis);
466#ifndef QT_BOOTSTRAPPED
472 TIME_ZONE_INFORMATION tzInfo;
473 if (GetTimeZoneInformation(&tzInfo) != TIME_ZONE_ID_INVALID) {
474 int bias = tzInfo.Bias;
476 if (tzInfo.StandardDate.wMonth)
477 bias += tzInfo.StandardBias;
479 return -bias * SECS_PER_MIN;
483 const time_t curr = time(
nullptr);
486
487
488
489
490
491
492
493
494
495
496
497# if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
499 if (gmtime_r(&curr, &t)) {
500 time_t mkt = qMkTime(&t);
501 int offset =
int(curr - mkt);
502 Q_ASSERT(std::abs(offset) <= SECS_PER_DAY);
506 if (
struct tm *tp = gmtime(&curr)) {
508 time_t mkt = qMkTime(&t);
509 int offset =
int(curr - mkt);
510 Q_ASSERT(std::abs(offset) <= SECS_PER_DAY);
516 qDebug(
"Unable to determine current standard time offset from UTC");
525 return QDateTimePrivate::expressUtcAsLocal(atMSecsSinceEpoch).offset;
533 const auto epoch = QRoundingDown::qDivMod<MSECS_PER_SEC>(utcMillis);
534 const time_t epochSeconds = epoch.quotient;
535 const int msec = epoch.remainder;
536 Q_ASSERT(msec >= 0 && msec < MSECS_PER_SEC);
537 if (qint64(epochSeconds) * MSECS_PER_SEC + msec != utcMillis)
541 if (!qLocalTime(epochSeconds, &local))
544 auto jd = tmToJd(local);
548 const qint64 daySeconds = tmSecsWithinDay(local);
549 Q_ASSERT(0 <= daySeconds && daySeconds < SECS_PER_DAY);
550 qint64 localSeconds, localMillis;
551 if (Q_UNLIKELY(daysAndSecondsOverflow(*jd, daySeconds, &localSeconds)
552 || secondsAndMillisOverflow(localSeconds, qint64(msec), &localMillis))) {
556 = local.tm_isdst ? QDateTimePrivate::DaylightTime : QDateTimePrivate::StandardTime;
557 return { localMillis,
int(localSeconds - epochSeconds), dst };
562 auto use = resolveLocalTime(QRoundingDown::qDiv<MSECS_PER_SEC>(local), resolve);
566 if (use.local.tm_zone)
567 return QString::fromLocal8Bit(use.local.tm_zone);
569 return qTzName(use.local.tm_isdst > 0 ? 1 : 0);
575 qint64 localSecs = local / MSECS_PER_SEC;
576 auto use = resolveLocalTime(localSecs, resolve);
580 qint64 millis = local - localSecs * MSECS_PER_SEC;
582 Q_ASSERT(local < 0 ? (millis <= 0 && millis > -MSECS_PER_SEC)
583 : (millis >= 0 && millis < MSECS_PER_SEC));
585 QDateTimePrivate::DaylightStatus dst =
586 use.local.tm_isdst > 0 ? QDateTimePrivate::DaylightTime : QDateTimePrivate::StandardTime;
589 const int offset = use.local.tm_gmtoff;
590 localSecs = offset + use.utcSecs;
593 int offset = localSecs - use.utcSecs;
594 auto jd = tmToJd(use.local);
596 return {local, offset, dst,
false};
598 qint64 daySecs = tmSecsWithinDay(use.local);
599 Q_ASSERT(0 <= daySecs && daySecs < SECS_PER_DAY);
600 if (daySecs > 0 && *jd < JULIAN_DAY_FOR_EPOCH) {
602 daySecs -= SECS_PER_DAY;
604 if (Q_UNLIKELY(daysAndSecondsOverflow(*jd, daySecs, &localSecs)))
605 return {local, offset, dst,
false};
608 offset = localSecs - use.utcSecs;
616 if (secondsAndMillisOverflow(localSecs, millis, &revised))
617 return {local, offset, QDateTimePrivate::UnknownDaylightTime,
false};
618 return {revised, offset, dst,
true};
622
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
652 Q_ASSERT(QGregorianCalendar::julianFromParts(1970, 1, 1) == JULIAN_DAY_FOR_EPOCH);
654 constexpr qint64 TIME_T_MAX = std::numeric_limits<time_t>::max();
655 using Bounds = std::numeric_limits<qint64>;
656 constexpr bool isNarrow = Bounds::max() / MSECS_PER_SEC > TIME_T_MAX;
657 if constexpr (isNarrow) {
658 const qint64 msecsMax = quint64(TIME_T_MAX) * MSECS_PER_SEC - 1 + MSECS_PER_SEC;
659 const qint64 msecsMin = -1 - msecsMax;
661 struct tm local = {};
662 local.tm_year = tmYearFromQYear(1901);
666 return {qMkTime(&local) == -1 ? 0 : msecsMin, msecsMax,
false,
false};
668 const struct {
int year; qint64 millis; } starts[] = {
669 {
int(QDateTime::YearRange::First) + 1, Bounds::min() },
671 { 1, -Q_INT64_C(62135596800000) },
673 { 1582, -Q_INT64_C(12244089600000) },
675 { 1752, -Q_INT64_C(6879427200000) },
677 { 1900, -Q_INT64_C(2208988800000) },
679 {
int(QDateTime::YearRange::Last) - 1, Bounds::max() },
681 { 3000, Q_INT64_C(32535215999999) },
686 quint64(std::numeric_limits<qint32>::max()) * MSECS_PER_SEC - 1 + MSECS_PER_SEC;
689 for (
const auto c : ends) {
690 struct tm local = {};
691 local.tm_year = tmYearFromQYear(c.year);
695 local.tm_min = local.tm_sec = 59;
697 if (qMkTime(&local) != -1) {
703 bool startMin =
true;
704 for (
const auto c : starts) {
706 local.tm_year = tmYearFromQYear(c.year);
710 if (qMkTime(&local) != -1)
711 return {c.millis, stop, startMin, stopMax};
714 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)