2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2017-08-17 16:34:09 +00:00
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
2016-01-21 02:33:50 +00:00
|
|
|
** Copyright (C) 2016 Intel Corporation.
|
2016-01-15 07:08:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** 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.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qplatformdefs.h"
|
|
|
|
#include "private/qdatetime_p.h"
|
2017-09-11 16:14:51 +00:00
|
|
|
#if QT_CONFIG(datetimeparser)
|
2013-08-08 21:17:57 +00:00
|
|
|
#include "private/qdatetimeparser_p.h"
|
2017-09-11 16:14:51 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "qdatastream.h"
|
|
|
|
#include "qset.h"
|
|
|
|
#include "qlocale.h"
|
|
|
|
#include "qdatetime.h"
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
#include "qtimezoneprivate_p.h"
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "qregexp.h"
|
|
|
|
#include "qdebug.h"
|
2011-05-24 07:14:43 +00:00
|
|
|
#ifndef Q_OS_WIN
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2012-06-18 13:53:30 +00:00
|
|
|
#include <cmath>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <time.h>
|
2012-02-01 09:19:07 +00:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
# include <qt_windows.h>
|
2013-09-13 10:51:58 +00:00
|
|
|
# ifdef Q_OS_WINRT
|
|
|
|
# include "qfunctions_winrt.h"
|
|
|
|
# endif
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
|
2011-10-21 07:55:40 +00:00
|
|
|
#if defined(Q_OS_MAC)
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <private/qcore_mac_p.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2013-08-14 20:54:55 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
Date/Time Constants
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
enum {
|
|
|
|
SECS_PER_DAY = 86400,
|
|
|
|
MSECS_PER_DAY = 86400000,
|
|
|
|
SECS_PER_HOUR = 3600,
|
|
|
|
MSECS_PER_HOUR = 3600000,
|
|
|
|
SECS_PER_MIN = 60,
|
|
|
|
MSECS_PER_MIN = 60000,
|
2013-08-12 19:21:42 +00:00
|
|
|
TIME_T_MAX = 2145916799, // int maximum 2037-12-31T23:59:59 UTC
|
2012-01-09 20:50:00 +00:00
|
|
|
JULIAN_DAY_FOR_EPOCH = 2440588 // result of julianDayFromDate(1970, 1, 1)
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 20:54:55 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
QDate static helper functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
static inline QDate fixedDate(int y, int m, int d)
|
|
|
|
{
|
|
|
|
QDate result(y, m, 1);
|
|
|
|
result.setDate(y, m, qMin(d, result.daysInMonth()));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-01-06 13:40:50 +00:00
|
|
|
/*
|
2016-01-26 13:52:30 +00:00
|
|
|
Division, rounding down (rather than towards zero).
|
2016-01-06 13:40:50 +00:00
|
|
|
|
2016-01-26 13:52:30 +00:00
|
|
|
From C++11 onwards, integer division is defined to round towards zero, so we
|
|
|
|
can rely on that when implementing this. This is only used with denominator b
|
|
|
|
> 0, so we only have to treat negative numerator, a, specially.
|
2016-01-06 13:40:50 +00:00
|
|
|
*/
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
static inline qint64 floordiv(qint64 a, int b)
|
|
|
|
{
|
2016-01-26 13:52:30 +00:00
|
|
|
return (a - (a < 0 ? b - 1 : 0)) / b;
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int floordiv(int a, int b)
|
|
|
|
{
|
2016-01-26 13:52:30 +00:00
|
|
|
return (a - (a < 0 ? b - 1 : 0)) / b;
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
}
|
2012-01-09 20:50:00 +00:00
|
|
|
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
static inline qint64 julianDayFromDate(int year, int month, int day)
|
|
|
|
{
|
|
|
|
// Adjust for no year 0
|
2012-01-09 20:50:00 +00:00
|
|
|
if (year < 0)
|
|
|
|
++year;
|
|
|
|
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
/*
|
|
|
|
* Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php
|
|
|
|
* This formula is correct for all julian days, when using mathematical integer
|
|
|
|
* division (round to negative infinity), not c++11 integer division (round to zero)
|
|
|
|
*/
|
|
|
|
int a = floordiv(14 - month, 12);
|
|
|
|
qint64 y = (qint64)year + 4800 - a;
|
|
|
|
int m = month + 12 * a - 3;
|
|
|
|
return day + floordiv(153 * m + 2, 5) + 365 * y + floordiv(y, 4) - floordiv(y, 100) + floordiv(y, 400) - 32045;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
struct ParsedDate
|
|
|
|
{
|
|
|
|
int year, month, day;
|
|
|
|
};
|
|
|
|
|
2015-01-22 10:47:45 +00:00
|
|
|
// prevent this function from being inlined into all 10 users
|
|
|
|
Q_NEVER_INLINE
|
2015-01-20 22:48:22 +00:00
|
|
|
static ParsedDate getDateFromJulianDay(qint64 julianDay)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
/*
|
|
|
|
* Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php
|
|
|
|
* This formula is correct for all julian days, when using mathematical integer
|
|
|
|
* division (round to negative infinity), not c++11 integer division (round to zero)
|
|
|
|
*/
|
|
|
|
qint64 a = julianDay + 32044;
|
|
|
|
qint64 b = floordiv(4 * a + 3, 146097);
|
|
|
|
int c = a - floordiv(146097 * b, 4);
|
|
|
|
|
|
|
|
int d = floordiv(4 * c + 3, 1461);
|
|
|
|
int e = c - floordiv(1461 * d, 4);
|
|
|
|
int m = floordiv(5 * e + 2, 153);
|
|
|
|
|
|
|
|
int day = e - floordiv(153 * m + 2, 5) + 1;
|
|
|
|
int month = m + 3 - 12 * floordiv(m, 10);
|
|
|
|
int year = 100 * b + d - 4800 + floordiv(m, 10);
|
|
|
|
|
|
|
|
// Adjust for no year 0
|
|
|
|
if (year <= 0)
|
|
|
|
--year ;
|
|
|
|
|
2017-04-19 23:43:33 +00:00
|
|
|
return { year, month, day };
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 20:54:55 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
Date/Time formatting helper functions
|
|
|
|
*****************************************************************************/
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
static const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
|
|
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_CONFIG(textdate)
|
2015-01-20 20:21:13 +00:00
|
|
|
static const char qt_shortMonthNames[][4] = {
|
2011-04-27 10:05:43 +00:00
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
2013-07-13 09:01:34 +00:00
|
|
|
|
2015-01-21 09:38:48 +00:00
|
|
|
static int qt_monthNumberFromShortName(QStringRef shortName)
|
2013-07-13 09:01:34 +00:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < sizeof(qt_shortMonthNames) / sizeof(qt_shortMonthNames[0]); ++i) {
|
2015-01-20 20:32:49 +00:00
|
|
|
if (shortName == QLatin1String(qt_shortMonthNames[i], 3))
|
2013-07-13 09:01:34 +00:00
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-21 10:17:50 +00:00
|
|
|
static int qt_monthNumberFromShortName(const QString &shortName)
|
2015-01-21 09:38:48 +00:00
|
|
|
{ return qt_monthNumberFromShortName(QStringRef(&shortName)); }
|
|
|
|
|
|
|
|
static int fromShortMonthName(const QStringRef &monthName)
|
|
|
|
{
|
|
|
|
// Assume that English monthnames are the default
|
|
|
|
int month = qt_monthNumberFromShortName(monthName);
|
|
|
|
if (month != -1)
|
|
|
|
return month;
|
|
|
|
// If English names can't be found, search the localized ones
|
|
|
|
for (int i = 1; i <= 12; ++i) {
|
2017-08-17 16:34:09 +00:00
|
|
|
if (monthName == QLocale::system().monthName(i, QLocale::ShortFormat))
|
2015-01-21 09:38:48 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-19 11:28:13 +00:00
|
|
|
#endif // textdate
|
2013-07-26 21:08:15 +00:00
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2015-01-21 09:17:00 +00:00
|
|
|
struct ParsedRfcDateTime {
|
|
|
|
QDate date;
|
|
|
|
QTime time;
|
|
|
|
int utcOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ParsedRfcDateTime rfcDateImpl(const QString &s)
|
|
|
|
{
|
|
|
|
ParsedRfcDateTime result;
|
2013-08-14 20:54:55 +00:00
|
|
|
|
2017-07-24 00:56:55 +00:00
|
|
|
// Matches "Wdy, dd Mon yyyy HH:mm:ss ±hhmm" (Wdy, being optional)
|
2013-08-14 20:54:55 +00:00
|
|
|
QRegExp rex(QStringLiteral("^(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
|
|
|
|
if (s.indexOf(rex) == 0) {
|
2015-01-20 23:51:34 +00:00
|
|
|
const QStringList cap = rex.capturedTexts();
|
2015-01-21 09:17:00 +00:00
|
|
|
result.date = QDate(cap[3].toInt(), qt_monthNumberFromShortName(cap[2]), cap[1].toInt());
|
|
|
|
if (!cap[4].isEmpty())
|
|
|
|
result.time = QTime(cap[4].toInt(), cap[5].toInt(), cap[6].toInt());
|
|
|
|
const bool positiveOffset = (cap[7] == QLatin1String("+"));
|
|
|
|
const int hourOffset = cap[8].toInt();
|
|
|
|
const int minOffset = cap[9].toInt();
|
|
|
|
result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
|
2013-08-14 20:54:55 +00:00
|
|
|
} else {
|
2017-07-24 00:56:55 +00:00
|
|
|
// Matches "Wdy Mon dd HH:mm:ss yyyy"
|
2013-08-14 20:54:55 +00:00
|
|
|
QRegExp rex(QStringLiteral("^[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?"));
|
|
|
|
if (s.indexOf(rex) == 0) {
|
2015-01-20 23:51:34 +00:00
|
|
|
const QStringList cap = rex.capturedTexts();
|
2015-01-21 09:17:00 +00:00
|
|
|
result.date = QDate(cap[6].toInt(), qt_monthNumberFromShortName(cap[1]), cap[2].toInt());
|
|
|
|
if (!cap[3].isEmpty())
|
|
|
|
result.time = QTime(cap[3].toInt(), cap[4].toInt(), cap[5].toInt());
|
|
|
|
const bool positiveOffset = (cap[7] == QLatin1String("+"));
|
|
|
|
const int hourOffset = cap[8].toInt();
|
|
|
|
const int minOffset = cap[9].toInt();
|
|
|
|
result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60));
|
2013-08-14 20:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 09:17:00 +00:00
|
|
|
return result;
|
2013-08-14 20:54:55 +00:00
|
|
|
}
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2013-02-25 13:36:29 +00:00
|
|
|
|
2015-01-03 17:06:38 +00:00
|
|
|
// Return offset in [+-]HH:mm format
|
2013-02-25 13:36:29 +00:00
|
|
|
static QString toOffsetString(Qt::DateFormat format, int offset)
|
|
|
|
{
|
2015-01-22 11:23:14 +00:00
|
|
|
return QString::asprintf("%c%02d%s%02d",
|
|
|
|
offset >= 0 ? '+' : '-',
|
|
|
|
qAbs(offset) / SECS_PER_HOUR,
|
|
|
|
// Qt::ISODate puts : between the hours and minutes, but Qt:TextDate does not:
|
|
|
|
format == Qt::TextDate ? "" : ":",
|
|
|
|
(qAbs(offset) / 60) % 60);
|
2013-02-25 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2015-01-03 17:06:38 +00:00
|
|
|
// Parse offset in [+-]HH[[:]mm] format
|
2016-04-07 16:09:52 +00:00
|
|
|
static int fromOffsetString(const QStringRef &offsetString, bool *valid) Q_DECL_NOTHROW
|
2013-02-25 13:36:29 +00:00
|
|
|
{
|
|
|
|
*valid = false;
|
|
|
|
|
|
|
|
const int size = offsetString.size();
|
|
|
|
if (size < 2 || size > 6)
|
|
|
|
return 0;
|
|
|
|
|
2014-02-04 11:36:27 +00:00
|
|
|
// sign will be +1 for a positive and -1 for a negative offset
|
|
|
|
int sign;
|
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
// First char must be + or -
|
2014-02-04 11:36:27 +00:00
|
|
|
const QChar signChar = offsetString.at(0);
|
|
|
|
if (signChar == QLatin1Char('+'))
|
|
|
|
sign = 1;
|
|
|
|
else if (signChar == QLatin1Char('-'))
|
|
|
|
sign = -1;
|
|
|
|
else
|
2013-02-25 13:36:29 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Split the hour and minute parts
|
2016-04-07 16:09:52 +00:00
|
|
|
const QStringRef time = offsetString.mid(1);
|
|
|
|
int hhLen = time.indexOf(QLatin1Char(':'));
|
|
|
|
int mmIndex;
|
|
|
|
if (hhLen == -1)
|
|
|
|
mmIndex = hhLen = 2; // [+-]HHmm or [+-]HH format
|
|
|
|
else
|
|
|
|
mmIndex = hhLen + 1;
|
2013-02-25 13:36:29 +00:00
|
|
|
|
2016-04-07 16:09:52 +00:00
|
|
|
const QStringRef hhRef = time.left(hhLen);
|
2013-02-25 13:36:29 +00:00
|
|
|
bool ok = false;
|
2016-04-07 16:09:52 +00:00
|
|
|
const int hour = hhRef.toInt(&ok);
|
2013-02-25 13:36:29 +00:00
|
|
|
if (!ok)
|
|
|
|
return 0;
|
|
|
|
|
2016-04-07 16:09:52 +00:00
|
|
|
const QStringRef mmRef = time.mid(mmIndex);
|
|
|
|
const int minute = mmRef.isEmpty() ? 0 : mmRef.toInt(&ok);
|
2013-02-25 13:36:29 +00:00
|
|
|
if (!ok || minute < 0 || minute > 59)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*valid = true;
|
2014-02-04 11:36:27 +00:00
|
|
|
return sign * ((hour * 60) + minute) * 60;
|
2013-02-25 13:36:29 +00:00
|
|
|
}
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
QDate member functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.5
|
|
|
|
|
|
|
|
\enum QDate::MonthNameType
|
|
|
|
|
|
|
|
This enum describes the types of the string representation used
|
|
|
|
for the month name.
|
|
|
|
|
|
|
|
\value DateFormat This type of name can be used for date-to-string formatting.
|
|
|
|
\value StandaloneFormat This type is used when you need to enumerate months or weekdays.
|
|
|
|
Usually standalone names are represented in singular forms with
|
|
|
|
capitalized first letter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QDate
|
2012-08-23 10:22:38 +00:00
|
|
|
\inmodule QtCore
|
2011-04-27 10:05:43 +00:00
|
|
|
\reentrant
|
|
|
|
\brief The QDate class provides date functions.
|
|
|
|
|
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
A QDate object represents a particular date. This can be expressed as a
|
|
|
|
calendar date, i.e. year, month, and day numbers, in the proleptic Gregorian
|
|
|
|
calendar.
|
2018-09-11 15:47:39 +00:00
|
|
|
|
|
|
|
A QDate object is typically created by giving the year, month, and day
|
2019-05-24 12:20:43 +00:00
|
|
|
numbers explicitly. Note that QDate interprets year numbers less than 100 as
|
|
|
|
presented, i.e., as years 1 through 99, without adding any offset. The
|
|
|
|
static function currentDate() creates a QDate object containing the date
|
|
|
|
read from the system clock. An explicit date can also be set using
|
|
|
|
setDate(). The fromString() function returns a QDate given a string and a
|
|
|
|
date format which is used to interpret the date within the string.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The year(), month(), and day() functions provide access to the
|
|
|
|
year, month, and day numbers. Also, dayOfWeek() and dayOfYear()
|
|
|
|
functions are provided. The same information is provided in
|
|
|
|
textual format by the toString(), shortDayName(), longDayName(),
|
|
|
|
shortMonthName(), and longMonthName() functions.
|
|
|
|
|
|
|
|
QDate provides a full set of operators to compare two QDate
|
|
|
|
objects where smaller means earlier, and larger means later.
|
|
|
|
|
|
|
|
You can increment (or decrement) a date by a given number of days
|
|
|
|
using addDays(). Similarly you can use addMonths() and addYears().
|
|
|
|
The daysTo() function returns the number of days between two
|
|
|
|
dates.
|
|
|
|
|
|
|
|
The daysInMonth() and daysInYear() functions return how many days
|
|
|
|
there are in this date's month and year, respectively. The
|
|
|
|
isLeapYear() function indicates whether a date is in a leap year.
|
|
|
|
|
2018-02-27 13:10:50 +00:00
|
|
|
\section1 Remarks
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\section2 No Year 0
|
|
|
|
|
2018-09-11 15:47:39 +00:00
|
|
|
There is no year 0. Dates in that year are considered invalid. The year -1
|
|
|
|
is the year "1 before Christ" or "1 before current era." The day before 1
|
|
|
|
January 1 CE, QDate(1, 1, 1), is 31 December 1 BCE, QDate(-1, 12, 31).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\section2 Range of Valid Dates
|
|
|
|
|
2012-08-06 13:31:33 +00:00
|
|
|
Dates are stored internally as a Julian Day number, an integer count of
|
2011-08-25 06:54:16 +00:00
|
|
|
every day in a contiguous range, with 24 November 4714 BCE in the Gregorian
|
|
|
|
calendar being Julian Day 0 (1 January 4713 BCE in the Julian calendar).
|
|
|
|
As well as being an efficient and accurate way of storing an absolute date,
|
2019-05-24 12:20:43 +00:00
|
|
|
it is suitable for converting a date into other calendar systems such as
|
2011-08-25 06:54:16 +00:00
|
|
|
Hebrew, Islamic or Chinese. The Julian Day number can be obtained using
|
|
|
|
QDate::toJulianDay() and can be set using QDate::fromJulianDay().
|
|
|
|
|
|
|
|
The range of dates able to be stored by QDate as a Julian Day number is
|
Fix the gregorian date <-> julian day calculations in QDate
The old code is just plain wrong for negative julian days. Replaced
with plain math from The Calendar FAQ [1], which is correct for all
julian days, provided you use mathematical integer division (round to
negative infinity) rather than c++11 integer division (round to zero).
[1] http://www.tondering.dk/claus/cal/julperiod.php
While the conversion code works for up to around JD +/- (2^63/4), we
only use an int for the year in the API, so this patch limits minJd()
and maxJd() to 1 Jan (2^31) BC and 31 Dec (2^31-1) AD, respectively.
Note that while the new conversion code looks like it would be more
expensive than the old, gcc will in fact be able to optimize it to be
slightly faster (probably because x86 hardware implements round to
negative infinity, and so GCC manages to optimize floordiv to a single
instruction, compared to the three instuctions needed for operator/).
In the following test application, run with a release mode Qt and
redirecting stderr to /dev/null, I measured an improvement from
6.81s +/- 0.08s to 6.26s +/- 0.16s user time over five runs on an
otherwise idle x86_64 system.
int main(int, char *[])
{
int year, month, day;
qint64 jd;
for (qint64 i = Q_INT64_C(-1048576) ; i < Q_INT64_C(1048576); ++i) {
QDate::fromJulianDay(i).getDate(&year, &month, &day);
jd = QDate(year, month, day).toJulianDay();
qDebug() << jd << year << month << day;
}
}
Change-Id: Ifd0dd01f0027f260401f7f9b4f1201d2b7a3b087
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
2012-10-08 04:46:28 +00:00
|
|
|
for technical reasons limited to between -784350574879 and 784354017364,
|
|
|
|
which means from before 2 billion BCE to after 2 billion CE.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa QTime, QDateTime, QDateEdit, QDateTimeEdit, QCalendarWidget
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QDate::QDate()
|
|
|
|
|
|
|
|
Constructs a null date. Null dates are invalid.
|
|
|
|
|
|
|
|
\sa isNull(), isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a date with year \a y, month \a m and day \a d.
|
|
|
|
|
|
|
|
If the specified date is invalid, the date is not set and
|
2013-10-02 14:51:05 +00:00
|
|
|
isValid() returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-03-08 14:52:27 +00:00
|
|
|
\warning Years 1 to 99 are interpreted as is. Year 0 is invalid.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDate::QDate(int y, int m, int d)
|
|
|
|
{
|
|
|
|
setDate(y, m, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::isNull() const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the date is null; otherwise returns \c false. A null
|
2011-04-27 10:05:43 +00:00
|
|
|
date is invalid.
|
|
|
|
|
|
|
|
\note The behavior of this function is equivalent to isValid().
|
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-08-17 09:31:23 +00:00
|
|
|
\fn bool QDate::isValid() const
|
2011-08-25 06:54:16 +00:00
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is valid; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa isNull()
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the year of this date. Negative numbers indicate years
|
2011-08-25 06:54:16 +00:00
|
|
|
before 1 CE, such that year -44 is 44 BCE.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa month(), day()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::year() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
return getDateFromJulianDay(jd).year;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number corresponding to the month of this date, using
|
|
|
|
the following convention:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li 1 = "January"
|
|
|
|
\li 2 = "February"
|
|
|
|
\li 3 = "March"
|
|
|
|
\li 4 = "April"
|
|
|
|
\li 5 = "May"
|
|
|
|
\li 6 = "June"
|
|
|
|
\li 7 = "July"
|
|
|
|
\li 8 = "August"
|
|
|
|
\li 9 = "September"
|
|
|
|
\li 10 = "October"
|
|
|
|
\li 11 = "November"
|
|
|
|
\li 12 = "December"
|
2011-04-27 10:05:43 +00:00
|
|
|
\endlist
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa year(), day()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::month() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
return getDateFromJulianDay(jd).month;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the day of the month (1 to 31) of this date.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa year(), month(), dayOfWeek()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::day() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
return getDateFromJulianDay(jd).day;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2012-02-02 10:29:54 +00:00
|
|
|
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa day(), dayOfYear(), Qt::DayOfWeek
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::dayOfWeek() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2011-08-25 06:54:16 +00:00
|
|
|
if (jd >= 0)
|
|
|
|
return (jd % 7) + 1;
|
|
|
|
else
|
|
|
|
return ((jd + 1) % 7) + 7;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the day of the year (1 to 365 or 366 on leap years) for
|
|
|
|
this date.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa day(), dayOfWeek()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::dayOfYear() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return jd - julianDayFromDate(year(), 1, 1) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of days in the month (28 to 31) for this date.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa day(), daysInYear()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::daysInMonth() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
const ParsedDate pd = getDateFromJulianDay(jd);
|
|
|
|
if (pd.month == 2 && isLeapYear(pd.year))
|
2011-04-27 10:05:43 +00:00
|
|
|
return 29;
|
|
|
|
else
|
2015-01-20 22:48:22 +00:00
|
|
|
return monthDays[pd.month];
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of days in the year (365 or 366) for this date.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa day(), daysInMonth()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::daysInYear() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return 0;
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
return isLeapYear(getDateFromJulianDay(jd).year) ? 366 : 365;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the week number (1 to 53), and stores the year in
|
|
|
|
*\a{yearNumber} unless \a yearNumber is null (the default).
|
|
|
|
|
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
|
|
|
In accordance with ISO 8601, weeks start on Monday and the first
|
|
|
|
Thursday of a year is always in week 1 of that year. Most years
|
|
|
|
have 52 weeks, but some have 53.
|
|
|
|
|
|
|
|
*\a{yearNumber} is not always the same as year(). For example, 1
|
|
|
|
January 2000 has week number 52 in the year 1999, and 31 December
|
|
|
|
2002 has week number 1 in the year 2003.
|
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDate::weekNumber(int *yearNumber) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int year = QDate::year();
|
2015-02-27 12:17:16 +00:00
|
|
|
int yday = dayOfYear();
|
2011-04-27 10:05:43 +00:00
|
|
|
int wday = dayOfWeek();
|
2015-02-27 12:17:16 +00:00
|
|
|
|
|
|
|
int week = (yday - wday + 10) / 7;
|
|
|
|
|
|
|
|
if (week == 0) {
|
|
|
|
// last week of previous year
|
|
|
|
--year;
|
|
|
|
week = (yday + 365 + (QDate::isLeapYear(year) ? 1 : 0) - wday + 10) / 7;
|
|
|
|
Q_ASSERT(week == 52 || week == 53);
|
|
|
|
} else if (week == 53) {
|
|
|
|
// maybe first week of next year
|
2016-01-04 18:31:31 +00:00
|
|
|
int w = (yday - 365 - (QDate::isLeapYear(year) ? 1 : 0) - wday + 10) / 7;
|
2015-02-27 12:17:16 +00:00
|
|
|
if (w > 0) {
|
2011-04-27 10:05:43 +00:00
|
|
|
++year;
|
2015-02-27 12:17:16 +00:00
|
|
|
week = w;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2015-02-27 12:17:16 +00:00
|
|
|
Q_ASSERT(week == 53 || week == 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2015-02-27 12:17:16 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (yearNumber != 0)
|
|
|
|
*yearNumber = year;
|
2015-02-27 12:17:16 +00:00
|
|
|
return week;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_DEPRECATED_SINCE(5, 11) && QT_CONFIG(textdate)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\since 4.5
|
2017-08-17 16:34:09 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the short name of the \a month for the representation specified
|
|
|
|
by \a type.
|
|
|
|
|
|
|
|
The months are enumerated using the following convention:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li 1 = "Jan"
|
|
|
|
\li 2 = "Feb"
|
|
|
|
\li 3 = "Mar"
|
|
|
|
\li 4 = "Apr"
|
|
|
|
\li 5 = "May"
|
|
|
|
\li 6 = "Jun"
|
|
|
|
\li 7 = "Jul"
|
|
|
|
\li 8 = "Aug"
|
|
|
|
\li 9 = "Sep"
|
|
|
|
\li 10 = "Oct"
|
|
|
|
\li 11 = "Nov"
|
|
|
|
\li 12 = "Dec"
|
2011-04-27 10:05:43 +00:00
|
|
|
\endlist
|
|
|
|
|
2013-07-26 21:48:11 +00:00
|
|
|
The month names will be localized according to the system's
|
|
|
|
locale settings, i.e. using QLocale::system().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns an empty string if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toString(), longMonthName(), shortDayName(), longDayName()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QString QDate::shortMonthName(int month, QDate::MonthNameType type)
|
|
|
|
{
|
2017-06-13 17:10:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case QDate::DateFormat:
|
|
|
|
return QLocale::system().monthName(month, QLocale::ShortFormat);
|
|
|
|
case QDate::StandaloneFormat:
|
|
|
|
return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.5
|
2017-08-17 16:34:09 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the long name of the \a month for the representation specified
|
|
|
|
by \a type.
|
|
|
|
|
|
|
|
The months are enumerated using the following convention:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li 1 = "January"
|
|
|
|
\li 2 = "February"
|
|
|
|
\li 3 = "March"
|
|
|
|
\li 4 = "April"
|
|
|
|
\li 5 = "May"
|
|
|
|
\li 6 = "June"
|
|
|
|
\li 7 = "July"
|
|
|
|
\li 8 = "August"
|
|
|
|
\li 9 = "September"
|
|
|
|
\li 10 = "October"
|
|
|
|
\li 11 = "November"
|
|
|
|
\li 12 = "December"
|
2011-04-27 10:05:43 +00:00
|
|
|
\endlist
|
|
|
|
|
2013-07-26 21:48:11 +00:00
|
|
|
The month names will be localized according to the system's
|
|
|
|
locale settings, i.e. using QLocale::system().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns an empty string if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toString(), shortMonthName(), shortDayName(), longDayName()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QString QDate::longMonthName(int month, MonthNameType type)
|
|
|
|
{
|
2017-06-13 17:10:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case QDate::DateFormat:
|
|
|
|
return QLocale::system().monthName(month, QLocale::LongFormat);
|
|
|
|
case QDate::StandaloneFormat:
|
|
|
|
return QLocale::system().standaloneMonthName(month, QLocale::LongFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.5
|
2017-08-17 16:34:09 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the short name of the \a weekday for the representation specified
|
|
|
|
by \a type.
|
|
|
|
|
|
|
|
The days are enumerated using the following convention:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li 1 = "Mon"
|
|
|
|
\li 2 = "Tue"
|
|
|
|
\li 3 = "Wed"
|
|
|
|
\li 4 = "Thu"
|
|
|
|
\li 5 = "Fri"
|
|
|
|
\li 6 = "Sat"
|
|
|
|
\li 7 = "Sun"
|
2011-04-27 10:05:43 +00:00
|
|
|
\endlist
|
|
|
|
|
2013-07-26 21:48:11 +00:00
|
|
|
The day names will be localized according to the system's
|
|
|
|
locale settings, i.e. using QLocale::system().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns an empty string if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toString(), shortMonthName(), longMonthName(), longDayName()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QString QDate::shortDayName(int weekday, MonthNameType type)
|
|
|
|
{
|
2017-06-13 17:10:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case QDate::DateFormat:
|
|
|
|
return QLocale::system().dayName(weekday, QLocale::ShortFormat);
|
|
|
|
case QDate::StandaloneFormat:
|
|
|
|
return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.5
|
2017-08-17 16:34:09 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the long name of the \a weekday for the representation specified
|
|
|
|
by \a type.
|
|
|
|
|
|
|
|
The days are enumerated using the following convention:
|
|
|
|
|
|
|
|
\list
|
2012-03-01 14:28:31 +00:00
|
|
|
\li 1 = "Monday"
|
|
|
|
\li 2 = "Tuesday"
|
|
|
|
\li 3 = "Wednesday"
|
|
|
|
\li 4 = "Thursday"
|
|
|
|
\li 5 = "Friday"
|
|
|
|
\li 6 = "Saturday"
|
|
|
|
\li 7 = "Sunday"
|
2011-04-27 10:05:43 +00:00
|
|
|
\endlist
|
|
|
|
|
2013-07-26 21:48:11 +00:00
|
|
|
The day names will be localized according to the system's
|
|
|
|
locale settings, i.e. using QLocale::system().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns an empty string if the date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toString(), shortDayName(), shortMonthName(), longMonthName()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QString QDate::longDayName(int weekday, MonthNameType type)
|
|
|
|
{
|
2017-06-13 17:10:06 +00:00
|
|
|
switch (type) {
|
|
|
|
case QDate::DateFormat:
|
|
|
|
return QLocale::system().dayName(weekday, QLocale::LongFormat);
|
|
|
|
case QDate::StandaloneFormat:
|
|
|
|
return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2015-01-22 14:04:30 +00:00
|
|
|
return QString();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2018-09-19 11:28:13 +00:00
|
|
|
#endif // textdate && deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_CONFIG(textdate)
|
2015-02-12 20:07:13 +00:00
|
|
|
static QString toStringTextDate(QDate date)
|
|
|
|
{
|
|
|
|
const ParsedDate pd = getDateFromJulianDay(date.toJulianDay());
|
|
|
|
static const QLatin1Char sp(' ');
|
2017-08-17 16:34:09 +00:00
|
|
|
return QLocale::system().dayName(date.dayOfWeek(), QLocale::ShortFormat) + sp
|
|
|
|
+ QLocale::system().monthName(pd.month, QLocale::ShortFormat) + sp
|
2015-02-12 20:07:13 +00:00
|
|
|
+ QString::number(pd.day) + sp
|
|
|
|
+ QString::number(pd.year);
|
|
|
|
}
|
2018-09-19 11:28:13 +00:00
|
|
|
#endif // textdate
|
2015-02-12 20:07:13 +00:00
|
|
|
|
|
|
|
static QString toStringIsoDate(qint64 jd)
|
|
|
|
{
|
|
|
|
const ParsedDate pd = getDateFromJulianDay(jd);
|
|
|
|
if (pd.year >= 0 && pd.year <= 9999)
|
|
|
|
return QString::asprintf("%04d-%02d-%02d", pd.year, pd.month, pd.day);
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn QString QDate::toString(Qt::DateFormat format) const
|
|
|
|
|
|
|
|
\overload
|
|
|
|
|
|
|
|
Returns the date as a string. The \a format parameter determines
|
|
|
|
the format of the string.
|
|
|
|
|
|
|
|
If the \a format is Qt::TextDate, the string is formatted in
|
|
|
|
the default way. QDate::shortDayName() and QDate::shortMonthName()
|
|
|
|
are used to generate the string, so the day and month names will
|
2013-07-26 21:48:11 +00:00
|
|
|
be localized names using the system locale, i.e. QLocale::system(). An
|
2012-12-12 14:29:02 +00:00
|
|
|
example of this formatting is "Sat May 20 1995".
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the \a format is Qt::ISODate, the string format corresponds
|
|
|
|
to the ISO 8601 extended specification for representations of
|
2017-07-24 00:56:55 +00:00
|
|
|
dates and times, taking the form yyyy-MM-dd, where yyyy is the
|
|
|
|
year, MM is the month of the year (between 01 and 12), and dd is
|
2011-04-27 10:05:43 +00:00
|
|
|
the day of the month between 01 and 31.
|
|
|
|
|
|
|
|
If the \a format is Qt::SystemLocaleShortDate or
|
|
|
|
Qt::SystemLocaleLongDate, the string format depends on the locale
|
|
|
|
settings of the system. Identical to calling
|
|
|
|
QLocale::system().toString(date, QLocale::ShortFormat) or
|
|
|
|
QLocale::system().toString(date, QLocale::LongFormat).
|
|
|
|
|
|
|
|
If the \a format is Qt::DefaultLocaleShortDate or
|
|
|
|
Qt::DefaultLocaleLongDate, the string format depends on the
|
|
|
|
default application locale. This is the locale set with
|
|
|
|
QLocale::setDefault(), or the system locale if no default locale
|
2014-09-04 09:52:29 +00:00
|
|
|
has been set. Identical to calling
|
|
|
|
\l {QLocale::toString()}{QLocale().toString(date, QLocale::ShortFormat) } or
|
|
|
|
\l {QLocale::toString()}{QLocale().toString(date, QLocale::LongFormat)}.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-13 09:01:34 +00:00
|
|
|
If the \a format is Qt::RFC2822Date, the string is formatted in
|
|
|
|
an \l{RFC 2822} compatible way. An example of this formatting is
|
|
|
|
"20 May 1995".
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
If the date is invalid, an empty string will be returned.
|
|
|
|
|
|
|
|
\warning The Qt::ISODate format is only valid for years in the
|
|
|
|
range 0 to 9999. This restriction may apply to locale-aware
|
|
|
|
formats as well, depending on the locale settings.
|
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa fromString(), shortDayName(), shortMonthName(), QLocale::toString()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2013-07-18 09:37:57 +00:00
|
|
|
QString QDate::toString(Qt::DateFormat format) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return QString();
|
2013-07-18 09:37:57 +00:00
|
|
|
|
|
|
|
switch (format) {
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::SystemLocaleDate:
|
|
|
|
case Qt::SystemLocaleShortDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale::system().toString(*this, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::SystemLocaleLongDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale::system().toString(*this, QLocale::LongFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::LocaleDate:
|
|
|
|
case Qt::DefaultLocaleShortDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale().toString(*this, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::DefaultLocaleLongDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale().toString(*this, QLocale::LongFormat);
|
|
|
|
case Qt::RFC2822Date:
|
2017-01-31 00:04:45 +00:00
|
|
|
return QLocale::c().toString(*this, QStringViewLiteral("dd MMM yyyy"));
|
2011-04-27 10:05:43 +00:00
|
|
|
default:
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_CONFIG(textdate)
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::TextDate:
|
2015-02-12 20:07:13 +00:00
|
|
|
return toStringTextDate(*this);
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
case Qt::ISODate:
|
2016-10-10 11:44:52 +00:00
|
|
|
case Qt::ISODateWithMs:
|
2015-02-12 20:07:13 +00:00
|
|
|
return toStringIsoDate(jd);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-04-11 10:25:37 +00:00
|
|
|
\fn QString QDate::toString(const QString &format) const
|
|
|
|
\fn QString QDate::toString(QStringView format) const
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Returns the date as a string. The \a format parameter determines
|
|
|
|
the format of the result string.
|
|
|
|
|
|
|
|
These expressions may be used:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li d \li the day as number without a leading zero (1 to 31)
|
|
|
|
\row \li dd \li the day as number with a leading zero (01 to 31)
|
|
|
|
\row \li ddd
|
|
|
|
\li the abbreviated localized day name (e.g. 'Mon' to 'Sun').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li dddd
|
|
|
|
\li the long localized day name (e.g. 'Monday' to 'Sunday').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li M \li the month as number without a leading zero (1 to 12)
|
|
|
|
\row \li MM \li the month as number with a leading zero (01 to 12)
|
|
|
|
\row \li MMM
|
|
|
|
\li the abbreviated localized month name (e.g. 'Jan' to 'Dec').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li MMMM
|
|
|
|
\li the long localized month name (e.g. 'January' to 'December').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li yy \li the year as two digit number (00 to 99)
|
|
|
|
\row \li yyyy \li the year as four digit number. If the year is negative,
|
2011-04-27 10:05:43 +00:00
|
|
|
a minus sign is prepended in addition.
|
|
|
|
\endtable
|
|
|
|
|
2018-11-07 16:02:07 +00:00
|
|
|
Any sequence of characters enclosed in single quotes will be included
|
|
|
|
verbatim in the output string (stripped of the quotes), even if it contains
|
|
|
|
formatting characters. Two consecutive single quotes ("''") are replaced by
|
|
|
|
a single quote in the output. All other characters in the format string are
|
|
|
|
included verbatim in the output string.
|
|
|
|
|
|
|
|
Formats without separators (e.g. "ddMM") are supported but must be used with
|
|
|
|
care, as the resulting strings aren't always reliably readable (e.g. if "dM"
|
|
|
|
produces "212" it could mean either the 2nd of December or the 21st of
|
|
|
|
February).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Example format strings (assuming that the QDate is the 20 July
|
|
|
|
1969):
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Format \li Result
|
|
|
|
\row \li dd.MM.yyyy \li 20.07.1969
|
|
|
|
\row \li ddd MMMM d yy \li Sun July 20 69
|
|
|
|
\row \li 'The day is' dddd \li The day is Sunday
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
If the datetime is invalid, an empty string will be returned.
|
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa fromString(), QDateTime::toString(), QTime::toString(), QLocale::toString()
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
*/
|
2017-04-11 10:25:37 +00:00
|
|
|
QString QDate::toString(QStringView format) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-09-08 17:24:04 +00:00
|
|
|
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2017-04-11 10:25:37 +00:00
|
|
|
|
|
|
|
#if QT_STRINGVIEW_LEVEL < 2
|
|
|
|
QString QDate::toString(const QString &format) const
|
|
|
|
{
|
2017-04-19 23:33:21 +00:00
|
|
|
return toString(qToStringViewIgnoringNull(format));
|
2017-04-11 10:25:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
2012-03-20 18:37:07 +00:00
|
|
|
\fn bool QDate::setYMD(int y, int m, int d)
|
2012-01-16 20:20:39 +00:00
|
|
|
|
|
|
|
\deprecated in 5.0, use setDate() instead.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Sets the date's year \a y, month \a m, and day \a d.
|
|
|
|
|
|
|
|
If \a y is in the range 0 to 99, it is interpreted as 1900 to
|
|
|
|
1999.
|
2012-10-16 10:57:31 +00:00
|
|
|
Returns \c false if the date is invalid.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Use setDate() instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.2
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Sets the date's \a year, \a month, and \a day. Returns \c true if
|
|
|
|
the date is valid; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the specified date is invalid, the QDate object is set to be
|
|
|
|
invalid.
|
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
bool QDate::setDate(int year, int month, int day)
|
|
|
|
{
|
2011-08-25 06:54:16 +00:00
|
|
|
if (isValid(year, month, day))
|
2011-04-27 10:05:43 +00:00
|
|
|
jd = julianDayFromDate(year, month, day);
|
2011-08-25 06:54:16 +00:00
|
|
|
else
|
|
|
|
jd = nullJd();
|
|
|
|
|
|
|
|
return isValid();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.5
|
|
|
|
|
|
|
|
Extracts the date's year, month, and day, and assigns them to
|
|
|
|
*\a year, *\a month, and *\a day. The pointers may be null.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if the date is invalid.
|
|
|
|
|
2016-02-16 11:56:26 +00:00
|
|
|
\note In Qt versions prior to 5.7, this function is marked as non-\c{const}.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa year(), month(), day(), isValid()
|
|
|
|
*/
|
2016-02-16 11:56:26 +00:00
|
|
|
void QDate::getDate(int *year, int *month, int *day) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2015-01-20 22:48:22 +00:00
|
|
|
ParsedDate pd = { 0, 0, 0 };
|
|
|
|
if (isValid())
|
|
|
|
pd = getDateFromJulianDay(jd);
|
|
|
|
|
|
|
|
if (year)
|
|
|
|
*year = pd.year;
|
|
|
|
if (month)
|
|
|
|
*month = pd.month;
|
|
|
|
if (day)
|
|
|
|
*day = pd.day;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 11:56:26 +00:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
/*!
|
|
|
|
\overload
|
|
|
|
\internal
|
|
|
|
*/
|
|
|
|
void QDate::getDate(int *year, int *month, int *day)
|
|
|
|
{
|
|
|
|
qAsConst(*this).getDate(year, month, day);
|
|
|
|
}
|
|
|
|
#endif // < Qt 6
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Returns a QDate object containing a date \a ndays later than the
|
|
|
|
date of this object (or earlier if \a ndays is negative).
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns a null date if the current date is invalid or the new date is
|
2012-09-04 22:52:53 +00:00
|
|
|
out of range.
|
2011-07-17 18:30:12 +00:00
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa addMonths(), addYears(), daysTo()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-08-25 06:54:16 +00:00
|
|
|
QDate QDate::addDays(qint64 ndays) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull())
|
|
|
|
return QDate();
|
|
|
|
|
2012-10-09 13:44:42 +00:00
|
|
|
// Due to limits on minJd() and maxJd() we know that any overflow
|
|
|
|
// will be invalid and caught by fromJulianDay().
|
|
|
|
return fromJulianDay(jd + ndays);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QDate object containing a date \a nmonths later than the
|
|
|
|
date of this object (or earlier if \a nmonths is negative).
|
|
|
|
|
|
|
|
\note If the ending day/month combination does not exist in the
|
|
|
|
resulting month/year, this function will return a date that is the
|
|
|
|
latest valid date.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa addDays(), addYears()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDate QDate::addMonths(int nmonths) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return QDate();
|
|
|
|
if (!nmonths)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
int old_y, y, m, d;
|
2015-01-20 22:48:22 +00:00
|
|
|
{
|
|
|
|
const ParsedDate pd = getDateFromJulianDay(jd);
|
|
|
|
y = pd.year;
|
|
|
|
m = pd.month;
|
|
|
|
d = pd.day;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
old_y = y;
|
|
|
|
|
|
|
|
bool increasing = nmonths > 0;
|
|
|
|
|
|
|
|
while (nmonths != 0) {
|
|
|
|
if (nmonths < 0 && nmonths + 12 <= 0) {
|
|
|
|
y--;
|
|
|
|
nmonths+=12;
|
|
|
|
} else if (nmonths < 0) {
|
|
|
|
m+= nmonths;
|
|
|
|
nmonths = 0;
|
|
|
|
if (m <= 0) {
|
|
|
|
--y;
|
|
|
|
m += 12;
|
|
|
|
}
|
|
|
|
} else if (nmonths - 12 >= 0) {
|
|
|
|
y++;
|
|
|
|
nmonths -= 12;
|
|
|
|
} else if (m == 12) {
|
|
|
|
y++;
|
|
|
|
m = 0;
|
|
|
|
} else {
|
|
|
|
m += nmonths;
|
|
|
|
nmonths = 0;
|
|
|
|
if (m > 12) {
|
|
|
|
++y;
|
|
|
|
m -= 12;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// was there a sign change?
|
|
|
|
if ((old_y > 0 && y <= 0) ||
|
|
|
|
(old_y < 0 && y >= 0))
|
|
|
|
// yes, adjust the date by +1 or -1 years
|
|
|
|
y += increasing ? +1 : -1;
|
|
|
|
|
|
|
|
return fixedDate(y, m, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QDate object containing a date \a nyears later than the
|
|
|
|
date of this object (or earlier if \a nyears is negative).
|
|
|
|
|
|
|
|
\note If the ending day/month combination does not exist in the
|
|
|
|
resulting year (i.e., if the date was Feb 29 and the final year is
|
|
|
|
not a leap year), this function will return a date that is the
|
|
|
|
latest valid date (that is, Feb 28).
|
|
|
|
|
|
|
|
\sa addDays(), addMonths()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDate QDate::addYears(int nyears) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return QDate();
|
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
ParsedDate pd = getDateFromJulianDay(jd);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
int old_y = pd.year;
|
|
|
|
pd.year += nyears;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// was there a sign change?
|
2015-01-20 22:48:22 +00:00
|
|
|
if ((old_y > 0 && pd.year <= 0) ||
|
|
|
|
(old_y < 0 && pd.year >= 0))
|
2011-04-27 10:05:43 +00:00
|
|
|
// yes, adjust the date by +1 or -1 years
|
2015-01-20 22:48:22 +00:00
|
|
|
pd.year += nyears > 0 ? +1 : -1;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-20 22:48:22 +00:00
|
|
|
return fixedDate(pd.year, pd.month, pd.day);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of days from this date to \a d (which is
|
|
|
|
negative if \a d is earlier than this date).
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if either date is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa addDays()
|
|
|
|
*/
|
|
|
|
|
2011-08-25 06:54:16 +00:00
|
|
|
qint64 QDate::daysTo(const QDate &d) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isNull() || d.isNull())
|
|
|
|
return 0;
|
|
|
|
|
2011-08-25 06:54:16 +00:00
|
|
|
// Due to limits on minJd() and maxJd() we know this will never overflow
|
2011-04-27 10:05:43 +00:00
|
|
|
return d.jd - jd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::operator==(const QDate &d) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is equal to \a d; otherwise returns
|
2011-04-27 10:05:43 +00:00
|
|
|
false.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::operator!=(const QDate &d) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is different from \a d; otherwise
|
|
|
|
returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::operator<(const QDate &d) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is earlier than \a d; otherwise returns
|
2011-04-27 10:05:43 +00:00
|
|
|
false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::operator<=(const QDate &d) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is earlier than or equal to \a d;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::operator>(const QDate &d) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is later than \a d; otherwise returns
|
2011-04-27 10:05:43 +00:00
|
|
|
false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::operator>=(const QDate &d) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this date is later than or equal to \a d;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QDate::currentDate()
|
|
|
|
Returns the current date, as reported by the system clock.
|
|
|
|
|
|
|
|
\sa QTime::currentTime(), QDateTime::currentDateTime()
|
|
|
|
*/
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn QDate QDate::fromString(const QString &string, Qt::DateFormat format)
|
|
|
|
|
|
|
|
Returns the QDate represented by the \a string, using the
|
|
|
|
\a format given, or an invalid date if the string cannot be
|
|
|
|
parsed.
|
|
|
|
|
|
|
|
Note for Qt::TextDate: It is recommended that you use the
|
|
|
|
English short month names (e.g. "Jan"). Although localized month
|
|
|
|
names can also be used, they depend on the user's locale settings.
|
2016-09-08 16:57:46 +00:00
|
|
|
|
|
|
|
\sa toString(), QLocale::toDate()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2013-07-18 09:37:57 +00:00
|
|
|
QDate QDate::fromString(const QString& string, Qt::DateFormat format)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-07-18 09:37:57 +00:00
|
|
|
if (string.isEmpty())
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDate();
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
switch (format) {
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::SystemLocaleDate:
|
|
|
|
case Qt::SystemLocaleShortDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale::system().toDate(string, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::SystemLocaleLongDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale::system().toDate(string, QLocale::LongFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::LocaleDate:
|
|
|
|
case Qt::DefaultLocaleShortDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale().toDate(string, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::DefaultLocaleLongDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale().toDate(string, QLocale::LongFormat);
|
2015-01-21 09:17:00 +00:00
|
|
|
case Qt::RFC2822Date:
|
|
|
|
return rfcDateImpl(string).date;
|
2011-04-27 10:05:43 +00:00
|
|
|
default:
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_CONFIG(textdate)
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::TextDate: {
|
2014-05-14 13:25:30 +00:00
|
|
|
QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
if (parts.count() != 4)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDate();
|
|
|
|
|
2014-05-14 13:25:30 +00:00
|
|
|
QStringRef monthName = parts.at(1);
|
2015-01-21 09:38:48 +00:00
|
|
|
const int month = fromShortMonthName(monthName);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (month == -1) {
|
2015-01-21 09:38:48 +00:00
|
|
|
// Month name matches neither English nor other localised name.
|
|
|
|
return QDate();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
bool ok = false;
|
|
|
|
int year = parts.at(3).toInt(&ok);
|
|
|
|
if (!ok)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDate();
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
return QDate(year, month, parts.at(2).toInt());
|
|
|
|
}
|
2018-09-19 11:28:13 +00:00
|
|
|
#endif // textdate
|
2013-07-18 09:37:57 +00:00
|
|
|
case Qt::ISODate: {
|
2014-01-06 19:03:37 +00:00
|
|
|
// Semi-strict parsing, must be long enough and have non-numeric separators
|
|
|
|
if (string.size() < 10 || string.at(4).isDigit() || string.at(7).isDigit()
|
|
|
|
|| (string.size() > 10 && string.at(10).isDigit())) {
|
|
|
|
return QDate();
|
|
|
|
}
|
2014-09-14 13:26:52 +00:00
|
|
|
const int year = string.midRef(0, 4).toInt();
|
2013-07-18 09:37:57 +00:00
|
|
|
if (year <= 0 || year > 9999)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDate();
|
2014-09-14 13:26:52 +00:00
|
|
|
return QDate(year, string.midRef(5, 2).toInt(), string.midRef(8, 2).toInt());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2018-01-16 11:38:23 +00:00
|
|
|
\fn QDate QDate::fromString(const QString &string, const QString &format)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the QDate represented by the \a string, using the \a
|
|
|
|
format given, or an invalid date if the string cannot be parsed.
|
|
|
|
|
|
|
|
These expressions may be used for the format:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li d \li The day as a number without a leading zero (1 to 31)
|
|
|
|
\row \li dd \li The day as a number with a leading zero (01 to 31)
|
|
|
|
\row \li ddd
|
|
|
|
\li The abbreviated localized day name (e.g. 'Mon' to 'Sun').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li dddd
|
|
|
|
\li The long localized day name (e.g. 'Monday' to 'Sunday').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li M \li The month as a number without a leading zero (1 to 12)
|
|
|
|
\row \li MM \li The month as a number with a leading zero (01 to 12)
|
|
|
|
\row \li MMM
|
|
|
|
\li The abbreviated localized month name (e.g. 'Jan' to 'Dec').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li MMMM
|
|
|
|
\li The long localized month name (e.g. 'January' to 'December').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li yy \li The year as two digit number (00 to 99)
|
|
|
|
\row \li yyyy \li The year as four digit number. If the year is negative,
|
2011-04-27 10:05:43 +00:00
|
|
|
a minus sign is prepended in addition.
|
|
|
|
\endtable
|
|
|
|
|
|
|
|
All other input characters will be treated as text. Any sequence
|
|
|
|
of characters that are enclosed in single quotes will also be
|
|
|
|
treated as text and will not be used as an expression. For example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the format is not satisfied, an invalid QDate is returned. The
|
|
|
|
expressions that don't expect leading zeroes (d, M) will be
|
|
|
|
greedy. This means that they will use two digits even if this
|
|
|
|
will put them outside the accepted range of values and leaves too
|
|
|
|
few digits for other sections. For example, the following format
|
|
|
|
string could have meant January 30 but the M will grab two
|
|
|
|
digits, resulting in an invalid date:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For any field that is not represented in the format the following
|
|
|
|
defaults are used:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Field \li Default value
|
|
|
|
\row \li Year \li 1900
|
|
|
|
\row \li Month \li 1
|
|
|
|
\row \li Day \li 1
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
The following examples demonstrate the default values:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 3
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa toString(), QDateTime::fromString(), QTime::fromString(),
|
|
|
|
QLocale::toDate()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDate QDate::fromString(const QString &string, const QString &format)
|
|
|
|
{
|
|
|
|
QDate date;
|
2017-09-11 16:14:51 +00:00
|
|
|
#if QT_CONFIG(datetimeparser)
|
2011-04-27 10:05:43 +00:00
|
|
|
QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
|
2016-09-08 17:24:04 +00:00
|
|
|
// dt.setDefaultLocale(QLocale::c()); ### Qt 6
|
2011-04-27 10:05:43 +00:00
|
|
|
if (dt.parseFormat(format))
|
|
|
|
dt.fromString(string, &date, 0);
|
|
|
|
#else
|
|
|
|
Q_UNUSED(string);
|
|
|
|
Q_UNUSED(format);
|
|
|
|
#endif
|
|
|
|
return date;
|
|
|
|
}
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\overload
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the specified date (\a year, \a month, and \a
|
|
|
|
day) is valid; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 4
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa isNull(), setDate()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDate::isValid(int year, int month, int day)
|
|
|
|
{
|
2012-01-09 20:50:00 +00:00
|
|
|
// there is no year 0 in the Gregorian calendar
|
2011-08-25 06:54:16 +00:00
|
|
|
if (year == 0)
|
2011-04-27 10:05:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return (day > 0 && month > 0 && month <= 12) &&
|
|
|
|
(day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDate::isLeapYear(int year)
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the specified \a year is a leap year; otherwise
|
|
|
|
returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDate::isLeapYear(int y)
|
|
|
|
{
|
2012-01-09 20:50:00 +00:00
|
|
|
// No year 0 in Gregorian calendar, so -1, -5, -9 etc are leap years
|
|
|
|
if ( y < 1)
|
|
|
|
++y;
|
|
|
|
|
|
|
|
return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
/*! \fn static QDate QDate::fromJulianDay(qint64 jd)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Converts the Julian day \a jd to a QDate.
|
|
|
|
|
|
|
|
\sa toJulianDay()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn int QDate::toJulianDay() const
|
|
|
|
|
|
|
|
Converts the date to a Julian day.
|
|
|
|
|
|
|
|
\sa fromJulianDay()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
QTime member functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QTime
|
2012-08-23 10:22:38 +00:00
|
|
|
\inmodule QtCore
|
2011-04-27 10:05:43 +00:00
|
|
|
\reentrant
|
|
|
|
|
|
|
|
\brief The QTime class provides clock time functions.
|
|
|
|
|
|
|
|
|
2018-09-11 15:47:39 +00:00
|
|
|
A QTime object contains a clock time, which it can express as the
|
|
|
|
numbers of hours, minutes, seconds, and milliseconds since
|
|
|
|
midnight. It can read the current time from the system clock and
|
|
|
|
measure a span of elapsed time. It provides functions for
|
|
|
|
comparing times and for manipulating a time by adding a number of
|
|
|
|
milliseconds.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QTime uses the 24-hour clock format; it has no concept of AM/PM.
|
|
|
|
Unlike QDateTime, QTime knows nothing about time zones or
|
2015-11-09 11:05:49 +00:00
|
|
|
daylight-saving time (DST).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
A QTime object is typically created either by giving the number of hours,
|
|
|
|
minutes, seconds, and milliseconds explicitly, or by using the static
|
|
|
|
function currentTime(), which creates a QTime object that represents the
|
|
|
|
system's local time.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The hour(), minute(), second(), and msec() functions provide
|
|
|
|
access to the number of hours, minutes, seconds, and milliseconds
|
|
|
|
of the time. The same information is provided in textual format by
|
|
|
|
the toString() function.
|
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
The addSecs() and addMSecs() functions provide the time a given
|
|
|
|
number of seconds or milliseconds later than a given time.
|
|
|
|
Correspondingly, the number of seconds or milliseconds
|
2011-04-27 10:05:43 +00:00
|
|
|
between two times can be found using secsTo() or msecsTo().
|
|
|
|
|
2018-09-11 15:47:39 +00:00
|
|
|
QTime provides a full set of operators to compare two QTime
|
|
|
|
objects; an earlier time is considered smaller than a later one;
|
|
|
|
if A.msecsTo(B) is positive, then A < B.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QTime can be used to measure a span of elapsed time using the
|
|
|
|
start(), restart(), and elapsed() functions.
|
|
|
|
|
|
|
|
\sa QDate, QDateTime
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QTime::QTime()
|
|
|
|
|
|
|
|
Constructs a null time object. A null time can be a QTime(0, 0, 0, 0)
|
2013-10-02 14:51:05 +00:00
|
|
|
(i.e., midnight) object, except that isNull() returns \c true and isValid()
|
|
|
|
returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa isNull(), isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a time with hour \a h, minute \a m, seconds \a s and
|
|
|
|
milliseconds \a ms.
|
|
|
|
|
|
|
|
\a h must be in the range 0 to 23, \a m and \a s must be in the
|
|
|
|
range 0 to 59, and \a ms must be in the range 0 to 999.
|
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QTime::QTime(int h, int m, int s, int ms)
|
|
|
|
{
|
|
|
|
setHMS(h, m, s, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::isNull() const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the time is null (i.e., the QTime object was
|
2011-04-27 10:05:43 +00:00
|
|
|
constructed using the default constructor); otherwise returns
|
|
|
|
false. A null time is also an invalid time.
|
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the time is valid; otherwise returns \c false. For example,
|
2011-04-27 10:05:43 +00:00
|
|
|
the time 23:30:55.746 is valid, but 24:12:30 is invalid.
|
|
|
|
|
|
|
|
\sa isNull()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QTime::isValid() const
|
|
|
|
{
|
|
|
|
return mds > NullTime && mds < MSECS_PER_DAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the hour part (0 to 23) of the time.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns -1 if the time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa minute(), second(), msec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::hour() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid())
|
|
|
|
return -1;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return ds() / MSECS_PER_HOUR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the minute part (0 to 59) of the time.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns -1 if the time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa hour(), second(), msec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::minute() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid())
|
|
|
|
return -1;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the second part (0 to 59) of the time.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns -1 if the time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa hour(), minute(), msec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::second() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid())
|
|
|
|
return -1;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return (ds() / 1000)%SECS_PER_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the millisecond part (0 to 999) of the time.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns -1 if the time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa hour(), minute(), second()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::msec() const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid())
|
|
|
|
return -1;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return ds() % 1000;
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\overload
|
|
|
|
|
2013-04-24 11:38:13 +00:00
|
|
|
Returns the time as a string. The \a format parameter determines
|
|
|
|
the format of the string.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-01-03 17:06:38 +00:00
|
|
|
If \a format is Qt::TextDate, the string format is HH:mm:ss;
|
2013-09-16 09:55:07 +00:00
|
|
|
e.g. 1 second before midnight would be "23:59:59".
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If \a format is Qt::ISODate, the string format corresponds to the
|
2013-09-16 09:55:07 +00:00
|
|
|
ISO 8601 extended specification for representations of dates,
|
2016-10-10 11:44:52 +00:00
|
|
|
represented by HH:mm:ss. To include milliseconds in the ISO 8601
|
|
|
|
date, use the \a format Qt::ISODateWithMs, which corresponds to
|
|
|
|
HH:mm:ss.zzz.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the \a format is Qt::SystemLocaleShortDate or
|
|
|
|
Qt::SystemLocaleLongDate, the string format depends on the locale
|
|
|
|
settings of the system. Identical to calling
|
|
|
|
QLocale::system().toString(time, QLocale::ShortFormat) or
|
|
|
|
QLocale::system().toString(time, QLocale::LongFormat).
|
|
|
|
|
|
|
|
If the \a format is Qt::DefaultLocaleShortDate or
|
|
|
|
Qt::DefaultLocaleLongDate, the string format depends on the
|
|
|
|
default application locale. This is the locale set with
|
|
|
|
QLocale::setDefault(), or the system locale if no default locale
|
2014-09-04 09:52:29 +00:00
|
|
|
has been set. Identical to calling
|
|
|
|
|
|
|
|
\l {QLocale::toString()}{QLocale().toString(time, QLocale::ShortFormat)} or
|
|
|
|
\l {QLocale::toString()}{QLocale().toString(time, QLocale::LongFormat)}.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-13 09:01:34 +00:00
|
|
|
If the \a format is Qt::RFC2822Date, the string is formatted in
|
|
|
|
an \l{RFC 2822} compatible way. An example of this formatting is
|
|
|
|
"23:59:20".
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
If the time is invalid, an empty string will be returned.
|
2013-07-18 09:37:57 +00:00
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa fromString(), QDate::toString(), QDateTime::toString(), QLocale::toString()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QString QTime::toString(Qt::DateFormat format) const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case Qt::SystemLocaleDate:
|
|
|
|
case Qt::SystemLocaleShortDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale::system().toString(*this, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::SystemLocaleLongDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale::system().toString(*this, QLocale::LongFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::LocaleDate:
|
|
|
|
case Qt::DefaultLocaleShortDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale().toString(*this, QLocale::ShortFormat);
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::DefaultLocaleLongDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
return QLocale().toString(*this, QLocale::LongFormat);
|
2016-10-10 11:44:52 +00:00
|
|
|
case Qt::ISODateWithMs:
|
|
|
|
return QString::asprintf("%02d:%02d:%02d.%03d", hour(), minute(), second(), msec());
|
2013-07-13 09:01:34 +00:00
|
|
|
case Qt::RFC2822Date:
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::ISODate:
|
|
|
|
case Qt::TextDate:
|
2013-07-18 09:37:57 +00:00
|
|
|
default:
|
2015-01-22 18:28:01 +00:00
|
|
|
return QString::asprintf("%02d:%02d:%02d", hour(), minute(), second());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-04-11 10:25:37 +00:00
|
|
|
\fn QString QTime::toString(const QString &format) const
|
|
|
|
\fn QString QTime::toString(QStringView format) const
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Returns the time as a string. The \a format parameter determines
|
|
|
|
the format of the result string.
|
|
|
|
|
|
|
|
These expressions may be used:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li h
|
|
|
|
\li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
|
|
|
|
\row \li hh
|
|
|
|
\li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
|
|
|
|
\row \li H
|
|
|
|
\li the hour without a leading zero (0 to 23, even with AM/PM display)
|
|
|
|
\row \li HH
|
|
|
|
\li the hour with a leading zero (00 to 23, even with AM/PM display)
|
|
|
|
\row \li m \li the minute without a leading zero (0 to 59)
|
|
|
|
\row \li mm \li the minute with a leading zero (00 to 59)
|
2016-09-30 18:18:05 +00:00
|
|
|
\row \li s \li the whole second, without any leading zero (0 to 59)
|
|
|
|
\row \li ss \li the whole second, with a leading zero where applicable (00 to 59)
|
|
|
|
\row \li z \li the fractional part of the second, to go after a decimal
|
|
|
|
point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
|
|
|
|
reports the seconds to full available (millisecond) precision
|
|
|
|
without trailing zeroes.
|
|
|
|
\row \li zzz \li the fractional part of the second, to millisecond
|
|
|
|
precision, including trailing zeroes where applicable (000 to 999).
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li AP or A
|
2016-09-01 11:29:46 +00:00
|
|
|
\li use AM/PM display. \e A/AP will be replaced by either
|
|
|
|
QLocale::amText() or QLocale::pmText().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li ap or a
|
2016-09-01 11:29:46 +00:00
|
|
|
\li use am/pm display. \e a/ap will be replaced by a lower-case version of
|
|
|
|
QLocale::amText() or QLocale::pmText().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li t \li the timezone (for example "CEST")
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
2018-11-07 16:02:07 +00:00
|
|
|
Any sequence of characters enclosed in single quotes will be included
|
|
|
|
verbatim in the output string (stripped of the quotes), even if it contains
|
|
|
|
formatting characters. Two consecutive single quotes ("''") are replaced by
|
|
|
|
a single quote in the output. All other characters in the format string are
|
|
|
|
included verbatim in the output string.
|
|
|
|
|
|
|
|
Formats without separators (e.g. "ddMM") are supported but must be used with
|
|
|
|
care, as the resulting strings aren't always reliably readable (e.g. if "dM"
|
|
|
|
produces "212" it could mean either the 2nd of December or the 21st of
|
|
|
|
February).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-09-01 11:29:46 +00:00
|
|
|
Example format strings (assuming that the QTime is 14:13:09.042 and the system
|
|
|
|
locale is \c{en_US})
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Format \li Result
|
|
|
|
\row \li hh:mm:ss.zzz \li 14:13:09.042
|
|
|
|
\row \li h:m:s ap \li 2:13:9 pm
|
|
|
|
\row \li H:m:s a \li 14:13:9 pm
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
If the time is invalid, an empty string will be returned.
|
2011-04-27 10:05:43 +00:00
|
|
|
If \a format is empty, the default format "hh:mm:ss" is used.
|
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa fromString(), QDate::toString(), QDateTime::toString(), QLocale::toString()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2017-04-11 10:25:37 +00:00
|
|
|
QString QTime::toString(QStringView format) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-09-08 17:24:04 +00:00
|
|
|
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2017-04-11 10:25:37 +00:00
|
|
|
|
|
|
|
#if QT_STRINGVIEW_VERSION < 2
|
|
|
|
QString QTime::toString(const QString &format) const
|
|
|
|
{
|
2017-04-19 23:33:21 +00:00
|
|
|
return toString(qToStringViewIgnoringNull(format));
|
2017-04-11 10:25:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2017-04-11 10:25:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Sets the time to hour \a h, minute \a m, seconds \a s and
|
|
|
|
milliseconds \a ms.
|
|
|
|
|
|
|
|
\a h must be in the range 0 to 23, \a m and \a s must be in the
|
|
|
|
range 0 to 59, and \a ms must be in the range 0 to 999.
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the set time is valid; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QTime::setHMS(int h, int m, int s, int ms)
|
|
|
|
{
|
|
|
|
if (!isValid(h,m,s,ms)) {
|
|
|
|
mds = NullTime; // make this invalid
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QTime object containing a time \a s seconds later
|
|
|
|
than the time of this object (or earlier if \a s is negative).
|
|
|
|
|
|
|
|
Note that the time will wrap if it passes midnight.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns a null time if this time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 5
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa addMSecs(), secsTo(), QDateTime::addSecs()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QTime QTime::addSecs(int s) const
|
|
|
|
{
|
2015-08-11 16:59:10 +00:00
|
|
|
s %= SECS_PER_DAY;
|
2011-04-27 10:05:43 +00:00
|
|
|
return addMSecs(s * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of seconds from this time to \a t.
|
|
|
|
If \a t is earlier than this time, the number of seconds returned
|
|
|
|
is negative.
|
|
|
|
|
|
|
|
Because QTime measures time within a day and there are 86400
|
|
|
|
seconds in a day, the result is always between -86400 and 86400.
|
|
|
|
|
|
|
|
secsTo() does not take into account any milliseconds.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if either time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addSecs(), QDateTime::secsTo()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::secsTo(const QTime &t) const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid() || !t.isValid())
|
|
|
|
return 0;
|
|
|
|
|
2012-07-31 09:34:43 +00:00
|
|
|
// Truncate milliseconds as we do not want to consider them.
|
|
|
|
int ourSeconds = ds() / 1000;
|
|
|
|
int theirSeconds = t.ds() / 1000;
|
|
|
|
return theirSeconds - ourSeconds;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QTime object containing a time \a ms milliseconds later
|
|
|
|
than the time of this object (or earlier if \a ms is negative).
|
|
|
|
|
|
|
|
Note that the time will wrap if it passes midnight. See addSecs()
|
|
|
|
for an example.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns a null time if this time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addSecs(), msecsTo(), QDateTime::addMSecs()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QTime QTime::addMSecs(int ms) const
|
|
|
|
{
|
|
|
|
QTime t;
|
2011-07-17 18:30:12 +00:00
|
|
|
if (isValid()) {
|
|
|
|
if (ms < 0) {
|
2015-11-04 15:53:48 +00:00
|
|
|
// %,/ not well-defined for -ve, so always work with +ve.
|
2011-07-17 18:30:12 +00:00
|
|
|
int negdays = (MSECS_PER_DAY - ms) / MSECS_PER_DAY;
|
|
|
|
t.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY;
|
|
|
|
} else {
|
|
|
|
t.mds = (ds() + ms) % MSECS_PER_DAY;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of milliseconds from this time to \a t.
|
|
|
|
If \a t is earlier than this time, the number of milliseconds returned
|
|
|
|
is negative.
|
|
|
|
|
|
|
|
Because QTime measures time within a day and there are 86400
|
|
|
|
seconds in a day, the result is always between -86400000 and
|
|
|
|
86400000 ms.
|
|
|
|
|
2011-07-17 18:30:12 +00:00
|
|
|
Returns 0 if either time is invalid.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa secsTo(), addMSecs(), QDateTime::msecsTo()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::msecsTo(const QTime &t) const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid() || !t.isValid())
|
|
|
|
return 0;
|
2016-03-23 09:25:46 +00:00
|
|
|
return t.ds() - ds();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::operator==(const QTime &t) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this time is equal to \a t; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::operator!=(const QTime &t) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this time is different from \a t; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::operator<(const QTime &t) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this time is earlier than \a t; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::operator<=(const QTime &t) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this time is earlier than or equal to \a t;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::operator>(const QTime &t) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this time is later than \a t; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QTime::operator>=(const QTime &t) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this time is later than or equal to \a t;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-13 12:17:08 +00:00
|
|
|
/*!
|
|
|
|
\fn QTime QTime::fromMSecsSinceStartOfDay(int msecs)
|
|
|
|
|
|
|
|
Returns a new QTime instance with the time set to the number of \a msecs
|
|
|
|
since the start of the day, i.e. since 00:00:00.
|
|
|
|
|
|
|
|
If \a msecs falls outside the valid range an invalid QTime will be returned.
|
|
|
|
|
|
|
|
\sa msecsSinceStartOfDay()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn int QTime::msecsSinceStartOfDay() const
|
|
|
|
|
|
|
|
Returns the number of msecs since the start of the day, i.e. since 00:00:00.
|
|
|
|
|
|
|
|
\sa fromMSecsSinceStartOfDay()
|
|
|
|
*/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn QTime::currentTime()
|
|
|
|
|
|
|
|
Returns the current time as reported by the system clock.
|
|
|
|
|
|
|
|
Note that the accuracy depends on the accuracy of the underlying
|
|
|
|
operating system; not all systems provide 1-millisecond accuracy.
|
2019-05-24 12:20:43 +00:00
|
|
|
|
|
|
|
Furthermore, currentTime() only increases within each day; it shall drop by
|
|
|
|
24 hours each time midnight passes; and, beside this, changes in it may not
|
|
|
|
correspond to elapsed time, if a daylight-saving transition intervenes.
|
|
|
|
|
|
|
|
\sa QDateTime::currentDateTime(), QDateTime::curentDateTimeUtc()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-09-12 12:18:45 +00:00
|
|
|
static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format, bool *isMidnight24)
|
2012-06-18 13:53:30 +00:00
|
|
|
{
|
2013-07-18 09:37:57 +00:00
|
|
|
if (isMidnight24)
|
|
|
|
*isMidnight24 = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
const int size = string.size();
|
|
|
|
if (size < 5)
|
2012-06-18 13:53:30 +00:00
|
|
|
return QTime();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
bool ok = false;
|
|
|
|
int hour = string.mid(0, 2).toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
return QTime();
|
|
|
|
const int minute = string.mid(3, 2).toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
return QTime();
|
|
|
|
int second = 0;
|
|
|
|
int msec = 0;
|
|
|
|
|
|
|
|
if (size == 5) {
|
2015-01-03 17:06:38 +00:00
|
|
|
// HH:mm format
|
2013-07-18 09:37:57 +00:00
|
|
|
second = 0;
|
|
|
|
msec = 0;
|
|
|
|
} else if (string.at(5) == QLatin1Char(',') || string.at(5) == QLatin1Char('.')) {
|
|
|
|
if (format == Qt::TextDate)
|
2012-06-18 13:53:30 +00:00
|
|
|
return QTime();
|
2015-01-03 17:06:38 +00:00
|
|
|
// ISODate HH:mm.ssssss format
|
2013-07-18 09:37:57 +00:00
|
|
|
// We only want 5 digits worth of fraction of minute. This follows the existing
|
|
|
|
// behavior that determines how milliseconds are read; 4 millisecond digits are
|
|
|
|
// read and then rounded to 3. If we read at most 5 digits for fraction of minute,
|
|
|
|
// the maximum amount of millisecond digits it will expand to once converted to
|
|
|
|
// seconds is 4. E.g. 12:34,99999 will expand to 12:34:59.9994. The milliseconds
|
|
|
|
// will then be rounded up AND clamped to 999.
|
2013-09-12 12:18:45 +00:00
|
|
|
|
|
|
|
const QStringRef minuteFractionStr = string.mid(6, 5);
|
|
|
|
const long minuteFractionInt = minuteFractionStr.toLong(&ok);
|
2012-06-18 13:53:30 +00:00
|
|
|
if (!ok)
|
|
|
|
return QTime();
|
2014-01-17 16:27:26 +00:00
|
|
|
const float minuteFraction = double(minuteFractionInt) / (std::pow(double(10), minuteFractionStr.count()));
|
2013-09-12 12:18:45 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
const float secondWithMs = minuteFraction * 60;
|
|
|
|
const float secondNoMs = std::floor(secondWithMs);
|
|
|
|
const float secondFraction = secondWithMs - secondNoMs;
|
|
|
|
second = secondNoMs;
|
|
|
|
msec = qMin(qRound(secondFraction * 1000.0), 999);
|
|
|
|
} else {
|
2015-01-03 17:06:38 +00:00
|
|
|
// HH:mm:ss or HH:mm:ss.zzz
|
2013-07-18 09:37:57 +00:00
|
|
|
second = string.mid(6, 2).toInt(&ok);
|
2012-06-18 13:53:30 +00:00
|
|
|
if (!ok)
|
|
|
|
return QTime();
|
2013-07-18 09:37:57 +00:00
|
|
|
if (size > 8 && (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.'))) {
|
2013-09-12 12:18:45 +00:00
|
|
|
const QStringRef msecStr(string.mid(9, 4));
|
|
|
|
int msecInt = msecStr.isEmpty() ? 0 : msecStr.toInt(&ok);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!ok)
|
|
|
|
return QTime();
|
2014-01-17 16:27:26 +00:00
|
|
|
const double secondFraction(msecInt / (std::pow(double(10), msecStr.count())));
|
2013-07-18 09:37:57 +00:00
|
|
|
msec = qMin(qRound(secondFraction * 1000.0), 999);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2012-06-18 13:53:30 +00:00
|
|
|
}
|
2013-07-18 09:37:57 +00:00
|
|
|
|
2016-10-10 11:44:52 +00:00
|
|
|
const bool isISODate = format == Qt::ISODate || format == Qt::ISODateWithMs;
|
|
|
|
if (isISODate && hour == 24 && minute == 0 && second == 0 && msec == 0) {
|
2013-07-18 09:37:57 +00:00
|
|
|
if (isMidnight24)
|
|
|
|
*isMidnight24 = true;
|
|
|
|
hour = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2012-06-18 13:53:30 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
return QTime(hour, minute, second, msec);
|
|
|
|
}
|
2012-06-18 13:53:30 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QTime QTime::fromString(const QString &string, Qt::DateFormat format)
|
|
|
|
|
|
|
|
Returns the time represented in the \a string as a QTime using the
|
|
|
|
\a format given, or an invalid time if this is not possible.
|
|
|
|
|
|
|
|
Note that fromString() uses a "C" locale encoded string to convert
|
|
|
|
milliseconds to a float value. If the default locale is not "C",
|
|
|
|
this may result in two conversion attempts (if the conversion
|
|
|
|
fails for the default locale). This should be considered an
|
|
|
|
implementation detail.
|
2016-09-08 16:57:46 +00:00
|
|
|
|
|
|
|
\sa toString(), QLocale::toTime()
|
2012-06-18 13:53:30 +00:00
|
|
|
*/
|
2013-07-18 09:37:57 +00:00
|
|
|
QTime QTime::fromString(const QString& string, Qt::DateFormat format)
|
2012-06-18 13:53:30 +00:00
|
|
|
{
|
2013-07-18 09:37:57 +00:00
|
|
|
if (string.isEmpty())
|
|
|
|
return QTime();
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case Qt::SystemLocaleDate:
|
|
|
|
case Qt::SystemLocaleShortDate:
|
|
|
|
return QLocale::system().toTime(string, QLocale::ShortFormat);
|
|
|
|
case Qt::SystemLocaleLongDate:
|
|
|
|
return QLocale::system().toTime(string, QLocale::LongFormat);
|
|
|
|
case Qt::LocaleDate:
|
|
|
|
case Qt::DefaultLocaleShortDate:
|
|
|
|
return QLocale().toTime(string, QLocale::ShortFormat);
|
|
|
|
case Qt::DefaultLocaleLongDate:
|
|
|
|
return QLocale().toTime(string, QLocale::LongFormat);
|
2015-01-21 09:17:00 +00:00
|
|
|
case Qt::RFC2822Date:
|
|
|
|
return rfcDateImpl(string).time;
|
2013-07-18 09:37:57 +00:00
|
|
|
case Qt::ISODate:
|
2016-10-10 11:44:52 +00:00
|
|
|
case Qt::ISODateWithMs:
|
2013-07-18 09:37:57 +00:00
|
|
|
case Qt::TextDate:
|
|
|
|
default:
|
2017-03-12 21:43:04 +00:00
|
|
|
return fromIsoTimeString(QStringRef(&string), format, 0);
|
2013-07-18 09:37:57 +00:00
|
|
|
}
|
2012-06-18 13:53:30 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
2018-01-16 11:38:23 +00:00
|
|
|
\fn QTime QTime::fromString(const QString &string, const QString &format)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the QTime represented by the \a string, using the \a
|
|
|
|
format given, or an invalid time if the string cannot be parsed.
|
|
|
|
|
|
|
|
These expressions may be used for the format:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li h
|
|
|
|
\li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
|
|
|
|
\row \li hh
|
|
|
|
\li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
|
|
|
|
\row \li m \li the minute without a leading zero (0 to 59)
|
|
|
|
\row \li mm \li the minute with a leading zero (00 to 59)
|
2016-09-30 18:18:05 +00:00
|
|
|
\row \li s \li the whole second, without any leading zero (0 to 59)
|
|
|
|
\row \li ss \li the whole second, with a leading zero where applicable (00 to 59)
|
|
|
|
\row \li z \li the fractional part of the second, to go after a decimal
|
|
|
|
point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
|
|
|
|
reports the seconds to full available (millisecond) precision
|
|
|
|
without trailing zeroes.
|
|
|
|
\row \li zzz \li the fractional part of the second, to millisecond
|
|
|
|
precision, including trailing zeroes where applicable (000 to 999).
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li AP
|
|
|
|
\li interpret as an AM/PM time. \e AP must be either "AM" or "PM".
|
|
|
|
\row \li ap
|
|
|
|
\li Interpret as an AM/PM time. \e ap must be either "am" or "pm".
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
All other input characters will be treated as text. Any sequence
|
|
|
|
of characters that are enclosed in single quotes will also be
|
|
|
|
treated as text and not be used as an expression.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 6
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
If the format is not satisfied, an invalid QTime is returned.
|
2011-04-27 10:05:43 +00:00
|
|
|
Expressions that do not expect leading zeroes to be given (h, m, s
|
|
|
|
and z) are greedy. This means that they will use two digits even if
|
|
|
|
this puts them outside the range of accepted values and leaves too
|
|
|
|
few digits for other sections. For example, the following string
|
|
|
|
could have meant 00:07:10, but the m will grab two digits, resulting
|
|
|
|
in an invalid time:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 7
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Any field that is not represented in the format will be set to zero.
|
|
|
|
For example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 8
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa toString(), QDateTime::fromString(), QDate::fromString(),
|
|
|
|
QLocale::toTime()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QTime QTime::fromString(const QString &string, const QString &format)
|
|
|
|
{
|
|
|
|
QTime time;
|
2017-09-11 16:14:51 +00:00
|
|
|
#if QT_CONFIG(datetimeparser)
|
2011-04-27 10:05:43 +00:00
|
|
|
QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString);
|
2016-09-08 17:24:04 +00:00
|
|
|
// dt.setDefaultLocale(QLocale::c()); ### Qt 6
|
2011-04-27 10:05:43 +00:00
|
|
|
if (dt.parseFormat(format))
|
|
|
|
dt.fromString(string, 0, &time);
|
|
|
|
#else
|
|
|
|
Q_UNUSED(string);
|
|
|
|
Q_UNUSED(format);
|
|
|
|
#endif
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\overload
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the specified time is valid; otherwise returns
|
2011-04-27 10:05:43 +00:00
|
|
|
false.
|
|
|
|
|
|
|
|
The time is valid if \a h is in the range 0 to 23, \a m and
|
|
|
|
\a s are in the range 0 to 59, and \a ms is in the range 0 to 999.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 9
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool QTime::isValid(int h, int m, int s, int ms)
|
|
|
|
{
|
|
|
|
return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets this time to the current time. This is practical for timing:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 10
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa restart(), elapsed(), currentTime()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QTime::start()
|
|
|
|
{
|
|
|
|
*this = currentTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets this time to the current time and returns the number of
|
|
|
|
milliseconds that have elapsed since the last time start() or
|
|
|
|
restart() was called.
|
|
|
|
|
|
|
|
This function is guaranteed to be atomic and is thus very handy
|
|
|
|
for repeated measurements. Call start() to start the first
|
|
|
|
measurement, and restart() for each later measurement.
|
|
|
|
|
|
|
|
Note that the counter wraps to zero 24 hours after the last call
|
|
|
|
to start() or restart().
|
|
|
|
|
|
|
|
\warning If the system's clock setting has been changed since the
|
|
|
|
last time start() or restart() was called, the result is
|
2015-11-09 11:05:49 +00:00
|
|
|
undefined. This can happen when daylight-saving time is turned on
|
2011-04-27 10:05:43 +00:00
|
|
|
or off.
|
|
|
|
|
|
|
|
\sa start(), elapsed(), currentTime()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::restart()
|
|
|
|
{
|
|
|
|
QTime t = currentTime();
|
|
|
|
int n = msecsTo(t);
|
|
|
|
if (n < 0) // passed midnight
|
|
|
|
n += 86400*1000;
|
|
|
|
*this = t;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of milliseconds that have elapsed since the
|
|
|
|
last time start() or restart() was called.
|
|
|
|
|
|
|
|
Note that the counter wraps to zero 24 hours after the last call
|
|
|
|
to start() or restart.
|
|
|
|
|
|
|
|
Note that the accuracy depends on the accuracy of the underlying
|
|
|
|
operating system; not all systems provide 1-millisecond accuracy.
|
|
|
|
|
|
|
|
\warning If the system's clock setting has been changed since the
|
|
|
|
last time start() or restart() was called, the result is
|
2015-11-09 11:05:49 +00:00
|
|
|
undefined. This can happen when daylight-saving time is turned on
|
2011-04-27 10:05:43 +00:00
|
|
|
or off.
|
|
|
|
|
|
|
|
\sa start(), restart()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QTime::elapsed() const
|
|
|
|
{
|
|
|
|
int n = msecsTo(currentTime());
|
|
|
|
if (n < 0) // passed midnight
|
|
|
|
n += 86400 * 1000;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-08-12 19:21:42 +00:00
|
|
|
/*****************************************************************************
|
2013-08-14 20:54:55 +00:00
|
|
|
QDateTime static helper functions
|
2013-08-12 19:21:42 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
// get the types from QDateTime (through QDateTimePrivate)
|
|
|
|
typedef QDateTimePrivate::QDateTimeShortData ShortData;
|
|
|
|
typedef QDateTimePrivate::QDateTimeData QDateTimeData;
|
|
|
|
|
2013-08-12 19:21:42 +00:00
|
|
|
// Calls the platform variant of tzset
|
|
|
|
static void qt_tzset()
|
|
|
|
{
|
2016-03-23 09:25:46 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2013-08-12 19:21:42 +00:00
|
|
|
_tzset();
|
|
|
|
#else
|
|
|
|
tzset();
|
|
|
|
#endif // Q_OS_WIN
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the platform variant of timezone, i.e. the standard time offset
|
|
|
|
// The timezone external variable is documented as always holding the
|
|
|
|
// Standard Time offset as seconds west of Greenwich, i.e. UTC+01:00 is -3600
|
|
|
|
// Note this may not be historicaly accurate.
|
|
|
|
// Relies on tzset, mktime, or localtime having been called to populate timezone
|
|
|
|
static int qt_timezone()
|
|
|
|
{
|
2016-03-23 09:25:46 +00:00
|
|
|
#if defined(_MSC_VER)
|
2013-08-12 19:21:42 +00:00
|
|
|
long offset;
|
|
|
|
_get_timezone(&offset);
|
|
|
|
return offset;
|
2014-01-17 15:16:08 +00:00
|
|
|
#elif defined(Q_OS_BSD4) && !defined(Q_OS_DARWIN)
|
|
|
|
time_t clock = time(NULL);
|
|
|
|
struct tm t;
|
|
|
|
localtime_r(&clock, &t);
|
2014-02-02 21:42:12 +00:00
|
|
|
// QTBUG-36080 Workaround for systems without the POSIX timezone
|
|
|
|
// variable. This solution is not very efficient but fixing it is up to
|
|
|
|
// the libc implementations.
|
|
|
|
//
|
|
|
|
// tm_gmtoff has some important differences compared to the timezone
|
|
|
|
// variable:
|
|
|
|
// - It returns the number of seconds east of UTC, and we want the
|
|
|
|
// number of seconds west of UTC.
|
|
|
|
// - It also takes DST into account, so we need to adjust it to always
|
|
|
|
// get the Standard Time offset.
|
2016-05-22 11:59:27 +00:00
|
|
|
return -t.tm_gmtoff + (t.tm_isdst ? (long)SECS_PER_HOUR : 0L);
|
2015-10-27 01:24:00 +00:00
|
|
|
#elif defined(Q_OS_INTEGRITY)
|
|
|
|
return 0;
|
2013-08-12 19:21:42 +00:00
|
|
|
#else
|
|
|
|
return timezone;
|
|
|
|
#endif // Q_OS_WIN
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:54:55 +00:00
|
|
|
// Returns the tzname, assume tzset has been called already
|
|
|
|
static QString qt_tzname(QDateTimePrivate::DaylightStatus daylightStatus)
|
|
|
|
{
|
|
|
|
int isDst = (daylightStatus == QDateTimePrivate::DaylightTime) ? 1 : 0;
|
2018-01-04 13:33:06 +00:00
|
|
|
#if defined(Q_CC_MSVC)
|
2013-08-14 20:54:55 +00:00
|
|
|
size_t s = 0;
|
|
|
|
char name[512];
|
|
|
|
if (_get_tzname(&s, name, 512, isDst))
|
|
|
|
return QString();
|
|
|
|
return QString::fromLocal8Bit(name);
|
|
|
|
#else
|
|
|
|
return QString::fromLocal8Bit(tzname[isDst]);
|
|
|
|
#endif // Q_OS_WIN
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:14:51 +00:00
|
|
|
#if QT_CONFIG(datetimeparser) && QT_CONFIG(timezone)
|
2016-12-15 09:57:17 +00:00
|
|
|
/*
|
|
|
|
\internal
|
|
|
|
Implemented here to share qt_tzname()
|
|
|
|
*/
|
|
|
|
int QDateTimeParser::startsWithLocalTimeZone(const QStringRef name)
|
|
|
|
{
|
|
|
|
QDateTimePrivate::DaylightStatus zones[2] = {
|
|
|
|
QDateTimePrivate::StandardTime,
|
|
|
|
QDateTimePrivate::DaylightTime
|
|
|
|
};
|
|
|
|
for (const auto z : zones) {
|
|
|
|
QString zone(qt_tzname(z));
|
|
|
|
if (name.startsWith(zone))
|
|
|
|
return zone.size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-09-11 16:14:51 +00:00
|
|
|
#endif // datetimeparser && timezone
|
2016-12-15 09:57:17 +00:00
|
|
|
|
2013-09-27 12:15:31 +00:00
|
|
|
// Calls the platform variant of mktime for the given date, time and daylightStatus,
|
2013-08-14 20:54:55 +00:00
|
|
|
// and updates the date, time, daylightStatus and abbreviation with the returned values
|
|
|
|
// If the date falls outside the 1970 to 2037 range supported by mktime / time_t
|
|
|
|
// then null date/time will be returned, you should adjust the date first if
|
|
|
|
// you need a guaranteed result.
|
|
|
|
static qint64 qt_mktime(QDate *date, QTime *time, QDateTimePrivate::DaylightStatus *daylightStatus,
|
2015-02-21 20:20:00 +00:00
|
|
|
QString *abbreviation, bool *ok = 0)
|
2013-08-14 20:54:55 +00:00
|
|
|
{
|
|
|
|
const qint64 msec = time->msec();
|
|
|
|
int yy, mm, dd;
|
|
|
|
date->getDate(&yy, &mm, &dd);
|
|
|
|
|
|
|
|
// All other platforms provide standard C library time functions
|
|
|
|
tm local;
|
2015-11-03 16:14:56 +00:00
|
|
|
memset(&local, 0, sizeof(local)); // tm_[wy]day plus any non-standard fields
|
2013-08-14 20:54:55 +00:00
|
|
|
local.tm_sec = time->second();
|
|
|
|
local.tm_min = time->minute();
|
|
|
|
local.tm_hour = time->hour();
|
|
|
|
local.tm_mday = dd;
|
|
|
|
local.tm_mon = mm - 1;
|
|
|
|
local.tm_year = yy - 1900;
|
2013-09-27 12:15:31 +00:00
|
|
|
if (daylightStatus)
|
|
|
|
local.tm_isdst = int(*daylightStatus);
|
|
|
|
else
|
|
|
|
local.tm_isdst = -1;
|
2015-11-03 16:14:56 +00:00
|
|
|
|
2013-08-14 20:54:55 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
int hh = local.tm_hour;
|
|
|
|
#endif // Q_OS_WIN
|
|
|
|
time_t secsSinceEpoch = mktime(&local);
|
|
|
|
if (secsSinceEpoch != time_t(-1)) {
|
|
|
|
*date = QDate(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
|
|
|
|
*time = QTime(local.tm_hour, local.tm_min, local.tm_sec, msec);
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
// Windows mktime for the missing hour subtracts 1 hour from the time
|
|
|
|
// instead of adding 1 hour. If time differs and is standard time then
|
|
|
|
// this has happened, so add 2 hours to the time and 1 hour to the msecs
|
|
|
|
if (local.tm_isdst == 0 && local.tm_hour != hh) {
|
|
|
|
if (time->hour() >= 22)
|
|
|
|
*date = date->addDays(1);
|
|
|
|
*time = time->addSecs(2 * SECS_PER_HOUR);
|
|
|
|
secsSinceEpoch += SECS_PER_HOUR;
|
|
|
|
local.tm_isdst = 1;
|
|
|
|
}
|
|
|
|
#endif // Q_OS_WIN
|
|
|
|
if (local.tm_isdst >= 1) {
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::DaylightTime;
|
|
|
|
if (abbreviation)
|
|
|
|
*abbreviation = qt_tzname(QDateTimePrivate::DaylightTime);
|
|
|
|
} else if (local.tm_isdst == 0) {
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::StandardTime;
|
|
|
|
if (abbreviation)
|
|
|
|
*abbreviation = qt_tzname(QDateTimePrivate::StandardTime);
|
|
|
|
} else {
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::UnknownDaylightTime;
|
|
|
|
if (abbreviation)
|
|
|
|
*abbreviation = qt_tzname(QDateTimePrivate::StandardTime);
|
|
|
|
}
|
|
|
|
if (ok)
|
|
|
|
*ok = true;
|
|
|
|
} else {
|
|
|
|
*date = QDate();
|
|
|
|
*time = QTime();
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::UnknownDaylightTime;
|
|
|
|
if (abbreviation)
|
|
|
|
*abbreviation = QString();
|
|
|
|
if (ok)
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((qint64)secsSinceEpoch * 1000) + msec;
|
|
|
|
}
|
|
|
|
|
2013-08-12 19:21:42 +00:00
|
|
|
// Calls the platform variant of localtime for the given msecs, and updates
|
2015-11-09 11:05:49 +00:00
|
|
|
// the date, time, and DST status with the returned values.
|
2013-08-12 19:21:42 +00:00
|
|
|
static bool qt_localtime(qint64 msecsSinceEpoch, QDate *localDate, QTime *localTime,
|
|
|
|
QDateTimePrivate::DaylightStatus *daylightStatus)
|
|
|
|
{
|
|
|
|
const time_t secsSinceEpoch = msecsSinceEpoch / 1000;
|
|
|
|
const int msec = msecsSinceEpoch % 1000;
|
|
|
|
|
|
|
|
tm local;
|
|
|
|
bool valid = false;
|
|
|
|
|
2014-05-16 13:11:39 +00:00
|
|
|
// localtime() is required to work as if tzset() was called before it.
|
|
|
|
// localtime_r() does not have this requirement, so make an explicit call.
|
2017-04-10 15:37:08 +00:00
|
|
|
// The explicit call should also request the timezone info be re-parsed.
|
2014-05-16 13:11:39 +00:00
|
|
|
qt_tzset();
|
2016-12-29 16:11:24 +00:00
|
|
|
#if QT_CONFIG(thread) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
|
2013-08-12 19:21:42 +00:00
|
|
|
// Use the reentrant version of localtime() where available
|
|
|
|
// as is thread-safe and doesn't use a shared static data area
|
|
|
|
tm *res = 0;
|
|
|
|
res = localtime_r(&secsSinceEpoch, &local);
|
|
|
|
if (res)
|
|
|
|
valid = true;
|
2018-01-04 13:33:06 +00:00
|
|
|
#elif defined(Q_CC_MSVC)
|
2013-08-12 19:21:42 +00:00
|
|
|
if (!_localtime64_s(&local, &secsSinceEpoch))
|
|
|
|
valid = true;
|
|
|
|
#else
|
|
|
|
// Returns shared static data which may be overwritten at any time
|
|
|
|
// So copy the result asap
|
|
|
|
tm *res = 0;
|
|
|
|
res = localtime(&secsSinceEpoch);
|
|
|
|
if (res) {
|
|
|
|
local = *res;
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (valid) {
|
|
|
|
*localDate = QDate(local.tm_year + 1900, local.tm_mon + 1, local.tm_mday);
|
|
|
|
*localTime = QTime(local.tm_hour, local.tm_min, local.tm_sec, msec);
|
|
|
|
if (daylightStatus) {
|
|
|
|
if (local.tm_isdst > 0)
|
|
|
|
*daylightStatus = QDateTimePrivate::DaylightTime;
|
|
|
|
else if (local.tm_isdst < 0)
|
|
|
|
*daylightStatus = QDateTimePrivate::UnknownDaylightTime;
|
|
|
|
else
|
|
|
|
*daylightStatus = QDateTimePrivate::StandardTime;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
*localDate = QDate();
|
|
|
|
*localTime = QTime();
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::UnknownDaylightTime;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts an msecs value into a date and time
|
|
|
|
static void msecsToTime(qint64 msecs, QDate *date, QTime *time)
|
|
|
|
{
|
|
|
|
qint64 jd = JULIAN_DAY_FOR_EPOCH;
|
|
|
|
qint64 ds = 0;
|
|
|
|
|
|
|
|
if (qAbs(msecs) >= MSECS_PER_DAY) {
|
|
|
|
jd += (msecs / MSECS_PER_DAY);
|
|
|
|
msecs %= MSECS_PER_DAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msecs < 0) {
|
|
|
|
ds = MSECS_PER_DAY - msecs - 1;
|
|
|
|
jd -= ds / MSECS_PER_DAY;
|
|
|
|
ds = ds % MSECS_PER_DAY;
|
|
|
|
ds = MSECS_PER_DAY - ds - 1;
|
|
|
|
} else {
|
|
|
|
ds = msecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (date)
|
|
|
|
*date = QDate::fromJulianDay(jd);
|
|
|
|
if (time)
|
|
|
|
*time = QTime::fromMSecsSinceStartOfDay(ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a date/time value into msecs
|
|
|
|
static qint64 timeToMSecs(const QDate &date, const QTime &time)
|
|
|
|
{
|
|
|
|
return ((date.toJulianDay() - JULIAN_DAY_FOR_EPOCH) * MSECS_PER_DAY)
|
|
|
|
+ time.msecsSinceStartOfDay();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an MSecs Since Epoch into Local Time
|
|
|
|
static bool epochMSecsToLocalTime(qint64 msecs, QDate *localDate, QTime *localTime,
|
|
|
|
QDateTimePrivate::DaylightStatus *daylightStatus = 0)
|
|
|
|
{
|
|
|
|
if (msecs < 0) {
|
|
|
|
// Docs state any LocalTime before 1970-01-01 will *not* have any Daylight Time applied
|
|
|
|
// Instead just use the standard offset from UTC to convert to UTC time
|
|
|
|
qt_tzset();
|
|
|
|
msecsToTime(msecs - qt_timezone() * 1000, localDate, localTime);
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::StandardTime;
|
|
|
|
return true;
|
|
|
|
} else if (msecs > (qint64(TIME_T_MAX) * 1000)) {
|
2015-11-09 11:05:49 +00:00
|
|
|
// Docs state any LocalTime after 2037-12-31 *will* have any DST applied
|
2013-08-12 19:21:42 +00:00
|
|
|
// but this may fall outside the supported time_t range, so need to fake it.
|
|
|
|
// Use existing method to fake the conversion, but this is deeply flawed as it may
|
|
|
|
// apply the conversion from the wrong day number, e.g. if rule is last Sunday of month
|
|
|
|
// TODO Use QTimeZone when available to apply the future rule correctly
|
|
|
|
QDate utcDate;
|
|
|
|
QTime utcTime;
|
|
|
|
msecsToTime(msecs, &utcDate, &utcTime);
|
|
|
|
int year, month, day;
|
|
|
|
utcDate.getDate(&year, &month, &day);
|
|
|
|
// 2037 is not a leap year, so make sure date isn't Feb 29
|
|
|
|
if (month == 2 && day == 29)
|
|
|
|
--day;
|
|
|
|
QDate fakeDate(2037, month, day);
|
|
|
|
qint64 fakeMsecs = QDateTime(fakeDate, utcTime, Qt::UTC).toMSecsSinceEpoch();
|
|
|
|
bool res = qt_localtime(fakeMsecs, localDate, localTime, daylightStatus);
|
|
|
|
*localDate = localDate->addDays(fakeDate.daysTo(utcDate));
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
// Falls inside time_t suported range so can use localtime
|
|
|
|
return qt_localtime(msecs, localDate, localTime, daylightStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 02:17:25 +00:00
|
|
|
// Convert a LocalTime expressed in local msecs encoding and the corresponding
|
2015-11-09 11:05:49 +00:00
|
|
|
// DST status into a UTC epoch msecs. Optionally populate the returned
|
2015-03-11 02:17:25 +00:00
|
|
|
// values from mktime for the adjusted local date and time.
|
|
|
|
static qint64 localMSecsToEpochMSecs(qint64 localMsecs,
|
|
|
|
QDateTimePrivate::DaylightStatus *daylightStatus,
|
|
|
|
QDate *localDate = 0, QTime *localTime = 0,
|
2015-02-21 20:20:00 +00:00
|
|
|
QString *abbreviation = 0)
|
2013-08-12 19:21:42 +00:00
|
|
|
{
|
|
|
|
QDate dt;
|
|
|
|
QTime tm;
|
|
|
|
msecsToTime(localMsecs, &dt, &tm);
|
|
|
|
|
2015-11-03 16:16:45 +00:00
|
|
|
const qint64 msecsMax = qint64(TIME_T_MAX) * 1000;
|
2013-08-12 19:21:42 +00:00
|
|
|
|
|
|
|
if (localMsecs <= qint64(MSECS_PER_DAY)) {
|
|
|
|
|
2015-11-09 11:05:49 +00:00
|
|
|
// Docs state any LocalTime before 1970-01-01 will *not* have any DST applied
|
2013-08-12 19:21:42 +00:00
|
|
|
|
|
|
|
// First, if localMsecs is within +/- 1 day of minimum time_t try mktime in case it does
|
2015-11-09 11:05:49 +00:00
|
|
|
// fall after minimum and needs proper DST conversion
|
2013-08-12 19:21:42 +00:00
|
|
|
if (localMsecs >= -qint64(MSECS_PER_DAY)) {
|
|
|
|
bool valid;
|
|
|
|
qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation, &valid);
|
|
|
|
if (valid && utcMsecs >= 0) {
|
|
|
|
// mktime worked and falls in valid range, so use it
|
|
|
|
if (localDate)
|
|
|
|
*localDate = dt;
|
|
|
|
if (localTime)
|
|
|
|
*localTime = tm;
|
|
|
|
return utcMsecs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we don't call mktime then need to call tzset to get offset
|
|
|
|
qt_tzset();
|
|
|
|
}
|
|
|
|
// Time is clearly before 1970-01-01 so just use standard offset to convert
|
|
|
|
qint64 utcMsecs = localMsecs + qt_timezone() * 1000;
|
|
|
|
if (localDate || localTime)
|
|
|
|
msecsToTime(localMsecs, localDate, localTime);
|
|
|
|
if (daylightStatus)
|
|
|
|
*daylightStatus = QDateTimePrivate::StandardTime;
|
|
|
|
if (abbreviation)
|
|
|
|
*abbreviation = qt_tzname(QDateTimePrivate::StandardTime);
|
|
|
|
return utcMsecs;
|
|
|
|
|
|
|
|
} else if (localMsecs >= msecsMax - MSECS_PER_DAY) {
|
|
|
|
|
2015-11-09 11:05:49 +00:00
|
|
|
// Docs state any LocalTime after 2037-12-31 *will* have any DST applied
|
2013-08-12 19:21:42 +00:00
|
|
|
// but this may fall outside the supported time_t range, so need to fake it.
|
|
|
|
|
|
|
|
// First, if localMsecs is within +/- 1 day of maximum time_t try mktime in case it does
|
2015-11-09 11:05:49 +00:00
|
|
|
// fall before maximum and can use proper DST conversion
|
2013-08-12 19:21:42 +00:00
|
|
|
if (localMsecs <= msecsMax + MSECS_PER_DAY) {
|
|
|
|
bool valid;
|
|
|
|
qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation, &valid);
|
|
|
|
if (valid && utcMsecs <= msecsMax) {
|
|
|
|
// mktime worked and falls in valid range, so use it
|
|
|
|
if (localDate)
|
|
|
|
*localDate = dt;
|
|
|
|
if (localTime)
|
|
|
|
*localTime = tm;
|
|
|
|
return utcMsecs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Use existing method to fake the conversion, but this is deeply flawed as it may
|
|
|
|
// apply the conversion from the wrong day number, e.g. if rule is last Sunday of month
|
|
|
|
// TODO Use QTimeZone when available to apply the future rule correctly
|
|
|
|
int year, month, day;
|
|
|
|
dt.getDate(&year, &month, &day);
|
|
|
|
// 2037 is not a leap year, so make sure date isn't Feb 29
|
|
|
|
if (month == 2 && day == 29)
|
|
|
|
--day;
|
|
|
|
QDate fakeDate(2037, month, day);
|
|
|
|
qint64 fakeDiff = fakeDate.daysTo(dt);
|
2015-02-21 20:20:00 +00:00
|
|
|
qint64 utcMsecs = qt_mktime(&fakeDate, &tm, daylightStatus, abbreviation);
|
2013-08-12 19:21:42 +00:00
|
|
|
if (localDate)
|
|
|
|
*localDate = fakeDate.addDays(fakeDiff);
|
|
|
|
if (localTime)
|
|
|
|
*localTime = tm;
|
|
|
|
QDate utcDate;
|
|
|
|
QTime utcTime;
|
|
|
|
msecsToTime(utcMsecs, &utcDate, &utcTime);
|
|
|
|
utcDate = utcDate.addDays(fakeDiff);
|
|
|
|
utcMsecs = timeToMSecs(utcDate, utcTime);
|
|
|
|
return utcMsecs;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Clearly falls inside 1970-2037 suported range so can use mktime
|
2015-02-21 20:20:00 +00:00
|
|
|
qint64 utcMsecs = qt_mktime(&dt, &tm, daylightStatus, abbreviation);
|
2013-08-12 19:21:42 +00:00
|
|
|
if (localDate)
|
|
|
|
*localDate = dt;
|
|
|
|
if (localTime)
|
|
|
|
*localTime = tm;
|
|
|
|
return utcMsecs;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 02:15:23 +00:00
|
|
|
static inline bool specCanBeSmall(Qt::TimeSpec spec)
|
|
|
|
{
|
|
|
|
return spec == Qt::LocalTime || spec == Qt::UTC;
|
|
|
|
}
|
|
|
|
|
2016-06-23 05:00:08 +00:00
|
|
|
static inline bool msecsCanBeSmall(qint64 msecs)
|
|
|
|
{
|
|
|
|
if (!QDateTimeData::CanBeSmall)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ShortData sd;
|
|
|
|
sd.msecs = qintptr(msecs);
|
|
|
|
return sd.msecs == msecs;
|
|
|
|
}
|
|
|
|
|
2016-05-12 06:14:46 +00:00
|
|
|
static Q_DECL_CONSTEXPR inline
|
|
|
|
QDateTimePrivate::StatusFlags mergeSpec(QDateTimePrivate::StatusFlags status, Qt::TimeSpec spec)
|
|
|
|
{
|
|
|
|
return QDateTimePrivate::StatusFlags((status & ~QDateTimePrivate::TimeSpecMask) |
|
|
|
|
(int(spec) << QDateTimePrivate::TimeSpecShift));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Q_DECL_CONSTEXPR inline Qt::TimeSpec extractSpec(QDateTimePrivate::StatusFlags status)
|
|
|
|
{
|
|
|
|
return Qt::TimeSpec((status & QDateTimePrivate::TimeSpecMask) >> QDateTimePrivate::TimeSpecShift);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the Daylight Status if LocalTime set via msecs
|
|
|
|
static Q_DECL_RELAXED_CONSTEXPR inline QDateTimePrivate::StatusFlags
|
|
|
|
mergeDaylightStatus(QDateTimePrivate::StatusFlags sf, QDateTimePrivate::DaylightStatus status)
|
|
|
|
{
|
|
|
|
sf &= ~QDateTimePrivate::DaylightMask;
|
|
|
|
if (status == QDateTimePrivate::DaylightTime) {
|
|
|
|
sf |= QDateTimePrivate::SetToDaylightTime;
|
|
|
|
} else if (status == QDateTimePrivate::StandardTime) {
|
|
|
|
sf |= QDateTimePrivate::SetToStandardTime;
|
|
|
|
}
|
|
|
|
return sf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the DST Status if LocalTime set via msecs
|
|
|
|
static Q_DECL_RELAXED_CONSTEXPR inline
|
|
|
|
QDateTimePrivate::DaylightStatus extractDaylightStatus(QDateTimePrivate::StatusFlags status)
|
|
|
|
{
|
|
|
|
if (status & QDateTimePrivate::SetToDaylightTime)
|
|
|
|
return QDateTimePrivate::DaylightTime;
|
|
|
|
if (status & QDateTimePrivate::SetToStandardTime)
|
|
|
|
return QDateTimePrivate::StandardTime;
|
|
|
|
return QDateTimePrivate::UnknownDaylightTime;
|
|
|
|
}
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static inline qint64 getMSecs(const QDateTimeData &d)
|
2016-05-12 23:10:54 +00:00
|
|
|
{
|
2016-05-01 20:20:52 +00:00
|
|
|
if (d.isShort()) {
|
|
|
|
// same as, but producing better code
|
|
|
|
//return d.data.msecs;
|
|
|
|
return qintptr(d.d) >> 8;
|
|
|
|
}
|
2016-05-12 23:10:54 +00:00
|
|
|
return d->m_msecs;
|
|
|
|
}
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static inline QDateTimePrivate::StatusFlags getStatus(const QDateTimeData &d)
|
2016-05-12 06:14:46 +00:00
|
|
|
{
|
2016-05-01 20:20:52 +00:00
|
|
|
if (d.isShort()) {
|
|
|
|
// same as, but producing better code
|
|
|
|
//return StatusFlag(d.data.status);
|
|
|
|
return QDateTimePrivate::StatusFlag(qintptr(d.d) & 0xFF);
|
|
|
|
}
|
2016-05-12 06:14:46 +00:00
|
|
|
return d->m_status;
|
|
|
|
}
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static inline Qt::TimeSpec getSpec(const QDateTimeData &d)
|
2016-05-12 06:14:46 +00:00
|
|
|
{
|
|
|
|
return extractSpec(getStatus(d));
|
|
|
|
}
|
|
|
|
|
2016-11-21 17:17:07 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
|
|
|
void QDateTimePrivate::setUtcOffsetByTZ(qint64 atMSecsSinceEpoch)
|
|
|
|
{
|
|
|
|
m_offsetFromUtc = m_timeZone.d->offsetFromUtc(atMSecsSinceEpoch);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
// Refresh the LocalTime validity and offset
|
|
|
|
static void refreshDateTime(QDateTimeData &d)
|
2016-05-01 20:20:52 +00:00
|
|
|
{
|
2016-05-13 20:35:04 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
const auto spec = extractSpec(status);
|
|
|
|
const qint64 msecs = getMSecs(d);
|
|
|
|
qint64 epochMSecs = 0;
|
|
|
|
int offsetFromUtc = 0;
|
|
|
|
QDate testDate;
|
|
|
|
QTime testTime;
|
|
|
|
Q_ASSERT(spec == Qt::TimeZone || spec == Qt::LocalTime);
|
2016-05-01 20:20:52 +00:00
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2016-05-13 20:35:04 +00:00
|
|
|
// If not valid time zone then is invalid
|
|
|
|
if (spec == Qt::TimeZone) {
|
2016-11-21 17:17:07 +00:00
|
|
|
if (!d->m_timeZone.isValid()) {
|
2016-05-13 20:35:04 +00:00
|
|
|
status &= ~QDateTimePrivate::ValidDateTime;
|
2016-11-21 17:17:07 +00:00
|
|
|
} else {
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
epochMSecs = QDateTimePrivate::zoneMSecsToEpochMSecs(msecs, d->m_timeZone, extractDaylightStatus(status), &testDate, &testTime);
|
2016-11-21 17:17:07 +00:00
|
|
|
d->setUtcOffsetByTZ(epochMSecs);
|
|
|
|
}
|
2016-05-13 20:35:04 +00:00
|
|
|
}
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2016-05-01 20:20:52 +00:00
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
// If not valid date and time then is invalid
|
|
|
|
if (!(status & QDateTimePrivate::ValidDate) || !(status & QDateTimePrivate::ValidTime)) {
|
|
|
|
status &= ~QDateTimePrivate::ValidDateTime;
|
|
|
|
if (status & QDateTimePrivate::ShortData) {
|
|
|
|
d.data.status = status;
|
|
|
|
} else {
|
|
|
|
d->m_status = status;
|
|
|
|
d->m_offsetFromUtc = 0;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-05-01 20:20:52 +00:00
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
// We have a valid date and time and a Qt::LocalTime or Qt::TimeZone that needs calculating
|
|
|
|
// LocalTime and TimeZone might fall into a "missing" DST transition hour
|
|
|
|
// Calling toEpochMSecs will adjust the returned date/time if it does
|
|
|
|
if (spec == Qt::LocalTime) {
|
|
|
|
auto dstStatus = extractDaylightStatus(status);
|
|
|
|
epochMSecs = localMSecsToEpochMSecs(msecs, &dstStatus, &testDate, &testTime);
|
|
|
|
}
|
|
|
|
if (timeToMSecs(testDate, testTime) == msecs) {
|
|
|
|
status |= QDateTimePrivate::ValidDateTime;
|
|
|
|
// Cache the offset to use in offsetFromUtc()
|
|
|
|
offsetFromUtc = (msecs - epochMSecs) / 1000;
|
2016-05-01 20:20:52 +00:00
|
|
|
} else {
|
2016-05-13 20:35:04 +00:00
|
|
|
status &= ~QDateTimePrivate::ValidDateTime;
|
2016-05-01 20:20:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
if (status & QDateTimePrivate::ShortData) {
|
|
|
|
d.data.status = status;
|
|
|
|
} else {
|
|
|
|
d->m_status = status;
|
|
|
|
d->m_offsetFromUtc = offsetFromUtc;
|
|
|
|
}
|
2013-08-14 20:54:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
// Check the UTC / offsetFromUTC validity
|
|
|
|
static void checkValidDateTime(QDateTimeData &d)
|
2013-02-11 13:44:37 +00:00
|
|
|
{
|
2016-05-13 20:35:04 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
auto spec = extractSpec(status);
|
|
|
|
switch (spec) {
|
|
|
|
case Qt::OffsetFromUTC:
|
|
|
|
case Qt::UTC:
|
|
|
|
// for these, a valid date and a valid time imply a valid QDateTime
|
|
|
|
if ((status & QDateTimePrivate::ValidDate) && (status & QDateTimePrivate::ValidTime))
|
|
|
|
status |= QDateTimePrivate::ValidDateTime;
|
|
|
|
else
|
|
|
|
status &= ~QDateTimePrivate::ValidDateTime;
|
|
|
|
if (status & QDateTimePrivate::ShortData)
|
|
|
|
d.data.status = status;
|
|
|
|
else
|
|
|
|
d->m_status = status;
|
|
|
|
break;
|
|
|
|
case Qt::TimeZone:
|
|
|
|
case Qt::LocalTime:
|
|
|
|
// for these, we need to check whether the timezone is valid and whether
|
|
|
|
// the time is valid in that timezone. Expensive, but no other option.
|
|
|
|
refreshDateTime(d);
|
|
|
|
break;
|
|
|
|
}
|
2013-02-11 13:44:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static void setTimeSpec(QDateTimeData &d, Qt::TimeSpec spec, int offsetSeconds)
|
2013-08-12 12:20:56 +00:00
|
|
|
{
|
2016-05-12 23:10:54 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
status &= ~(QDateTimePrivate::ValidDateTime | QDateTimePrivate::DaylightMask |
|
|
|
|
QDateTimePrivate::TimeSpecMask);
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2013-08-12 12:20:56 +00:00
|
|
|
switch (spec) {
|
|
|
|
case Qt::OffsetFromUTC:
|
2016-05-12 22:45:02 +00:00
|
|
|
if (offsetSeconds == 0)
|
|
|
|
spec = Qt::UTC;
|
2013-08-12 12:20:56 +00:00
|
|
|
break;
|
2013-02-11 13:44:37 +00:00
|
|
|
case Qt::TimeZone:
|
|
|
|
// Use system time zone instead
|
2016-05-12 22:45:02 +00:00
|
|
|
spec = Qt::LocalTime;
|
2016-06-27 13:43:33 +00:00
|
|
|
Q_FALLTHROUGH();
|
2013-08-12 12:20:56 +00:00
|
|
|
case Qt::UTC:
|
|
|
|
case Qt::LocalTime:
|
2016-05-12 22:45:02 +00:00
|
|
|
offsetSeconds = 0;
|
2013-08-12 12:20:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-12 22:45:02 +00:00
|
|
|
|
2016-05-12 06:14:46 +00:00
|
|
|
status = mergeSpec(status, spec);
|
2016-05-01 20:20:52 +00:00
|
|
|
if (d.isShort() && offsetSeconds == 0) {
|
|
|
|
d.data.status = status;
|
|
|
|
} else {
|
|
|
|
d.detach();
|
|
|
|
d->m_status = status & ~QDateTimePrivate::ShortData;
|
|
|
|
d->m_offsetFromUtc = offsetSeconds;
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2016-05-01 20:20:52 +00:00
|
|
|
d->m_timeZone = QTimeZone();
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2016-05-01 20:20:52 +00:00
|
|
|
}
|
2013-08-12 12:20:56 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static void setDateTime(QDateTimeData &d, const QDate &date, const QTime &time)
|
2013-08-12 19:21:42 +00:00
|
|
|
{
|
|
|
|
// If the date is valid and the time is not we set time to 00:00:00
|
|
|
|
QTime useTime = time;
|
|
|
|
if (!useTime.isValid() && date.isValid())
|
|
|
|
useTime = QTime::fromMSecsSinceStartOfDay(0);
|
|
|
|
|
2016-05-12 23:10:54 +00:00
|
|
|
QDateTimePrivate::StatusFlags newStatus = 0;
|
2013-08-12 19:21:42 +00:00
|
|
|
|
|
|
|
// Set date value and status
|
|
|
|
qint64 days = 0;
|
|
|
|
if (date.isValid()) {
|
|
|
|
days = date.toJulianDay() - JULIAN_DAY_FOR_EPOCH;
|
2016-05-12 23:10:54 +00:00
|
|
|
newStatus = QDateTimePrivate::ValidDate;
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set time value and status
|
|
|
|
int ds = 0;
|
|
|
|
if (useTime.isValid()) {
|
|
|
|
ds = useTime.msecsSinceStartOfDay();
|
2016-05-12 23:10:54 +00:00
|
|
|
newStatus |= QDateTimePrivate::ValidTime;
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set msecs serial value
|
2016-05-01 20:20:52 +00:00
|
|
|
qint64 msecs = (days * MSECS_PER_DAY) + ds;
|
|
|
|
if (d.isShort()) {
|
|
|
|
// let's see if we can keep this short
|
2016-06-23 05:00:08 +00:00
|
|
|
if (msecsCanBeSmall(msecs)) {
|
2016-05-01 20:20:52 +00:00
|
|
|
// yes, we can
|
2016-06-23 05:00:08 +00:00
|
|
|
d.data.msecs = qintptr(msecs);
|
2016-05-01 20:20:52 +00:00
|
|
|
d.data.status &= ~(QDateTimePrivate::ValidityMask | QDateTimePrivate::DaylightMask);
|
|
|
|
d.data.status |= newStatus;
|
|
|
|
} else {
|
|
|
|
// nope...
|
|
|
|
d.detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!d.isShort()) {
|
|
|
|
d.detach();
|
|
|
|
d->m_msecs = msecs;
|
|
|
|
d->m_status &= ~(QDateTimePrivate::ValidityMask | QDateTimePrivate::DaylightMask);
|
|
|
|
d->m_status |= newStatus;
|
|
|
|
}
|
2013-08-19 14:50:54 +00:00
|
|
|
|
|
|
|
// Set if date and time are valid
|
2016-05-12 23:10:54 +00:00
|
|
|
checkValidDateTime(d);
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static QPair<QDate, QTime> getDateTime(const QDateTimeData &d)
|
2013-08-12 19:21:42 +00:00
|
|
|
{
|
2015-01-21 14:04:59 +00:00
|
|
|
QPair<QDate, QTime> result;
|
2016-05-12 23:10:54 +00:00
|
|
|
qint64 msecs = getMSecs(d);
|
|
|
|
auto status = getStatus(d);
|
2016-05-12 06:14:46 +00:00
|
|
|
msecsToTime(msecs, &result.first, &result.second);
|
2013-08-12 19:21:42 +00:00
|
|
|
|
2016-05-12 23:10:54 +00:00
|
|
|
if (!status.testFlag(QDateTimePrivate::ValidDate))
|
2015-01-21 14:04:59 +00:00
|
|
|
result.first = QDate();
|
2013-08-12 19:21:42 +00:00
|
|
|
|
2016-05-12 23:10:54 +00:00
|
|
|
if (!status.testFlag(QDateTimePrivate::ValidTime))
|
2015-01-21 14:04:59 +00:00
|
|
|
result.second = QTime();
|
|
|
|
|
|
|
|
return result;
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
QDateTime::Data member functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-06-23 05:00:08 +00:00
|
|
|
inline QDateTime::Data::Data()
|
|
|
|
{
|
|
|
|
// default-constructed data has a special exception:
|
|
|
|
// it can be small even if CanBeSmall == false
|
|
|
|
// (optimization so we don't allocate memory in the default constructor)
|
|
|
|
quintptr value = quintptr(mergeSpec(QDateTimePrivate::ShortData, Qt::LocalTime));
|
|
|
|
d = reinterpret_cast<QDateTimePrivate *>(value);
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
inline QDateTime::Data::Data(Qt::TimeSpec spec)
|
2013-08-19 14:50:54 +00:00
|
|
|
{
|
2016-05-13 02:15:23 +00:00
|
|
|
if (CanBeSmall && Q_LIKELY(specCanBeSmall(spec))) {
|
2016-06-17 22:06:50 +00:00
|
|
|
d = reinterpret_cast<QDateTimePrivate *>(quintptr(mergeSpec(QDateTimePrivate::ShortData, spec)));
|
2016-05-13 20:35:04 +00:00
|
|
|
} else {
|
|
|
|
// the structure is too small, we need to detach
|
|
|
|
d = new QDateTimePrivate;
|
|
|
|
d->ref.ref();
|
|
|
|
d->m_status = mergeSpec(0, spec);
|
2013-08-19 14:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
inline QDateTime::Data::Data(const Data &other)
|
|
|
|
: d(other.d)
|
2013-08-19 14:50:54 +00:00
|
|
|
{
|
2016-05-13 02:15:23 +00:00
|
|
|
if (!isShort()) {
|
|
|
|
// check if we could shrink
|
2016-06-23 05:00:08 +00:00
|
|
|
if (specCanBeSmall(extractSpec(d->m_status)) && msecsCanBeSmall(d->m_msecs)) {
|
|
|
|
ShortData sd;
|
|
|
|
sd.msecs = qintptr(d->m_msecs);
|
2016-05-13 02:15:23 +00:00
|
|
|
sd.status = d->m_status | QDateTimePrivate::ShortData;
|
|
|
|
data = sd;
|
|
|
|
} else {
|
|
|
|
// no, have to keep it big
|
|
|
|
d->ref.ref();
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 20:35:04 +00:00
|
|
|
}
|
2013-08-19 14:50:54 +00:00
|
|
|
|
2016-05-13 02:33:00 +00:00
|
|
|
inline QDateTime::Data::Data(Data &&other)
|
|
|
|
: d(other.d)
|
|
|
|
{
|
2016-06-23 05:00:08 +00:00
|
|
|
// reset the other to a short state
|
|
|
|
Data dummy;
|
|
|
|
Q_ASSERT(dummy.isShort());
|
|
|
|
other.d = dummy.d;
|
2016-05-13 02:33:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
inline QDateTime::Data &QDateTime::Data::operator=(const Data &other)
|
|
|
|
{
|
|
|
|
if (d == other.d)
|
|
|
|
return *this;
|
2016-05-12 23:15:23 +00:00
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
auto x = d;
|
|
|
|
d = other.d;
|
2016-05-13 02:15:23 +00:00
|
|
|
if (!other.isShort()) {
|
|
|
|
// check if we could shrink
|
2016-06-23 05:00:08 +00:00
|
|
|
if (specCanBeSmall(extractSpec(other.d->m_status)) && msecsCanBeSmall(other.d->m_msecs)) {
|
|
|
|
ShortData sd;
|
|
|
|
sd.msecs = qintptr(other.d->m_msecs);
|
2016-05-13 02:15:23 +00:00
|
|
|
sd.status = other.d->m_status | QDateTimePrivate::ShortData;
|
|
|
|
data = sd;
|
|
|
|
} else {
|
|
|
|
// no, have to keep it big
|
|
|
|
other.d->ref.ref();
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 18:20:58 +00:00
|
|
|
|
2016-06-23 05:00:08 +00:00
|
|
|
if (!(quintptr(x) & QDateTimePrivate::ShortData) && !x->ref.deref())
|
2016-05-13 20:35:04 +00:00
|
|
|
delete x;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-05-12 23:15:23 +00:00
|
|
|
|
2016-05-13 20:35:04 +00:00
|
|
|
inline QDateTime::Data::~Data()
|
|
|
|
{
|
|
|
|
if (!isShort() && !d->ref.deref())
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool QDateTime::Data::isShort() const
|
|
|
|
{
|
2016-06-23 05:00:08 +00:00
|
|
|
bool b = quintptr(d) & QDateTimePrivate::ShortData;
|
|
|
|
|
2017-02-21 19:01:46 +00:00
|
|
|
// sanity check:
|
|
|
|
Q_ASSERT(b || (d->m_status & QDateTimePrivate::ShortData) == 0);
|
|
|
|
|
2016-06-23 05:00:08 +00:00
|
|
|
// even if CanBeSmall = false, we have short data for a default-constructed
|
|
|
|
// QDateTime object. But it's unlikely.
|
|
|
|
if (CanBeSmall)
|
|
|
|
return Q_LIKELY(b);
|
|
|
|
return Q_UNLIKELY(b);
|
2016-05-13 20:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void QDateTime::Data::detach()
|
|
|
|
{
|
|
|
|
QDateTimePrivate *x;
|
|
|
|
bool wasShort = isShort();
|
|
|
|
if (wasShort) {
|
|
|
|
// force enlarging
|
|
|
|
x = new QDateTimePrivate;
|
|
|
|
x->m_status = QDateTimePrivate::StatusFlag(data.status & ~QDateTimePrivate::ShortData);
|
|
|
|
x->m_msecs = data.msecs;
|
2016-05-01 20:20:52 +00:00
|
|
|
} else {
|
2016-05-13 20:35:04 +00:00
|
|
|
if (d->ref.load() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
x = new QDateTimePrivate(*d);
|
2016-05-01 20:20:52 +00:00
|
|
|
}
|
2016-05-13 20:35:04 +00:00
|
|
|
|
|
|
|
x->ref.store(1);
|
|
|
|
if (!wasShort && !d->ref.deref())
|
|
|
|
delete d;
|
|
|
|
d = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const QDateTimePrivate *QDateTime::Data::operator->() const
|
|
|
|
{
|
|
|
|
Q_ASSERT(!isShort());
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QDateTimePrivate *QDateTime::Data::operator->()
|
|
|
|
{
|
|
|
|
// should we attempt to detach here?
|
|
|
|
Q_ASSERT(!isShort());
|
|
|
|
Q_ASSERT(d->ref.load() == 1);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
QDateTimePrivate member functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
Q_NEVER_INLINE
|
|
|
|
QDateTime::Data QDateTimePrivate::create(const QDate &toDate, const QTime &toTime, Qt::TimeSpec toSpec,
|
|
|
|
int offsetSeconds)
|
|
|
|
{
|
|
|
|
QDateTime::Data result(toSpec);
|
|
|
|
setTimeSpec(result, toSpec, offsetSeconds);
|
|
|
|
setDateTime(result, toDate, toTime);
|
|
|
|
return result;
|
2013-08-19 14:50:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2016-05-13 20:35:04 +00:00
|
|
|
inline QDateTime::Data QDateTimePrivate::create(const QDate &toDate, const QTime &toTime,
|
|
|
|
const QTimeZone &toTimeZone)
|
|
|
|
{
|
|
|
|
QDateTime::Data result(Qt::TimeZone);
|
|
|
|
Q_ASSERT(!result.isShort());
|
|
|
|
|
|
|
|
result.d->m_status = mergeSpec(result.d->m_status, Qt::TimeZone);
|
|
|
|
result.d->m_timeZone = toTimeZone;
|
|
|
|
setDateTime(result, toDate, toTime);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
// Convert a TimeZone time expressed in zone msecs encoding into a UTC epoch msecs
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
// DST transitions are disambiguated by hint.
|
2016-05-01 20:20:52 +00:00
|
|
|
inline qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QTimeZone &zone,
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
DaylightStatus hint,
|
2017-12-06 10:35:05 +00:00
|
|
|
QDate *zoneDate, QTime *zoneTime)
|
2013-02-11 13:44:37 +00:00
|
|
|
{
|
|
|
|
// Get the effective data from QTimeZone
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs, int(hint));
|
2017-12-06 10:35:05 +00:00
|
|
|
// Docs state any time before 1970-01-01 will *not* have any DST applied
|
2015-11-09 11:05:49 +00:00
|
|
|
// but all affected times afterwards will have DST applied.
|
2017-12-06 10:35:05 +00:00
|
|
|
if (data.atMSecsSinceEpoch < 0) {
|
|
|
|
msecsToTime(zoneMSecs, zoneDate, zoneTime);
|
|
|
|
return zoneMSecs - data.standardTimeOffset * 1000;
|
2013-02-11 13:44:37 +00:00
|
|
|
} else {
|
2017-12-06 10:35:05 +00:00
|
|
|
msecsToTime(data.atMSecsSinceEpoch + data.offsetFromUtc * 1000, zoneDate, zoneTime);
|
|
|
|
return data.atMSecsSinceEpoch;
|
2013-02-11 13:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
QDateTime member functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QDateTime
|
2012-08-23 10:22:38 +00:00
|
|
|
\inmodule QtCore
|
2012-11-30 15:40:14 +00:00
|
|
|
\ingroup shared
|
2011-04-27 10:05:43 +00:00
|
|
|
\reentrant
|
|
|
|
\brief The QDateTime class provides date and time functions.
|
|
|
|
|
|
|
|
|
2018-09-11 15:47:39 +00:00
|
|
|
A QDateTime object encodes a calendar date and a clock time (a
|
|
|
|
"datetime"). It combines features of the QDate and QTime classes.
|
2011-04-27 10:05:43 +00:00
|
|
|
It can read the current datetime from the system clock. It
|
|
|
|
provides functions for comparing datetimes and for manipulating a
|
|
|
|
datetime by adding a number of seconds, days, months, or years.
|
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
QDateTime can describe datetimes with respect to \l{Qt::LocalTime}{local
|
|
|
|
time}, to \l{Qt::UTC}{UTC}, to a specified \l{{Qt::OffsetFromUTC}{offset
|
|
|
|
from UTC} or to a specified \l{{Qt::TimeZone}{time zone}, in conjunction
|
|
|
|
with the QTimeZone class. For example, a time zone of "Europe/Berlin" will
|
|
|
|
apply the daylight-saving rules as used in Germany since 1970. In contrast,
|
|
|
|
an offset from UTC of +3600 seconds is one hour ahead of UTC (usually
|
|
|
|
written in ISO standard notation as "UTC+01:00"), with no daylight-saving
|
|
|
|
offset or changes. When using either local time or a specified time zone,
|
|
|
|
time-zone transitions such as the starts and ends of daylight-saving time
|
|
|
|
(DST) are taken into account. The choice of system used to represent a
|
|
|
|
datetime is described as its "timespec".
|
|
|
|
|
|
|
|
A QDateTime object is typically created either by giving a date and time
|
|
|
|
explicitly in the constructor, or by using a static function such as
|
|
|
|
currentDateTime() or fromMSecsSinceEpoch(). The date and time can be changed
|
|
|
|
with setDate() and setTime(). A datetime can also be set using the
|
|
|
|
setMSecsSinceEpoch() function that takes the time, in milliseconds, since
|
|
|
|
00:00:00 on January 1, 1970. The fromString() function returns a QDateTime,
|
|
|
|
given a string and a date format used to interpret the date within the
|
|
|
|
string.
|
|
|
|
|
|
|
|
QDateTime::currentDateTime() returns a QDateTime that expresses the current
|
|
|
|
time with respect to local time. QDateTime::currentDateTimeUtc() returns a
|
|
|
|
QDateTime that expresses the current time with respect to UTC.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The date() and time() functions provide access to the date and
|
|
|
|
time parts of the datetime. The same information is provided in
|
|
|
|
textual format by the toString() function.
|
|
|
|
|
|
|
|
QDateTime provides a full set of operators to compare two
|
2012-09-04 22:52:53 +00:00
|
|
|
QDateTime objects, where smaller means earlier and larger means
|
2011-04-27 10:05:43 +00:00
|
|
|
later.
|
|
|
|
|
|
|
|
You can increment (or decrement) a datetime by a given number of
|
2019-05-24 12:20:43 +00:00
|
|
|
milliseconds using addMSecs(), seconds using addSecs(), or days using
|
|
|
|
addDays(). Similarly, you can use addMonths() and addYears(). The daysTo()
|
|
|
|
function returns the number of days between two datetimes, secsTo() returns
|
|
|
|
the number of seconds between two datetimes, and msecsTo() returns the
|
|
|
|
number of milliseconds between two datetimes. These operations are aware of
|
|
|
|
daylight-saving time (DST) and other time-zone transitions, where
|
|
|
|
applicable.
|
|
|
|
|
|
|
|
Use toTimeSpec() to express a datetime in local time or UTC,
|
|
|
|
toOffsetFromUtc() to express in terms of an offset from UTC, or toTimeZone()
|
|
|
|
to express it with respect to a general time zone. You can use timeSpec() to
|
|
|
|
find out what time-spec a QDateTime object stores its time relative to. When
|
|
|
|
that is Qt::TimeZone, you can use timeZone() to find out which zone it is
|
|
|
|
using.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\note QDateTime does not account for leap seconds.
|
|
|
|
|
2018-02-27 13:10:50 +00:00
|
|
|
\section1 Remarks
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\section2 No Year 0
|
|
|
|
|
|
|
|
There is no year 0. Dates in that year are considered invalid. The
|
|
|
|
year -1 is the year "1 before Christ" or "1 before current era."
|
2011-08-25 06:54:16 +00:00
|
|
|
The day before 1 January 1 CE is 31 December 1 BCE.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\section2 Range of Valid Dates
|
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
The range of values that QDateTime can represent is dependent on the
|
|
|
|
internal storage implementation. QDateTime is currently stored in a qint64
|
|
|
|
as a serial msecs value encoding the date and time. This restricts the date
|
|
|
|
range to about +/- 292 million years, compared to the QDate range of +/- 2
|
|
|
|
billion years. Care must be taken when creating a QDateTime with extreme
|
|
|
|
values that you do not overflow the storage. The exact range of supported
|
|
|
|
values varies depending on the Qt::TimeSpec and time zone.
|
|
|
|
|
|
|
|
\section2 Use of Timezones
|
|
|
|
|
|
|
|
QDateTime uses the system's time zone information to determine the current
|
|
|
|
local time zone and its offset from UTC. If the system is not configured
|
|
|
|
correctly or not up-to-date, QDateTime will give wrong results.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
QDateTime likewise uses system-provided information to determine the offsets
|
|
|
|
of other timezones from UTC. If this information is incomplete or out of
|
|
|
|
date, QDateTime will give wrong results. See the QTimeZone documentation for
|
|
|
|
more details.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
On modern Unix systems, this means QDateTime usually has accurate
|
|
|
|
information about historical transitions (including DST, see below) whenever
|
|
|
|
possible. On Windows, where the system doesn't support historical timezone
|
|
|
|
data, historical accuracy is not maintained with respect to timezone
|
|
|
|
transitions, notably including DST.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-11-09 11:05:49 +00:00
|
|
|
\section2 Daylight-Saving Time (DST)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
QDateTime takes into account transitions between Standard Time and
|
|
|
|
Daylight-Saving Time. For example, if the transition is at 2am and the clock
|
|
|
|
goes forward to 3am, then there is a "missing" hour from 02:00:00 to
|
|
|
|
02:59:59.999 which QDateTime considers to be invalid. Any date arithmetic
|
|
|
|
performed will take this missing hour into account and return a valid
|
|
|
|
result. For example, adding one minute to 01:59:59 will get 03:00:00.
|
|
|
|
|
|
|
|
The range of valid dates taking DST into account is 1970-01-01 to the
|
|
|
|
present, and rules are in place for handling DST correctly until 2037-12-31,
|
|
|
|
but these could change. For dates falling outside that range, QDateTime
|
|
|
|
makes a \e{best guess} using the rules for year 1970 or 2037, but we can't
|
|
|
|
guarantee accuracy. This means QDateTime doesn't take into account changes
|
|
|
|
in a time zone before 1970, even if the system's time zone database provides
|
|
|
|
that information.
|
|
|
|
|
|
|
|
\section2 Offsets From UTC
|
|
|
|
|
|
|
|
There is no explicit size restriction on an offset from UTC, but there is an
|
|
|
|
implicit limit imposed when using the toString() and fromString() methods
|
|
|
|
which use a [+|-]hh:mm format, effectively limiting the range to +/- 99
|
|
|
|
hours and 59 minutes and whole minutes only. Note that currently no time
|
|
|
|
zone lies outside the range of +/- 14 hours.
|
2013-02-11 13:44:37 +00:00
|
|
|
|
|
|
|
\sa QDate, QTime, QDateTimeEdit, QTimeZone
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a null datetime (i.e. null date and null time). A null
|
|
|
|
datetime is invalid, since the date is invalid.
|
|
|
|
|
|
|
|
\sa isValid()
|
|
|
|
*/
|
2016-05-01 20:20:52 +00:00
|
|
|
QDateTime::QDateTime() Q_DECL_NOEXCEPT_EXPR(Data::CanBeSmall)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a datetime with the given \a date, a valid
|
|
|
|
time(00:00:00.000), and sets the timeSpec() to Qt::LocalTime.
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime::QDateTime(const QDate &date)
|
2016-05-01 20:20:52 +00:00
|
|
|
: d(QDateTimePrivate::create(date, QTime(0, 0, 0), Qt::LocalTime, 0))
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs a datetime with the given \a date and \a time, using
|
|
|
|
the time specification defined by \a spec.
|
|
|
|
|
|
|
|
If \a date is valid and \a time is not, the time will be set to midnight.
|
2013-02-25 13:36:29 +00:00
|
|
|
|
|
|
|
If \a spec is Qt::OffsetFromUTC then it will be set to Qt::UTC, i.e. an
|
|
|
|
offset of 0 seconds. To create a Qt::OffsetFromUTC datetime use the
|
|
|
|
correct constructor.
|
2013-02-11 13:44:37 +00:00
|
|
|
|
|
|
|
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
|
|
|
|
i.e. the current system time zone. To create a Qt::TimeZone datetime
|
|
|
|
use the correct constructor.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec)
|
2016-05-01 20:20:52 +00:00
|
|
|
: d(QDateTimePrivate::create(date, time, spec, 0))
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-02-25 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Constructs a datetime with the given \a date and \a time, using
|
|
|
|
the time specification defined by \a spec and \a offsetSeconds seconds.
|
|
|
|
|
|
|
|
If \a date is valid and \a time is not, the time will be set to midnight.
|
|
|
|
|
|
|
|
If the \a spec is not Qt::OffsetFromUTC then \a offsetSeconds will be ignored.
|
|
|
|
|
|
|
|
If the \a spec is Qt::OffsetFromUTC and \a offsetSeconds is 0 then the
|
|
|
|
timeSpec() will be set to Qt::UTC, i.e. an offset of 0 seconds.
|
2013-02-11 13:44:37 +00:00
|
|
|
|
|
|
|
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
|
|
|
|
i.e. the current system time zone. To create a Qt::TimeZone datetime
|
|
|
|
use the correct constructor.
|
2013-02-25 13:36:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds)
|
2016-05-01 20:20:52 +00:00
|
|
|
: d(QDateTimePrivate::create(date, time, spec, offsetSeconds))
|
2013-02-25 13:36:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Constructs a datetime with the given \a date and \a time, using
|
|
|
|
the Time Zone specified by \a timeZone.
|
|
|
|
|
|
|
|
If \a date is valid and \a time is not, the time will be set to 00:00:00.
|
|
|
|
|
|
|
|
If \a timeZone is invalid then the datetime will be invalid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime::QDateTime(const QDate &date, const QTime &time, const QTimeZone &timeZone)
|
2016-05-01 20:20:52 +00:00
|
|
|
: d(QDateTimePrivate::create(date, time, timeZone))
|
2013-02-11 13:44:37 +00:00
|
|
|
{
|
|
|
|
}
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Constructs a copy of the \a other datetime.
|
|
|
|
*/
|
2016-05-01 20:20:52 +00:00
|
|
|
QDateTime::QDateTime(const QDateTime &other) Q_DECL_NOTHROW
|
2011-04-27 10:05:43 +00:00
|
|
|
: d(other.d)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-13 02:33:00 +00:00
|
|
|
/*!
|
|
|
|
\since 5.8
|
|
|
|
Moves the content of the temporary \a other datetime to this object and
|
|
|
|
leaves \a other in an unspecified (but proper) state.
|
|
|
|
*/
|
|
|
|
QDateTime::QDateTime(QDateTime &&other) Q_DECL_NOTHROW
|
|
|
|
: d(std::move(other.d))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Destroys the datetime.
|
|
|
|
*/
|
|
|
|
QDateTime::~QDateTime()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Makes a copy of the \a other datetime and returns a reference to the
|
|
|
|
copy.
|
|
|
|
*/
|
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
QDateTime &QDateTime::operator=(const QDateTime &other) Q_DECL_NOTHROW
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
d = other.d;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-04-05 12:49:02 +00:00
|
|
|
/*!
|
|
|
|
\fn void QDateTime::swap(QDateTime &other)
|
|
|
|
\since 5.0
|
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
Swaps this datetime with \a other. This operation is very fast
|
2012-04-05 12:49:02 +00:00
|
|
|
and never fails.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if both the date and the time are null; otherwise
|
|
|
|
returns \c false. A null datetime is invalid.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa QDate::isNull(), QTime::isNull(), isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDateTime::isNull() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
return !status.testFlag(QDateTimePrivate::ValidDate) &&
|
|
|
|
!status.testFlag(QDateTimePrivate::ValidTime);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if both the date and the time are valid and they are valid in
|
|
|
|
the current Qt::TimeSpec, otherwise returns \c false.
|
2013-08-19 14:50:54 +00:00
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
If the timeSpec() is Qt::LocalTime or Qt::TimeZone then the date and time are
|
2015-11-09 11:05:49 +00:00
|
|
|
checked to see if they fall in the Standard Time to Daylight-Saving Time transition
|
2013-08-19 14:50:54 +00:00
|
|
|
hour, i.e. if the transition is at 2am and the clock goes forward to 3am
|
|
|
|
then the time from 02:00:00 to 02:59:59.999 is considered to be invalid.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa QDate::isValid(), QTime::isValid()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDateTime::isValid() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
return status & QDateTimePrivate::ValidDateTime;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the date part of the datetime.
|
|
|
|
|
|
|
|
\sa setDate(), time(), timeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDate QDateTime::date() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
if (!status.testFlag(QDateTimePrivate::ValidDate))
|
2013-09-26 14:51:48 +00:00
|
|
|
return QDate();
|
2013-08-12 19:21:42 +00:00
|
|
|
QDate dt;
|
2016-05-12 23:10:54 +00:00
|
|
|
msecsToTime(getMSecs(d), &dt, 0);
|
2013-08-12 19:21:42 +00:00
|
|
|
return dt;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the time part of the datetime.
|
|
|
|
|
|
|
|
\sa setTime(), date(), timeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QTime QDateTime::time() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = getStatus(d);
|
|
|
|
if (!status.testFlag(QDateTimePrivate::ValidTime))
|
2013-09-26 14:51:48 +00:00
|
|
|
return QTime();
|
2013-08-12 19:21:42 +00:00
|
|
|
QTime tm;
|
2016-05-12 23:10:54 +00:00
|
|
|
msecsToTime(getMSecs(d), 0, &tm);
|
2013-08-12 19:21:42 +00:00
|
|
|
return tm;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the time specification of the datetime.
|
|
|
|
|
|
|
|
\sa setTimeSpec(), date(), time(), Qt::TimeSpec
|
|
|
|
*/
|
|
|
|
|
|
|
|
Qt::TimeSpec QDateTime::timeSpec() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
return getSpec(d);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Returns the time zone of the datetime.
|
|
|
|
|
|
|
|
If the timeSpec() is Qt::LocalTime then an instance of the current system
|
|
|
|
time zone will be returned. Note however that if you copy this time zone
|
|
|
|
the instance will not remain in sync if the system time zone changes.
|
|
|
|
|
|
|
|
\sa setTimeZone(), Qt::TimeSpec
|
|
|
|
*/
|
|
|
|
|
|
|
|
QTimeZone QDateTime::timeZone() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
switch (getSpec(d)) {
|
2013-02-11 13:44:37 +00:00
|
|
|
case Qt::UTC:
|
2015-01-15 00:27:31 +00:00
|
|
|
return QTimeZone::utc();
|
2015-01-15 00:33:03 +00:00
|
|
|
case Qt::OffsetFromUTC:
|
2015-09-22 11:17:50 +00:00
|
|
|
return QTimeZone(d->m_offsetFromUtc);
|
2015-01-15 00:27:31 +00:00
|
|
|
case Qt::TimeZone:
|
2015-01-15 00:33:03 +00:00
|
|
|
Q_ASSERT(d->m_timeZone.isValid());
|
2013-02-11 13:44:37 +00:00
|
|
|
return d->m_timeZone;
|
|
|
|
case Qt::LocalTime:
|
2015-01-15 00:27:31 +00:00
|
|
|
return QTimeZone::systemTimeZone();
|
2013-02-11 13:44:37 +00:00
|
|
|
}
|
|
|
|
return QTimeZone();
|
|
|
|
}
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Returns the current Offset From UTC in seconds.
|
|
|
|
|
|
|
|
If the timeSpec() is Qt::OffsetFromUTC this will be the value originally set.
|
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
If the timeSpec() is Qt::TimeZone this will be the offset effective in the
|
2015-11-09 11:05:49 +00:00
|
|
|
Time Zone including any Daylight-Saving Offset.
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
If the timeSpec() is Qt::LocalTime this will be the difference between the
|
2015-11-09 11:05:49 +00:00
|
|
|
Local Time and UTC including any Daylight-Saving Offset.
|
2013-02-25 13:36:29 +00:00
|
|
|
|
|
|
|
If the timeSpec() is Qt::UTC this will be 0.
|
|
|
|
|
|
|
|
\sa setOffsetFromUtc()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int QDateTime::offsetFromUtc() const
|
|
|
|
{
|
2016-05-01 20:20:52 +00:00
|
|
|
if (!d.isShort())
|
|
|
|
return d->m_offsetFromUtc;
|
|
|
|
if (!isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto spec = getSpec(d);
|
|
|
|
if (spec == Qt::LocalTime) {
|
|
|
|
// we didn't cache the value, so we need to calculate it now...
|
|
|
|
qint64 msecs = getMSecs(d);
|
|
|
|
return (msecs - toMSecsSinceEpoch()) / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT(spec == Qt::UTC);
|
|
|
|
return 0;
|
2013-02-25 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:38:00 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Returns the Time Zone Abbreviation for the datetime.
|
|
|
|
|
|
|
|
If the timeSpec() is Qt::UTC this will be "UTC".
|
|
|
|
|
|
|
|
If the timeSpec() is Qt::OffsetFromUTC this will be in the format
|
|
|
|
"UTC[+-]00:00".
|
|
|
|
|
|
|
|
If the timeSpec() is Qt::LocalTime then the host system is queried for the
|
|
|
|
correct abbreviation.
|
|
|
|
|
|
|
|
Note that abbreviations may or may not be localized.
|
|
|
|
|
|
|
|
Note too that the abbreviation is not guaranteed to be a unique value,
|
|
|
|
i.e. different time zones may have the same abbreviation.
|
|
|
|
|
|
|
|
\sa timeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QString QDateTime::timeZoneAbbreviation() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
switch (getSpec(d)) {
|
2013-08-12 12:20:56 +00:00
|
|
|
case Qt::UTC:
|
2016-08-26 11:05:24 +00:00
|
|
|
return QLatin1String("UTC");
|
2013-08-12 12:20:56 +00:00
|
|
|
case Qt::OffsetFromUTC:
|
2016-08-26 11:05:24 +00:00
|
|
|
return QLatin1String("UTC") + toOffsetString(Qt::ISODate, d->m_offsetFromUtc);
|
2013-02-11 13:44:37 +00:00
|
|
|
case Qt::TimeZone:
|
2016-08-26 11:05:24 +00:00
|
|
|
#if !QT_CONFIG(timezone)
|
2016-01-18 11:40:41 +00:00
|
|
|
break;
|
|
|
|
#else
|
2016-05-12 05:27:36 +00:00
|
|
|
return d->m_timeZone.d->abbreviation(toMSecsSinceEpoch());
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-08-12 19:21:42 +00:00
|
|
|
case Qt::LocalTime: {
|
2013-07-17 09:38:00 +00:00
|
|
|
QString abbrev;
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = extractDaylightStatus(getStatus(d));
|
2016-05-12 23:10:54 +00:00
|
|
|
localMSecsToEpochMSecs(getMSecs(d), &status, 0, 0, &abbrev);
|
2013-07-17 09:38:00 +00:00
|
|
|
return abbrev;
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 12:20:56 +00:00
|
|
|
return QString();
|
2013-07-17 09:38:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 14:35:25 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
2015-11-09 11:05:49 +00:00
|
|
|
Returns if this datetime falls in Daylight-Saving Time.
|
2013-09-02 14:35:25 +00:00
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
If the Qt::TimeSpec is not Qt::LocalTime or Qt::TimeZone then will always
|
2013-09-02 14:35:25 +00:00
|
|
|
return false.
|
|
|
|
|
|
|
|
\sa timeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDateTime::isDaylightTime() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
switch (getSpec(d)) {
|
2013-09-02 14:35:25 +00:00
|
|
|
case Qt::UTC:
|
|
|
|
case Qt::OffsetFromUTC:
|
|
|
|
return false;
|
2013-02-11 13:44:37 +00:00
|
|
|
case Qt::TimeZone:
|
2016-08-26 11:05:24 +00:00
|
|
|
#if !QT_CONFIG(timezone)
|
2016-01-18 11:40:41 +00:00
|
|
|
break;
|
|
|
|
#else
|
2013-02-11 13:44:37 +00:00
|
|
|
return d->m_timeZone.d->isDaylightTime(toMSecsSinceEpoch());
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-09-02 14:35:25 +00:00
|
|
|
case Qt::LocalTime: {
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = extractDaylightStatus(getStatus(d));
|
2013-09-27 12:15:31 +00:00
|
|
|
if (status == QDateTimePrivate::UnknownDaylightTime)
|
2016-05-12 23:10:54 +00:00
|
|
|
localMSecsToEpochMSecs(getMSecs(d), &status);
|
2013-09-02 14:35:25 +00:00
|
|
|
return (status == QDateTimePrivate::DaylightTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
2015-01-14 23:34:45 +00:00
|
|
|
Sets the date part of this datetime to \a date. If no time is set yet, it
|
|
|
|
is set to midnight. If \a date is invalid, this QDateTime becomes invalid.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa date(), setTime(), setTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QDateTime::setDate(const QDate &date)
|
|
|
|
{
|
2016-05-12 23:10:54 +00:00
|
|
|
setDateTime(d, date, time());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2015-01-14 23:34:45 +00:00
|
|
|
Sets the time part of this datetime to \a time. If \a time is not valid,
|
|
|
|
this function sets it to midnight. Therefore, it's possible to clear any
|
|
|
|
set time in a QDateTime by setting it to a default QTime:
|
|
|
|
|
|
|
|
\code
|
|
|
|
QDateTime dt = QDateTime::currentDateTime();
|
|
|
|
dt.setTime(QTime());
|
|
|
|
\endcode
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa time(), setDate(), setTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QDateTime::setTime(const QTime &time)
|
|
|
|
{
|
2016-05-12 23:10:54 +00:00
|
|
|
setDateTime(d, date(), time);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the time specification used in this datetime to \a spec.
|
2012-09-03 13:26:39 +00:00
|
|
|
The datetime will refer to a different point in time.
|
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
If \a spec is Qt::OffsetFromUTC then the timeSpec() will be set
|
|
|
|
to Qt::UTC, i.e. an effective offset of 0.
|
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
|
|
|
|
i.e. the current system time zone.
|
|
|
|
|
2012-09-03 13:26:39 +00:00
|
|
|
Example:
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 19
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
\sa timeSpec(), setDate(), setTime(), setTimeZone(), Qt::TimeSpec
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void QDateTime::setTimeSpec(Qt::TimeSpec spec)
|
|
|
|
{
|
2016-05-12 23:10:54 +00:00
|
|
|
QT_PREPEND_NAMESPACE(setTimeSpec(d, spec, 0));
|
|
|
|
checkValidDateTime(d);
|
2013-02-25 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Sets the timeSpec() to Qt::OffsetFromUTC and the offset to \a offsetSeconds.
|
|
|
|
The datetime will refer to a different point in time.
|
|
|
|
|
|
|
|
The maximum and minimum offset is 14 positive or negative hours. If
|
|
|
|
\a offsetSeconds is larger or smaller than that, then the result is
|
|
|
|
undefined.
|
|
|
|
|
|
|
|
If \a offsetSeconds is 0 then the timeSpec() will be set to Qt::UTC.
|
|
|
|
|
|
|
|
\sa isValid(), offsetFromUtc()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QDateTime::setOffsetFromUtc(int offsetSeconds)
|
|
|
|
{
|
2016-05-12 23:10:54 +00:00
|
|
|
QT_PREPEND_NAMESPACE(setTimeSpec(d, Qt::OffsetFromUTC, offsetSeconds));
|
|
|
|
checkValidDateTime(d);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Sets the time zone used in this datetime to \a toZone.
|
|
|
|
The datetime will refer to a different point in time.
|
|
|
|
|
|
|
|
If \a toZone is invalid then the datetime will be invalid.
|
|
|
|
|
|
|
|
\sa timeZone(), Qt::TimeSpec
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QDateTime::setTimeZone(const QTimeZone &toZone)
|
|
|
|
{
|
2016-05-01 20:20:52 +00:00
|
|
|
d.detach(); // always detach
|
2016-05-12 06:14:46 +00:00
|
|
|
d->m_status = mergeSpec(d->m_status, Qt::TimeZone);
|
2013-02-11 13:44:37 +00:00
|
|
|
d->m_offsetFromUtc = 0;
|
|
|
|
d->m_timeZone = toZone;
|
2016-05-12 23:10:54 +00:00
|
|
|
refreshDateTime(d);
|
2013-02-11 13:44:37 +00:00
|
|
|
}
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\since 4.7
|
|
|
|
|
|
|
|
Returns the datetime as the number of milliseconds that have passed
|
|
|
|
since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC).
|
|
|
|
|
|
|
|
On systems that do not support time zones, this function will
|
|
|
|
behave as if local time were Qt::UTC.
|
|
|
|
|
|
|
|
The behavior for this function is undefined if the datetime stored in
|
|
|
|
this object is not valid. However, for all valid dates, this function
|
|
|
|
returns a unique value.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\sa toSecsSinceEpoch(), setMSecsSinceEpoch()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
qint64 QDateTime::toMSecsSinceEpoch() const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
switch (getSpec(d)) {
|
2016-05-12 05:27:36 +00:00
|
|
|
case Qt::UTC:
|
2016-05-01 20:20:52 +00:00
|
|
|
return getMSecs(d);
|
|
|
|
|
2016-05-12 06:14:46 +00:00
|
|
|
case Qt::OffsetFromUTC:
|
2016-05-01 20:20:52 +00:00
|
|
|
return d->m_msecs - (d->m_offsetFromUtc * 1000);
|
2016-05-12 05:27:36 +00:00
|
|
|
|
|
|
|
case Qt::LocalTime: {
|
|
|
|
// recalculate the local timezone
|
2016-05-12 06:14:46 +00:00
|
|
|
auto status = extractDaylightStatus(getStatus(d));
|
2016-05-12 23:10:54 +00:00
|
|
|
return localMSecsToEpochMSecs(getMSecs(d), &status);
|
2016-05-12 05:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::TimeZone:
|
2016-08-26 11:05:24 +00:00
|
|
|
#if !QT_CONFIG(timezone)
|
2016-07-19 17:51:41 +00:00
|
|
|
return 0;
|
2016-05-12 05:27:36 +00:00
|
|
|
#else
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
return QDateTimePrivate::zoneMSecsToEpochMSecs(d->m_msecs, d->m_timeZone,
|
|
|
|
extractDaylightStatus(getStatus(d)));
|
2016-05-12 05:27:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
return 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\since 5.8
|
|
|
|
|
|
|
|
Returns the datetime as the number of seconds that have passed since
|
|
|
|
1970-01-01T00:00:00.000, Coordinated Universal Time (Qt::UTC).
|
|
|
|
|
|
|
|
On systems that do not support time zones, this function will
|
|
|
|
behave as if local time were Qt::UTC.
|
|
|
|
|
|
|
|
The behavior for this function is undefined if the datetime stored in
|
|
|
|
this object is not valid. However, for all valid dates, this function
|
|
|
|
returns a unique value.
|
|
|
|
|
|
|
|
\sa toMSecsSinceEpoch(), setSecsSinceEpoch()
|
|
|
|
*/
|
|
|
|
qint64 QDateTime::toSecsSinceEpoch() const
|
|
|
|
{
|
|
|
|
return toMSecsSinceEpoch() / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if QT_DEPRECATED_SINCE(5, 8)
|
|
|
|
/*!
|
|
|
|
\deprecated
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Returns the datetime as the number of seconds that have passed
|
|
|
|
since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
|
|
|
|
|
|
|
|
On systems that do not support time zones, this function will
|
|
|
|
behave as if local time were Qt::UTC.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\note This function returns a 32-bit unsigned integer and is deprecated.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the date is outside the range 1970-01-01T00:00:00 to
|
|
|
|
2106-02-07T06:28:14, this function returns -1 cast to an unsigned integer
|
|
|
|
(i.e., 0xFFFFFFFF).
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
To get an extended range, use toMSecsSinceEpoch() or toSecsSinceEpoch().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\sa toSecsSinceEpoch(), toMSecsSinceEpoch(), setTime_t()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
uint QDateTime::toTime_t() const
|
|
|
|
{
|
2013-08-19 14:50:54 +00:00
|
|
|
if (!isValid())
|
|
|
|
return uint(-1);
|
2016-05-12 05:27:36 +00:00
|
|
|
qint64 retval = toMSecsSinceEpoch() / 1000;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (quint64(retval) >= Q_UINT64_C(0xFFFFFFFF))
|
|
|
|
return uint(-1);
|
|
|
|
return uint(retval);
|
|
|
|
}
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.7
|
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
Sets the date and time given the number of milliseconds \a msecs that have
|
2011-04-27 10:05:43 +00:00
|
|
|
passed since 1970-01-01T00:00:00.000, Coordinated Universal Time
|
|
|
|
(Qt::UTC). On systems that do not support time zones this function
|
|
|
|
will behave as if local time were Qt::UTC.
|
|
|
|
|
2013-02-04 16:08:24 +00:00
|
|
|
Note that passing the minimum of \c qint64
|
|
|
|
(\c{std::numeric_limits<qint64>::min()}) to \a msecs will result in
|
|
|
|
undefined behavior.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\sa toMSecsSinceEpoch(), setSecsSinceEpoch()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
void QDateTime::setMSecsSinceEpoch(qint64 msecs)
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
const auto spec = getSpec(d);
|
|
|
|
auto status = getStatus(d);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-05-12 23:28:22 +00:00
|
|
|
status &= ~QDateTimePrivate::ValidityMask;
|
|
|
|
switch (spec) {
|
2013-08-12 19:21:42 +00:00
|
|
|
case Qt::UTC:
|
2016-05-12 23:28:22 +00:00
|
|
|
status = status
|
2013-08-19 14:50:54 +00:00
|
|
|
| QDateTimePrivate::ValidDate
|
|
|
|
| QDateTimePrivate::ValidTime
|
|
|
|
| QDateTimePrivate::ValidDateTime;
|
2013-08-12 19:21:42 +00:00
|
|
|
break;
|
|
|
|
case Qt::OffsetFromUTC:
|
2016-05-12 23:28:22 +00:00
|
|
|
msecs = msecs + (d->m_offsetFromUtc * 1000);
|
|
|
|
status = status
|
2013-08-19 14:50:54 +00:00
|
|
|
| QDateTimePrivate::ValidDate
|
|
|
|
| QDateTimePrivate::ValidTime
|
|
|
|
| QDateTimePrivate::ValidDateTime;
|
2013-08-12 19:21:42 +00:00
|
|
|
break;
|
2013-02-11 13:44:37 +00:00
|
|
|
case Qt::TimeZone:
|
2016-05-01 20:20:52 +00:00
|
|
|
Q_ASSERT(!d.isShort());
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2015-11-09 11:05:49 +00:00
|
|
|
// Docs state any LocalTime before 1970-01-01 will *not* have any DST applied
|
|
|
|
// but all affected times afterwards will have DST applied.
|
2016-05-01 20:20:52 +00:00
|
|
|
d.detach();
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
if (msecs >= 0) {
|
|
|
|
status = mergeDaylightStatus(status,
|
|
|
|
d->m_timeZone.d->isDaylightTime(msecs)
|
|
|
|
? QDateTimePrivate::DaylightTime
|
|
|
|
: QDateTimePrivate::StandardTime);
|
2013-02-11 13:44:37 +00:00
|
|
|
d->m_offsetFromUtc = d->m_timeZone.d->offsetFromUtc(msecs);
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
} else {
|
|
|
|
status = mergeDaylightStatus(status, QDateTimePrivate::StandardTime);
|
2013-02-11 13:44:37 +00:00
|
|
|
d->m_offsetFromUtc = d->m_timeZone.d->standardTimeOffset(msecs);
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
}
|
2016-05-12 23:28:22 +00:00
|
|
|
msecs = msecs + (d->m_offsetFromUtc * 1000);
|
|
|
|
status = status
|
2013-02-11 13:44:37 +00:00
|
|
|
| QDateTimePrivate::ValidDate
|
|
|
|
| QDateTimePrivate::ValidTime
|
2015-01-15 00:33:03 +00:00
|
|
|
| QDateTimePrivate::ValidDateTime;
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
break;
|
2013-08-12 19:21:42 +00:00
|
|
|
case Qt::LocalTime: {
|
|
|
|
QDate dt;
|
|
|
|
QTime tm;
|
2016-05-12 23:28:22 +00:00
|
|
|
QDateTimePrivate::DaylightStatus dstStatus;
|
|
|
|
epochMSecsToLocalTime(msecs, &dt, &tm, &dstStatus);
|
2016-05-12 23:10:54 +00:00
|
|
|
setDateTime(d, dt, tm);
|
|
|
|
msecs = getMSecs(d);
|
2016-05-12 06:14:46 +00:00
|
|
|
status = mergeDaylightStatus(getStatus(d), dstStatus);
|
2013-08-12 19:21:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2016-05-12 23:28:22 +00:00
|
|
|
|
2016-06-23 05:00:08 +00:00
|
|
|
if (msecsCanBeSmall(msecs) && d.isShort()) {
|
2016-05-01 20:20:52 +00:00
|
|
|
// we can keep short
|
2016-06-23 05:00:08 +00:00
|
|
|
d.data.msecs = qintptr(msecs);
|
|
|
|
d.data.status = status;
|
2016-05-01 20:20:52 +00:00
|
|
|
} else {
|
|
|
|
d.detach();
|
2017-02-21 19:01:46 +00:00
|
|
|
d->m_status = status & ~QDateTimePrivate::ShortData;
|
2016-05-01 20:20:52 +00:00
|
|
|
d->m_msecs = msecs;
|
|
|
|
}
|
2016-05-12 23:28:22 +00:00
|
|
|
|
|
|
|
if (spec == Qt::LocalTime || spec == Qt::TimeZone)
|
2016-05-12 23:10:54 +00:00
|
|
|
refreshDateTime(d);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
/*!
|
|
|
|
\since 5.8
|
|
|
|
|
|
|
|
Sets the date and time given the number of seconds \a secs that have
|
|
|
|
passed since 1970-01-01T00:00:00.000, Coordinated Universal Time
|
|
|
|
(Qt::UTC). On systems that do not support time zones this function
|
|
|
|
will behave as if local time were Qt::UTC.
|
|
|
|
|
|
|
|
\sa toSecsSinceEpoch(), setMSecsSinceEpoch()
|
|
|
|
*/
|
|
|
|
void QDateTime::setSecsSinceEpoch(qint64 secs)
|
|
|
|
{
|
|
|
|
setMSecsSinceEpoch(secs * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if QT_DEPRECATED_SINCE(5, 8)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn void QDateTime::setTime_t(uint seconds)
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Sets the date and time given the number of \a seconds that have
|
|
|
|
passed since 1970-01-01T00:00:00, Coordinated Universal Time
|
|
|
|
(Qt::UTC). On systems that do not support time zones this function
|
|
|
|
will behave as if local time were Qt::UTC.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\note This function is deprecated. For new code, use setSecsSinceEpoch().
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toTime_t()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QDateTime::setTime_t(uint secsSince1Jan1970UTC)
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
setMSecsSinceEpoch((qint64)secsSince1Jan1970UTC * 1000);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn QString QDateTime::toString(Qt::DateFormat format) const
|
|
|
|
|
|
|
|
\overload
|
|
|
|
|
|
|
|
Returns the datetime as a string in the \a format given.
|
|
|
|
|
|
|
|
If the \a format is Qt::TextDate, the string is formatted in
|
|
|
|
the default way. QDate::shortDayName(), QDate::shortMonthName(),
|
|
|
|
and QTime::toString() are used to generate the string, so the
|
2013-07-26 21:48:11 +00:00
|
|
|
day and month names will be localized names using the system locale,
|
|
|
|
i.e. QLocale::system(). An example of this formatting is
|
2013-09-16 09:55:07 +00:00
|
|
|
"Wed May 20 03:40:13 1998".
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the \a format is Qt::ISODate, the string format corresponds
|
2013-09-16 09:55:07 +00:00
|
|
|
to the ISO 8601 extended specification for representations of
|
2017-07-24 00:56:55 +00:00
|
|
|
dates and times, taking the form yyyy-MM-ddTHH:mm:ss[Z|[+|-]HH:mm],
|
2013-09-16 09:55:07 +00:00
|
|
|
depending on the timeSpec() of the QDateTime. If the timeSpec()
|
|
|
|
is Qt::UTC, Z will be appended to the string; if the timeSpec() is
|
|
|
|
Qt::OffsetFromUTC, the offset in hours and minutes from UTC will
|
2016-10-10 11:44:52 +00:00
|
|
|
be appended to the string. To include milliseconds in the ISO 8601
|
|
|
|
date, use the \a format Qt::ISODateWithMs, which corresponds to
|
2017-07-24 00:56:55 +00:00
|
|
|
yyyy-MM-ddTHH:mm:ss.zzz[Z|[+|-]HH:mm].
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If the \a format is Qt::SystemLocaleShortDate or
|
|
|
|
Qt::SystemLocaleLongDate, the string format depends on the locale
|
|
|
|
settings of the system. Identical to calling
|
|
|
|
QLocale::system().toString(datetime, QLocale::ShortFormat) or
|
|
|
|
QLocale::system().toString(datetime, QLocale::LongFormat).
|
|
|
|
|
|
|
|
If the \a format is Qt::DefaultLocaleShortDate or
|
|
|
|
Qt::DefaultLocaleLongDate, the string format depends on the
|
|
|
|
default application locale. This is the locale set with
|
|
|
|
QLocale::setDefault(), or the system locale if no default locale
|
|
|
|
has been set. Identical to calling QLocale().toString(datetime,
|
|
|
|
QLocale::ShortFormat) or QLocale().toString(datetime,
|
|
|
|
QLocale::LongFormat).
|
|
|
|
|
2013-07-13 09:01:34 +00:00
|
|
|
If the \a format is Qt::RFC2822Date, the string is formatted
|
|
|
|
following \l{RFC 2822}.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
If the datetime is invalid, an empty string will be returned.
|
|
|
|
|
|
|
|
\warning The Qt::ISODate format is only valid for years in the
|
|
|
|
range 0 to 9999. This restriction may apply to locale-aware
|
|
|
|
formats as well, depending on the locale settings.
|
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa fromString(), QDate::toString(), QTime::toString(),
|
|
|
|
QLocale::toString()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
QString QDateTime::toString(Qt::DateFormat format) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QString buf;
|
|
|
|
if (!isValid())
|
|
|
|
return buf;
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
switch (format) {
|
|
|
|
case Qt::SystemLocaleDate:
|
|
|
|
case Qt::SystemLocaleShortDate:
|
|
|
|
return QLocale::system().toString(*this, QLocale::ShortFormat);
|
|
|
|
case Qt::SystemLocaleLongDate:
|
|
|
|
return QLocale::system().toString(*this, QLocale::LongFormat);
|
|
|
|
case Qt::LocaleDate:
|
|
|
|
case Qt::DefaultLocaleShortDate:
|
|
|
|
return QLocale().toString(*this, QLocale::ShortFormat);
|
|
|
|
case Qt::DefaultLocaleLongDate:
|
|
|
|
return QLocale().toString(*this, QLocale::LongFormat);
|
|
|
|
case Qt::RFC2822Date: {
|
2017-01-31 00:04:45 +00:00
|
|
|
buf = QLocale::c().toString(*this, QStringViewLiteral("dd MMM yyyy hh:mm:ss "));
|
2016-05-01 20:20:52 +00:00
|
|
|
buf += toOffsetString(Qt::TextDate, offsetFromUtc());
|
2013-07-18 09:37:57 +00:00
|
|
|
return buf;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-07-18 09:37:57 +00:00
|
|
|
default:
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_CONFIG(textdate)
|
2013-08-12 19:21:42 +00:00
|
|
|
case Qt::TextDate: {
|
2016-05-12 23:10:54 +00:00
|
|
|
const QPair<QDate, QTime> p = getDateTime(d);
|
2016-11-21 16:51:22 +00:00
|
|
|
buf = p.first.toString(Qt::TextDate);
|
|
|
|
// Insert time between date's day and year:
|
|
|
|
buf.insert(buf.lastIndexOf(QLatin1Char(' ')),
|
|
|
|
QLatin1Char(' ') + p.second.toString(Qt::TextDate));
|
2016-11-21 16:48:04 +00:00
|
|
|
// Append zone/offset indicator, as appropriate:
|
|
|
|
switch (timeSpec()) {
|
|
|
|
case Qt::LocalTime:
|
|
|
|
break;
|
|
|
|
# if QT_CONFIG(timezone)
|
|
|
|
case Qt::TimeZone:
|
|
|
|
buf += QLatin1Char(' ') + d->m_timeZone.abbreviation(*this);
|
|
|
|
break;
|
|
|
|
# endif
|
|
|
|
default:
|
2016-07-06 08:32:54 +00:00
|
|
|
buf += QLatin1String(" GMT");
|
2016-05-12 06:14:46 +00:00
|
|
|
if (getSpec(d) == Qt::OffsetFromUTC)
|
2016-05-12 23:10:54 +00:00
|
|
|
buf += toOffsetString(Qt::TextDate, offsetFromUtc());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-07-18 09:37:57 +00:00
|
|
|
return buf;
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
2016-10-10 11:44:52 +00:00
|
|
|
case Qt::ISODate:
|
|
|
|
case Qt::ISODateWithMs: {
|
2016-05-12 23:10:54 +00:00
|
|
|
const QPair<QDate, QTime> p = getDateTime(d);
|
2015-01-21 14:04:59 +00:00
|
|
|
const QDate &dt = p.first;
|
|
|
|
const QTime &tm = p.second;
|
2013-08-12 19:21:42 +00:00
|
|
|
buf = dt.toString(Qt::ISODate);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (buf.isEmpty())
|
|
|
|
return QString(); // failed to convert
|
2013-07-18 09:37:57 +00:00
|
|
|
buf += QLatin1Char('T');
|
2016-10-10 11:44:52 +00:00
|
|
|
buf += tm.toString(format);
|
2016-05-12 06:14:46 +00:00
|
|
|
switch (getSpec(d)) {
|
2013-08-12 12:20:56 +00:00
|
|
|
case Qt::UTC:
|
2013-07-18 09:37:57 +00:00
|
|
|
buf += QLatin1Char('Z');
|
|
|
|
break;
|
2013-08-12 12:20:56 +00:00
|
|
|
case Qt::OffsetFromUTC:
|
2016-11-21 17:17:07 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
|
|
|
case Qt::TimeZone:
|
|
|
|
#endif
|
2016-05-01 20:20:52 +00:00
|
|
|
buf += toOffsetString(Qt::ISODate, offsetFromUtc());
|
2013-07-18 09:37:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return buf;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-04-11 10:25:37 +00:00
|
|
|
\fn QString QDateTime::toString(const QString &format) const
|
|
|
|
\fn QString QDateTime::toString(QStringView format) const
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Returns the datetime as a string. The \a format parameter
|
|
|
|
determines the format of the result string.
|
|
|
|
|
|
|
|
These expressions may be used for the date:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li d \li the day as number without a leading zero (1 to 31)
|
|
|
|
\row \li dd \li the day as number with a leading zero (01 to 31)
|
|
|
|
\row \li ddd
|
|
|
|
\li the abbreviated localized day name (e.g. 'Mon' to 'Sun').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li dddd
|
2016-11-21 16:05:49 +00:00
|
|
|
\li the long localized day name (e.g. 'Monday' to 'Sunday').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li M \li the month as number without a leading zero (1-12)
|
|
|
|
\row \li MM \li the month as number with a leading zero (01-12)
|
|
|
|
\row \li MMM
|
|
|
|
\li the abbreviated localized month name (e.g. 'Jan' to 'Dec').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li MMMM
|
|
|
|
\li the long localized month name (e.g. 'January' to 'December').
|
2013-07-26 21:48:11 +00:00
|
|
|
Uses the system locale to localize the name, i.e. QLocale::system().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li yy \li the year as two digit number (00-99)
|
|
|
|
\row \li yyyy \li the year as four digit number
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
These expressions may be used for the time:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li h
|
|
|
|
\li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
|
|
|
|
\row \li hh
|
|
|
|
\li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
|
2013-07-26 21:48:11 +00:00
|
|
|
\row \li H
|
|
|
|
\li the hour without a leading zero (0 to 23, even with AM/PM display)
|
|
|
|
\row \li HH
|
|
|
|
\li the hour with a leading zero (00 to 23, even with AM/PM display)
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li m \li the minute without a leading zero (0 to 59)
|
|
|
|
\row \li mm \li the minute with a leading zero (00 to 59)
|
2016-09-30 18:18:05 +00:00
|
|
|
\row \li s \li the whole second without a leading zero (0 to 59)
|
|
|
|
\row \li ss \li the whole second with a leading zero where applicable (00 to 59)
|
|
|
|
\row \li z \li the fractional part of the second, to go after a decimal
|
|
|
|
point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
|
|
|
|
reports the seconds to full available (millisecond) precision
|
|
|
|
without trailing zeroes.
|
|
|
|
\row \li zzz \li the fractional part of the second, to millisecond
|
|
|
|
precision, including trailing zeroes where applicable (000 to 999).
|
2013-07-26 21:48:11 +00:00
|
|
|
\row \li AP or A
|
|
|
|
\li use AM/PM display. \e A/AP will be replaced by either "AM" or "PM".
|
|
|
|
\row \li ap or a
|
|
|
|
\li use am/pm display. \e a/ap will be replaced by either "am" or "pm".
|
|
|
|
\row \li t \li the timezone (for example "CEST")
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
2018-11-07 16:02:07 +00:00
|
|
|
Any sequence of characters enclosed in single quotes will be included
|
|
|
|
verbatim in the output string (stripped of the quotes), even if it contains
|
|
|
|
formatting characters. Two consecutive single quotes ("''") are replaced by
|
|
|
|
a single quote in the output. All other characters in the format string are
|
|
|
|
included verbatim in the output string.
|
|
|
|
|
|
|
|
Formats without separators (e.g. "ddMM") are supported but must be used with
|
|
|
|
care, as the resulting strings aren't always reliably readable (e.g. if "dM"
|
|
|
|
produces "212" it could mean either the 2nd of December or the 21st of
|
|
|
|
February).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Example format strings (assumed that the QDateTime is 21 May 2001
|
2016-09-30 18:18:05 +00:00
|
|
|
14:13:09.120):
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Format \li Result
|
|
|
|
\row \li dd.MM.yyyy \li 21.05.2001
|
|
|
|
\row \li ddd MMMM d yy \li Tue May 21 01
|
2016-09-30 18:18:05 +00:00
|
|
|
\row \li hh:mm:ss.zzz \li 14:13:09.120
|
|
|
|
\row \li hh:mm:ss.z \li 14:13:09.12
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li h:m:s ap \li 2:13:9 pm
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
If the datetime is invalid, an empty string will be returned.
|
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa fromString(), QDate::toString(), QTime::toString(), QLocale::toString()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2017-04-11 10:25:37 +00:00
|
|
|
QString QDateTime::toString(QStringView format) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2016-09-08 17:24:04 +00:00
|
|
|
return QLocale::system().toString(*this, format); // QLocale::c() ### Qt6
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2017-04-11 10:25:37 +00:00
|
|
|
|
|
|
|
#if QT_STRINGVIEW_LEVEL < 2
|
|
|
|
QString QDateTime::toString(const QString &format) const
|
|
|
|
{
|
2017-04-19 23:33:21 +00:00
|
|
|
return toString(qToStringViewIgnoringNull(format));
|
2017-04-11 10:25:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-05-01 20:20:52 +00:00
|
|
|
static inline void massageAdjustedDateTime(const QDateTimeData &d, QDate *date, QTime *time)
|
2015-11-04 09:26:37 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
If we have just adjusted to a day with a DST transition, our given time
|
|
|
|
may lie in the transition hour (either missing or duplicated). For any
|
|
|
|
other time, telling mktime (deep in the bowels of localMSecsToEpochMSecs)
|
|
|
|
we don't know its DST-ness will produce no adjustment (just a decision as
|
|
|
|
to its DST-ness); but for a time in spring's missing hour it'll adjust the
|
|
|
|
time while picking a DST-ness. (Handling of autumn is trickier, as either
|
|
|
|
DST-ness is valid, without adjusting the time. We might want to propagate
|
2016-05-12 06:14:46 +00:00
|
|
|
the daylight status in that case, but it's hard to do so without breaking
|
2015-11-04 09:26:37 +00:00
|
|
|
(far more common) other cases; and it makes little difference, as the two
|
|
|
|
answers do then differ only in DST-ness.)
|
|
|
|
*/
|
2016-05-12 06:14:46 +00:00
|
|
|
auto spec = getSpec(d);
|
2015-11-04 09:26:37 +00:00
|
|
|
if (spec == Qt::LocalTime) {
|
|
|
|
QDateTimePrivate::DaylightStatus status = QDateTimePrivate::UnknownDaylightTime;
|
|
|
|
localMSecsToEpochMSecs(timeToMSecs(*date, *time), &status, date, time);
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2015-11-04 09:26:37 +00:00
|
|
|
} else if (spec == Qt::TimeZone) {
|
QDateTime, QTimeZone: fix mappings from zone time to UTC
Such mappings are ill-defined in the presence of daylight-savings time
(DST); at its transitions, you need information about whether DST is
active or not to determine the correct UTC value. Existing code did
not have a way to be told that hint, so could not be correct.
Fixing this required changing the (thankfully private) APIs by which
QDateTime accessed QTimeZone's information stipulated by zone time.
In QDateTime, this required propagating the needed hint, when DST
status was known.
QAndroidTimeZonePrivate overloaded QTimeZonePrivate::dataForLocalTime
with an implementation that works whenever !hasTransitions(); the base
implementation handled this case lamely, so I've moved the Android
implementation there, to have only one place for both re-writes.
Amended tst_QDateTime's expected failures; passing a date and time to
the constructor *is* ambiguous when the moment indicated is in a
transition. I have changed which way we resolve that ambiguity.
Added round-trip test of QDateTime's fromMSecs/toMSecs (but as a
QTimeZone test, since that's what's actually getting tested), based on
a test-case from Marko Kangas. Initially failed for various zones,
each at one hour-offset; and, on some platforms, for some zones, at
all offsets. These last revealed that a platform may claim to have
zone information yet, for some zones, lack it (or have very incomplete
information). In each case, despite this, the platform does give
offsetFromUtc(). (The test also found another pre-existing bug on
Linux; fixed in an earlier commit.)
To accommodate these gaps in transition data, the transition-based
code now falls back to the offsetFromUtc()-based code (used when there
are no transitions) if it can't find a previous transition (which, in
any case, it needs to do its job).
Task-number: QTBUG-56460
Task-number: QTBUG-56397
Task-number: QTBUG-52284
Change-Id: I2f7422a9e9d3767940b1901d887c6a2c1f36ac9f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2016-06-14 11:53:23 +00:00
|
|
|
QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(*date, *time),
|
|
|
|
d->m_timeZone,
|
|
|
|
QDateTimePrivate::UnknownDaylightTime,
|
|
|
|
date, time);
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2015-11-04 09:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Returns a QDateTime object containing a datetime \a ndays days
|
|
|
|
later than the datetime of this object (or earlier if \a ndays is
|
|
|
|
negative).
|
|
|
|
|
2013-08-19 14:50:54 +00:00
|
|
|
If the timeSpec() is Qt::LocalTime and the resulting
|
2015-11-09 11:05:49 +00:00
|
|
|
date and time fall in the Standard Time to Daylight-Saving Time transition
|
2013-08-19 14:50:54 +00:00
|
|
|
hour then the result will be adjusted accordingly, i.e. if the transition
|
|
|
|
is at 2am and the clock goes forward to 3am and the result falls between
|
|
|
|
2am and 3am then the result will be adjusted to fall after 3am.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa daysTo(), addMonths(), addYears(), addSecs()
|
|
|
|
*/
|
|
|
|
|
2011-08-25 06:54:16 +00:00
|
|
|
QDateTime QDateTime::addDays(qint64 ndays) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-08-01 13:51:08 +00:00
|
|
|
QDateTime dt(*this);
|
2016-05-12 23:10:54 +00:00
|
|
|
QPair<QDate, QTime> p = getDateTime(d);
|
2015-01-21 14:04:59 +00:00
|
|
|
QDate &date = p.first;
|
|
|
|
QTime &time = p.second;
|
2013-08-19 14:50:54 +00:00
|
|
|
date = date.addDays(ndays);
|
2016-05-12 06:38:21 +00:00
|
|
|
massageAdjustedDateTime(dt.d, &date, &time);
|
2016-05-12 23:10:54 +00:00
|
|
|
setDateTime(dt.d, date, time);
|
2013-08-01 13:51:08 +00:00
|
|
|
return dt;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QDateTime object containing a datetime \a nmonths months
|
|
|
|
later than the datetime of this object (or earlier if \a nmonths
|
|
|
|
is negative).
|
|
|
|
|
2013-08-19 14:50:54 +00:00
|
|
|
If the timeSpec() is Qt::LocalTime and the resulting
|
2015-11-09 11:05:49 +00:00
|
|
|
date and time fall in the Standard Time to Daylight-Saving Time transition
|
2013-08-19 14:50:54 +00:00
|
|
|
hour then the result will be adjusted accordingly, i.e. if the transition
|
|
|
|
is at 2am and the clock goes forward to 3am and the result falls between
|
|
|
|
2am and 3am then the result will be adjusted to fall after 3am.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa daysTo(), addDays(), addYears(), addSecs()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime QDateTime::addMonths(int nmonths) const
|
|
|
|
{
|
2013-08-01 13:51:08 +00:00
|
|
|
QDateTime dt(*this);
|
2016-05-12 23:10:54 +00:00
|
|
|
QPair<QDate, QTime> p = getDateTime(d);
|
2015-01-21 14:04:59 +00:00
|
|
|
QDate &date = p.first;
|
|
|
|
QTime &time = p.second;
|
2013-08-19 14:50:54 +00:00
|
|
|
date = date.addMonths(nmonths);
|
2016-05-12 06:38:21 +00:00
|
|
|
massageAdjustedDateTime(dt.d, &date, &time);
|
2016-05-12 23:10:54 +00:00
|
|
|
setDateTime(dt.d, date, time);
|
2013-08-01 13:51:08 +00:00
|
|
|
return dt;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QDateTime object containing a datetime \a nyears years
|
|
|
|
later than the datetime of this object (or earlier if \a nyears is
|
|
|
|
negative).
|
|
|
|
|
2013-08-19 14:50:54 +00:00
|
|
|
If the timeSpec() is Qt::LocalTime and the resulting
|
2015-11-09 11:05:49 +00:00
|
|
|
date and time fall in the Standard Time to Daylight-Saving Time transition
|
2013-08-19 14:50:54 +00:00
|
|
|
hour then the result will be adjusted accordingly, i.e. if the transition
|
|
|
|
is at 2am and the clock goes forward to 3am and the result falls between
|
|
|
|
2am and 3am then the result will be adjusted to fall after 3am.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa daysTo(), addDays(), addMonths(), addSecs()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime QDateTime::addYears(int nyears) const
|
|
|
|
{
|
2013-08-01 13:51:08 +00:00
|
|
|
QDateTime dt(*this);
|
2016-05-12 23:10:54 +00:00
|
|
|
QPair<QDate, QTime> p = getDateTime(d);
|
2015-01-21 14:04:59 +00:00
|
|
|
QDate &date = p.first;
|
|
|
|
QTime &time = p.second;
|
2013-08-19 14:50:54 +00:00
|
|
|
date = date.addYears(nyears);
|
2016-05-12 06:38:21 +00:00
|
|
|
massageAdjustedDateTime(dt.d, &date, &time);
|
2016-05-12 23:10:54 +00:00
|
|
|
setDateTime(dt.d, date, time);
|
2013-08-01 13:51:08 +00:00
|
|
|
return dt;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QDateTime object containing a datetime \a s seconds
|
|
|
|
later than the datetime of this object (or earlier if \a s is
|
|
|
|
negative).
|
|
|
|
|
2012-10-17 11:56:50 +00:00
|
|
|
If this datetime is invalid, an invalid datetime will be returned.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addMSecs(), secsTo(), addDays(), addMonths(), addYears()
|
|
|
|
*/
|
|
|
|
|
2012-10-09 13:15:53 +00:00
|
|
|
QDateTime QDateTime::addSecs(qint64 s) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return addMSecs(s * 1000);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a QDateTime object containing a datetime \a msecs miliseconds
|
|
|
|
later than the datetime of this object (or earlier if \a msecs is
|
|
|
|
negative).
|
|
|
|
|
2012-10-17 11:56:50 +00:00
|
|
|
If this datetime is invalid, an invalid datetime will be returned.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addSecs(), msecsTo(), addDays(), addMonths(), addYears()
|
|
|
|
*/
|
|
|
|
QDateTime QDateTime::addMSecs(qint64 msecs) const
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
if (!isValid())
|
|
|
|
return QDateTime();
|
|
|
|
|
|
|
|
QDateTime dt(*this);
|
2016-05-12 06:14:46 +00:00
|
|
|
auto spec = getSpec(d);
|
2016-05-01 20:20:52 +00:00
|
|
|
if (spec == Qt::LocalTime || spec == Qt::TimeZone) {
|
2015-11-09 11:05:49 +00:00
|
|
|
// Convert to real UTC first in case crosses DST transition
|
2016-05-12 05:27:36 +00:00
|
|
|
dt.setMSecsSinceEpoch(toMSecsSinceEpoch() + msecs);
|
2016-05-01 20:20:52 +00:00
|
|
|
} else {
|
2013-08-12 19:21:42 +00:00
|
|
|
// No need to convert, just add on
|
2016-05-01 20:20:52 +00:00
|
|
|
if (d.isShort()) {
|
|
|
|
// need to check if we need to enlarge first
|
|
|
|
msecs += dt.d.data.msecs;
|
2016-06-23 05:00:08 +00:00
|
|
|
if (msecsCanBeSmall(msecs)) {
|
|
|
|
dt.d.data.msecs = qintptr(msecs);
|
|
|
|
} else {
|
2016-05-01 20:20:52 +00:00
|
|
|
dt.d.detach();
|
|
|
|
dt.d->m_msecs = msecs;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dt.d.detach();
|
|
|
|
dt.d->m_msecs += msecs;
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 22:14:38 +00:00
|
|
|
return dt;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of days from this datetime to the \a other
|
2012-08-06 10:08:42 +00:00
|
|
|
datetime. The number of days is counted as the number of times
|
|
|
|
midnight is reached between this datetime to the \a other
|
|
|
|
datetime. This means that a 10 minute difference from 23:55 to
|
|
|
|
0:05 the next day counts as one day.
|
|
|
|
|
|
|
|
If the \a other datetime is earlier than this datetime,
|
2011-04-27 10:05:43 +00:00
|
|
|
the value returned is negative.
|
|
|
|
|
2012-08-06 10:08:42 +00:00
|
|
|
Example:
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 15
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addDays(), secsTo(), msecsTo()
|
|
|
|
*/
|
|
|
|
|
2011-08-25 06:54:16 +00:00
|
|
|
qint64 QDateTime::daysTo(const QDateTime &other) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return date().daysTo(other.date());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of seconds from this datetime to the \a other
|
|
|
|
datetime. If the \a other datetime is earlier than this datetime,
|
|
|
|
the value returned is negative.
|
|
|
|
|
|
|
|
Before performing the comparison, the two datetimes are converted
|
2015-11-09 11:05:49 +00:00
|
|
|
to Qt::UTC to ensure that the result is correct if daylight-saving
|
|
|
|
(DST) applies to one of the two datetimes but not the other.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-17 13:30:55 +00:00
|
|
|
Returns 0 if either datetime is invalid.
|
2011-07-17 18:30:12 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 11
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa addSecs(), daysTo(), QTime::secsTo()
|
|
|
|
*/
|
|
|
|
|
2012-10-09 13:15:53 +00:00
|
|
|
qint64 QDateTime::secsTo(const QDateTime &other) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return (msecsTo(other) / 1000);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the number of milliseconds from this datetime to the \a other
|
|
|
|
datetime. If the \a other datetime is earlier than this datetime,
|
|
|
|
the value returned is negative.
|
|
|
|
|
|
|
|
Before performing the comparison, the two datetimes are converted
|
2015-11-09 11:05:49 +00:00
|
|
|
to Qt::UTC to ensure that the result is correct if daylight-saving
|
|
|
|
(DST) applies to one of the two datetimes and but not the other.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-08-17 13:30:55 +00:00
|
|
|
Returns 0 if either datetime is invalid.
|
2011-07-17 18:30:12 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addMSecs(), daysTo(), QTime::msecsTo()
|
|
|
|
*/
|
|
|
|
|
|
|
|
qint64 QDateTime::msecsTo(const QDateTime &other) const
|
|
|
|
{
|
2011-07-17 18:30:12 +00:00
|
|
|
if (!isValid() || !other.isValid())
|
|
|
|
return 0;
|
|
|
|
|
2016-05-12 05:27:36 +00:00
|
|
|
return other.toMSecsSinceEpoch() - toMSecsSinceEpoch();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-02-25 13:36:29 +00:00
|
|
|
\fn QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-03 13:26:39 +00:00
|
|
|
Returns a copy of this datetime converted to the given time
|
2013-02-25 13:36:29 +00:00
|
|
|
\a spec.
|
|
|
|
|
|
|
|
If \a spec is Qt::OffsetFromUTC then it is set to Qt::UTC. To set to a
|
|
|
|
spec of Qt::OffsetFromUTC use toOffsetFromUtc().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
If \a spec is Qt::TimeZone then it is set to Qt::LocalTime,
|
|
|
|
i.e. the local Time Zone.
|
|
|
|
|
2012-09-03 13:26:39 +00:00
|
|
|
Example:
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 16
|
|
|
|
|
2019-05-24 12:20:43 +00:00
|
|
|
\sa timeSpec(), toTimeZone(), toOffsetFromUtc()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
if (getSpec(d) == spec && (spec == Qt::UTC || spec == Qt::LocalTime))
|
2013-10-15 15:36:36 +00:00
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (!isValid()) {
|
|
|
|
QDateTime ret = *this;
|
|
|
|
ret.setTimeSpec(spec);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-12 05:27:36 +00:00
|
|
|
return fromMSecsSinceEpoch(toMSecsSinceEpoch(), spec, 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
\fn QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const
|
|
|
|
|
|
|
|
Returns a copy of this datetime converted to a spec of Qt::OffsetFromUTC
|
|
|
|
with the given \a offsetSeconds.
|
|
|
|
|
|
|
|
If the \a offsetSeconds equals 0 then a UTC datetime will be returned
|
|
|
|
|
|
|
|
\sa setOffsetFromUtc(), offsetFromUtc(), toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
if (getSpec(d) == Qt::OffsetFromUTC
|
|
|
|
&& d->m_offsetFromUtc == offsetSeconds)
|
2013-10-15 15:36:36 +00:00
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (!isValid()) {
|
|
|
|
QDateTime ret = *this;
|
|
|
|
ret.setOffsetFromUtc(offsetSeconds);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-12 05:27:36 +00:00
|
|
|
return fromMSecsSinceEpoch(toMSecsSinceEpoch(), Qt::OffsetFromUTC, offsetSeconds);
|
2013-02-25 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Returns a copy of this datetime converted to the given \a timeZone
|
|
|
|
|
|
|
|
\sa timeZone(), toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime QDateTime::toTimeZone(const QTimeZone &timeZone) const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
if (getSpec(d) == Qt::TimeZone && d->m_timeZone == timeZone)
|
2013-10-15 15:36:36 +00:00
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (!isValid()) {
|
|
|
|
QDateTime ret = *this;
|
|
|
|
ret.setTimeZone(timeZone);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-12 05:27:36 +00:00
|
|
|
return fromMSecsSinceEpoch(toMSecsSinceEpoch(), timeZone);
|
2013-02-11 13:44:37 +00:00
|
|
|
}
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this datetime is equal to the \a other datetime;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator!=()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDateTime::operator==(const QDateTime &other) const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
if (getSpec(d) == Qt::LocalTime
|
|
|
|
&& getStatus(d) == getStatus(other.d)) {
|
2016-05-12 23:10:54 +00:00
|
|
|
return getMSecs(d) == getMSecs(other.d);
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
2013-08-08 22:14:38 +00:00
|
|
|
// Convert to UTC and compare
|
|
|
|
return (toMSecsSinceEpoch() == other.toMSecsSinceEpoch());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDateTime::operator!=(const QDateTime &other) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this datetime is different from the \a other
|
|
|
|
datetime; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Two datetimes are different if either the date, the time, or the
|
|
|
|
time zone components are different.
|
|
|
|
|
|
|
|
\sa operator==()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this datetime is earlier than the \a other
|
|
|
|
datetime; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool QDateTime::operator<(const QDateTime &other) const
|
|
|
|
{
|
2016-05-12 06:14:46 +00:00
|
|
|
if (getSpec(d) == Qt::LocalTime
|
|
|
|
&& getStatus(d) == getStatus(other.d)) {
|
2016-05-12 23:10:54 +00:00
|
|
|
return getMSecs(d) < getMSecs(other.d);
|
2013-08-12 19:21:42 +00:00
|
|
|
}
|
2013-08-08 22:14:38 +00:00
|
|
|
// Convert to UTC and compare
|
|
|
|
return (toMSecsSinceEpoch() < other.toMSecsSinceEpoch());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDateTime::operator<=(const QDateTime &other) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this datetime is earlier than or equal to the
|
|
|
|
\a other datetime; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDateTime::operator>(const QDateTime &other) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this datetime is later than the \a other datetime;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QDateTime::operator>=(const QDateTime &other) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this datetime is later than or equal to the
|
|
|
|
\a other datetime; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QDateTime QDateTime::currentDateTime()
|
|
|
|
Returns the current datetime, as reported by the system clock, in
|
|
|
|
the local time zone.
|
|
|
|
|
|
|
|
\sa currentDateTimeUtc(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QDateTime QDateTime::currentDateTimeUtc()
|
|
|
|
\since 4.7
|
|
|
|
Returns the current datetime, as reported by the system clock, in
|
|
|
|
UTC.
|
|
|
|
|
|
|
|
\sa currentDateTime(), QDate::currentDate(), QTime::currentTime(), toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn qint64 QDateTime::currentMSecsSinceEpoch()
|
|
|
|
\since 4.7
|
|
|
|
|
|
|
|
Returns the number of milliseconds since 1970-01-01T00:00:00 Universal
|
|
|
|
Coordinated Time. This number is like the POSIX time_t variable, but
|
|
|
|
expressed in milliseconds instead.
|
|
|
|
|
|
|
|
\sa currentDateTime(), currentDateTimeUtc(), toTime_t(), toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
2016-06-27 11:32:30 +00:00
|
|
|
/*!
|
|
|
|
\fn qint64 QDateTime::currentSecsSinceEpoch()
|
|
|
|
\since 5.8
|
|
|
|
|
|
|
|
Returns the number of seconds since 1970-01-01T00:00:00 Universal
|
|
|
|
Coordinated Time.
|
|
|
|
|
|
|
|
\sa currentMSecsSinceEpoch()
|
|
|
|
*/
|
|
|
|
|
2013-09-15 16:49:05 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2011-04-27 10:05:43 +00:00
|
|
|
static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0)
|
|
|
|
{
|
|
|
|
return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDate QDate::currentDate()
|
|
|
|
{
|
|
|
|
QDate d;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
memset(&st, 0, sizeof(SYSTEMTIME));
|
|
|
|
GetLocalTime(&st);
|
|
|
|
d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTime QTime::currentTime()
|
|
|
|
{
|
|
|
|
QTime ct;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
memset(&st, 0, sizeof(SYSTEMTIME));
|
|
|
|
GetLocalTime(&st);
|
2011-07-17 18:30:12 +00:00
|
|
|
ct.setHMS(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
|
2011-04-27 10:05:43 +00:00
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QDateTime::currentDateTime()
|
|
|
|
{
|
|
|
|
QDate d;
|
|
|
|
QTime t;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
memset(&st, 0, sizeof(SYSTEMTIME));
|
|
|
|
GetLocalTime(&st);
|
|
|
|
d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
|
|
|
|
t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
|
|
|
|
return QDateTime(d, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QDateTime::currentDateTimeUtc()
|
|
|
|
{
|
|
|
|
QDate d;
|
|
|
|
QTime t;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
memset(&st, 0, sizeof(SYSTEMTIME));
|
|
|
|
GetSystemTime(&st);
|
|
|
|
d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
|
|
|
|
t.mds = msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
|
|
|
|
return QDateTime(d, t, Qt::UTC);
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:27:18 +00:00
|
|
|
qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
SYSTEMTIME st;
|
|
|
|
memset(&st, 0, sizeof(SYSTEMTIME));
|
|
|
|
GetSystemTime(&st);
|
|
|
|
|
|
|
|
return msecsFromDecomposed(st.wHour, st.wMinute, st.wSecond, st.wMilliseconds) +
|
2012-01-09 20:50:00 +00:00
|
|
|
qint64(julianDayFromDate(st.wYear, st.wMonth, st.wDay)
|
|
|
|
- julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400000);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
qint64 QDateTime::currentSecsSinceEpoch() Q_DECL_NOTHROW
|
|
|
|
{
|
|
|
|
SYSTEMTIME st;
|
|
|
|
memset(&st, 0, sizeof(SYSTEMTIME));
|
|
|
|
GetSystemTime(&st);
|
|
|
|
|
|
|
|
return st.wHour * SECS_PER_HOUR + st.wMinute * SECS_PER_MIN + st.wSecond +
|
|
|
|
qint64(julianDayFromDate(st.wYear, st.wMonth, st.wDay)
|
|
|
|
- julianDayFromDate(1970, 1, 1)) * Q_INT64_C(86400);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#elif defined(Q_OS_UNIX)
|
|
|
|
QDate QDate::currentDate()
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return QDateTime::currentDateTime().date();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTime QTime::currentTime()
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return QDateTime::currentDateTime().time();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QDateTime::currentDateTime()
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), Qt::LocalTime);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime QDateTime::currentDateTimeUtc()
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
return fromMSecsSinceEpoch(currentMSecsSinceEpoch(), Qt::UTC);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-06 14:27:18 +00:00
|
|
|
qint64 QDateTime::currentMSecsSinceEpoch() Q_DECL_NOTHROW
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
// posix compliant system
|
|
|
|
// we have milliseconds
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return qint64(tv.tv_sec) * Q_INT64_C(1000) + tv.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
qint64 QDateTime::currentSecsSinceEpoch() Q_DECL_NOTHROW
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return qint64(tv.tv_sec);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
#else
|
|
|
|
#error "What system is this?"
|
|
|
|
#endif
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
#if QT_DEPRECATED_SINCE(5, 8)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\since 4.2
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of \a seconds
|
|
|
|
that have passed since 1970-01-01T00:00:00, Coordinated Universal
|
2013-03-01 15:36:31 +00:00
|
|
|
Time (Qt::UTC) and converted to Qt::LocalTime. On systems that do not
|
|
|
|
support time zones, the time will be set as if local time were Qt::UTC.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\note This function is deprecated. Please use fromSecsSinceEpoch() in new
|
|
|
|
code.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toTime_t(), setTime_t()
|
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromTime_t(uint seconds)
|
|
|
|
{
|
2013-03-01 15:36:31 +00:00
|
|
|
return fromMSecsSinceEpoch((qint64)seconds * 1000, Qt::LocalTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.2
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\deprecated
|
2013-03-01 15:36:31 +00:00
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of \a seconds
|
|
|
|
that have passed since 1970-01-01T00:00:00, Coordinated Universal
|
|
|
|
Time (Qt::UTC) and converted to the given \a spec.
|
|
|
|
|
|
|
|
If the \a spec is not Qt::OffsetFromUTC then the \a offsetSeconds will be
|
|
|
|
ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0
|
|
|
|
then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\note This function is deprecated. Please use fromSecsSinceEpoch() in new
|
|
|
|
code.
|
|
|
|
|
2013-03-01 15:36:31 +00:00
|
|
|
\sa toTime_t(), setTime_t()
|
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromTime_t(uint seconds, Qt::TimeSpec spec, int offsetSeconds)
|
|
|
|
{
|
|
|
|
return fromMSecsSinceEpoch((qint64)seconds * 1000, spec, offsetSeconds);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\deprecated
|
2013-02-11 13:44:37 +00:00
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of \a seconds
|
|
|
|
that have passed since 1970-01-01T00:00:00, Coordinated Universal
|
|
|
|
Time (Qt::UTC) and with the given \a timeZone.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\note This function is deprecated. Please use fromSecsSinceEpoch() in new
|
|
|
|
code.
|
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
\sa toTime_t(), setTime_t()
|
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromTime_t(uint seconds, const QTimeZone &timeZone)
|
|
|
|
{
|
|
|
|
return fromMSecsSinceEpoch((qint64)seconds * 1000, timeZone);
|
|
|
|
}
|
|
|
|
#endif
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
#endif // QT_DEPRECATED_SINCE(5, 8)
|
2013-02-11 13:44:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\since 4.7
|
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of milliseconds, \a msecs,
|
|
|
|
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
|
2013-03-01 15:36:31 +00:00
|
|
|
Time (Qt::UTC), and converted to Qt::LocalTime. On systems that do not
|
|
|
|
support time zones, the time will be set as if local time were Qt::UTC.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Note that there are possible values for \a msecs that lie outside the valid
|
|
|
|
range of QDateTime, both negative and positive. The behavior of this
|
|
|
|
function is undefined for those values.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\sa toMSecsSinceEpoch(), setMSecsSinceEpoch()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs)
|
|
|
|
{
|
2013-03-01 15:36:31 +00:00
|
|
|
return fromMSecsSinceEpoch(msecs, Qt::LocalTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of milliseconds \a msecs
|
|
|
|
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
|
|
|
|
Time (Qt::UTC) and converted to the given \a spec.
|
|
|
|
|
|
|
|
Note that there are possible values for \a msecs that lie outside the valid
|
|
|
|
range of QDateTime, both negative and positive. The behavior of this
|
|
|
|
function is undefined for those values.
|
|
|
|
|
|
|
|
If the \a spec is not Qt::OffsetFromUTC then the \a offsetSeconds will be
|
|
|
|
ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0
|
|
|
|
then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds.
|
|
|
|
|
2013-02-11 13:44:37 +00:00
|
|
|
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
|
|
|
|
i.e. the current system time zone.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\sa toMSecsSinceEpoch(), setMSecsSinceEpoch()
|
2013-03-01 15:36:31 +00:00
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds)
|
|
|
|
{
|
2013-08-08 22:14:38 +00:00
|
|
|
QDateTime dt;
|
2016-05-12 23:10:54 +00:00
|
|
|
QT_PREPEND_NAMESPACE(setTimeSpec(dt.d, spec, offsetSeconds));
|
2013-08-08 22:14:38 +00:00
|
|
|
dt.setMSecsSinceEpoch(msecs);
|
|
|
|
return dt;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
/*!
|
|
|
|
\since 5.8
|
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of seconds \a secs
|
|
|
|
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
|
|
|
|
Time (Qt::UTC) and converted to the given \a spec.
|
|
|
|
|
|
|
|
Note that there are possible values for \a secs that lie outside the valid
|
|
|
|
range of QDateTime, both negative and positive. The behavior of this
|
|
|
|
function is undefined for those values.
|
|
|
|
|
|
|
|
If the \a spec is not Qt::OffsetFromUTC then the \a offsetSeconds will be
|
|
|
|
ignored. If the \a spec is Qt::OffsetFromUTC and the \a offsetSeconds is 0
|
|
|
|
then the spec will be set to Qt::UTC, i.e. an offset of 0 seconds.
|
|
|
|
|
|
|
|
If \a spec is Qt::TimeZone then the spec will be set to Qt::LocalTime,
|
|
|
|
i.e. the current system time zone.
|
|
|
|
|
|
|
|
\sa toSecsSinceEpoch(), setSecsSinceEpoch()
|
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds)
|
|
|
|
{
|
|
|
|
return fromMSecsSinceEpoch(secs * 1000, spec, offsetSeconds);
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
/*!
|
|
|
|
\since 5.2
|
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of milliseconds \a msecs
|
|
|
|
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
|
|
|
|
Time (Qt::UTC) and with the given \a timeZone.
|
|
|
|
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
\sa fromSecsSinceEpoch()
|
2013-02-11 13:44:37 +00:00
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone)
|
|
|
|
{
|
|
|
|
QDateTime dt;
|
|
|
|
dt.setTimeZone(timeZone);
|
|
|
|
dt.setMSecsSinceEpoch(msecs);
|
|
|
|
return dt;
|
|
|
|
}
|
QDateTime: introduce {to,from,set,current}SecsSinceEpoch
These new functions use a 64-bit integer in the API, instead of the
broken 32-bit unsigned integer that the previous xxxTime_t functions
used. That was a design flaw when the API was introduced back in Qt 4.2,
so I'm deprecating the API and slating it for removal in 6.0.
The changes to qfilesystemmetadata_p.h and quuid.cpp are necessary to
build the bootstrap library. The rest of the adaptation to the new API
will come in the next commit.
[ChangeLog][QtCore][QDateTime] Introduced toSecsSinceEpoch,
fromSecsSinceEpoch and setSecsSinceEpoch functions, which use 64-bit
integers to represent the number of seconds.
[ChangeLog][QtCore][QDateTime] The toTime_t, fromTime_t and setTime_t
functions are deprecated and will be removed in Qt 6.0. For new code,
use the equivalent functions with "SecsSinceEpoch" in the name, or the
equivalent ones with millisecond accurancy that have existed since
Qt 4.7.
Change-Id: Ib57b52598e2f452985e9fffd145a355d0e7ff48d
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
2016-06-21 21:08:22 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.8
|
|
|
|
|
|
|
|
Returns a datetime whose date and time are the number of seconds \a secs
|
|
|
|
that have passed since 1970-01-01T00:00:00.000, Coordinated Universal
|
|
|
|
Time (Qt::UTC) and with the given \a timeZone.
|
|
|
|
|
|
|
|
\sa fromMSecsSinceEpoch()
|
|
|
|
*/
|
|
|
|
QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone)
|
|
|
|
{
|
|
|
|
return fromMSecsSinceEpoch(secs * 1000, timeZone);
|
|
|
|
}
|
2013-02-11 13:44:37 +00:00
|
|
|
#endif
|
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
#if QT_DEPRECATED_SINCE(5, 2)
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
2013-02-25 13:36:29 +00:00
|
|
|
\since 4.4
|
|
|
|
\internal
|
|
|
|
\obsolete
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
This method was added in 4.4 but never documented as public. It was replaced
|
|
|
|
in 5.2 with public method setOffsetFromUtc() for consistency with QTimeZone.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
This method should never be made public.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
\sa setOffsetFromUtc()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
void QDateTime::setUtcOffset(int seconds)
|
|
|
|
{
|
2013-02-25 13:36:29 +00:00
|
|
|
setOffsetFromUtc(seconds);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-02-25 13:36:29 +00:00
|
|
|
\since 4.4
|
|
|
|
\internal
|
|
|
|
\obsolete
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
This method was added in 4.4 but never documented as public. It was replaced
|
|
|
|
in 5.1 with public method offsetFromUTC() for consistency with QTimeZone.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
This method should never be made public.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-02-25 13:36:29 +00:00
|
|
|
\sa offsetFromUTC()
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
int QDateTime::utcOffset() const
|
|
|
|
{
|
2013-02-25 13:36:29 +00:00
|
|
|
return offsetFromUtc();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-02-25 13:36:29 +00:00
|
|
|
#endif // QT_DEPRECATED_SINCE
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
|
|
|
|
|
|
|
|
Returns the QDateTime represented by the \a string, using the
|
|
|
|
\a format given, or an invalid datetime if this is not possible.
|
|
|
|
|
|
|
|
Note for Qt::TextDate: It is recommended that you use the
|
|
|
|
English short month names (e.g. "Jan"). Although localized month
|
|
|
|
names can also be used, they depend on the user's locale settings.
|
2016-09-08 16:57:46 +00:00
|
|
|
|
|
|
|
\sa toString(), QLocale::toDateTime()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2013-07-18 09:37:57 +00:00
|
|
|
QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-07-18 09:37:57 +00:00
|
|
|
if (string.isEmpty())
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDateTime();
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
switch (format) {
|
|
|
|
case Qt::SystemLocaleDate:
|
|
|
|
case Qt::SystemLocaleShortDate:
|
|
|
|
return QLocale::system().toDateTime(string, QLocale::ShortFormat);
|
|
|
|
case Qt::SystemLocaleLongDate:
|
|
|
|
return QLocale::system().toDateTime(string, QLocale::LongFormat);
|
|
|
|
case Qt::LocaleDate:
|
|
|
|
case Qt::DefaultLocaleShortDate:
|
|
|
|
return QLocale().toDateTime(string, QLocale::ShortFormat);
|
|
|
|
case Qt::DefaultLocaleLongDate:
|
|
|
|
return QLocale().toDateTime(string, QLocale::LongFormat);
|
2013-07-13 09:01:34 +00:00
|
|
|
case Qt::RFC2822Date: {
|
2015-01-21 09:17:00 +00:00
|
|
|
const ParsedRfcDateTime rfc = rfcDateImpl(string);
|
2013-07-13 09:01:34 +00:00
|
|
|
|
2015-01-21 09:17:00 +00:00
|
|
|
if (!rfc.date.isValid() || !rfc.time.isValid())
|
2013-07-13 09:01:34 +00:00
|
|
|
return QDateTime();
|
|
|
|
|
2015-01-21 09:17:00 +00:00
|
|
|
QDateTime dateTime(rfc.date, rfc.time, Qt::UTC);
|
|
|
|
dateTime.setOffsetFromUtc(rfc.utcOffset);
|
2013-07-13 09:01:34 +00:00
|
|
|
return dateTime;
|
|
|
|
}
|
2016-10-10 11:44:52 +00:00
|
|
|
case Qt::ISODate:
|
|
|
|
case Qt::ISODateWithMs: {
|
2013-07-18 09:37:57 +00:00
|
|
|
const int size = string.size();
|
|
|
|
if (size < 10)
|
|
|
|
return QDateTime();
|
|
|
|
|
2013-09-12 12:18:45 +00:00
|
|
|
QDate date = QDate::fromString(string.left(10), Qt::ISODate);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!date.isValid())
|
|
|
|
return QDateTime();
|
|
|
|
if (size == 10)
|
|
|
|
return QDateTime(date);
|
|
|
|
|
2017-08-01 15:27:36 +00:00
|
|
|
Qt::TimeSpec spec = Qt::LocalTime;
|
|
|
|
QStringRef isoString(&string);
|
|
|
|
isoString = isoString.mid(10); // trim "yyyy-MM-dd"
|
|
|
|
|
|
|
|
// Must be left with T and at least one digit for the hour:
|
|
|
|
if (isoString.size() < 2
|
|
|
|
|| !(isoString.startsWith(QLatin1Char('T'))
|
|
|
|
// FIXME: QSql relies on QVariant::toDateTime() accepting a space here:
|
|
|
|
|| isoString.startsWith(QLatin1Char(' ')))) {
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
isoString = isoString.mid(1); // trim 'T' (or space)
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
int offset = 0;
|
2015-01-03 17:06:38 +00:00
|
|
|
// Check end of string for Time Zone definition, either Z for UTC or [+-]HH:mm for Offset
|
2013-07-18 09:37:57 +00:00
|
|
|
if (isoString.endsWith(QLatin1Char('Z'))) {
|
|
|
|
spec = Qt::UTC;
|
2017-08-01 15:27:36 +00:00
|
|
|
isoString.chop(1); // trim 'Z'
|
2013-07-18 09:37:57 +00:00
|
|
|
} else {
|
2013-09-12 12:18:45 +00:00
|
|
|
// the loop below is faster but functionally equal to:
|
|
|
|
// const int signIndex = isoString.indexOf(QRegExp(QStringLiteral("[+-]")));
|
2014-12-15 15:24:42 +00:00
|
|
|
int signIndex = isoString.size() - 1;
|
2017-08-01 15:27:36 +00:00
|
|
|
Q_ASSERT(signIndex >= 0);
|
2013-09-12 12:18:45 +00:00
|
|
|
bool found = false;
|
|
|
|
{
|
|
|
|
const QChar plus = QLatin1Char('+');
|
|
|
|
const QChar minus = QLatin1Char('-');
|
|
|
|
do {
|
|
|
|
QChar character(isoString.at(signIndex));
|
|
|
|
found = character == plus || character == minus;
|
2017-08-01 15:27:36 +00:00
|
|
|
} while (!found && --signIndex >= 0);
|
2013-09-12 12:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2013-07-18 09:37:57 +00:00
|
|
|
bool ok;
|
2014-05-14 13:25:30 +00:00
|
|
|
offset = fromOffsetString(isoString.mid(signIndex), &ok);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!ok)
|
|
|
|
return QDateTime();
|
|
|
|
isoString = isoString.left(signIndex);
|
|
|
|
spec = Qt::OffsetFromUTC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Might be end of day (24:00, including variants), which QTime considers invalid.
|
|
|
|
// ISO 8601 (section 4.2.3) says that 24:00 is equivalent to 00:00 the next day.
|
|
|
|
bool isMidnight24 = false;
|
2016-10-10 11:44:52 +00:00
|
|
|
QTime time = fromIsoTimeString(isoString, format, &isMidnight24);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!time.isValid())
|
|
|
|
return QDateTime();
|
|
|
|
if (isMidnight24)
|
|
|
|
date = date.addDays(1);
|
|
|
|
return QDateTime(date, time, spec, offset);
|
|
|
|
}
|
2018-09-19 11:28:13 +00:00
|
|
|
#if QT_CONFIG(textdate)
|
2011-04-27 10:05:43 +00:00
|
|
|
case Qt::TextDate: {
|
2014-05-14 13:25:30 +00:00
|
|
|
QVector<QStringRef> parts = string.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
if ((parts.count() < 5) || (parts.count() > 6))
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDateTime();
|
|
|
|
|
|
|
|
// Accept "Sun Dec 1 13:02:00 1974" and "Sun 1. Dec 13:02:00 1974"
|
2013-07-18 09:37:57 +00:00
|
|
|
int month = 0;
|
|
|
|
int day = 0;
|
|
|
|
bool ok = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
// First try month then day
|
2011-04-27 10:05:43 +00:00
|
|
|
month = fromShortMonthName(parts.at(1));
|
2013-07-18 09:37:57 +00:00
|
|
|
if (month)
|
|
|
|
day = parts.at(2).toInt();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
// If failed try day then month
|
|
|
|
if (!month || !day) {
|
2011-04-27 10:05:43 +00:00
|
|
|
month = fromShortMonthName(parts.at(2));
|
2013-07-18 09:37:57 +00:00
|
|
|
if (month) {
|
2014-05-14 13:25:30 +00:00
|
|
|
QStringRef dayStr = parts.at(1);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (dayStr.endsWith(QLatin1Char('.'))) {
|
2014-05-14 13:25:30 +00:00
|
|
|
dayStr = dayStr.left(dayStr.size() - 1);
|
2013-07-18 09:37:57 +00:00
|
|
|
day = dayStr.toInt();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
// If both failed, give up
|
|
|
|
if (!month || !day)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDateTime();
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
// Year can be before or after time, "Sun Dec 1 1974 13:02:00" or "Sun Dec 1 13:02:00 1974"
|
|
|
|
// Guess which by looking for ':' in the time
|
|
|
|
int year = 0;
|
|
|
|
int yearPart = 0;
|
|
|
|
int timePart = 0;
|
|
|
|
if (parts.at(3).contains(QLatin1Char(':'))) {
|
|
|
|
yearPart = 4;
|
|
|
|
timePart = 3;
|
|
|
|
} else if (parts.at(4).contains(QLatin1Char(':'))) {
|
|
|
|
yearPart = 3;
|
|
|
|
timePart = 4;
|
|
|
|
} else {
|
|
|
|
return QDateTime();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 09:37:57 +00:00
|
|
|
year = parts.at(yearPart).toInt(&ok);
|
|
|
|
if (!ok)
|
|
|
|
return QDateTime();
|
|
|
|
|
|
|
|
QDate date(year, month, day);
|
|
|
|
if (!date.isValid())
|
|
|
|
return QDateTime();
|
|
|
|
|
2014-05-14 13:25:30 +00:00
|
|
|
QVector<QStringRef> timeParts = parts.at(timePart).split(QLatin1Char(':'));
|
2013-07-18 09:37:57 +00:00
|
|
|
if (timeParts.count() < 2 || timeParts.count() > 3)
|
|
|
|
return QDateTime();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
int hour = timeParts.at(0).toInt(&ok);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!ok)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDateTime();
|
|
|
|
|
|
|
|
int minute = timeParts.at(1).toInt(&ok);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!ok)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QDateTime();
|
|
|
|
|
2013-06-14 11:43:16 +00:00
|
|
|
int second = 0;
|
|
|
|
int millisecond = 0;
|
|
|
|
if (timeParts.count() > 2) {
|
2016-04-07 12:09:00 +00:00
|
|
|
const QVector<QStringRef> secondParts = timeParts.at(2).split(QLatin1Char('.'));
|
2013-06-14 11:43:16 +00:00
|
|
|
if (secondParts.size() > 2) {
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
second = secondParts.first().toInt(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secondParts.size() > 1) {
|
|
|
|
millisecond = secondParts.last().toInt(&ok);
|
|
|
|
if (!ok) {
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 11:43:16 +00:00
|
|
|
QTime time(hour, minute, second, millisecond);
|
2013-07-18 09:37:57 +00:00
|
|
|
if (!time.isValid())
|
|
|
|
return QDateTime();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (parts.count() == 5)
|
|
|
|
return QDateTime(date, time, Qt::LocalTime);
|
|
|
|
|
2014-05-14 13:25:30 +00:00
|
|
|
QStringRef tz = parts.at(5);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
|
|
|
|
return QDateTime();
|
2014-05-14 13:25:30 +00:00
|
|
|
tz = tz.mid(3);
|
2013-02-25 13:36:29 +00:00
|
|
|
if (!tz.isEmpty()) {
|
|
|
|
int offset = fromOffsetString(tz, &ok);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!ok)
|
|
|
|
return QDateTime();
|
2013-02-25 13:36:29 +00:00
|
|
|
return QDateTime(date, time, Qt::OffsetFromUTC, offset);
|
|
|
|
} else {
|
|
|
|
return QDateTime(date, time, Qt::UTC);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-19 11:28:13 +00:00
|
|
|
#endif // textdate
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2018-01-16 11:38:23 +00:00
|
|
|
\fn QDateTime QDateTime::fromString(const QString &string, const QString &format)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the QDateTime represented by the \a string, using the \a
|
|
|
|
format given, or an invalid datetime if the string cannot be parsed.
|
|
|
|
|
|
|
|
These expressions may be used for the date part of the format string:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li d \li the day as number without a leading zero (1 to 31)
|
|
|
|
\row \li dd \li the day as number with a leading zero (01 to 31)
|
|
|
|
\row \li ddd
|
|
|
|
\li the abbreviated localized day name (e.g. 'Mon' to 'Sun').
|
2011-04-27 10:05:43 +00:00
|
|
|
Uses QDate::shortDayName().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li dddd
|
|
|
|
\li the long localized day name (e.g. 'Monday' to 'Sunday').
|
2011-04-27 10:05:43 +00:00
|
|
|
Uses QDate::longDayName().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li M \li the month as number without a leading zero (1-12)
|
|
|
|
\row \li MM \li the month as number with a leading zero (01-12)
|
|
|
|
\row \li MMM
|
|
|
|
\li the abbreviated localized month name (e.g. 'Jan' to 'Dec').
|
2011-04-27 10:05:43 +00:00
|
|
|
Uses QDate::shortMonthName().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li MMMM
|
|
|
|
\li the long localized month name (e.g. 'January' to 'December').
|
2011-04-27 10:05:43 +00:00
|
|
|
Uses QDate::longMonthName().
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li yy \li the year as two digit number (00-99)
|
|
|
|
\row \li yyyy \li the year as four digit number
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
\note Unlike the other version of this function, day and month names must
|
|
|
|
be given in the user's local language. It is only possible to use the English
|
|
|
|
names if the user's language is English.
|
|
|
|
|
|
|
|
These expressions may be used for the time part of the format string:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Expression \li Output
|
|
|
|
\row \li h
|
|
|
|
\li the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
|
|
|
|
\row \li hh
|
|
|
|
\li the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
|
|
|
|
\row \li H
|
|
|
|
\li the hour without a leading zero (0 to 23, even with AM/PM display)
|
|
|
|
\row \li HH
|
|
|
|
\li the hour with a leading zero (00 to 23, even with AM/PM display)
|
|
|
|
\row \li m \li the minute without a leading zero (0 to 59)
|
|
|
|
\row \li mm \li the minute with a leading zero (00 to 59)
|
2016-09-30 18:18:05 +00:00
|
|
|
\row \li s \li the whole second without a leading zero (0 to 59)
|
|
|
|
\row \li ss \li the whole second with a leading zero where applicable (00 to 59)
|
|
|
|
\row \li z \li the fractional part of the second, to go after a decimal
|
|
|
|
point, without trailing zeroes (0 to 999). Thus "\c{s.z}"
|
|
|
|
reports the seconds to full available (millisecond) precision
|
|
|
|
without trailing zeroes.
|
|
|
|
\row \li zzz \li the fractional part of the second, to millisecond
|
|
|
|
precision, including trailing zeroes where applicable (000 to 999).
|
2012-03-01 14:28:31 +00:00
|
|
|
\row \li AP or A
|
|
|
|
\li interpret as an AM/PM time. \e AP must be either "AM" or "PM".
|
|
|
|
\row \li ap or a
|
|
|
|
\li Interpret as an AM/PM time. \e ap must be either "am" or "pm".
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
All other input characters will be treated as text. Any sequence
|
2012-08-06 13:31:33 +00:00
|
|
|
of characters that are enclosed in single quotes will also be
|
2011-04-27 10:05:43 +00:00
|
|
|
treated as text and not be used as an expression.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 12
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
If the format is not satisfied, an invalid QDateTime is returned.
|
2011-04-27 10:05:43 +00:00
|
|
|
The expressions that don't have leading zeroes (d, M, h, m, s, z) will be
|
|
|
|
greedy. This means that they will use two digits even if this will
|
|
|
|
put them outside the range and/or leave too few digits for other
|
|
|
|
sections.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 13
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This could have meant 1 January 00:30.00 but the M will grab
|
|
|
|
two digits.
|
|
|
|
|
2013-01-03 12:01:17 +00:00
|
|
|
Incorrectly specified fields of the \a string will cause an invalid
|
|
|
|
QDateTime to be returned. For example, consider the following code,
|
|
|
|
where the two digit year 12 is read as 1912 (see the table below for all
|
|
|
|
field defaults); the resulting datetime is invalid because 23 April 1912
|
|
|
|
was a Tuesday, not a Monday:
|
|
|
|
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 20
|
|
|
|
|
|
|
|
The correct code is:
|
|
|
|
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 21
|
|
|
|
|
2012-09-04 22:52:53 +00:00
|
|
|
For any field that is not represented in the format, the following
|
2011-04-27 10:05:43 +00:00
|
|
|
defaults are used:
|
|
|
|
|
|
|
|
\table
|
2012-03-01 14:28:31 +00:00
|
|
|
\header \li Field \li Default value
|
|
|
|
\row \li Year \li 1900
|
|
|
|
\row \li Month \li 1 (January)
|
|
|
|
\row \li Day \li 1
|
|
|
|
\row \li Hour \li 0
|
|
|
|
\row \li Minute \li 0
|
|
|
|
\row \li Second \li 0
|
2011-04-27 10:05:43 +00:00
|
|
|
\endtable
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 14
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-09-08 16:57:46 +00:00
|
|
|
\sa toString(), QDate::fromString(), QTime::fromString(),
|
|
|
|
QLocale::toDateTime()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDateTime QDateTime::fromString(const QString &string, const QString &format)
|
|
|
|
{
|
2017-09-11 16:14:51 +00:00
|
|
|
#if QT_CONFIG(datetimeparser)
|
2011-04-27 10:05:43 +00:00
|
|
|
QTime time;
|
|
|
|
QDate date;
|
|
|
|
|
|
|
|
QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString);
|
2016-09-08 17:24:04 +00:00
|
|
|
// dt.setDefaultLocale(QLocale::c()); ### Qt 6
|
2011-04-27 10:05:43 +00:00
|
|
|
if (dt.parseFormat(format) && dt.fromString(string, &date, &time))
|
|
|
|
return QDateTime(date, time);
|
|
|
|
#else
|
|
|
|
Q_UNUSED(string);
|
|
|
|
Q_UNUSED(format);
|
|
|
|
#endif
|
2016-04-29 09:02:31 +00:00
|
|
|
return QDateTime();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn QDateTime QDateTime::toLocalTime() const
|
|
|
|
|
|
|
|
Returns a datetime containing the date and time information in
|
|
|
|
this datetime, but specified using the Qt::LocalTime definition.
|
|
|
|
|
2012-09-03 13:26:39 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 17
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn QDateTime QDateTime::toUTC() const
|
|
|
|
|
|
|
|
Returns a datetime containing the date and time information in
|
|
|
|
this datetime, but specified using the Qt::UTC definition.
|
|
|
|
|
2012-09-03 13:26:39 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
\snippet code/src_corelib_tools_qdatetime.cpp 18
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa toTimeSpec()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
Date/time stream functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QT_NO_DATASTREAM
|
|
|
|
/*!
|
|
|
|
\relates QDate
|
|
|
|
|
|
|
|
Writes the \a date to stream \a out.
|
|
|
|
|
|
|
|
\sa {Serializing Qt Data Types}
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator<<(QDataStream &out, const QDate &date)
|
|
|
|
{
|
2012-03-06 01:17:07 +00:00
|
|
|
if (out.version() < QDataStream::Qt_5_0)
|
|
|
|
return out << quint32(date.jd);
|
|
|
|
else
|
|
|
|
return out << qint64(date.jd);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QDate
|
|
|
|
|
|
|
|
Reads a date from stream \a in into the \a date.
|
|
|
|
|
|
|
|
\sa {Serializing Qt Data Types}
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator>>(QDataStream &in, QDate &date)
|
|
|
|
{
|
2012-03-06 01:17:07 +00:00
|
|
|
if (in.version() < QDataStream::Qt_5_0) {
|
|
|
|
quint32 jd;
|
|
|
|
in >> jd;
|
2012-08-28 16:00:24 +00:00
|
|
|
// Older versions consider 0 an invalid jd.
|
|
|
|
date.jd = (jd != 0 ? jd : QDate::nullJd());
|
2012-03-06 01:17:07 +00:00
|
|
|
} else {
|
|
|
|
qint64 jd;
|
|
|
|
in >> jd;
|
|
|
|
date.jd = jd;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QTime
|
|
|
|
|
|
|
|
Writes \a time to stream \a out.
|
|
|
|
|
|
|
|
\sa {Serializing Qt Data Types}
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator<<(QDataStream &out, const QTime &time)
|
|
|
|
{
|
2016-07-01 11:49:46 +00:00
|
|
|
if (out.version() >= QDataStream::Qt_4_0) {
|
|
|
|
return out << quint32(time.mds);
|
|
|
|
} else {
|
|
|
|
// Qt3 had no support for reading -1, QTime() was valid and serialized as 0
|
|
|
|
return out << quint32(time.isNull() ? 0 : time.mds);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QTime
|
|
|
|
|
|
|
|
Reads a time from stream \a in into the given \a time.
|
|
|
|
|
|
|
|
\sa {Serializing Qt Data Types}
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator>>(QDataStream &in, QTime &time)
|
|
|
|
{
|
|
|
|
quint32 ds;
|
|
|
|
in >> ds;
|
2016-07-01 11:49:46 +00:00
|
|
|
if (in.version() >= QDataStream::Qt_4_0) {
|
|
|
|
time.mds = int(ds);
|
|
|
|
} else {
|
|
|
|
// Qt3 would write 0 for a null time
|
|
|
|
time.mds = (ds == 0) ? QTime::NullTime : int(ds);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QDateTime
|
|
|
|
|
|
|
|
Writes \a dateTime to the \a out stream.
|
|
|
|
|
|
|
|
\sa {Serializing Qt Data Types}
|
|
|
|
*/
|
|
|
|
QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime)
|
|
|
|
{
|
2015-01-21 14:04:59 +00:00
|
|
|
QPair<QDate, QTime> dateAndTime;
|
2013-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
if (out.version() >= QDataStream::Qt_5_2) {
|
|
|
|
|
|
|
|
// In 5.2 we switched to using Qt::TimeSpec and added offset support
|
2016-05-12 23:10:54 +00:00
|
|
|
dateAndTime = getDateTime(dateTime.d);
|
2015-01-21 14:04:59 +00:00
|
|
|
out << dateAndTime << qint8(dateTime.timeSpec());
|
2013-08-20 21:15:29 +00:00
|
|
|
if (dateTime.timeSpec() == Qt::OffsetFromUTC)
|
|
|
|
out << qint32(dateTime.offsetFromUtc());
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
else if (dateTime.timeSpec() == Qt::TimeZone)
|
|
|
|
out << dateTime.timeZone();
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
} else if (out.version() == QDataStream::Qt_5_0) {
|
|
|
|
|
|
|
|
// In Qt 5.0 we incorrectly serialised all datetimes as UTC.
|
2013-08-12 19:21:42 +00:00
|
|
|
// This approach is wrong and should not be used again; it breaks
|
|
|
|
// the guarantee that a deserialised local datetime is the same time
|
|
|
|
// of day, regardless of which timezone it was serialised in.
|
2016-05-12 23:10:54 +00:00
|
|
|
dateAndTime = getDateTime((dateTime.isValid() ? dateTime.toUTC() : dateTime).d);
|
2015-01-21 14:04:59 +00:00
|
|
|
out << dateAndTime << qint8(dateTime.timeSpec());
|
2013-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
} else if (out.version() >= QDataStream::Qt_4_0) {
|
|
|
|
|
|
|
|
// From 4.0 to 5.1 (except 5.0) we used QDateTimePrivate::Spec
|
2016-05-12 23:10:54 +00:00
|
|
|
dateAndTime = getDateTime(dateTime.d);
|
2015-01-21 14:04:59 +00:00
|
|
|
out << dateAndTime;
|
2015-09-22 13:13:40 +00:00
|
|
|
switch (dateTime.timeSpec()) {
|
|
|
|
case Qt::UTC:
|
|
|
|
out << (qint8)QDateTimePrivate::UTC;
|
|
|
|
break;
|
|
|
|
case Qt::OffsetFromUTC:
|
|
|
|
out << (qint8)QDateTimePrivate::OffsetFromUTC;
|
|
|
|
break;
|
|
|
|
case Qt::TimeZone:
|
|
|
|
out << (qint8)QDateTimePrivate::TimeZone;
|
|
|
|
break;
|
|
|
|
case Qt::LocalTime:
|
|
|
|
out << (qint8)QDateTimePrivate::LocalUnknown;
|
|
|
|
break;
|
2013-08-12 12:20:56 +00:00
|
|
|
}
|
2013-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
} else { // version < QDataStream::Qt_4_0
|
|
|
|
|
|
|
|
// Before 4.0 there was no TimeSpec, only Qt::LocalTime was supported
|
2016-05-12 23:10:54 +00:00
|
|
|
dateAndTime = getDateTime(dateTime.d);
|
2015-01-21 14:04:59 +00:00
|
|
|
out << dateAndTime;
|
2013-08-20 21:15:29 +00:00
|
|
|
|
Serialise QDateTime as UTC and timeSpec() instead of private spec.
QDateTime currently serialises its private Spec enum. If a user was to
deserialise the individual components of a QDateTime separately, the
resulting timeSpec may be invalid when cast to the Qt::TimeSpec enum.
E.g.:
QDateTime dateTime(QDate(2012, 8, 14), QTime(8, 0, 0), Qt::UTC);
// ... serialise
// ... deserialise date, time, spec separately.
// spec == 2, the value of QDateTimePrivate::UTC.
// spec != UTC, will be set to LocalUnknown.
QDateTime deserialised(date, time, spec);
This patch serialises QDateTime objects in UTC and the value of
timeSpec() as opposed to QDateTimePrivate's spec. This changes the
serialisation behaviour of QDateTime for version 13 of QDataStream.
Task-number: QTBUG-4057
Change-Id: If650e7960dca7b6ab44b8233410a6369c41df73a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2012-08-16 13:55:31 +00:00
|
|
|
}
|
2013-08-20 21:15:29 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QDateTime
|
|
|
|
|
|
|
|
Reads a datetime from the stream \a in into \a dateTime.
|
|
|
|
|
|
|
|
\sa {Serializing Qt Data Types}
|
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
|
|
|
|
{
|
2013-08-12 19:21:42 +00:00
|
|
|
QDate dt;
|
|
|
|
QTime tm;
|
2013-08-20 21:15:29 +00:00
|
|
|
qint8 ts = 0;
|
|
|
|
Qt::TimeSpec spec = Qt::LocalTime;
|
|
|
|
qint32 offset = 0;
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
QTimeZone tz;
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
if (in.version() >= QDataStream::Qt_5_2) {
|
|
|
|
|
|
|
|
// In 5.2 we switched to using Qt::TimeSpec and added offset support
|
|
|
|
in >> dt >> tm >> ts;
|
|
|
|
spec = static_cast<Qt::TimeSpec>(ts);
|
2013-02-11 13:44:37 +00:00
|
|
|
if (spec == Qt::OffsetFromUTC) {
|
2013-02-25 13:36:29 +00:00
|
|
|
in >> offset;
|
2013-02-11 13:44:37 +00:00
|
|
|
dateTime = QDateTime(dt, tm, spec, offset);
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2013-02-11 13:44:37 +00:00
|
|
|
} else if (spec == Qt::TimeZone) {
|
|
|
|
in >> tz;
|
|
|
|
dateTime = QDateTime(dt, tm, tz);
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2013-02-11 13:44:37 +00:00
|
|
|
} else {
|
|
|
|
dateTime = QDateTime(dt, tm, spec);
|
|
|
|
}
|
2013-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
} else if (in.version() == QDataStream::Qt_5_0) {
|
|
|
|
|
|
|
|
// In Qt 5.0 we incorrectly serialised all datetimes as UTC
|
|
|
|
in >> dt >> tm >> ts;
|
|
|
|
spec = static_cast<Qt::TimeSpec>(ts);
|
|
|
|
dateTime = QDateTime(dt, tm, Qt::UTC);
|
|
|
|
dateTime = dateTime.toTimeSpec(spec);
|
|
|
|
|
|
|
|
} else if (in.version() >= QDataStream::Qt_4_0) {
|
|
|
|
|
|
|
|
// From 4.0 to 5.1 (except 5.0) we used QDateTimePrivate::Spec
|
|
|
|
in >> dt >> tm >> ts;
|
2013-08-12 12:20:56 +00:00
|
|
|
switch ((QDateTimePrivate::Spec)ts) {
|
|
|
|
case QDateTimePrivate::UTC:
|
2013-08-20 21:15:29 +00:00
|
|
|
spec = Qt::UTC;
|
2013-08-12 12:20:56 +00:00
|
|
|
break;
|
|
|
|
case QDateTimePrivate::OffsetFromUTC:
|
2013-08-20 21:15:29 +00:00
|
|
|
spec = Qt::OffsetFromUTC;
|
2013-08-12 12:20:56 +00:00
|
|
|
break;
|
2013-02-11 13:44:37 +00:00
|
|
|
case QDateTimePrivate::TimeZone:
|
|
|
|
spec = Qt::TimeZone;
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2016-01-18 11:40:41 +00:00
|
|
|
// FIXME: need to use a different constructor !
|
|
|
|
#endif
|
2013-02-11 13:44:37 +00:00
|
|
|
break;
|
2013-08-12 12:20:56 +00:00
|
|
|
case QDateTimePrivate::LocalUnknown:
|
|
|
|
case QDateTimePrivate::LocalStandard:
|
|
|
|
case QDateTimePrivate::LocalDST:
|
2013-08-20 21:15:29 +00:00
|
|
|
spec = Qt::LocalTime;
|
2013-08-12 12:20:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-08-20 21:15:29 +00:00
|
|
|
dateTime = QDateTime(dt, tm, spec, offset);
|
|
|
|
|
|
|
|
} else { // version < QDataStream::Qt_4_0
|
|
|
|
|
|
|
|
// Before 4.0 there was no TimeSpec, only Qt::LocalTime was supported
|
|
|
|
in >> dt >> tm;
|
|
|
|
dateTime = QDateTime(dt, tm, spec, offset);
|
|
|
|
|
Serialise QDateTime as UTC and timeSpec() instead of private spec.
QDateTime currently serialises its private Spec enum. If a user was to
deserialise the individual components of a QDateTime separately, the
resulting timeSpec may be invalid when cast to the Qt::TimeSpec enum.
E.g.:
QDateTime dateTime(QDate(2012, 8, 14), QTime(8, 0, 0), Qt::UTC);
// ... serialise
// ... deserialise date, time, spec separately.
// spec == 2, the value of QDateTimePrivate::UTC.
// spec != UTC, will be set to LocalUnknown.
QDateTime deserialised(date, time, spec);
This patch serialises QDateTime objects in UTC and the value of
timeSpec() as opposed to QDateTimePrivate's spec. This changes the
serialisation behaviour of QDateTime for version 13 of QDataStream.
Task-number: QTBUG-4057
Change-Id: If650e7960dca7b6ab44b8233410a6369c41df73a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2012-08-16 13:55:31 +00:00
|
|
|
}
|
2013-08-12 19:21:42 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
#endif // QT_NO_DATASTREAM
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2013-08-14 20:54:55 +00:00
|
|
|
Date / Time Debug Streams
|
2011-04-27 10:05:43 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-09-19 11:31:18 +00:00
|
|
|
#if !defined(QT_NO_DEBUG_STREAM) && QT_CONFIG(datestring)
|
2011-04-27 10:05:43 +00:00
|
|
|
QDebug operator<<(QDebug dbg, const QDate &date)
|
|
|
|
{
|
2015-01-23 15:19:11 +00:00
|
|
|
QDebugStateSaver saver(dbg);
|
2017-06-02 13:18:43 +00:00
|
|
|
dbg.nospace() << "QDate(";
|
|
|
|
if (date.isValid())
|
|
|
|
dbg.nospace() << date.toString(Qt::ISODate);
|
|
|
|
else
|
|
|
|
dbg.nospace() << "Invalid";
|
|
|
|
dbg.nospace() << ')';
|
2015-01-23 15:19:11 +00:00
|
|
|
return dbg;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug dbg, const QTime &time)
|
|
|
|
{
|
2015-01-23 15:19:11 +00:00
|
|
|
QDebugStateSaver saver(dbg);
|
2017-06-02 13:18:43 +00:00
|
|
|
dbg.nospace() << "QTime(";
|
|
|
|
if (time.isValid())
|
|
|
|
dbg.nospace() << time.toString(QStringViewLiteral("HH:mm:ss.zzz"));
|
|
|
|
else
|
|
|
|
dbg.nospace() << "Invalid";
|
|
|
|
dbg.nospace() << ')';
|
2015-01-23 15:19:11 +00:00
|
|
|
return dbg;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug dbg, const QDateTime &date)
|
|
|
|
{
|
2015-01-23 15:19:11 +00:00
|
|
|
QDebugStateSaver saver(dbg);
|
2015-02-15 20:01:39 +00:00
|
|
|
dbg.nospace() << "QDateTime(";
|
2017-06-02 13:18:43 +00:00
|
|
|
if (date.isValid()) {
|
|
|
|
const Qt::TimeSpec ts = date.timeSpec();
|
|
|
|
dbg.noquote() << date.toString(QStringViewLiteral("yyyy-MM-dd HH:mm:ss.zzz t"))
|
|
|
|
<< ' ' << ts;
|
|
|
|
switch (ts) {
|
|
|
|
case Qt::UTC:
|
|
|
|
break;
|
|
|
|
case Qt::OffsetFromUTC:
|
|
|
|
dbg.space() << date.offsetFromUtc() << 's';
|
|
|
|
break;
|
|
|
|
case Qt::TimeZone:
|
2016-08-26 11:05:24 +00:00
|
|
|
#if QT_CONFIG(timezone)
|
2017-06-02 13:18:43 +00:00
|
|
|
dbg.space() << date.timeZone().id();
|
2016-08-26 11:05:24 +00:00
|
|
|
#endif // timezone
|
2017-06-02 13:18:43 +00:00
|
|
|
break;
|
|
|
|
case Qt::LocalTime:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dbg.nospace() << "Invalid";
|
2013-03-21 19:44:33 +00:00
|
|
|
}
|
2017-06-02 13:18:43 +00:00
|
|
|
return dbg.nospace() << ')';
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2018-09-19 11:31:18 +00:00
|
|
|
#endif // debug_stream && datestring
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-06-28 13:53:41 +00:00
|
|
|
/*! \fn uint qHash(const QDateTime &key, uint seed = 0)
|
|
|
|
\relates QHash
|
|
|
|
\since 5.0
|
|
|
|
|
|
|
|
Returns the hash value for the \a key, using \a seed to seed the calculation.
|
|
|
|
*/
|
|
|
|
uint qHash(const QDateTime &key, uint seed)
|
|
|
|
{
|
|
|
|
// Use to toMSecsSinceEpoch instead of individual qHash functions for
|
|
|
|
// QDate/QTime/spec/offset because QDateTime::operator== converts both arguments
|
|
|
|
// to the same timezone. If we don't, qHash would return different hashes for
|
|
|
|
// two QDateTimes that are equivalent once converted to the same timezone.
|
|
|
|
return qHash(key.toMSecsSinceEpoch(), seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \fn uint qHash(const QDate &key, uint seed = 0)
|
|
|
|
\relates QHash
|
|
|
|
\since 5.0
|
|
|
|
|
|
|
|
Returns the hash value for the \a key, using \a seed to seed the calculation.
|
|
|
|
*/
|
2012-08-02 13:51:52 +00:00
|
|
|
uint qHash(const QDate &key, uint seed) Q_DECL_NOTHROW
|
2012-06-28 13:53:41 +00:00
|
|
|
{
|
|
|
|
return qHash(key.toJulianDay(), seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \fn uint qHash(const QTime &key, uint seed = 0)
|
|
|
|
\relates QHash
|
|
|
|
\since 5.0
|
|
|
|
|
|
|
|
Returns the hash value for the \a key, using \a seed to seed the calculation.
|
|
|
|
*/
|
2012-08-02 13:51:52 +00:00
|
|
|
uint qHash(const QTime &key, uint seed) Q_DECL_NOTHROW
|
2012-06-28 13:53:41 +00:00
|
|
|
{
|
2013-08-13 12:17:08 +00:00
|
|
|
return qHash(key.msecsSinceStartOfDay(), seed);
|
2012-06-28 13:53:41 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QT_END_NAMESPACE
|