1/*
2 This file is part of the KDE libraries
3 Copyright (c) 2005-2011 David Jarvie <djarvie@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/** @file
22 * Date/times with associated time zone
23 * @author David Jarvie <djarvie@kde.org>.
24 */
25
26#ifndef _KDATETIME_H_
27#define _KDATETIME_H_
28
29#include <kdecore_export.h>
30#include <ktimezone.h>
31
32#include <QtCore/QMetaType>
33#include <QtCore/QSharedDataPointer>
34
35class QDataStream;
36class KDateTimePrivate;
37class KDateTimeSpecPrivate;
38
39/**
40 * @short A class representing a date and time with an associated time zone
41 *
42 * Topics:
43 * - @ref intro
44 * - @ref manipulation
45 * - @ref compatibility
46 *
47 * @section intro Introduction
48 *
49 * The class KDateTime combines a date and time with support for an
50 * associated time zone or UTC offset. When manipulating KDateTime objects,
51 * their time zones or UTC offsets are automatically taken into account. KDateTime
52 * can also be set to represent a date-only value with no associated time.
53 *
54 * The class uses QDateTime internally to represent date/time values, and
55 * therefore uses the Gregorian calendar for dates starting from 15 October 1582,
56 * and the Julian calendar for dates up to 4 October 1582. The minimum year
57 * number is -4712 (4713 BC), while the upper limit is more than 11,000,000. The
58 * actual adoption of the Gregorian calendar after 1582 was slow; the last European
59 * country to adopt it, Greece, did so only in 1923. See QDateTime Considerations
60 * section below for further discussion of the date range limitations.
61 *
62 * The time specification types which KDateTime supports are:
63 * - the UTC time zone
64 * - a local time with a specified offset from UTC
65 * - a local time in a specified time zone
66 * - a local time using the current system time zone (a special case of the
67 * previous item)
68 * - local clock time, using whatever the local system clock says on whichever
69 * computer it happens to be on. In this case, the equivalent UTC time will
70 * vary depending on system. As a result, calculations involving local clock
71 * times do not necessarily produce reliable results.
72 *
73 * These characteristics are more fully described in the description of the
74 * SpecType enumeration. Also see
75 * <a href="http://www.w3.org/TR/timezone/">W3C: Working with Time Zones</a>
76 * for a good overview of the different ways of representing times.
77 *
78 * To set the time specification, use one of the setTimeSpec() methods, to get
79 * the time specification, call timeSpec(), isUtc(), isLocalZone(),
80 * isOffsetFromUtc() or isClockTime(). To determine whether two KDateTime
81 * instances have the same time specification, call timeSpec() on each and
82 * compare the returned values using KDateTime::Spec::operator==().
83 *
84 * @section manipulation Date and Time Manipulation
85 *
86 * A KDateTime object can be created by passing a date and time in its
87 * constructor, together with a time specification.
88 *
89 * If both the date and time are null, isNull() returns true. If the date, time
90 * and time specification are all valid, isValid() returns true.
91 *
92 * A KDateTime object can be converted to a different time specification by
93 * using toUtc(), toLocalZone() or toClockTime(). It can be converted to a
94 * specific time zone by toZone(). To return the time as an elapsed time since
95 * 1 January 1970 (as used by time(2)), use toTime_t(). The results of time
96 * zone conversions are cached to minimize the need for recalculation. Each
97 * KDateTime object caches its UTC equivalent and the last time zone
98 * conversion performed.
99 *
100 * The date and time can be set either in the constructor, or afterwards by
101 * calling setDate(), setTime() or setDateTime(). To return the date and/or
102 * time components of the KDateTime, use date(), time() and dateTime(). You
103 * can determine whether the KDateTime represents a date and time, or a date
104 * only, by isDateOnly(). You can change between a date and time or a date only
105 * value using setDateOnly().
106 *
107 * You can increment or decrement the date/time using addSecs(), addDays(),
108 * addMonths() and addYears(). The interval between two date/time values can
109 * be found using secsTo() or daysTo().
110 *
111 * The comparison operators (operator==(), operator<(), etc.) all take the time
112 * zone properly into account; if the two KDateTime objects have different time
113 * zones, they are first converted to UTC before the comparison is
114 * performed. An alternative to the comparison operators is compare() which will
115 * in addition tell you if a KDateTime object overlaps with another when one or
116 * both are date-only values.
117 *
118 * KDateTime values may be converted to and from a string representation using
119 * the toString() and fromString() methods. These handle a variety of text
120 * formats including ISO 8601 and RFC 2822.
121 *
122 * KDateTime uses Qt's facilities to implicitly share data. Copying instances
123 * is very efficient, and copied instances share cached UTC and time zone
124 * conversions even after the copy is performed. A separate copy of the data is
125 * created whenever a non-const method is called. If you want to force the
126 * creation of a separate copy of the data (e.g. if you want two copies to
127 * cache different time zone conversions), call detach().
128 *
129 * @section compatibility QDateTime Considerations
130 *
131 * KDateTime's interface is designed to be as compatible as possible with that
132 * of QDateTime, but with adjustments to cater for time zone handling. Because
133 * QDateTime lacks virtual methods, KDateTime is not inherited from QDateTime,
134 * but instead is implemented using a private QDateTime object.
135 *
136 * The date range restriction due to the use of QDateTime internally may at
137 * first sight seem a design limitation. However, two factors should be
138 * considered:
139 *
140 * - there are significant problems in the representation of dates before the
141 * Gregorian calendar was adopted. The date of adoption of the Gregorian
142 * calendar varied from place to place, and in the Julian calendar the
143 * date of the new year varied so that in different places the year number
144 * could differ by one. So any date/time system which attempted to represent
145 * dates as actually used in history would be too specialized to belong to
146 * the core KDE libraries. Date/time systems for scientific applications can
147 * be much simpler, but may differ from historical records.
148 *
149 * - time zones were not invented until the middle of the 19th century. Before
150 * that, solar time was used.
151 *
152 * Because of these issues, together with the fact that KDateTime's aim is to
153 * provide automatic time zone handling for date/time values, QDateTime was
154 * chosen as the basis for KDateTime. For those who need an extended date
155 * range, other classes exist.
156 *
157 * @section simulation Simulation Facility
158 *
159 * This class provides a facility to simulate the local system time, which
160 * affects all functions using or returning the system time. This facility is
161 * provided for testing purposes only, and is only available if the library is
162 * compiled with debug enabled. In release mode, simulation is inoperative and
163 * the real local system time is used at all times. Use
164 * setSimulatedSystemTime() to set or clear the simulated time. To read the
165 * real (not simulated) system time, use realCurrentLocalDateTime().
166 *
167 * @see KTimeZone, KSystemTimeZones, QDateTime, QDate, QTime
168 * @see <a href="http://www.w3.org/TR/timezone/">W3C: Working with Time Zones</a>
169 * @author David Jarvie \<djarvie@kde.org\>.
170 */
171class KDECORE_EXPORT KDateTime //krazy:exclude=dpointer (implicitly shared)
172{
173 public:
174 /**
175 * The time specification type of a KDateTime instance.
176 * This specifies how the date/time component of the KDateTime instance
177 * should be interpreted, i.e. what type of time zone (if any) the date/time
178 * is expressed in. For the full time specification (including time zone
179 * details), see KDateTime::Spec.
180 */
181 enum SpecType
182 {
183 Invalid, /**< an invalid time specification. */
184 UTC, /**< a UTC time. */
185 OffsetFromUTC, /**< a local time which has a fixed offset from UTC. */
186 TimeZone, /**< a time in a specified time zone. If the time zone is
187 * the current system time zone (i.e. that returned by
188 * KSystemTimeZones::local()), LocalZone may be used
189 * instead.
190 */
191 LocalZone, /**< a time in the current system time zone.
192 * When used to initialize a KDateTime or KDateTime::Spec
193 * instance, this is simply a shorthand for calling the
194 * setting method with a time zone parameter
195 * KSystemTimeZones::local(). Note that if the system is
196 * changed to a different time zone afterwards, the
197 * KDateTime instance will still use the original system
198 * time zone rather than adopting the new zone.
199 * When returned by a method, it indicates that the time
200 * zone stored in the instance is that currently returned
201 * by KSystemTimeZones::local().
202 */
203 ClockTime /**< a clock time which ignores time zones and simply uses
204 * whatever the local system clock says the time is. You
205 * could, for example, set a wake-up time of 07:30 on
206 * some date, and then no matter where you were in the
207 * world, you would be in time for breakfast as long as
208 * your computer was aligned with the local time.
209 *
210 * Note that any calculations which involve clock times
211 * cannot be guaranteed to be accurate, since by
212 * definition they contain no information about time
213 * zones or daylight savings changes.
214 */
215 };
216
217 /**
218 * The full time specification of a KDateTime instance.
219 * This specifies how the date/time component of the KDateTime instance
220 * should be interpreted, i.e. which time zone (if any) the date/time is
221 * expressed in.
222 */
223 class KDECORE_EXPORT Spec
224 {
225 public:
226 /**
227 * Constructs an invalid time specification.
228 */
229 Spec();
230
231 /**
232 * Constructs a time specification for a given time zone.
233 * If @p tz is KTimeZone::utc(), the time specification type is set to @c UTC.
234 *
235 * @param tz time zone
236 */
237 Spec(const KTimeZone &tz); // allow implicit conversion
238
239 /**
240 * Constructs a time specification.
241 *
242 * @param type time specification type, which should not be @c TimeZone
243 * @param utcOffset number of seconds to add to UTC to get the local
244 * time. Ignored if @p type is not @c OffsetFromUTC.
245 */
246 Spec(SpecType type, int utcOffset = 0); // allow implicit conversion
247
248 /**
249 * Copy constructor.
250 */
251 Spec(const Spec& spec);
252
253 /**
254 * Assignment operator.
255 */
256 Spec& operator=(const Spec& spec);
257
258 /**
259 * Destructor
260 */
261 ~Spec();
262
263 /**
264 * Returns whether the time specification is valid.
265 *
266 * @return @c true if valid, else @c false
267 */
268 bool isValid() const;
269
270 /**
271 * Returns the time zone for the date/time, according to the time
272 * specification type as follows:
273 * - @c TimeZone : the specified time zone is returned.
274 * - @c UTC : a UTC time zone is returned.
275 * - @c LocalZone : the current local time zone is returned.
276 *
277 * @return time zone as defined above, or invalid in all other cases
278 * @see isUtc(), isLocal()
279 */
280 KTimeZone timeZone() const;
281
282 /**
283 * Returns the time specification type, i.e. whether it is
284 * UTC, has a time zone, etc. If the type is the local time zone,
285 * @c TimeZone is returned; use isLocalZone() to check for the
286 * local time zone.
287 *
288 * @return specification type
289 * @see isLocalZone(), isClockTime(), isUtc(), timeZone()
290 */
291 SpecType type() const;
292
293 /**
294 * Returns whether the time specification is the current local
295 * system time zone.
296 *
297 * @return @c true if local system time zone
298 * @see isUtc(), isOffsetFromUtc(), timeZone()
299 */
300 bool isLocalZone() const;
301
302 /**
303 * Returns whether the time specification is a local clock time.
304 *
305 * @return @c true if local clock time
306 * @see isUtc(), timeZone()
307 */
308 bool isClockTime() const;
309
310 /**
311 * Returns whether the time specification is a UTC time.
312 * It is considered to be a UTC time if it is either type @c UTC,
313 * or is type @c OffsetFromUTC with a zero UTC offset.
314 *
315 * @return @c true if UTC
316 * @see isLocal(), isOffsetFromUtc(), timeZone()
317 */
318 bool isUtc() const;
319
320 /**
321 * Returns whether the time specification is a local time at a fixed
322 * offset from UTC.
323 *
324 * @return @c true if local time at fixed offset from UTC
325 * @see isLocal(), isUtc(), utcOffset()
326 */
327 bool isOffsetFromUtc() const;
328
329 /**
330 * Returns the UTC offset associated with the time specification. The
331 * UTC offset is the number of seconds to add to UTC to get the local time.
332 *
333 * @return UTC offset in seconds if type is @c OffsetFromUTC, else 0
334 * @see isOffsetFromUtc()
335 */
336 int utcOffset() const;
337
338 /**
339 * Initialises the time specification.
340 *
341 * @param type the time specification type. Note that @c TimeZone
342 * is invalid here.
343 * @param utcOffset number of seconds to add to UTC to get the local
344 * time. Ignored if @p spec is not @c OffsetFromUTC.
345 * @see type(), setType(const KTimeZone&)
346 */
347 void setType(SpecType type, int utcOffset = 0);
348
349 /**
350 * Sets the time zone for the time specification.
351 *
352 * To set the time zone to the current local system time zone,
353 * setType(LocalZone) may optionally be used instead.
354 *
355 * @param tz new time zone
356 * @see timeZone(), setType(SpecType)
357 */
358 void setType(const KTimeZone &tz);
359
360 /**
361 * Comparison operator.
362 *
363 * @return @c true if the two instances are identical, @c false otherwise
364 * @see equivalentTo()
365 */
366 bool operator==(const Spec &other) const;
367
368 bool operator!=(const Spec &other) const { return !operator==(other); }
369
370 /**
371 * Checks whether this instance is equivalent to another.
372 * The two instances are considered to be equivalent if any of the following
373 * conditions apply:
374 * - both instances are type @c ClockTime.
375 * - both instances are type @c OffsetFromUTC and their offsets from UTC are equal.
376 * - both instances are type @c TimeZone and their time zones are equal.
377 * - both instances are UTC. An instance is considered to be UTC if it is
378 * either type @c UTC, or is type @c OffsetFromUTC with a zero UTC offset.
379 *
380 * @return @c true if the two instances are equivalent, @c false otherwise
381 * @see operator==()
382 */
383 bool equivalentTo(const Spec &other) const;
384
385 /**
386 * The UTC time specification.
387 * Provided as a shorthand for KDateTime::Spec(KDateTime::UTC).
388 */
389 static Spec UTC();
390
391 /**
392 * The ClockTime time specification.
393 * Provided as a shorthand for KDateTime::Spec(KDateTime::ClockTime).
394 */
395 static Spec ClockTime();
396
397 /**
398 * Returns a UTC offset time specification.
399 * Provided as a shorthand for KDateTime::Spec(KDateTime::OffsetFromUTC, utcOffset).
400 *
401 * @param utcOffset number of seconds to add to UTC to get the local time
402 * @return UTC offset time specification
403 */
404 static Spec OffsetFromUTC(int utcOffset);
405
406 /**
407 * Returns a local time zone time specification.
408 * Provided as a shorthand for KDateTime::Spec(KDateTime::LocalZone).
409 *
410 * @return Local zone time specification
411 */
412 static Spec LocalZone();
413
414 private:
415 KDateTimeSpecPrivate* const d;
416 };
417
418 /** Format for strings representing date/time values. */
419 enum TimeFormat
420 {
421 ISODate, /**< ISO 8601 format, i.e. [±]YYYY-MM-DDThh[:mm[:ss[.sss]]]TZ,
422 * where TZ is the time zone offset (blank for local
423 * time, Z for UTC, or ±hhmm for an offset from UTC).
424 * When parsing a string, the ISO 8601 basic format,
425 * [±]YYYYMMDDThh[mm[ss[.sss]]]TZ, is also accepted. For
426 * date-only values, the formats [±]YYYY-MM-DD and
427 * [±]YYYYMMDD (without time zone specifier) are used. All
428 * formats may contain a day of the year instead of day
429 * and month.
430 * To allow for years past 9999, the year may optionally
431 * contain more than 4 digits. To avoid ambiguity, this is
432 * not allowed in the basic format containing a day
433 * of the year (i.e. when the date part is [±]YYYYDDD).
434 */
435 RFCDate, /**< RFC 2822 format,
436 * i.e. "[Wdy,] DD Mon YYYY hh:mm[:ss] ±hhmm". This format
437 * also covers RFCs 822, 850, 1036 and 1123.
438 * When parsing a string, it also accepts the format
439 * "Wdy Mon DD HH:MM:SS YYYY" specified by RFCs 850 and
440 * 1036. There is no valid date-only format.
441 */
442 RFCDateDay, /**< RFC 2822 format including day of the week,
443 * i.e. "Wdy, DD Mon YYYY hh:mm:ss ±hhmm"
444 */
445 QtTextDate, /**< Same format as Qt::TextDate (i.e. Day Mon DD hh:mm:ss YYYY)
446 * with, if not local time, the UTC offset appended. The
447 * time may be omitted to indicate a date-only value.
448 */
449 LocalDate, /**< Same format as Qt::LocalDate (i.e. locale dependent)
450 * with, if not local time, the UTC offset appended. The
451 * time may be omitted to indicate a date-only value.
452 */
453 RFC3339Date /**< RFC 3339 format,
454 * i.e. "YYYY-MM-DDThh:mm:ss[.sss](Z|±hh:mm)".
455 * There is no valid date-only format.
456 */
457
458 };
459
460 /**
461 * How this KDateTime compares with another.
462 * If any date-only value is involved, comparison of KDateTime values
463 * requires them to be considered as representing time periods. A date-only
464 * instance represents a time period from 00:00:00 to 23:59:59.999 on a given
465 * date, while a date/time instance can be considered to represent a time
466 * period whose start and end times are the same. They may therefore be
467 * earlier or later, or may overlap or be contained one within the other.
468 *
469 * Values may be OR'ed with each other in any combination of 'consecutive'
470 * intervals to represent different types of relationship.
471 *
472 * In the descriptions of the values below,
473 * - s1 = start time of this instance
474 * - e1 = end time of this instance
475 * - s2 = start time of other instance
476 * - e2 = end time of other instance.
477 */
478 enum Comparison
479 {
480 Before = 0x01, /**< This KDateTime is strictly earlier than the other,
481 * i.e. e1 < s2.
482 */
483 AtStart = 0x02, /**< This KDateTime starts at the same time as the other,
484 * and ends before the end of the other,
485 * i.e. s1 = s2, e1 < e2.
486 */
487 Inside = 0x04, /**< This KDateTime starts after the start of the other,
488 * and ends before the end of the other,
489 * i.e. s1 > s2, e1 < e2.
490 */
491 AtEnd = 0x08, /**< This KDateTime starts after the start of the other,
492 * and ends at the same time as the other,
493 * i.e. s1 > s2, e1 = e2.
494 */
495 After = 0x10, /**< This KDateTime is strictly later than the other,
496 * i.e. s1 > e2.
497 */
498 Equal = AtStart | Inside | AtEnd,
499 /**< Simultaneous, i.e. s1 = s2 && e1 = e2.
500 */
501 Outside = Before | AtStart | Inside | AtEnd | After,
502 /**< This KDateTime starts before the start of the other,
503 * and ends after the end of the other,
504 * i.e. s1 < s2, e1 > e2.
505 */
506 StartsAt = AtStart | Inside | AtEnd | After,
507 /**< This KDateTime starts at the same time as the other,
508 * and ends after the end of the other,
509 * i.e. s1 = s2, e1 > e2.
510 */
511 EndsAt = Before | AtStart | Inside | AtEnd
512 /**< This KDateTime starts before the start of the other,
513 * and ends at the same time as the other,
514 * i.e. s1 < s2, e1 = e2.
515 */
516 };
517
518
519 /**
520 * Constructs an invalid date/time.
521 */
522 KDateTime();
523
524 /**
525 * Constructs a date-only value expressed in a given time specification. The
526 * time is set to 00:00:00.
527 *
528 * The instance is initialised according to the time specification type of
529 * @p spec as follows:
530 * - @c UTC : date is stored as UTC.
531 * - @c OffsetFromUTC : date is a local time at the specified offset
532 * from UTC.
533 * - @c TimeZone : date is a local time in the specified time zone.
534 * - @c LocalZone : date is a local date in the current system time
535 * zone.
536 * - @c ClockTime : time zones are ignored.
537 *
538 * @param date date in the time zone indicated by @p spec
539 * @param spec time specification
540 */
541 explicit KDateTime(const QDate &date, const Spec &spec = Spec(LocalZone));
542
543 /**
544 * Constructs a date/time expressed as specified by @p spec.
545 *
546 * @p date and @p time are interpreted and stored according to the value of
547 * @p spec as follows:
548 * - @c UTC : @p date and @p time are in UTC.
549 * - @c OffsetFromUTC : date/time is a local time at the specified offset
550 * from UTC.
551 * - @c TimeZone : date/time is a local time in the specified time zone.
552 * - @c LocalZone : @p date and @p time are local times in the current
553 * system time zone.
554 * - @c ClockTime : time zones are ignored.
555 *
556 * @param date date in the time zone indicated by @p spec
557 * @param time time in the time zone indicated by @p spec
558 * @param spec time specification
559 */
560 KDateTime(const QDate &date, const QTime &time, const Spec &spec = Spec(LocalZone));
561
562 /**
563 * Constructs a date/time expressed in a given time specification.
564 *
565 * @p dt is interpreted and stored according to the time specification type
566 * of @p spec as follows:
567 * - @c UTC : @p dt is stored as a UTC value. If
568 * @c dt.timeSpec() is @c Qt::LocalTime, @p dt is first
569 * converted from the current system time zone to UTC
570 * before storage.
571 * - @c OffsetFromUTC : date/time is stored as a local time at the specified
572 * offset from UTC. If @c dt.timeSpec() is @c Qt::UTC,
573 * the time is adjusted by the UTC offset before
574 * storage. If @c dt.timeSpec() is @c Qt::LocalTime,
575 * it is assumed to be a local time at the specified
576 * offset from UTC, and is stored without adjustment.
577 * - @c TimeZone : if @p dt is specified as a UTC time (i.e. @c dt.timeSpec()
578 * is @c Qt::UTC), it is first converted to local time in
579 * specified time zone before being stored.
580 * - @c LocalZone : @p dt is stored as a local time in the current system
581 * time zone. If @c dt.timeSpec() is @c Qt::UTC, @p dt is
582 * first converted to local time before storage.
583 * - @c ClockTime : If @c dt.timeSpec() is @c Qt::UTC, @p dt is first
584 * converted to local time in the current system time zone
585 * before storage. After storage, the time is treated as a
586 * simple clock time, ignoring time zones.
587 *
588 * @param dt date and time
589 * @param spec time specification
590 */
591 KDateTime(const QDateTime &dt, const Spec &spec);
592
593 /**
594 * Constructs a date/time from a QDateTime.
595 * The KDateTime is expressed in either UTC or the local system time zone,
596 * according to @p dt.timeSpec().
597 *
598 * @param dt date and time
599 */
600 explicit KDateTime(const QDateTime &dt);
601
602 KDateTime(const KDateTime &other);
603 ~KDateTime();
604
605 KDateTime &operator=(const KDateTime &other);
606
607 /**
608 * Returns whether the date/time is null.
609 *
610 * @return @c true if both date and time are null, else @c false
611 * @see isValid(), QDateTime::isNull()
612 */
613 bool isNull() const;
614
615 /**
616 * Returns whether the date/time is valid.
617 *
618 * @return @c true if both date and time are valid, else @c false
619 * @see isNull(), QDateTime::isValid()
620 */
621 bool isValid() const;
622
623 /**
624 * Returns whether the instance represents a date/time or a date-only value.
625 *
626 * @return @c true if date-only, @c false if date and time
627 */
628 bool isDateOnly() const;
629
630 /**
631 * Returns the date part of the date/time. The value returned should be
632 * interpreted in terms of the instance's time zone or UTC offset.
633 *
634 * @return date value
635 * @see time(), dateTime()
636 */
637 QDate date() const;
638
639 /**
640 * Returns the time part of the date/time. The value returned should be
641 * interpreted in terms of the instance's time zone or UTC offset. If
642 * the instance is date-only, the time returned is 00:00:00.
643 *
644 * @return time value
645 * @see date(), dateTime(), isDateOnly()
646 */
647 QTime time() const;
648
649 /**
650 * Returns the date/time component of the instance, ignoring the time
651 * zone. The value returned should be interpreted in terms of the
652 * instance's time zone or UTC offset. The returned value's @c timeSpec()
653 * value will be @c Qt::UTC if the instance is a UTC time, else
654 * @c Qt::LocalTime. If the instance is date-only, the time value is set to
655 * 00:00:00.
656 *
657 * @return date/time
658 * @see date(), time()
659 */
660 QDateTime dateTime() const;
661
662 /**
663 * Returns the time zone for the date/time. If the date/time is specified
664 * as a UTC time, a UTC time zone is always returned.
665 *
666 * @return time zone, or invalid if a local time at a fixed UTC offset or a
667 * local clock time
668 * @see isUtc(), isLocal()
669 */
670 KTimeZone timeZone() const;
671
672 /**
673 * Returns the time specification of the date/time, i.e. whether it is
674 * UTC, what time zone it is, etc.
675 *
676 * @return time specification
677 * @see isLocalZone(), isClockTime(), isUtc(), timeZone()
678 */
679 Spec timeSpec() const;
680
681 /**
682 * Returns the time specification type of the date/time, i.e. whether it is
683 * UTC, has a time zone, etc. If the type is the local time zone,
684 * @c TimeZone is returned; use isLocalZone() to check for the local time
685 * zone.
686 *
687 * @return specification type
688 * @see timeSpec(), isLocalZone(), isClockTime(), isUtc(), timeZone()
689 */
690 SpecType timeType() const;
691
692 /**
693 * Returns whether the time zone for the date/time is the current local
694 * system time zone.
695 *
696 * @return @c true if local system time zone
697 * @see isUtc(), isOffsetFromUtc(), timeZone()
698 */
699 bool isLocalZone() const;
700
701 /**
702 * Returns whether the date/time is a local clock time.
703 *
704 * @return @c true if local clock time
705 * @see isUtc(), timeZone()
706 */
707 bool isClockTime() const;
708
709 /**
710 * Returns whether the date/time is a UTC time.
711 * It is considered to be a UTC time if it either has a UTC time
712 * specification (SpecType == UTC), or has a zero offset from UTC
713 * (SpecType == OffsetFromUTC with zero UTC offset).
714 *
715 * @return @c true if UTC
716 * @see isLocal(), isOffsetFromUtc(), timeZone()
717 */
718 bool isUtc() const;
719
720 /**
721 * Returns whether the date/time is a local time at a fixed offset from
722 * UTC.
723 *
724 * @return @c true if local time at fixed offset from UTC
725 * @see isLocal(), isUtc(), utcOffset()
726 */
727 bool isOffsetFromUtc() const;
728
729 /**
730 * Returns the UTC offset associated with the date/time. The UTC offset is
731 * the number of seconds to add to UTC to get the local time.
732 *
733 * @return UTC offset in seconds, or 0 if local clock time
734 * @see isClockTime()
735 */
736 int utcOffset() const;
737
738 /**
739 * Returns whether the date/time is the second occurrence of this time. This
740 * is only applicable to a date/time expressed in terms of a time zone (type
741 * @c TimeZone or @c LocalZone), around the time of change from daylight
742 * savings to standard time.
743 *
744 * When a shift from daylight savings time to standard time occurs, the local
745 * times (typically the previous hour) immediately preceding the shift occur
746 * twice. For example, if a time shift of 1 hour happens at 03:00, the clock
747 * jumps backwards to 02:00, so the local times between 02:00:00 and 02:59:59
748 * occur once before the shift, and again after the shift.
749 *
750 * For instances which are not of type @c TimeZone, or when the date/time is
751 * not near to a time shift, @c false is returned.
752 *
753 * @return @c true if the time is the second occurrence, @c false otherwise
754 * @see setSecondOccurrence()
755 */
756 bool isSecondOccurrence() const;
757
758 /**
759 * Returns the time converted to UTC. The converted time has a UTC offset
760 * of zero.
761 * If the instance is a local clock time, it is first set to the local time
762 * zone, and then converted to UTC.
763 * If the instance is a date-only value, a date-only UTC value is returned,
764 * with the date unchanged.
765 *
766 * @return converted time
767 * @see toOffsetFromUtc(), toLocalZone(), toZone(), toTimeSpec(), toTime_t(), KTimeZone::convert()
768 */
769 KDateTime toUtc() const;
770
771 /**
772 * Returns the time expressed as an offset from UTC, using the UTC offset
773 * associated with this instance's date/time. The date and time
774 * components are unchanged. For example, 14:15 on 12 Jan 2001, US Eastern
775 * time zone would return a KDateTime value of 14:15 on 12 Jan 2001 with a
776 * UTC offset of -18000 seconds (i.e. -5 hours).
777 *
778 * If the instance is a local clock time, the offset is set to that of the
779 * local time zone.
780 * If the instance is a date-only value, the offset is set to that at the
781 * start of the day.
782 *
783 * @return converted time
784 * @see toUtc(), toOffsetFromUtc(int), toLocalZone(), toZone(), toTimeSpec(), toTime_t(), KTimeZone::convert()
785 */
786 KDateTime toOffsetFromUtc() const;
787
788 /**
789 * Returns the time expressed as a specified offset from UTC.
790 *
791 * If the instance is a local clock time, it is first set to the local time
792 * zone, and then converted to the UTC offset.
793 * If the instance is a date-only value, a date-only clock time value is
794 * returned, with the date unchanged.
795 *
796 * @param utcOffset number of seconds to add to UTC to get the local time.
797 * @return converted time
798 * @see toUtc(), toOffsetFromUtc(), toLocalZone(), toZone(), toTimeSpec(), toTime_t(), KTimeZone::convert()
799 */
800 KDateTime toOffsetFromUtc(int utcOffset) const;
801
802 /**
803 * Returns the time converted to the current local system time zone.
804 * If the instance is a date-only value, a date-only local time zone value
805 * is returned, with the date unchanged.
806 *
807 * @return converted time
808 * @see toUtc(), toOffsetFromUtc(), toZone(), toTimeSpec(), KTimeZone::convert()
809 */
810 KDateTime toLocalZone() const;
811
812 /**
813 * Returns the time converted to the local clock time. The time is first
814 * converted to the local system time zone before setting its type to
815 * ClockTime, i.e. no associated time zone.
816 * If the instance is a date-only value, a date-only clock time value is
817 * returned, with the date unchanged.
818 *
819 * @return converted time
820 * @see toLocalZone(), toTimeSpec()
821 */
822 KDateTime toClockTime() const;
823
824 /**
825 * Returns the time converted to a specified time zone.
826 * If the instance is a local clock time, it is first set to the local time
827 * zone, and then converted to @p zone.
828 * If the instance is a date-only value, a date-only value in @p zone is
829 * returned, with the date unchanged.
830 *
831 * @param zone time zone to convert to
832 * @return converted time
833 * @see toUtc(), toOffsetFromUtc(), toLocalZone(), toTimeSpec(), KTimeZone::convert()
834 */
835 KDateTime toZone(const KTimeZone &zone) const;
836
837 /**
838 * Returns the time converted to a new time specification.
839 * If the instance is a local clock time, it is first set to the local time
840 * zone, and then converted to the @p spec time specification.
841 * If the instance is a date-only value, a date-only value is returned,
842 * with the date unchanged.
843 *
844 * @param spec new time specification
845 * @return converted time
846 * @see toLocalZone(), toUtc(), toOffsetFromUtc(), toZone(), KTimeZone::convert()
847 */
848 KDateTime toTimeSpec(const Spec &spec) const;
849
850 /**
851 * Returns the time converted to the time specification of another instance.
852 * If this instance is a local clock time, it is first set to the local time
853 * zone, and then converted to the @p spec time specification.
854 * If this instance is a date-only value, a date-only value is returned,
855 * with the date unchanged.
856 *
857 * @param dt instance providing the new time specification
858 * @return converted time
859 * @see toLocalZone(), toUtc(), toOffsetFromUtc(), toZone(), KTimeZone::convert()
860 */
861 KDateTime toTimeSpec(const KDateTime &dt) const;
862
863 /**
864 * Converts the time to a UTC time, measured in seconds since 00:00:00 UTC
865 * 1st January 1970 (as returned by time(2)).
866 *
867 * @return converted time, or @c uint(-1) if the date is out of range or invalid
868 * @see setTime_t()
869 */
870 uint toTime_t() const;
871
872 /**
873 * Sets the time to a UTC time, specified as seconds since 00:00:00 UTC
874 * 1st January 1970 (as returned by time(2)).
875 *
876 * @param seconds number of seconds since 00:00:00 UTC 1st January 1970
877 * @see toTime_t()
878 */
879 void setTime_t(qint64 seconds);
880
881 /**
882 * Sets the instance either to being a date and time value, or a date-only
883 * value. If its status is changed to date-only, its time is set to
884 * 00:00:00.
885 *
886 * @param dateOnly @c true to set to date-only, @c false to set to date
887 * and time.
888 * @see isDateOnly(), setTime()
889 */
890 void setDateOnly(bool dateOnly);
891
892 /**
893 * Sets the date part of the date/time.
894 *
895 * @param date new date value
896 * @see date(), setTime(), setTimeSpec(), setTime_t(), setDateOnly()
897 */
898 void setDate(const QDate &date);
899
900 /**
901 * Sets the time part of the date/time. If the instance was date-only, it
902 * is changed to being a date and time value.
903 *
904 * @param time new time value
905 * @see time(), setDate(), setTimeSpec(), setTime_t()
906 */
907 void setTime(const QTime &time);
908
909 /**
910 * Sets the date/time part of the instance, leaving the time specification
911 * unaffected.
912 *
913 * If @p dt is a local time (\code dt.timeSpec() == Qt::LocalTime \endcode)
914 * and the instance is UTC, @p dt is first converted from the current
915 * system time zone to UTC before being stored.
916 *
917 * If the instance was date-only, it is changed to being a date and time
918 * value.
919 *
920 * @param dt date and time
921 * @see dateTime(), setDate(), setTime(), setTimeSpec()
922 */
923 void setDateTime(const QDateTime &dt);
924
925 /**
926 * Changes the time specification of the instance.
927 *
928 * Any previous time zone is forgotten. The stored date/time component of
929 * the instance is left unchanged (except that its UTC/local time setting
930 * is set to correspond with @p spec). Usually this method will change the
931 * absolute time which this instance represents.
932 *
933 * @param spec new time specification
934 * @see timeSpec(), timeZone()
935 */
936 void setTimeSpec(const Spec &spec);
937
938 /**
939 * Sets whether the date/time is the second occurrence of this time. This
940 * is only applicable to a date/time expressed in terms of a time zone (type
941 * @c TimeZone or @c LocalZone), around the time of change from daylight
942 * savings to standard time.
943 *
944 * When a shift from daylight savings time to standard time occurs, the local
945 * times (typically the previous hour) immediately preceding the shift occur
946 * twice. For example, if a time shift of 1 hour happens at 03:00, the clock
947 * jumps backwards to 02:00, so the local times between 02:00:00 and 02:59:59
948 * occur once before the shift, and again after the shift.
949 *
950 * For instances which are not of type @c TimeZone, or when the date/time is
951 * not near to a time shift, calling this method has no effect.
952 *
953 * Note that most other setting methods clear the second occurrence indicator,
954 * so if you want to retain its setting, you must call setSecondOccurrence()
955 * again after changing the instance's value.
956 *
957 * @param second @c true to set as the second occurrence, @c false to set as
958 * the first occurrence
959 * @see isSecondOccurrence()
960 */
961 void setSecondOccurrence(bool second);
962
963 /**
964 * Returns a date/time @p msecs milliseconds later than the stored date/time.
965 *
966 * Except when the instance is a local clock time (type @c ClockTime), the
967 * calculation is done in UTC to ensure that the result takes proper account
968 * of clock changes (e.g. daylight savings) in the time zone. The result is
969 * expressed using the same time specification as the original instance.
970 *
971 * Note that if the instance is a local clock time (type @c ClockTime), any
972 * daylight savings changes or time zone changes during the period will
973 * render the result inaccurate.
974 *
975 * If the instance is date-only, @p msecs is rounded down to a whole number
976 * of days and that value is added to the date to find the result.
977 *
978 * @return resultant date/time
979 * @see addSecs(), addDays(), addMonths(), addYears(), secsTo()
980 */
981 KDateTime addMSecs(qint64 msecs) const;
982
983 /**
984 * Returns a date/time @p secs seconds later than the stored date/time.
985 *
986 * Except when the instance is a local clock time (type @c ClockTime), the
987 * calculation is done in UTC to ensure that the result takes proper account
988 * of clock changes (e.g. daylight savings) in the time zone. The result is
989 * expressed using the same time specification as the original instance.
990 *
991 * Note that if the instance is a local clock time (type @c ClockTime), any
992 * daylight savings changes or time zone changes during the period will
993 * render the result inaccurate.
994 *
995 * If the instance is date-only, @p secs is rounded down to a whole number
996 * of days and that value is added to the date to find the result.
997 *
998 * @return resultant date/time
999 * @see addMSecs(), addDays(), addMonths(), addYears(), secsTo()
1000 */
1001 KDateTime addSecs(qint64 secs) const;
1002
1003 /**
1004 * Returns a date/time @p days days later than the stored date/time.
1005 * The result is expressed using the same time specification as the
1006 * original instance.
1007 *
1008 * Note that if the instance is a local clock time (type @c ClockTime), any
1009 * daylight savings changes or time zone changes during the period may
1010 * render the result inaccurate.
1011 *
1012 * @return resultant date/time
1013 * @see addSecs(), addMonths(), addYears(), daysTo()
1014 */
1015 KDateTime addDays(int days) const;
1016
1017 /**
1018 * Returns a date/time @p months months later than the stored date/time.
1019 * The result is expressed using the same time specification as the
1020 * original instance.
1021 *
1022 * Note that if the instance is a local clock time (type @c ClockTime), any
1023 * daylight savings changes or time zone changes during the period may
1024 * render the result inaccurate.
1025 *
1026 * @return resultant date/time
1027 * @see addSecs(), addDays(), addYears(), daysTo()
1028 */
1029 KDateTime addMonths(int months) const;
1030
1031 /**
1032 * Returns a date/time @p years years later than the stored date/time.
1033 * The result is expressed using the same time specification as the
1034 * original instance.
1035 *
1036 * Note that if the instance is a local clock time (type @c ClockTime), any
1037 * daylight savings changes or time zone changes during the period may
1038 * render the result inaccurate.
1039 *
1040 * @return resultant date/time
1041 * @see addSecs(), addDays(), addMonths(), daysTo()
1042 */
1043 KDateTime addYears(int years) const;
1044
1045 /**
1046 * Returns the number of seconds from this date/time to the @p other date/time.
1047 *
1048 * Before performing the comparison, the two date/times are converted to UTC
1049 * to ensure that the result is correct if one of the two date/times has
1050 * daylight saving time (DST) and the other doesn't. The exception is when
1051 * both instances are local clock time, in which case no conversion to UTC
1052 * is done.
1053 *
1054 * Note that if either instance is a local clock time (type @c ClockTime),
1055 * the result cannot be guaranteed to be accurate, since by definition they
1056 * contain no information about time zones or daylight savings changes.
1057 *
1058 * If one instance is date-only and the other is date-time, the date-time
1059 * value is first converted to the same time specification as the date-only
1060 * value, and the result is the difference in days between the resultant
1061 * date and the date-only date.
1062 *
1063 * If both instances are date-only, the result is the difference in days
1064 * between the two dates, ignoring time zones.
1065 *
1066 * @param other other date/time
1067 * @return number of seconds difference
1068 * @see secsTo_long(), addSecs(), daysTo()
1069 */
1070 int secsTo(const KDateTime &other) const;
1071
1072 /**
1073 * Returns the number of seconds from this date/time to the @p other date/time.
1074 *
1075 * Before performing the comparison, the two date/times are converted to UTC
1076 * to ensure that the result is correct if one of the two date/times has
1077 * daylight saving time (DST) and the other doesn't. The exception is when
1078 * both instances are local clock time, in which case no conversion to UTC
1079 * is done.
1080 *
1081 * Note that if either instance is a local clock time (type @c ClockTime),
1082 * the result cannot be guaranteed to be accurate, since by definition they
1083 * contain no information about time zones or daylight savings changes.
1084 *
1085 * If one instance is date-only and the other is date-time, the date-time
1086 * value is first converted to the same time specification as the date-only
1087 * value, and the result is the difference in days between the resultant
1088 * date and the date-only date.
1089 *
1090 * If both instances are date-only, the result is the difference in days
1091 * between the two dates, ignoring time zones.
1092 *
1093 * @param other other date/time
1094 * @return number of seconds difference
1095 * @see secsTo(), addSecs(), daysTo()
1096 */
1097 qint64 secsTo_long(const KDateTime &other) const;
1098
1099 /**
1100 * Calculates the number of days from this date/time to the @p other date/time.
1101 * In calculating the result, @p other is first converted to this instance's
1102 * time zone. The number of days difference is then calculated ignoring
1103 * the time parts of the two date/times. For example, if this date/time
1104 * was 13:00 on 1 January 2000, and @p other was 02:00 on 2 January 2000,
1105 * the result would be 1.
1106 *
1107 * Note that if either instance is a local clock time (type @c ClockTime),
1108 * the result cannot be guaranteed to be accurate, since by definition they
1109 * contain no information about time zones or daylight savings changes.
1110 *
1111 * If one instance is date-only and the other is date-time, the date-time
1112 * value is first converted to the same time specification as the date-only
1113 * value, and the result is the difference in days between the resultant
1114 * date and the date-only date.
1115 *
1116 * If both instances are date-only, the calculation ignores time zones.
1117 *
1118 * @param other other date/time
1119 * @return number of days difference
1120 * @see secsTo(), addDays()
1121 */
1122 int daysTo(const KDateTime &other) const;
1123
1124 /**
1125 * Returns the current date and time, as reported by the system clock,
1126 * expressed in the local system time zone.
1127 *
1128 * @return current date/time
1129 * @see currentUtcDateTime(), currentDateTime()
1130 */
1131 static KDateTime currentLocalDateTime();
1132
1133 /**
1134 * Returns the current date and time, as reported by the system clock,
1135 * expressed in UTC.
1136 *
1137 * @return current date/time
1138 * @see currentLocalDateTime(), currentDateTime(), currentLocalDate(), currentLocalTime()
1139 */
1140 static KDateTime currentUtcDateTime();
1141
1142 /**
1143 * Returns the current date and time, as reported by the system clock,
1144 * expressed in a given time specification.
1145 *
1146 * @note To fetch the current date and time expressed in UTC or in the local
1147 * system time zone, it is more efficient to use currentUtcDateTime() or
1148 * currentLocalDateTime().
1149 *
1150 * @param spec time specification
1151 * @return current date/time
1152 * @see currentUtcDateTime(), currentLocalDateTime()
1153 */
1154 static KDateTime currentDateTime(const Spec &spec);
1155
1156 /**
1157 * Returns the current date in the local time zone, as reported by the
1158 * system clock.
1159 *
1160 * @return current date
1161 * @see currentLocalDateTime(), currentLocalTime()
1162 * @since 4.3
1163 */
1164 static QDate currentLocalDate();
1165
1166 /**
1167 * Returns the current time of day in the local time zone, as reported
1168 * by the system clock.
1169 *
1170 * @return current date
1171 * @see currentLocalDateTime(), currentLocalDate()
1172 * @since 4.3
1173 */
1174 static QTime currentLocalTime();
1175
1176 /**
1177 * Returns the date/time as a string. The @p format parameter determines the
1178 * format of the result string. The @p format codes used for the date and time
1179 * components follow those used elsewhere in KDE, and are similar but not
1180 * identical to those used by strftime(3). Conversion specifiers are
1181 * introduced by a '\%' character, and are replaced in @p format as follows:
1182 *
1183 * \b Date
1184 *
1185 * - \%y 2-digit year excluding century (00 - 99). Conversion is undefined
1186 * if year < 0.
1187 * - \%Y full year number
1188 * - %:m month number, without leading zero (1 - 12)
1189 * - \%m month number, 2 digits (01 - 12)
1190 * - \%b abbreviated month name in current locale
1191 * - \%B full month name in current locale
1192 * - %:b abbreviated month name in English (Jan, Feb, ...)
1193 * - %:B full month name in English
1194 * - \%e day of the month (1 - 31)
1195 * - \%d day of the month, 2 digits (01 - 31)
1196 * - \%a abbreviated weekday name in current locale
1197 * - \%A full weekday name in current locale
1198 * - %:a abbreviated weekday name in English (Mon, Tue, ...)
1199 * - %:A full weekday name in English
1200 *
1201 * \b Time
1202 *
1203 * - \%H hour in the 24 hour clock, 2 digits (00 - 23)
1204 * - \%k hour in the 24 hour clock, without leading zero (0 - 23)
1205 * - \%I hour in the 12 hour clock, 2 digits (01 - 12)
1206 * - \%l hour in the 12 hour clock, without leading zero (1 - 12)
1207 * - \%M minute, 2 digits (00 - 59)
1208 * - \%S seconds (00 - 59)
1209 * - %:S seconds preceded with ':', but omitted if seconds value is zero
1210 * - %:s milliseconds, 3 digits (000 - 999)
1211 * - \%P "am" or "pm" in the current locale, or if undefined there, in English
1212 * - \%p "AM" or "PM" in the current locale, or if undefined there, in English
1213 * - %:P "am" or "pm"
1214 * - %:p "AM" or "PM"
1215 *
1216 * \b Time zone
1217 *
1218 * - %:u UTC offset of the time zone in hours, e.g. -02. If the offset
1219 * is not a whole number of hours, the output is the same as for '\%U'.
1220 * - \%z UTC offset of the time zone in hours and minutes, e.g. -0200.
1221 * - %:z UTC offset of the time zone in hours and minutes, e.g. +02:00.
1222 * - \%Z time zone abbreviation, e.g. UTC, EDT, GMT. This is not guaranteed
1223 * to be unique among different time zones. If not applicable (i.e. if
1224 * the instance is type OffsetFromUTC), the UTC offset is substituted.
1225 * - %:Z time zone name, e.g. Europe/London. This is system dependent. If
1226 * not applicable (i.e. if the instance is type OffsetFromUTC), the
1227 * UTC offset is substituted.
1228 *
1229 * \b Other
1230 *
1231 * - %% literal '\%' character
1232 *
1233 * Note that if the instance has a time specification of ClockTime, the
1234 * time zone or UTC offset in the result will be blank.
1235 *
1236 * If you want to use the current locale's date format, you should call
1237 * KLocale::formatDate() to format the date part of the KDateTime.
1238 *
1239 * @param format format for the string
1240 * @return formatted string
1241 * @see fromString(), KLocale::formatDate()
1242 */
1243 QString toString(const QString &format) const;
1244
1245 /**
1246 * Returns the date/time as a string, formatted according to the @p format
1247 * parameter, with the UTC offset appended.
1248 *
1249 * Note that if the instance has a time specification of ClockTime, the UTC
1250 * offset in the result will be blank, except for RFC 2822 and RFC 3339
1251 * formats in which it will be the offset for the local system time zone.
1252 *
1253 * If the instance is date-only, the time will when @p format permits be
1254 * omitted from the output string. This applies to @p format = QtTextDate
1255 * or LocalDate. It also applies to @p format = ISODate when the instance
1256 * has a time specification of ClockTime. For all other cases, a time of
1257 * 00:00:00 will be output.
1258 *
1259 * For RFC 2822 format, set @p format to RFCDateDay to include the day
1260 * of the week, or to RFCDate to omit it.
1261 *
1262 * @param format format for output string
1263 * @return formatted string
1264 * @see fromString(), QDateTime::toString()
1265 */
1266 QString toString(TimeFormat format = ISODate) const;
1267
1268 /**
1269 * Returns the KDateTime represented by @p string, using the @p format given.
1270 *
1271 * This method is the inverse of toString(TimeFormat), except that it can
1272 * only return a time specification of UTC, OffsetFromUTC or ClockTime. An
1273 * actual named time zone cannot be returned since an offset from UTC only
1274 * partially specifies a time zone.
1275 *
1276 * The time specification of the result is determined by the UTC offset
1277 * present in the string:
1278 * - if the UTC offset is zero the result is type @c UTC.
1279 * - if the UTC offset is non-zero, the result is type @c OffsetFromUTC.
1280 * - if there is no UTC offset (when @p format permits this), the result is
1281 * by default type @c ClockTime. You can use setFromStringDefault() to
1282 * change this default.
1283 *
1284 * If no time is found in @p string, a date-only value is returned, except
1285 * when the specified @p format does not permit the time to be omitted, in
1286 * which case an error is returned. An error is therefore returned for
1287 * ISODate when @p string includes a time zone specification, and for
1288 * RFCDate in all cases.
1289 *
1290 * For RFC format strings (not RFC 3339), you should normally set @p format
1291 * to RFCDate. Only set it to RFCDateDay if you want to return an error
1292 * when the day of the week is omitted.
1293 *
1294 * For @p format = ISODate or RFCDate[Day], if an invalid KDateTime is
1295 * returned, you can check why @p format was considered invalid by use of
1296 * outOfRange(). If that method returns true, it indicates that @p format
1297 * was in fact valid, but the date lies outside the range which can be
1298 * represented by QDate.
1299 *
1300 * @param string string to convert
1301 * @param format format code. LocalDate cannot be used here.
1302 * @param negZero if non-null, the value is set to true if a UTC offset of
1303 * '-0000' is found or, for RFC 2822 format, an unrecognised
1304 * or invalid time zone abbreviation is found, else false.
1305 * @return KDateTime value, or an invalid KDateTime if either parameter is invalid
1306 * @see setFromStringDefault(), toString(), outOfRange(), QString::fromString()
1307 */
1308 static KDateTime fromString(const QString &string, TimeFormat format = ISODate, bool *negZero = 0);
1309
1310 /**
1311 * Returns the KDateTime represented by @p string, using the @p format
1312 * given, optionally using a time zone collection @p zones as the source of
1313 * time zone definitions. The @p format codes are basically the same as
1314 * those for toString(), and are similar but not identical to those used by
1315 * strftime(3).
1316 *
1317 * The @p format string consists of the same codes as that for
1318 * toString(). However, some codes which are distinct in toString() have
1319 * the same function as each other here.
1320 *
1321 * Numeric values without a stated number of digits permit, but do not
1322 * require, leading zeroes. The maximum number of digits consumed by a
1323 * numeric code is the minimum needed to cover the possible range of the
1324 * number (e.g. for minutes, the range is 0 - 59, so the maximum number of
1325 * digits consumed is 2). All non-numeric values are case insensitive.
1326 *
1327 * \b Date
1328 *
1329 * - \%y year excluding century (0 - 99). Years 0 - 50 return 2000 - 2050,
1330 * while years 51 - 99 return 1951 - 1999.
1331 * - \%Y full year number (4 digits with optional sign)
1332 * - %:Y full year number (>= 4 digits with optional sign)
1333 * - %:m month number (1 - 12)
1334 * - \%m month number, 2 digits (01 - 12)
1335 * - \%b
1336 * - \%B month name in the current locale or, if no match, in English,
1337 * abbreviated or in full
1338 * - %:b
1339 * - %:B month name in English, abbreviated or in full
1340 * - \%e day of the month (1 - 31)
1341 * - \%d day of the month, 2 digits (01 - 31)
1342 * - \%a
1343 * - \%A weekday name in the current locale or, if no match, in English,
1344 * abbreviated or in full
1345 * - %:a
1346 * - %:A weekday name in English, abbreviated or in full
1347 *
1348 * \b Time
1349 *
1350 * - \%H hour in the 24 hour clock, 2 digits (00 - 23)
1351 * - \%k hour in the 24 hour clock (0 - 23)
1352 * - \%I hour in the 12 hour clock, 2 digits (01 - 12)
1353 * - \%l hour in the 12 hour clock (1 - 12)
1354 * - \%M minute, 2 digits (00 - 59)
1355 * - %:M minute (0 - 59)
1356 * - \%S seconds, 2 digits (00 - 59)
1357 * - \%s seconds (0 - 59)
1358 * - %:S optional seconds value (0 - 59) preceded with ':'. If no colon is
1359 * found in @p string, no input is consumed and the seconds value is
1360 * set to zero.
1361 * - %:s fractional seconds value, preceded with a decimal point (either '.'
1362 * or the locale's decimal point symbol)
1363 * - \%P
1364 * - \%p "am" or "pm", in the current locale or, if no match, in
1365 * English. This format is only useful when used with \%I or \%l.
1366 * - %:P
1367 * - %:p "am" or "pm" in English. This format is only useful when used with
1368 * \%I or \%l.
1369 *
1370 * \b Time zone
1371 *
1372 * - %:u
1373 * - \%z UTC offset of the time zone in hours and optionally minutes,
1374 * e.g. -02, -0200.
1375 * - %:z UTC offset of the time zone in hours and minutes, colon separated,
1376 * e.g. +02:00.
1377 * - \%Z time zone abbreviation, consisting of alphanumeric characters,
1378 * e.g. UTC, EDT, GMT.
1379 * - %:Z time zone name, e.g. Europe/London. The name may contain any
1380 * characters and is delimited by the following character in the
1381 * @p format string. It will not work if you follow %:Z with another
1382 * escape sequence (except %% or \%t).
1383 *
1384 * \b Other
1385 *
1386 * - \%t matches one or more whitespace characters
1387 * - %% literal '\%' character
1388 *
1389 * Any other character must have a matching character in @p string, except
1390 * that a space will match zero or more whitespace characters in the input
1391 * string.
1392 *
1393 * If any time zone information is present in the string, the function
1394 * attempts to find a matching time zone in the @p zones collection. A time
1395 * zone name (format code %:Z) will provide an unambiguous look up in
1396 * @p zones. Any other type of time zone information (an abbreviated time
1397 * zone code (\%Z) or UTC offset (\%z, %:z, %:u) is searched for in @p zones
1398 * and if only one time zone is found to match, the result is set to that
1399 * zone. Otherwise:
1400 * - If more than one match of a UTC offset is found, the action taken is
1401 * determined by @p offsetIfAmbiguous: if @p offsetIfAmbiguous is true,
1402 * a local time with an offset from UTC (type @c OffsetFromUTC) will be
1403 * returned; if false an invalid KDateTime is returned.
1404 * - If more than one match of a time zone abbreviation is found, the UTC
1405 * offset for each matching time zone is compared and, if the offsets are
1406 * the same, a local time with an offset from UTC (type @c OffsetFromUTC)
1407 * will be returned provided that @p offsetIfAmbiguous is true. Otherwise
1408 * an invalid KDateTime is returned.
1409 * - If a time zone abbreviation does not match any time zone in @p zones,
1410 * or the abbreviation does not apply at the parsed date/time, an
1411 * invalid KDateTime is returned.
1412 * - If a time zone name does not match any time zone in @p zones, an
1413 * invalid KDateTime is returned.
1414 * - If the time zone UTC offset does not match any time zone in @p zones,
1415 * a local time with an offset from UTC (type @c OffsetFromUTC) is
1416 * returned.
1417 * If @p format contains more than one time zone or UTC offset code, an
1418 * error is returned.
1419 *
1420 * If no time zone information is present in the string, by default a local
1421 * clock time (type @c ClockTime) is returned. You can use
1422 * setFromStringDefault() to change this default.
1423 *
1424 * If no time is found in @p string, a date-only value is returned.
1425 *
1426 * If any inconsistencies are found, i.e. the same item of information
1427 * appears more than once but with different values, the weekday name does
1428 * not tally with the date, an invalid KDateTime is returned.
1429 *
1430 * If an invalid KDateTime is returned, you can check why @p format was
1431 * considered invalid by use of outOfRange(). If that method returns true,
1432 * it indicates that @p format was in fact valid, but the date lies outside
1433 * the range which can be represented by QDate.
1434 *
1435 * @param string string to convert
1436 * @param format format string
1437 * @param zones time zone collection, or null for none
1438 * @param offsetIfAmbiguous specifies what to do if more than one zone
1439 * matches the UTC offset found in the
1440 * string. Ignored if @p zones is null.
1441 * @return KDateTime value, or an invalid KDateTime if an error occurs, if
1442 * time zone information doesn't match any in @p zones, or if the
1443 * time zone information is ambiguous and @p offsetIfAmbiguous is
1444 * false
1445 * @see setFromStringDefault(), toString(), outOfRange()
1446 */
1447 static KDateTime fromString(const QString &string, const QString &format,
1448 const KTimeZones *zones = 0, bool offsetIfAmbiguous = true);
1449
1450 /**
1451 * Sets the default time specification for use by fromString() when no time
1452 * zone or UTC offset is found in the string being parsed, or when "-0000"
1453 * is found in an RFC 2822 string.
1454 *
1455 * By default, fromString() returns a local clock time (type @c ClockTime)
1456 * when no definite zone or UTC offset is found. You can use this method
1457 * to make it return the local time zone, UTC, or whatever you wish.
1458 *
1459 * @param spec the new default time specification
1460 * @see fromString()
1461 */
1462 static void setFromStringDefault(const Spec &spec);
1463
1464
1465 /**
1466 * Checks whether the date/time returned by the last call to fromString()
1467 * was invalid because an otherwise valid date was outside the range which
1468 * can be represented by QDate. This status occurs when fromString() read
1469 * a valid string containing a year earlier than -4712 (4713 BC). On exit
1470 * from fromString(), if outOfRange() returns @c true, isValid() will
1471 * return @c false.
1472 *
1473 * @return @c true if date was earlier than -4712, else @c false
1474 * @see isValid(), fromString()
1475 */
1476 bool outOfRange() const;
1477
1478 /**
1479 * Compare this instance with another to determine whether they are
1480 * simultaneous, earlier or later, and in the case of date-only values,
1481 * whether they overlap (i.e. partly coincide but are not wholly
1482 * simultaneous).
1483 * The comparison takes time zones into account: if the two instances have
1484 * different time zones, they are first converted to UTC before comparing.
1485 *
1486 * If both instances are date/time values, this instance is considered to
1487 * be either simultaneous, earlier or later, and does not overlap.
1488 *
1489 * If one instance is date-only and the other is a date/time, this instance
1490 * is either strictly earlier, strictly later, or overlaps.
1491 *
1492 * If both instance are date-only, they are considered simultaneous if both
1493 * their start of day and end of day times are simultaneous with each
1494 * other. (Both start and end of day times need to be considered in case a
1495 * daylight savings change occurs during that day.) Otherwise, this instance
1496 * can be strictly earlier, earlier but overlapping, later but overlapping,
1497 * or strictly later.
1498 *
1499 * Note that if either instance is a local clock time (type @c ClockTime),
1500 * the result cannot be guaranteed to be correct, since by definition they
1501 * contain no information about time zones or daylight savings changes.
1502 *
1503 * @return @c true if the two instances represent the same time, @c false otherwise
1504 * @see operator==(), operator!=(), operator<(), operator<=(), operator>=(), operator>()
1505 */
1506 Comparison compare(const KDateTime &other) const;
1507
1508 /**
1509 * Check whether this date/time is simultaneous with another.
1510 * The comparison takes time zones into account: if the two instances have
1511 * different time zones, they are first converted to UTC before comparing.
1512 *
1513 * Note that if either instance is a local clock time (type @c ClockTime),
1514 * the result cannot be guaranteed to be correct, since by definition they
1515 * contain no information about time zones or daylight savings changes.
1516 *
1517 * If one instance is date-only and the other is date/time, they are
1518 * considered unequal.
1519 *
1520 * If both instances are date-only, they are considered simultaneous if both
1521 * their start of day and end of day times are simultaneous with each
1522 * other. (Both start and end of day times need to be considered in case a
1523 * daylight saving change occurs during that day.)
1524 *
1525 * @return @c true if the two instances represent the same time, @c false otherwise
1526 * @see compare()
1527 */
1528 bool operator==(const KDateTime &other) const;
1529
1530 bool operator!=(const KDateTime &other) const { return !(*this == other); }
1531
1532 /**
1533 * Check whether this date/time is earlier than another.
1534 * The comparison takes time zones into account: if the two instances have
1535 * different time zones, they are first converted to UTC before comparing.
1536 *
1537 * Note that if either instance is a local clock time (type @c ClockTime),
1538 * the result cannot be guaranteed to be correct, since by definition they
1539 * contain no information about time zones or daylight savings changes.
1540 *
1541 * If one or both instances are date-only, the comparison returns true if
1542 * this date/time or day, falls wholly before the other date/time or
1543 * day. To achieve this, the time used in the comparison is the end of day
1544 * (if this instance is date-only) or the start of day (if the other
1545 * instance is date-only).
1546 *
1547 * @return @c true if this instance represents an earlier time than @p other,
1548 * @c false otherwise
1549 * @see compare()
1550 */
1551 bool operator<(const KDateTime &other) const;
1552
1553 bool operator<=(const KDateTime &other) const { return !(other < *this); }
1554 bool operator>(const KDateTime &other) const { return other < *this; }
1555 bool operator>=(const KDateTime &other) const { return !(*this < other); }
1556
1557 /**
1558 * Create a separate copy of this instance's data if it is implicitly shared
1559 * with another instance.
1560 *
1561 * You would normally only call this if you want different copies of the
1562 * same date/time value to cache conversions to different time zones. Because
1563 * only the last conversion to another time zone is cached, and the cached
1564 * value is implicitly shared, judicious use of detach() could improve
1565 * efficiency when handling several time zones. But take care: if used
1566 * inappropriately, it will reduce efficiency!
1567 */
1568 void detach();
1569
1570 /**
1571 * Set an adjustment to be applied when fetching the current system time.
1572 * This is applied by all KDateTime methods which return the system date
1573 * and/or time.
1574 *
1575 * The supplied date/time is used as the current simulated time and the
1576 * time adjustment is set to the difference between the real current time
1577 * and @p newTime. If @p newTime has a time zone, that time zone is set
1578 * to be the simulated local system time zone by calling
1579 * KSystemTimeZones::setLocalZone()).
1580 *
1581 * To cancel time simulation, supply an invalid @p newTime parameter.
1582 *
1583 * @warning This function is provided only for testing purposes, and should
1584 * not be used in released code. If the library is compiled without
1585 * debug enabled, setSimulatedSystemTime() has no effect.
1586 * To avoid confusion, it is recommended that calls to it should be
1587 * conditionally compiled, e.g.:
1588 * \code
1589 * #ifndef NDEBUG
1590 * KDateTime::simulateSystemTime(kdt);
1591 * #endif
1592 * \endcode
1593 *
1594 * @param newTime the current simulated time, or invalid to cancel simulation
1595 *
1596 * @see currentDateTime(), currentLocalDateTime(), currentUtcDateTime(),
1597 * currentLocalDate(), currentLocalTime()
1598 * @since 4.3
1599 */
1600 static void setSimulatedSystemTime(const KDateTime& newTime);
1601
1602 /**
1603 * Return the real (not simulated) system time.
1604 *
1605 * @warning This method is provided only for testing purposes, and should
1606 * not be used in released code. If the library is compiled without
1607 * debug enabled, currentLocalDateTime() and realCurrentLocalDateTime()
1608 * both return the real system time.
1609 * To avoid confusion, it is recommended that calls to
1610 * realCurrentLocalDateTime() should be conditionally compiled, e.g.:
1611 * \code
1612 * #ifndef NDEBUG
1613 * dt = KDateTime::realCurrentLocalDateTime();
1614 * #endif
1615 * \endcode
1616 *
1617 * @since 4.3
1618 */
1619 static KDateTime realCurrentLocalDateTime();
1620
1621 friend QDataStream KDECORE_EXPORT &operator<<(QDataStream &out, const KDateTime &dateTime);
1622 friend QDataStream KDECORE_EXPORT &operator>>(QDataStream &in, KDateTime &dateTime);
1623
1624 private:
1625 QSharedDataPointer<KDateTimePrivate> d;
1626};
1627
1628Q_DECLARE_METATYPE(KDateTime)
1629Q_DECLARE_METATYPE(KDateTime::Spec)
1630
1631/** Write @p spec to the datastream @p out, in binary format. */
1632QDataStream KDECORE_EXPORT &operator<<(QDataStream &out, const KDateTime::Spec &spec);
1633/** Read a KDateTime::Spec object into @p spec from @p in, in binary format. */
1634QDataStream KDECORE_EXPORT &operator>>(QDataStream &in, KDateTime::Spec &spec);
1635
1636/** Write @p dateTime to the datastream @p out, in binary format. */
1637QDataStream KDECORE_EXPORT &operator<<(QDataStream &out, const KDateTime &dateTime);
1638/** Read a KDateTime object into @p dateTime from @p in, in binary format. */
1639QDataStream KDECORE_EXPORT &operator>>(QDataStream &in, KDateTime &dateTime);
1640
1641#endif
1642