KAlarmCal

datetime.cpp
1 /*
2  * datetime.cpp - date/time with start-of-day time for date-only values
3  * This file is part of kalarmcal library, which provides access to KAlarm
4  * calendar data.
5  * SPDX-FileCopyrightText: 2003-2019 David Jarvie <[email protected]>
6  *
7  * SPDX-License-Identifier: LGPL-2.0-or-later
8  */
9 #include "datetime.h"
10 
11 #include <QTimeZone>
12 #include <QLocale>
13 
14 namespace KAlarmCal
15 {
16 
17 class Q_DECL_HIDDEN DateTime::Private
18 {
19 public:
20  Private() {}
21  Private(const QDate &d, const KADateTime::Spec &spec) : mDateTime(d, spec) {}
22  Private(const QDate &d, const QTime &t, const KADateTime::Spec &spec) : mDateTime(d, t, spec) {}
23  Private(const QDateTime &dt, const KADateTime::Spec &spec) : mDateTime(dt, spec) {}
24  Private(const QDateTime &dt) : mDateTime(dt) {}
25  Private(const KADateTime &dt) : mDateTime(dt) {}
26 
27  static QTime mStartOfDay;
28  KADateTime mDateTime;
29 };
30 
31 QTime DateTime::Private::mStartOfDay;
32 
34  : d(new Private)
35 {
36 }
37 
39  : d(new Private(d, spec))
40 {
41 }
42 
43 DateTime::DateTime(const QDate &d, const QTime &t, const KADateTime::Spec &spec)
44  : d(new Private(d, t, spec))
45 {
46 }
47 
49  : d(new Private(dt, spec))
50 {
51 }
52 
54  : d(new Private(dt))
55 {
56 }
57 
59  : d(new Private(dt))
60 {
61 }
62 
64  : d(new Private(*dt.d))
65 {
66 }
67 
68 DateTime::~DateTime()
69 {
70  delete d;
71 }
72 
74 {
75  if (&dt != this) {
76  *d = *dt.d;
77  }
78  return *this;
79 }
80 
82 {
83  d->mDateTime = dt;
84  return *this;
85 }
86 
87 bool DateTime::isNull() const
88 {
89  return d->mDateTime.isNull();
90 }
91 
92 bool DateTime::isValid() const
93 {
94  return d->mDateTime.isValid();
95 }
96 
98 {
99  return d->mDateTime.isDateOnly();
100 }
101 
102 void DateTime::setDateOnly(bool dateOnly)
103 {
104  d->mDateTime.setDateOnly(dateOnly);
105 }
106 
108 {
109  return d->mDateTime.date();
110 }
111 
113 {
114  d->mDateTime.setDate(date);
115 }
116 
118 {
119  return d->mDateTime.qDateTime();
120 }
121 
123 {
124  return d->mDateTime;
125 }
126 
128 {
129  return d->mDateTime.isDateOnly() ? d->mStartOfDay : d->mDateTime.time();
130 }
131 
132 void DateTime::setTime(const QTime &t)
133 {
134  d->mDateTime.setTime(t);
135 }
136 
138 {
139  if (d->mDateTime.isDateOnly()) {
140  QDateTime dt = d->mDateTime.qDateTime(); // preserve Qt::UTC or Qt::LocalTime
141  dt.setTime(d->mStartOfDay);
142  return dt;
143  }
144  return d->mDateTime.qDateTime();
145 }
146 
148 {
149  if (d->mDateTime.isDateOnly()) {
150  KADateTime dt = d->mDateTime;
151  dt.setTime(d->mStartOfDay);
152  return dt;
153  }
154  return d->mDateTime;
155 }
156 
158 {
159  if (d->mDateTime.isDateOnly()) {
160  QDateTime dt = d->mDateTime.qDateTime();
161  dt.setTime(QTime(0, 0));
162  return dt;
163  }
164  return d->mDateTime.qDateTime();
165 }
166 
168 {
169  if (d->mDateTime.isDateOnly()) {
170  KADateTime dt = d->mDateTime;
171  dt.setTime(QTime(0, 0));
172  return dt;
173  }
174  return d->mDateTime;
175 }
176 
178 {
179  return d->mDateTime.timeZone();
180 }
181 
183 {
184  return d->mDateTime.timeSpec();
185 }
186 
188 {
189  d->mDateTime.setTimeSpec(spec);
190 }
191 
193 {
194  return d->mDateTime.timeType();
195 }
196 
198 {
199  return d->mDateTime.isLocalZone();
200 }
201 
202 bool DateTime::isUtc() const
203 {
204  return d->mDateTime.isUtc();
205 }
206 
208 {
209  return d->mDateTime.isOffsetFromUtc();
210 }
211 
213 {
214  return d->mDateTime.utcOffset();
215 }
216 
218 {
219  return d->mDateTime.isSecondOccurrence();
220 }
221 
223 {
224  d->mDateTime.setSecondOccurrence(second);
225 }
226 
228 {
229  return DateTime(d->mDateTime.toUtc());
230 }
231 
233 {
234  return DateTime(d->mDateTime.toOffsetFromUtc());
235 }
236 
238 {
239  return DateTime(d->mDateTime.toOffsetFromUtc(utcOffset));
240 }
241 
243 {
244  return DateTime(d->mDateTime.toLocalZone());
245 }
246 
248 {
249  return DateTime(d->mDateTime.toZone(zone));
250 }
251 
253 {
254  return DateTime(d->mDateTime.toTimeSpec(spec));
255 }
256 
258 {
259  return d->mDateTime.toSecsSinceEpoch();
260 }
261 
262 uint DateTime::toTime_t() const
263 {
264  return d->mDateTime.toTime_t();
265 }
266 
268 {
269  d->mDateTime.setSecsSinceEpoch(secs);
270 }
271 
272 void DateTime::setTime_t(uint secs)
273 {
274  d->mDateTime.setTime_t(secs);
275 }
276 
278 {
279  return DateTime(d->mDateTime.addSecs(n));
280 }
281 
283 {
284  return DateTime(d->mDateTime.addSecs(n * 60));
285 }
286 
288 {
289  return DateTime(d->mDateTime.addDays(n));
290 }
291 
293 {
294  return DateTime(d->mDateTime.addMonths(n));
295 }
296 
298 {
299  return DateTime(d->mDateTime.addYears(n));
300 }
301 
302 int DateTime::daysTo(const DateTime &dt) const
303 {
304  return d->mDateTime.daysTo(dt.d->mDateTime);
305 }
306 
307 int DateTime::minsTo(const DateTime &dt) const
308 {
309  return d->mDateTime.secsTo(dt.d->mDateTime) / 60;
310 }
311 
312 int DateTime::secsTo(const DateTime &dt) const
313 {
314  return d->mDateTime.secsTo(dt.d->mDateTime);
315 }
316 
317 qint64 DateTime::secsTo_long(const DateTime &dt) const
318 {
319  return d->mDateTime.secsTo(dt.d->mDateTime);
320 }
321 
323 {
324  return d->mDateTime.toString(f);
325 }
326 
327 QString DateTime::toString(const QString &format) const
328 {
329  return d->mDateTime.toString(format);
330 }
331 
332 QString DateTime::formatLocale(bool shortFormat) const
333 {
335  return d->mDateTime.isDateOnly() ? QLocale().toString(d->mDateTime.date(), format)
336  : QLocale().toString(d->mDateTime.qDateTime(), format);
337 }
338 
340 {
341  Private::mStartOfDay = sod;
342 }
343 
345 {
346  return d->mDateTime.compare(other.d->mDateTime);
347 }
348 
350 {
351  return Private::mStartOfDay;
352 }
353 
354 bool operator==(const DateTime &dt1, const DateTime &dt2)
355 {
356  return dt1.d->mDateTime == dt2.d->mDateTime;
357 }
358 
359 bool operator==(const KADateTime &dt1, const DateTime &dt2)
360 {
361  return dt1 == dt2.d->mDateTime;
362 }
363 
364 bool operator<(const DateTime &dt1, const DateTime &dt2)
365 {
366  if (dt1.d->mDateTime.isDateOnly() && !dt2.d->mDateTime.isDateOnly()) {
367  KADateTime dt = dt1.d->mDateTime.addDays(1);
368  dt.setTime(DateTime::Private::mStartOfDay);
369  return dt <= dt2.d->mDateTime;
370  }
371  if (!dt1.d->mDateTime.isDateOnly() && dt2.d->mDateTime.isDateOnly()) {
372  KADateTime dt = dt2.d->mDateTime;
373  dt.setTime(DateTime::Private::mStartOfDay);
374  return dt1.d->mDateTime < dt;
375  }
376  return dt1.d->mDateTime < dt2.d->mDateTime;
377 }
378 
379 } // namespace KAlarmCal
380 
381 // vim: et sw=4:
DateTime toOffsetFromUtc() const
Returns the time expressed as an offset from UTC, using the UTC offset associated with this instance&#39;...
Definition: datetime.cpp:232
int daysTo(const DateTime &dt) const
Returns the number of days from this date or date-time to dt.
Definition: datetime.cpp:302
bool isLocalZone() const
Returns whether the time zone for the date/time is the current local system time zone.
Definition: datetime.cpp:197
QTime effectiveTime() const
Returns the time part of the value.
Definition: datetime.cpp:127
DateTime addMins(qint64 n) const
Returns a DateTime value mins minutes later than the value of this object.
Definition: datetime.cpp:282
QString toString(qlonglong i) const const
QTimeZone timeZone() const
Returns the time zone of the value.
Definition: datetime.cpp:177
DateTime addSecs(qint64 n) const
Returns a DateTime value secs seconds later than the value of this object.
Definition: datetime.cpp:277
A class representing a date and time with an associated time zone.
Definition: kadatetime.h:145
static QTime startOfDay()
Returns the start-of-day time.
Definition: datetime.cpp:349
void setTime(const QTime &time)
DateTime addDays(int n) const
Returns a DateTime value n days later than the value of this object.
Definition: datetime.cpp:287
qint64 secsTo_long(const DateTime &dt) const
Returns the number of seconds as a qint64 from this date or date-time to dt.
Definition: datetime.cpp:317
bool isNull() const
Returns true if the date is null and, if it is a date-time value, the time is also null...
Definition: datetime.cpp:87
static void setStartOfDay(const QTime &sod)
Sets the start-of-day time.
Definition: datetime.cpp:339
int minsTo(const DateTime &dt) const
Returns the number of minutes from this date or date-time to dt.
Definition: datetime.cpp:307
DateTime addYears(int n) const
Returns a DateTime value n years later than the value of this object.
Definition: datetime.cpp:297
KADateTime::SpecType timeType() const
Returns the time specification type of the date/time, i.e.
Definition: datetime.cpp:192
KADateTime calendarKDateTime() const
Returns the date and time of the value as written in the calendar.
Definition: datetime.cpp:167
DateTime toLocalZone() const
Returns the time converted to the current local system time zone.
Definition: datetime.cpp:242
int secsTo(const DateTime &dt) const
Returns the number of seconds from this date or date-time to dt.
Definition: datetime.cpp:312
int utcOffset() const
Returns the UTC offset associated with the date/time.
Definition: datetime.cpp:212
void setDate(const QDate &d)
Sets the date component of the value.
Definition: datetime.cpp:112
KALARMCAL_EXPORT friend bool operator==(const KAlarmCal::DateTime &dt1, const KAlarmCal::DateTime &dt2)
Returns true if the two values are equal.
Definition: datetime.cpp:354
void setSecsSinceEpoch(qint64 secs)
Sets the value to a specified date-time value.
Definition: datetime.cpp:267
void setTime(const QTime &time)
Sets the time part of the date/time.
QDateTime effectiveDateTime() const
Returns the date and time of the value.
Definition: datetime.cpp:137
QString toString(KADateTime::TimeFormat f=KADateTime::QtTextDate) const
Returns the value as a string.
Definition: datetime.cpp:322
bool isValid() const
Returns true if the date is valid and, if it is a date-time value, the time is also valid...
Definition: datetime.cpp:92
void setTimeSpec(const KADateTime::Spec &spec)
Changes the time specification of the value.
Definition: datetime.cpp:187
void setTime(const QTime &t)
Sets the time component of the value.
Definition: datetime.cpp:132
Comparison
How this KADateTime compares with another.
Definition: kadatetime.h:429
bool isDateOnly() const
Returns true if it is date-only value.
Definition: datetime.cpp:97
DateTime toZone(const QTimeZone &zone) const
Returns the time converted to a specified time zone.
Definition: datetime.cpp:247
KADateTime kDateTime() const
Returns the date and time of the value as a KADateTime.
Definition: datetime.cpp:122
DateTime toTimeSpec(const KADateTime::Spec &spec) const
Returns the time converted to a new time specification.
Definition: datetime.cpp:252
DateTime & operator=(const DateTime &dt)
Assignment operator.
Definition: datetime.cpp:73
QDateTime calendarDateTime() const
Returns the date and time of the value as written in the calendar.
Definition: datetime.cpp:157
QDate date() const
Returns the date part of the value.
Definition: datetime.cpp:107
void setSecondOccurrence(bool second)
Sets whether this is the second occurrence of this date/time.
Definition: datetime.cpp:222
KALARMCAL_EXPORT friend bool operator<(const KAlarmCal::DateTime &dt1, const KAlarmCal::DateTime &dt2)
Returns true if the dt1 is earlier than dt2.
Definition: datetime.cpp:364
QString formatLocale(bool shortFormat=true) const
Returns the value as a string, formatted according to the user&#39;s locale.
Definition: datetime.cpp:332
KALARMCAL_DEPRECATED void setTime_t(uint secs)
Sets the value to a specified date-time value.
Definition: datetime.cpp:272
DateTime addMonths(int n) const
Returns a DateTime value n months later than the value of this object.
Definition: datetime.cpp:292
bool isOffsetFromUtc() const
Returns whether the date/time is a local time at a fixed offset from UTC.
Definition: datetime.cpp:207
KADateTime::Spec timeSpec() const
Returns the time specification of the value.
Definition: datetime.cpp:182
KADateTime effectiveKDateTime() const
Returns the date and time of the value.
Definition: datetime.cpp:147
KADateTime::Comparison compare(const DateTime &other) const
Compare this value with another.
Definition: datetime.cpp:344
DateTime toUtc() const
Returns the time converted to UTC.
Definition: datetime.cpp:227
KALARMCAL_DEPRECATED uint toTime_t() const
Converts the time to a UTC time, measured in seconds since 00:00:00 UTC 1st January 1970 (as returned...
Definition: datetime.cpp:262
SpecType
The time specification type of a KADateTime instance.
Definition: kadatetime.h:155
bool isUtc() const
Returns whether the date/time is a UTC time.
Definition: datetime.cpp:202
TimeFormat
Format for strings representing date/time values.
Definition: kadatetime.h:366
The full time specification of a KADateTime instance.
Definition: kadatetime.h:180
void setDateOnly(bool d)
Sets the value to be either date-only or date-time.
Definition: datetime.cpp:102
qint64 toSecsSinceEpoch() const
Converts the time to a UTC time, measured in seconds since 00:00:00 UTC 1st January 1970 (as returned...
Definition: datetime.cpp:257
As KADateTime, but with a configurable start-of-day time for date-only values.
Definition: datetime.h:29
bool isSecondOccurrence() const
Returns whether the date/time is the second occurrence of this time.
Definition: datetime.cpp:217
DateTime()
Default constructor.
Definition: datetime.cpp:33
QDateTime qDateTime() const
Returns the date and time of the value.
Definition: datetime.cpp:117
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 23:15:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.