Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
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
4#include <QDate>
5#include <QTime>
6#include <QTimeZone>
7#include <chrono>
8#include <iostream>
9
11{
12 {
13 //! [0]
14 QDate d1(1995, 5, 17); // May 17, 1995
15 QDate d2(1995, 5, 20); // May 20, 1995
16 d1.daysTo(d2); // returns 3
17 d2.daysTo(d1); // returns -3
18 //! [0]
19 }
20
21 {
22 //! [1]
23 QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy");
24 // date is 1 December 2003
25 //! [1]
26 }
27
28 {
29 //! [2]
30 QDate date = QDate::fromString("130", "Md"); // invalid
31 //! [2]
32 }
33
34 {
35 //! [3]
36 QDate::fromString("1.30", "M.d"); // January 30 1900
37 QDate::fromString("20000110", "yyyyMMdd"); // January 10, 2000
38 QDate::fromString("20000110", "yyyyMd"); // January 10, 2000
39 //! [3]
40 }
41
42 {
43 //! [4]
44 QDate::isValid(2002, 5, 17); // true
45 QDate::isValid(2002, 2, 30); // false (Feb 30 does not exist)
46 QDate::isValid(2004, 2, 29); // true (2004 is a leap year)
47 QDate::isValid(2000, 2, 29); // true (2000 is a leap year)
48 QDate::isValid(2006, 2, 29); // false (2006 is not a leap year)
49 QDate::isValid(2100, 2, 29); // false (2100 is not a leap year)
50 QDate::isValid(1202, 6, 6); // true (even though 1202 is pre-Gregorian)
51 //! [4]
52 }
53
54 {
55 //! [5]
56 QTime n(14, 0, 0); // n == 14:00:00
57 QTime t;
58 t = n.addSecs(70); // t == 14:01:10
59 t = n.addSecs(-70); // t == 13:58:50
60 t = n.addSecs(10 * 60 * 60 + 5); // t == 00:00:05
61 t = n.addSecs(-15 * 60 * 60); // t == 23:00:00
62 //! [5]
63 }
64
65 {
66 //! [6]
67 QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss");
68 // time is 12:01.00
69 //! [6]
70 }
71
72 {
73 //! [7]
74 QTime time = QTime::fromString("00:710", "hh:ms"); // invalid
75 //! [7]
76 }
77
78 {
79 //! [8]
80 QTime time = QTime::fromString("1.30", "m.s");
81 // time is 00:01:30.000
82 //! [8]
83 }
84
85 {
86 //! [9]
87 QTime::isValid(21, 10, 30); // returns true
88 QTime::isValid(22, 5, 62); // returns false
89 //! [9]
90 }
91
92 {
93 //! [11]
94 QDateTime now = QDateTime::currentDateTime();
95 QDateTime xmas(QDate(now.date().year(), 12, 25).startOfDay());
96 qDebug("There are %d seconds to Christmas", now.secsTo(xmas));
97 //! [11]
98 }
99
100 {
101 //! [12]
102 QTime time1 = QTime::fromString("131", "HHh");
103 // time1 is 13:00:00
104 QTime time2 = QTime::fromString("1apA", "1amAM");
105 // time2 is 01:00:00
106
107 QDateTime dateTime2 = QDateTime::fromString("M1d1y9800:01:02",
108 "'M'M'd'd'y'yyhh:mm:ss");
109 // dateTime is 1 January 1998 00:01:02
110 //! [12]
111 }
112
113 {
114 //! [13]
115 QDateTime dateTime = QDateTime::fromString("130", "Mm"); // invalid
116 //! [13]
117 }
118
119 {
120 //! [14]
121 QDateTime dateTime = QDateTime::fromString("1.30.1", "M.d.s");
122 // dateTime is January 30 in 1900 at 00:00:01.
123 dateTime = QDateTime::fromString("12", "yy");
124 // dateTime is January 1 in 1912 at 00:00:00.
125 //! [14]
126 }
127
128 {
129 //! [15]
130 QDateTime startDate(QDate(2012, 7, 6), QTime(8, 30, 0));
131 QDateTime endDate(QDate(2012, 7, 7), QTime(16, 30, 0));
132 qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate);
133
134 startDate = QDateTime(QDate(2012, 7, 6), QTime(23, 55, 0));
135 endDate = QDateTime(QDate(2012, 7, 7), QTime(0, 5, 0));
136 qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate);
137
138 qSwap(startDate, endDate); // Make endDate before startDate.
139 qDebug() << "Days from startDate to endDate: " << startDate.daysTo(endDate);
140 //! [15]
141 }
142
143#if QT_DEPRECATED_SINCE(6, 9)
144 {
145 //! [16]
146 QDateTime local(QDateTime::currentDateTime());
147 QDateTime UTC(local.toTimeSpec(Qt::UTC));
148 qDebug() << "Local time is:" << local;
149 qDebug() << "UTC time is:" << UTC;
150 qDebug() << "No difference between times:" << local.secsTo(UTC);
151 //! [16]
152 }
153#endif // QT_DEPRECATED_SINCE(6, 9)
154
155 {
156 //! [17]
157 QDateTime UTC(QDateTime::currentDateTimeUtc());
158 QDateTime local(UTC.toLocalTime());
159 qDebug() << "UTC time is:" << UTC;
160 qDebug() << "Local time is:" << local;
161 qDebug() << "No difference between times:" << UTC.secsTo(local);
162 //! [17]
163 }
164
165 {
166 //! [18]
167 QDateTime local(QDateTime::currentDateTime());
168 QDateTime UTC(local.toUTC());
169 qDebug() << "Local time is:" << local;
170 qDebug() << "UTC time is:" << UTC;
171 qDebug() << "No difference between times:" << local.secsTo(UTC);
172 //! [18]
173 }
174
175#if QT_DEPRECATED_SINCE(6, 9)
176 {
177 //! [19]
178 QDateTime local(QDateTime::currentDateTime());
179 qDebug() << "Local time is:" << local;
180
181 QDateTime UTC(local);
182 UTC.setTimeSpec(Qt::UTC);
183 qDebug() << "UTC time is:" << UTC;
184
185 qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes.";
186 //! [19]
187 }
188#endif // QT_DEPRECATED_SINCE(6, 9)
189
190#if defined(__cpp_lib_crono) && __cpp_lib_crono > 201907L
191 {
192 using namespace std::chrono;
193
194 //! [22]
195 // 23 April 2012:
196 QDate date = std::chrono::year_month_day(std::chrono::year(2012),
197 std::chrono::month(4),
198 std::chrono::day(23));
199
200 // Same, under `using std::chrono` convenience:
201 QDate dateWithLiterals1 = 23d / April / 2012y;
202 QDate dateWithLiterals2 = 2012y / April / 23;
203
204 // Last day of February 2000
205 QDate lastDayFeb2020 = 2000y / February / last;
206
207 // First Monday of January 2020:
208 QDate firstMonday = 2020y / January / Monday[0];
209
210 // Last Monday of January 2020:
211 QDate lastMonday = 2020y / January / Monday[last];
212 //! [22]
213 }
214#endif
215
216 {
217 //! [23]
218 QDateTime local(QDateTime::currentDateTime());
219 QDateTime UTC(local.toTimeZone(QTimeZone::UTC));
220 qDebug() << "Local time is:" << local;
221 qDebug() << "UTC time is:" << UTC;
222 qDebug() << "No difference between times represented:" << local.secsTo(UTC);
223 //! [23]
224 }
225}
bool examples()
[3]