Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
src_corelib_time_qdatetime.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5QDate d1(1995, 5, 17); // May 17, 1995
6QDate d2(1995, 5, 20); // May 20, 1995
7d1.daysTo(d2); // returns 3
8d2.daysTo(d1); // returns -3
10
11
13QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy");
14// date is 1 December 2003
16
17
19QDate date = QDate::fromString("130", "Md"); // invalid
21
22
24QDate::fromString("1.30", "M.d"); // January 30 1900
25QDate::fromString("20000110", "yyyyMMdd"); // January 10, 2000
26QDate::fromString("20000110", "yyyyMd"); // January 10, 2000
28
29
31QDate::isValid(2002, 5, 17); // true
32QDate::isValid(2002, 2, 30); // false (Feb 30 does not exist)
33QDate::isValid(2004, 2, 29); // true (2004 is a leap year)
34QDate::isValid(2000, 2, 29); // true (2000 is a leap year)
35QDate::isValid(2006, 2, 29); // false (2006 is not a leap year)
36QDate::isValid(2100, 2, 29); // false (2100 is not a leap year)
37QDate::isValid(1202, 6, 6); // true (even though 1202 is pre-Gregorian)
39
40
42QTime n(14, 0, 0); // n == 14:00:00
44t = n.addSecs(70); // t == 14:01:10
45t = n.addSecs(-70); // t == 13:58:50
46t = n.addSecs(10 * 60 * 60 + 5); // t == 00:00:05
47t = n.addSecs(-15 * 60 * 60); // t == 23:00:00
49
50
52QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss");
53// time is 12:01.00
55
56
58QTime time = QTime::fromString("00:710", "hh:ms"); // invalid
60
61
63QTime time = QTime::fromString("1.30", "m.s");
64// time is 00:01:30.000
66
67
69QTime::isValid(21, 10, 30); // returns true
70QTime::isValid(22, 5, 62); // returns false
72
73
77qDebug("There are %d seconds to Christmas", now.secsTo(xmas));
79
80
82QTime time1 = QTime::fromString("131", "HHh");
83// time1 is 13:00:00
84QTime time1 = QTime::fromString("1apA", "1amAM");
85// time1 is 01:00:00
86
87QDateTime dateTime2 = QDateTime::fromString("M1d1y9800:01:02",
88 "'M'M'd'd'y'yyhh:mm:ss");
89// dateTime is 1 January 1998 00:01:02
91
92
94QDateTime dateTime = QDateTime::fromString("130", "Mm"); // invalid
96
97
99QDateTime dateTime = QDateTime::fromString("1.30.1", "M.d.s");
100// dateTime is January 30 in 1900 at 00:00:01.
101dateTime = QDateTime::fromString("12", "yy");
102// dateTime is January 1 in 1912 at 00:00:00.
104
106QDateTime startDate(QDate(2012, 7, 6), QTime(8, 30, 0));
107QDateTime endDate(QDate(2012, 7, 7), QTime(16, 30, 0));
108qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate);
109
110startDate = QDateTime(QDate(2012, 7, 6), QTime(23, 55, 0));
111endDate = QDateTime(QDate(2012, 7, 7), QTime(0, 5, 0));
112qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate);
113
114qSwap(startDate, endDate); // Make endDate before startDate.
115qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate);
117
120QDateTime UTC(local.toTimeSpec(Qt::UTC));
121qDebug() << "Local time is:" << local;
122qDebug() << "UTC time is:" << UTC;
123qDebug() << "No difference between times:" << local.secsTo(UTC);
125
128QDateTime local(UTC.toLocalTime());
129qDebug() << "UTC time is:" << UTC;
130qDebug() << "Local time is:" << local;
131qDebug() << "No difference between times:" << UTC.secsTo(local);
133
136QDateTime UTC(local.toUTC());
137qDebug() << "Local time is:" << local;
138qDebug() << "UTC time is:" << UTC;
139qDebug() << "No difference between times:" << local.secsTo(UTC);
141
144qDebug() << "Local time is:" << local;
145
146QDateTime UTC(local);
147UTC.setTimeSpec(Qt::UTC);
148qDebug() << "UTC time is:" << UTC;
149
150qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes.";
152
154QString string = "Monday, 23 April 12 22:51:41";
155QString format = "dddd, d MMMM yy hh:mm:ss";
156QDateTime invalid = QDateTime::fromString(string, format);
158
160QString string = "Tuesday, 23 April 12 22:51:41";
161QString format = "dddd, d MMMM yy hh:mm:ss";
162QDateTime valid = QDateTime::fromString(string, format);
164
166// 23 April 2012:
167QDate date = std::chrono::year_month_day(std::chrono::year(2012),
168 std::chrono::month(4),
169 std::chrono::day(23));
170
171// Same, under `using std::chrono` convenience:
172QDate dateWithLiterals1 = 23 / April / 2012y;
173QDate dateWithLiterals2 = 2012y / April / 23;
174
175// Last day of February 2000
176QDate lastDayFeb2020 = 2000y / February / last;
177
178// First Monday of January 2020:
179QDate firstMonday = 2020y / January / Monday[0];
180
181// Last Monday of January 2020:
182QDate lastMonday = 2020y / January / Monday[last];
184
187QDateTime UTC(local.toTimeSpec(QTimeZone::UTC));
188qDebug() << "Local time is:" << local;
189qDebug() << "UTC time is:" << UTC;
190qDebug() << "No difference between times represented:" << local.secsTo(UTC);
\inmodule QtCore\reentrant
Definition qdatetime.h:283
static QDateTime currentDateTime()
This is an overloaded member function, provided for convenience. It differs from the above function o...
qint64 secsTo(const QDateTime &) const
Returns the number of seconds from this datetime to the other datetime.
static QDateTime currentDateTimeUtc()
qint64 daysTo(const QDateTime &) const
Returns the number of days from this datetime to the other datetime.
QDate date() const
Returns the date part of the datetime.
\inmodule QtCore \reentrant
Definition qdatetime.h:29
qint64 daysTo(QDate d) const
Returns the number of days from this date to d (which is negative if d is earlier than this date).
constexpr bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition qdatetime.h:71
int year() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QDateTime startOfDay(const QTimeZone &zone) const
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore \reentrant
Definition qdatetime.h:215
bool isValid() const
Returns true if the time is valid; otherwise returns false.
QTime addSecs(int secs) const
Returns a QTime object containing a time s seconds later than the time of this object (or earlier if ...
@ UTC
#define qDebug
[1]
Definition qlogging.h:164
GLfloat n
GLint GLsizei GLsizei GLenum format
GLint y
GLdouble GLdouble t
Definition qopenglext.h:243
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
static double UTC(double t, double localTZA)
QDate d1(1995, 5, 17)
[0]
QDateTime dateTime
[12]
QDateTime dateTime2
QDateTime now
[9]
QDate d2(1995, 5, 20)
QDateTime startDate(QDate(2012, 7, 6), QTime(8, 30, 0))
[14]
QTime time1
[11]
QTime n(14, 0, 0)
[4]
QDateTime xmas(QDate(now.date().year(), 12, 25).startOfDay())
QDateTime endDate(QDate(2012, 7, 7), QTime(16, 30, 0))