File indexing completed on 2025-02-16 04:48:40
0001 /* 0002 * This file is part of kalarmcalendar library, which provides access to KAlarm 0003 * calendar data. 0004 * This is the Qt5 version of KDE 4 kdelibs/kdecore/date/kdatetime.h. 0005 * Program: kalarm 0006 * SPDX-FileCopyrightText: 2005-2022 David Jarvie <djarvie@kde.org> 0007 * 0008 * SPDX-License-Identifier: LGPL-2.0-or-later 0009 */ 0010 0011 /** @file 0012 * Date/times with associated time zone 0013 * @author David Jarvie <djarvie@kde.org>. 0014 */ 0015 0016 #pragma once 0017 0018 #include "kalarmcal_export.h" 0019 0020 #include <QDateTime> 0021 #include <QMetaType> 0022 #include <QSharedDataPointer> 0023 0024 class QDataStream; 0025 0026 namespace KAlarmCal 0027 { 0028 0029 class KADateTimePrivate; 0030 class KADateTimeSpecPrivate; 0031 0032 /** 0033 * @short A class representing a date and time with an associated time zone 0034 * 0035 * Topics: 0036 * - @ref intro 0037 * - @ref manipulation 0038 * - @ref compatibility 0039 * 0040 * @section intro Introduction 0041 * 0042 * The class KADateTime extends the functionality of QDateTime. It combines a 0043 * date and time with support for an associated time zone or UTC offset. When 0044 * manipulating KADateTime objects, their time zones or UTC offsets are 0045 * automatically taken into account. Extensions to QDateTime are: 0046 * - KADateTime can be set to represent a date-only value with no associated 0047 * time. 0048 * - Local date/times which occur twice due to daylight savings time shifts, 0049 * are handled properly, so that the two occurrences can be distinguished. 0050 * - Extra string formatting functions are provided. 0051 * 0052 * The class uses QDateTime internally to represent date/time values. This 0053 * imposes the following limitations: 0054 * - QDateTime uses the Gregorian calendar retroactively. If you need the 0055 * Julian calendar for historical dates (as commonly used prior to some date 0056 * between 1582 and 1923 depending on nation), please use @c KCalendarSystem 0057 * and related classes. 0058 * - QDateTime does not handle daylight savings time changes properly before 0059 * 1970, even when the system's time zone database contains this information. 0060 * 0061 * The time specification types which KADateTime supports are: 0062 * - the UTC time zone 0063 * - a local time with a specified offset from UTC 0064 * - a local time in a specified time zone 0065 * - a local time using the current system time zone. This follows any changes 0066 * to the system time zone. 0067 * 0068 * These characteristics are more fully described in the description of the 0069 * SpecType enumeration. Also see 0070 * <a href="http://www.w3.org/TR/timezone/">W3C: Working with Time Zones</a> 0071 * for a good overview of the different ways of representing times. 0072 * 0073 * To set the time specification, use one of the setTimeSpec() methods. To get 0074 * the time specification, call timeSpec(), isUtc(), isLocalZone(), or 0075 * isOffsetFromUtc(). To determine whether two KADateTime instances have the 0076 * same time specification, call timeSpec() on each and compare the returned 0077 * values using KADateTime::Spec::operator==(). 0078 * 0079 * @section manipulation Date and Time Manipulation 0080 * 0081 * A KADateTime object can be created by passing a date and time in its 0082 * constructor, together with a time specification. 0083 * 0084 * If both the date and time are null, isNull() returns true. If the date, time 0085 * and time specification are all valid, isValid() returns true. 0086 * 0087 * A KADateTime object can be converted to a different time specification by 0088 * using toUtc() or toLocalZone(). It can be converted to a specific time zone 0089 * by toZone(). To return the time as an elapsed time since 1 January 1970 (as 0090 * used by time(2)), use toSecsSinceEpoch(). The results of time zone 0091 * conversions are cached to minimize the need for recalculation. Each 0092 * KADateTime object caches its UTC equivalent and the last time zone 0093 * conversion performed. 0094 * 0095 * The date and time can be set either in the constructor, or afterwards by 0096 * calling setDate() or setTime(). To return the date and/or time components of 0097 * the KADateTime, use date(), time() and qDateTime(). You can determine whether 0098 * the KADateTime represents a date and time, or a date only, by isDateOnly(). 0099 * You can change between a date and time or a date only value using 0100 * setDateOnly(). 0101 * 0102 * You can increment or decrement the date/time using addSecs(), addDays(), 0103 * addMonths() and addYears(). The interval between two date/time values can 0104 * be found using secsTo() or daysTo(). 0105 * 0106 * The comparison operators (operator==(), operator<(), etc.) all take the time 0107 * zone properly into account; if the two KADateTime objects have different time 0108 * zones, they are first converted to UTC before the comparison is 0109 * performed. An alternative to the comparison operators is compare() which will 0110 * in addition tell you if a KADateTime object overlaps with another when one or 0111 * both are date-only values. 0112 * 0113 * KADateTime values may be converted to and from a string representation using 0114 * the toString() and fromString() methods. These handle a variety of text 0115 * formats including ISO 8601 and RFC 2822. 0116 * 0117 * KADateTime uses Qt's facilities to implicitly share data. Copying instances 0118 * is very efficient, and copied instances share cached UTC and time zone 0119 * conversions even after the copy is performed. A separate copy of the data is 0120 * created whenever a non-const method is called. If you want to force the 0121 * creation of a separate copy of the data (e.g. if you want two copies to 0122 * cache different time zone conversions), call detach(). 0123 * 0124 * @section compatibility QDateTime Considerations 0125 * 0126 * KADateTime's interface is designed to be as compatible as possible with that 0127 * of QDateTime. Because QDateTime lacks virtual methods, KADateTime is not 0128 * inherited from QDateTime, but instead is implemented using a private 0129 * QDateTime object. 0130 * 0131 * @section simulation Simulation Facility 0132 * 0133 * This class provides a facility to simulate the local system time, which 0134 * affects all functions using or returning the system time. This facility is 0135 * provided for testing purposes only, and is only available if the library is 0136 * compiled with debug enabled. In release mode, simulation is inoperative and 0137 * the real local system time is used at all times. Use 0138 * setSimulatedSystemTime() to set or clear the simulated time. To read the 0139 * real (not simulated) system time, use realCurrentLocalDateTime(). 0140 * 0141 * @see QTimeZone, QDateTime, QDate, QTime 0142 * @see <a href="http://www.w3.org/TR/timezone/">W3C: Working with Time Zones</a> 0143 * @author David Jarvie \<djarvie@kde.org\>. 0144 */ 0145 class KALARMCAL_EXPORT KADateTime 0146 { 0147 public: 0148 /** 0149 * The time specification type of a KADateTime instance. 0150 * This specifies how the date/time component of the KADateTime instance 0151 * should be interpreted, i.e. what type of time zone (if any) the date/time 0152 * is expressed in. For the full time specification (including time zone 0153 * details), see KADateTime::Spec. 0154 */ 0155 enum SpecType 0156 { 0157 Invalid, /**< an invalid time specification. */ 0158 UTC, /**< a UTC time. */ 0159 OffsetFromUTC, /**< a local time which has a fixed offset from UTC. */ 0160 TimeZone, /**< a time in a specified time zone. 0161 * If the time zone happens to be the current system time 0162 * zone (i.e. that returned by QTimeZone::systemTimeZone()), 0163 * that time zone will continue to be used, unlike in the 0164 * case of LocalZone, even if the system changes to use a 0165 * different time zone. 0166 */ 0167 LocalZone /**< a time in the current system time zone. 0168 * Note that if the system is changed to a different time 0169 * zone afterwards, the KADateTime instance will then use 0170 * the new system time zone, as returned currently by 0171 * QTimeZone::systemTimeZone(). 0172 */ 0173 }; 0174 0175 /** 0176 * The full time specification of a KADateTime instance. 0177 * This specifies how the date/time component of the KADateTime instance 0178 * should be interpreted, i.e. which time zone (if any) the date/time is 0179 * expressed in. 0180 */ 0181 class Spec 0182 { 0183 public: 0184 /** 0185 * Constructs an invalid time specification. 0186 */ 0187 Spec(); 0188 0189 /** 0190 * Constructs a time specification for a given time zone. 0191 * If @p tz is QTimeZone::utc(), the time specification type is set to @c UTC. 0192 * 0193 * @param tz time zone 0194 */ 0195 Spec(const QTimeZone& tz); //cppcheck-suppress noExplicitConstructor; Allow implicit conversion 0196 0197 /** 0198 * Constructs a time specification. 0199 * 0200 * @param type time specification type, which should not be @c TimeZone 0201 * @param utcOffset number of seconds to add to UTC to get the local 0202 * time. Ignored if @p type is not @c OffsetFromUTC. 0203 */ 0204 Spec(SpecType type, int utcOffset = 0); //cppcheck-suppress noExplicitConstructor; Allow implicit conversion 0205 0206 /** 0207 * Copy constructor. 0208 */ 0209 Spec(const Spec& spec); 0210 0211 /** 0212 * Assignment operator. 0213 */ 0214 Spec& operator=(const Spec& spec); 0215 0216 /** 0217 * Destructor 0218 */ 0219 ~Spec(); 0220 0221 /** 0222 * Returns whether the time specification is valid. 0223 * 0224 * @return @c true if valid, else @c false 0225 */ 0226 bool isValid() const; 0227 0228 /** 0229 * Returns the time zone for the date/time, according to the time 0230 * specification type as follows: 0231 * - @c TimeZone : the specified time zone is returned. 0232 * - @c UTC : a UTC time zone is returned. 0233 * - @c LocalZone : the current local time zone is returned. 0234 * 0235 * @return time zone as defined above, or invalid in all other cases 0236 * @see isUtc(), isLocal() 0237 */ 0238 QTimeZone timeZone() const; 0239 0240 /** 0241 * Returns the time specification type, i.e. whether it is 0242 * UTC, has a time zone, etc. 0243 * 0244 * @return specification type 0245 * @see isLocalZone(), isUtc(), timeZone() 0246 */ 0247 SpecType type() const; 0248 0249 /** 0250 * Returns whether the time specification follows the current local 0251 * system time zone. 0252 * 0253 * @return @c true if local system time zone 0254 * @see isUtc(), isOffsetFromUtc(), timeZone() 0255 */ 0256 bool isLocalZone() const; 0257 0258 /** 0259 * Returns whether the time specification is a UTC time. 0260 * It is considered to be a UTC time if it is either type @c UTC, 0261 * or is type @c OffsetFromUTC with a zero UTC offset. 0262 * 0263 * @return @c true if UTC 0264 * @see isLocal(), isOffsetFromUtc(), timeZone() 0265 */ 0266 bool isUtc() const; 0267 0268 /** 0269 * Returns whether the time specification is a local time at a fixed 0270 * offset from UTC. 0271 * 0272 * @return @c true if local time at fixed offset from UTC 0273 * @see isLocal(), isUtc(), utcOffset() 0274 */ 0275 bool isOffsetFromUtc() const; 0276 0277 /** 0278 * Returns the UTC offset associated with the time specification. The 0279 * UTC offset is the number of seconds to add to UTC to get the local time. 0280 * 0281 * @return UTC offset in seconds if type is @c OffsetFromUTC, else 0 0282 * @see isOffsetFromUtc() 0283 */ 0284 int utcOffset() const; 0285 0286 /** 0287 * Initialises the time specification. 0288 * 0289 * @param type the time specification type. Note that @c TimeZone 0290 * is invalid here. 0291 * @param utcOffset number of seconds to add to UTC to get the local 0292 * time. Ignored if @p spec is not @c OffsetFromUTC. 0293 * @see type(), setType(const QTimeZone&) 0294 */ 0295 void setType(SpecType type, int utcOffset = 0); 0296 0297 /** 0298 * Sets the time zone for the time specification. 0299 * 0300 * Note that setting the time zone to the current local system time zone 0301 * has a different effect from setType(LocalZone), since the latter 0302 * will follow changes to the system time zone. 0303 * 0304 * @param tz new time zone 0305 * @see timeZone(), setType(SpecType) 0306 */ 0307 void setType(const QTimeZone& tz); 0308 0309 /** 0310 * Comparison operator. 0311 * 0312 * @return @c true if the two instances are identical, @c false otherwise 0313 * @see equivalentTo() 0314 */ 0315 bool operator==(const Spec& other) const; 0316 0317 bool operator!=(const Spec& other) const 0318 { 0319 return !operator==(other); 0320 } 0321 0322 /** 0323 * Checks whether this instance is equivalent to another. 0324 * The two instances are considered to be equivalent if any of the following 0325 * conditions apply: 0326 * - both instances are type @c OffsetFromUTC and their offsets from UTC are equal. 0327 * - both instances are type @c TimeZone and their time zones are equal. 0328 * - one instance is type @c TimeZone, with the time zone set to the system time 0329 * zone, and the other instance is type @c LocalZone. 0330 * - both instances are type @c LocalZone. 0331 * - both instances are UTC. An instance is considered to be UTC if it is 0332 * either type @c UTC, or is type @c OffsetFromUTC with a zero UTC offset. 0333 * 0334 * @return @c true if the two instances are equivalent, @c false otherwise 0335 * @see operator==() 0336 */ 0337 bool equivalentTo(const Spec& other) const; 0338 0339 /** 0340 * The UTC time specification. 0341 * Provided as a shorthand for KADateTime::Spec(KADateTime::UTC). 0342 */ 0343 static Spec UTC(); 0344 0345 /** 0346 * Returns a UTC offset time specification. 0347 * Provided as a shorthand for KADateTime::Spec(KADateTime::OffsetFromUTC, utcOffset). 0348 * 0349 * @param utcOffset number of seconds to add to UTC to get the local time 0350 * @return UTC offset time specification 0351 */ 0352 static Spec OffsetFromUTC(int utcOffset); 0353 0354 /** 0355 * Returns a local time zone time specification. 0356 * Provided as a shorthand for KADateTime::Spec(KADateTime::LocalZone). 0357 * 0358 * @return Local zone time specification 0359 */ 0360 static Spec LocalZone(); 0361 0362 private: 0363 KADateTimeSpecPrivate* const d; 0364 }; 0365 0366 /** Format for strings representing date/time values. */ 0367 enum TimeFormat 0368 { 0369 ISODate, /**< ISO 8601 format, i.e. [±]YYYY-MM-DDThh[:mm[:ss[.sss]]]TZ, 0370 * where TZ is the time zone offset (blank for local 0371 * time, Z for UTC, or ±hhmm for an offset from UTC). 0372 * When parsing a string, the ISO 8601 basic format, 0373 * [±]YYYYMMDDThh[mm[ss[.sss]]]TZ, is also accepted. For 0374 * date-only values, the formats [±]YYYY-MM-DD and 0375 * [±]YYYYMMDD (without time zone specifier) are used. All 0376 * formats may contain a day of the year instead of day 0377 * and month. 0378 * To allow for years past 9999, the year may optionally 0379 * contain more than 4 digits. To avoid ambiguity, this is 0380 * not allowed in the basic format containing a day 0381 * of the year (i.e. when the date part is [±]YYYYDDD). 0382 */ 0383 ISODateFull,/**< ISO 8601 format, always including a time zone. 0384 * This is the same as ISODate, except that the current 0385 * system time zone offset is appended for local times 0386 * (type LocalZone). 0387 */ 0388 RFCDate, /**< RFC 2822 format, 0389 * i.e. "[Wdy,] DD Mon YYYY hh:mm[:ss] ±hhmm". This format 0390 * also covers RFCs 822, 850, 1036 and 1123. 0391 * When parsing a string, it also accepts the format 0392 * "Wdy Mon DD HH:MM:SS YYYY" specified by RFCs 850 and 0393 * 1036. There is no valid date-only format. 0394 */ 0395 RFCDateDay, /**< RFC 2822 format including day of the week, 0396 * i.e. "Wdy, DD Mon YYYY hh:mm:ss ±hhmm" 0397 */ 0398 QtTextDate, /**< Same format as Qt::TextDate (i.e. Day Mon DD hh:mm:ss YYYY) 0399 * with, if not local time, the UTC offset appended. The 0400 * time may be omitted to indicate a date-only value. 0401 */ 0402 LocalDate, /**< Same format as Qt::LocalDate (i.e. locale dependent) 0403 * with, if not local time, the UTC offset appended. The 0404 * time may be omitted to indicate a date-only value. 0405 */ 0406 RFC3339Date /**< RFC 3339 format, 0407 * i.e. "YYYY-MM-DDThh:mm:ss[.sss](Z|±hh:mm)". 0408 * There is no valid date-only format. 0409 */ 0410 0411 }; 0412 0413 /** 0414 * How this KADateTime compares with another. 0415 * If any date-only value is involved, comparison of KADateTime values 0416 * requires them to be considered as representing time periods. A date-only 0417 * instance represents a time period from 00:00:00 to 23:59:59.999 on a given 0418 * date, while a date/time instance can be considered to represent a time 0419 * period whose start and end times are the same. They may therefore be 0420 * earlier or later, or may overlap or be contained one within the other. 0421 * 0422 * Values may be OR'ed with each other in any combination of 'consecutive' 0423 * intervals to represent different types of relationship. 0424 * 0425 * In the descriptions of the values below, 0426 * - s1 = start time of this instance 0427 * - e1 = end time of this instance 0428 * - s2 = start time of other instance 0429 * - e2 = end time of other instance. 0430 */ 0431 enum Comparison 0432 { 0433 Before = 0x01, /**< This KADateTime is strictly earlier than the other, 0434 * i.e. e1 < s2. 0435 */ 0436 AtStart = 0x02, /**< This KADateTime starts at the same time as the other, 0437 * and ends before the end of the other, 0438 * i.e. s1 = s2, e1 < e2. 0439 */ 0440 Inside = 0x04, /**< This KADateTime starts after the start of the other, 0441 * and ends before the end of the other, 0442 * i.e. s1 > s2, e1 < e2. 0443 */ 0444 AtEnd = 0x08, /**< This KADateTime starts after the start of the other, 0445 * and ends at the same time as the other, 0446 * i.e. s1 > s2, e1 = e2. 0447 */ 0448 After = 0x10, /**< This KADateTime is strictly later than the other, 0449 * i.e. s1 > e2. 0450 */ 0451 Equal = AtStart | Inside | AtEnd, 0452 /**< Simultaneous, i.e. s1 = s2 && e1 = e2. 0453 */ 0454 Outside = Before | AtStart | Inside | AtEnd | After, 0455 /**< This KADateTime starts before the start of the other, 0456 * and ends after the end of the other, 0457 * i.e. s1 < s2, e1 > e2. 0458 */ 0459 StartsAt = AtStart | Inside | AtEnd | After, 0460 /**< This KADateTime starts at the same time as the other, 0461 * and ends after the end of the other, 0462 * i.e. s1 = s2, e1 > e2. 0463 */ 0464 EndsAt = Before | AtStart | Inside | AtEnd 0465 /**< This KADateTime starts before the start of the other, 0466 * and ends at the same time as the other, 0467 * i.e. s1 < s2, e1 = e2. 0468 */ 0469 }; 0470 0471 /** 0472 * Constructs an invalid date/time. 0473 */ 0474 KADateTime(); 0475 0476 /** 0477 * Constructs a date-only value expressed in a given time specification. The 0478 * time is set to 00:00:00. 0479 * 0480 * The instance is initialised according to the time specification type of 0481 * @p spec as follows: 0482 * - @c UTC : date is stored as UTC. 0483 * - @c OffsetFromUTC : date is a local time at the specified offset 0484 * from UTC. 0485 * - @c TimeZone : date is a local time in the specified time zone. 0486 * - @c LocalZone : date is a local date in whatever the system time 0487 * zone is currently. 0488 * 0489 * @param date date in the time zone indicated by @p spec 0490 * @param spec time specification 0491 */ 0492 explicit KADateTime(const QDate& date, const Spec& spec = Spec(LocalZone)); 0493 0494 /** 0495 * Constructs a date/time expressed as specified by @p spec. 0496 * 0497 * @p date and @p time are interpreted and stored according to the value of 0498 * @p spec as follows: 0499 * - @c UTC : @p date and @p time are in UTC. 0500 * - @c OffsetFromUTC : date/time is a local time at the specified offset 0501 * from UTC. 0502 * - @c TimeZone : date/time is a local time in the specified time zone. 0503 * - @c LocalZone : @p date and @p time are local times in whatever the 0504 * system time zone is currently. 0505 * 0506 * If @p spec is @c TimeZone or @c LocalZone and the resultant date/time 0507 * occurs twice due to a daylight savings time shift, it is set to the first 0508 * occurrence (before the time shift). 0509 * 0510 * @param date date in the time zone indicated by @p spec 0511 * @param time time in the time zone indicated by @p spec 0512 * @param spec time specification 0513 */ 0514 KADateTime(const QDate& date, const QTime& time, const Spec& spec = Spec(LocalZone)); 0515 0516 /** 0517 * Constructs a date/time expressed in a given time specification. 0518 * 0519 * In detail, @p dt is interpreted and stored according to the time 0520 * specification type of @p spec as follows: 0521 * - @c UTC : @p dt is stored as a UTC value. If @c dt.timeSpec() 0522 * is not @c Qt::UTC, @p dt is first converted to UTC 0523 * before storage. 0524 * - @c OffsetFromUTC : date/time is stored as a time at the specified offset 0525 * from UTC. If @c dt.timeSpec() is not @c Qt::UTC, 0526 * the time is converted to UTC and then adjusted by 0527 * the UTC offset before storage. 0528 * - @c TimeZone : date/time is stored as a local time in the specified 0529 * time zone. If @c dt.timeSpec() is not the same time 0530 * zone, @p dt is converted to the specified time zone 0531 * before storage. 0532 * - @c LocalZone : @p dt is stored as a local time in the current system 0533 * time zone. If @c dt.timeSpec() is not @c Qt::LocalTime, 0534 * @p dt is first converted to the system time zone before 0535 * storage. 0536 * 0537 * @param dt date and time 0538 * @param spec time specification 0539 */ 0540 KADateTime(const QDateTime& dt, const Spec& spec); 0541 0542 /** 0543 * Constructs a date/time from a QDateTime. 0544 * The KADateTime's time specification is determined according to @p dt.timeSpec(). 0545 * 0546 * @param dt date and time 0547 */ 0548 explicit KADateTime(const QDateTime& dt); 0549 0550 KADateTime(const KADateTime& other); 0551 ~KADateTime(); 0552 0553 KADateTime& operator=(const KADateTime& other); 0554 0555 /** 0556 * Returns whether the date/time is null. 0557 * 0558 * @return @c true if both date and time are null, else @c false 0559 * @see isValid(), QDateTime::isNull() 0560 */ 0561 bool isNull() const; 0562 0563 /** 0564 * Returns whether the date/time is valid. 0565 * 0566 * @return @c true if both date and time are valid, else @c false 0567 * @see isNull(), QDateTime::isValid() 0568 */ 0569 bool isValid() const; 0570 0571 /** 0572 * Returns whether the instance represents a date/time or a date-only value. 0573 * 0574 * @return @c true if date-only, @c false if date and time 0575 */ 0576 bool isDateOnly() const; 0577 0578 /** 0579 * Returns the date part of the date/time. The value returned should be 0580 * interpreted in terms of the instance's time zone or UTC offset. 0581 * 0582 * @return date value 0583 * @see time(), qDateTime() 0584 */ 0585 QDate date() const; 0586 0587 /** 0588 * Returns the time part of the date/time. The value returned should be 0589 * interpreted in terms of the instance's time zone or UTC offset. If 0590 * the instance is date-only, the time returned is 00:00:00. 0591 * 0592 * @return time value 0593 * @see date(), qDateTime(), isDateOnly() 0594 */ 0595 QTime time() const; 0596 0597 /** 0598 * Converts the instance to a QDateTime value. 0599 * 0600 * If the instance is date-only, the time value is set to 00:00:00. 0601 * 0602 * @return date/time 0603 * @see date(), time() 0604 */ 0605 QDateTime qDateTime() const; 0606 0607 /** 0608 * Returns the time zone for the date/time. If the date/time is specified 0609 * as a UTC time, a UTC time zone is always returned. If it is specified as 0610 * LocalZone, the system time zone is returned. 0611 * 0612 * @return time zone, or invalid if a local time at a fixed UTC offset 0613 * @see isUtc(), isLocal() 0614 */ 0615 QTimeZone timeZone() const; 0616 0617 /** 0618 * Returns the time specification of the date/time, i.e. whether it is 0619 * UTC, what time zone it is, etc. 0620 * 0621 * @return time specification 0622 * @see isLocalZone(), isUtc(), timeZone() 0623 */ 0624 Spec timeSpec() const; 0625 0626 /** 0627 * Returns the time specification type of the date/time, i.e. whether it is 0628 * UTC, has a time zone, etc. 0629 * 0630 * @return specification type 0631 * @see timeSpec(), isLocalZone(), isUtc(), timeZone() 0632 */ 0633 SpecType timeType() const; 0634 0635 /** 0636 * Returns whether the time zone for the date/time follows the current local 0637 * system time zone. 0638 * 0639 * @return @c true if local system time zone 0640 * @see isUtc(), isOffsetFromUtc(), timeZone() 0641 */ 0642 bool isLocalZone() const; 0643 0644 /** 0645 * Returns whether the date/time is a UTC time. 0646 * It is considered to be a UTC time if it either has a UTC time 0647 * specification (SpecType == UTC), or has a zero offset from UTC 0648 * (SpecType == OffsetFromUTC with zero UTC offset). 0649 * 0650 * @return @c true if UTC 0651 * @see isLocal(), isOffsetFromUtc(), timeZone() 0652 */ 0653 bool isUtc() const; 0654 0655 /** 0656 * Returns whether the date/time is a local time at a fixed offset from 0657 * UTC. 0658 * 0659 * @return @c true if local time at fixed offset from UTC 0660 * @see isLocal(), isUtc(), utcOffset() 0661 */ 0662 bool isOffsetFromUtc() const; 0663 0664 /** 0665 * Returns the UTC offset associated with the date/time. The UTC offset is 0666 * the number of seconds to add to UTC to get the local time. 0667 * 0668 * @return UTC offset in seconds 0669 */ 0670 int utcOffset() const; 0671 0672 /** 0673 * Returns whether the date/time is the second occurrence of this time. This 0674 * is only applicable to a date/time expressed in terms of a time zone (type 0675 * @c TimeZone or @c LocalZone), around the time of change from daylight 0676 * savings to standard time. 0677 * 0678 * When a shift from daylight savings time to standard time occurs, the local 0679 * times (typically the previous hour) immediately preceding the shift occur 0680 * twice. For example, if a time shift of 1 hour happens at 03:00, the clock 0681 * jumps backwards to 02:00, so the local times between 02:00:00 and 02:59:59 0682 * occur once before the shift, and again after the shift. 0683 * 0684 * For instances which are not of type @c TimeZone, or when the date/time is 0685 * not near to a time shift, @c false is returned. 0686 * 0687 * @return @c true if the time is the second occurrence, @c false otherwise 0688 * @see setSecondOccurrence() 0689 */ 0690 bool isSecondOccurrence() const; 0691 0692 /** 0693 * Returns whether the date/time falls within daylight savings time in its 0694 * time zone. 0695 * 0696 * @return @c true if daylight savings time applies; 0697 * @c false if standard time applies, or if timeSpec() is not 0698 * TimeZone or LocalZone. 0699 */ 0700 bool isDaylightTime() const; 0701 0702 /** 0703 * Returns the time converted to UTC. The converted time has a UTC offset 0704 * of zero. 0705 * If the instance is a date-only value, a date-only UTC value is returned, 0706 * with the date unchanged. 0707 * 0708 * @return converted time 0709 * @see toOffsetFromUtc(), toLocalZone(), toZone(), toTimeSpec(), toSecsSinceEpoch(), QTimeZone::convert() 0710 */ 0711 KADateTime toUtc() const; 0712 0713 /** 0714 * Returns the time expressed as an offset from UTC, using the UTC offset 0715 * associated with this instance's date/time. The date and time 0716 * components are unchanged. For example, 14:15 on 12 Jan 2001, US Eastern 0717 * time zone would return a KADateTime value of 14:15 on 12 Jan 2001 with a 0718 * UTC offset of -18000 seconds (i.e. -5 hours). 0719 * 0720 * If the instance is a date-only value, the offset is set to that at the 0721 * start of the day. 0722 * 0723 * @return converted time 0724 * @see toUtc(), toOffsetFromUtc(int), toLocalZone(), toZone(), toTimeSpec(), toSecsSinceEpoch(), QTimeZone::convert() 0725 */ 0726 KADateTime toOffsetFromUtc() const; 0727 0728 /** 0729 * Returns the time expressed as a specified offset from UTC. 0730 * 0731 * If the instance is a date-only value, a date-only UTC offset time value 0732 * is returned, with the date unchanged. 0733 * 0734 * @param utcOffset number of seconds to add to UTC to get the local time. 0735 * @return converted time 0736 * @see toUtc(), toOffsetFromUtc(), toLocalZone(), toZone(), toTimeSpec(), toSecsSinceEpoch(), QTimeZone::convert() 0737 */ 0738 KADateTime toOffsetFromUtc(int utcOffset) const; 0739 0740 /** 0741 * Returns the time converted to the current local system time zone. 0742 * If the instance is a date-only value, a date-only local time zone value 0743 * is returned, with the date unchanged. 0744 * 0745 * @return converted time 0746 * @see toUtc(), toOffsetFromUtc(), toZone(), toTimeSpec(), QTimeZone::convert() 0747 */ 0748 KADateTime toLocalZone() const; 0749 0750 /** 0751 * Returns the time converted to a specified time zone. 0752 * If the instance is a date-only value, a date-only value in @p zone is 0753 * returned, with the date unchanged. 0754 * 0755 * @param zone time zone to convert to 0756 * @return converted time 0757 * @see toUtc(), toOffsetFromUtc(), toLocalZone(), toTimeSpec(), QTimeZone::convert() 0758 */ 0759 KADateTime toZone(const QTimeZone& zone) const; 0760 0761 /** 0762 * Returns the time converted to a new time specification. 0763 * If the instance is a date-only value, a date-only value is returned, 0764 * with the date unchanged. 0765 * 0766 * @param spec new time specification 0767 * @return converted time 0768 * @see toLocalZone(), toUtc(), toOffsetFromUtc(), toZone(), QTimeZone::convert() 0769 */ 0770 KADateTime toTimeSpec(const Spec& spec) const; 0771 0772 /** 0773 * Returns the time converted to the time specification of another instance. 0774 * If this instance is a date-only value, a date-only value is returned, 0775 * with the date unchanged. 0776 * 0777 * @param dt instance providing the new time specification 0778 * @return converted time 0779 * @see toLocalZone(), toUtc(), toOffsetFromUtc(), toZone(), QTimeZone::convert() 0780 */ 0781 KADateTime toTimeSpec(const KADateTime& dt) const; 0782 0783 /** 0784 * Converts the time to a UTC time, measured in seconds since 00:00:00 UTC 0785 * 1st January 1970 (as returned by time(2)). 0786 * 0787 * @return converted time, or @c LLONG_MIN if the date is out of range or invalid 0788 * @see setSecsSinceEpoch() 0789 */ 0790 qint64 toSecsSinceEpoch() const; 0791 0792 /** 0793 * Sets the time to a UTC time, specified as seconds since 00:00:00 UTC 0794 * 1st January 1970 (as returned by time(2)). 0795 * 0796 * @param seconds number of seconds since 00:00:00 UTC 1st January 1970 0797 * @see toSecsSinceEpoch() 0798 */ 0799 void setSecsSinceEpoch(qint64 seconds); 0800 0801 /** 0802 * Sets the instance either to being a date and time value, or a date-only 0803 * value. If its status is changed to date-only, its time is set to 0804 * 00:00:00. 0805 * 0806 * If the resultant date/time occurs twice due to a daylight savings time 0807 * shift, it is set to the first occurrence (before the time shift). 0808 * 0809 * @param dateOnly @c true to set to date-only, @c false to set to date 0810 * and time. 0811 * @see isDateOnly(), setTime() 0812 */ 0813 void setDateOnly(bool dateOnly); 0814 0815 /** 0816 * Sets the date part of the date/time. 0817 * 0818 * If the resultant date/time occurs twice due to a daylight savings time 0819 * shift, it is set to the first occurrence (before the time shift). 0820 * 0821 * @param date new date value 0822 * @see date(), setTime(), setTimeSpec(), setSecsSinceEpoch(), setDateOnly() 0823 */ 0824 void setDate(const QDate& date); 0825 0826 /** 0827 * Sets the time part of the date/time. If the instance was date-only, it 0828 * is changed to being a date and time value. 0829 * 0830 * If the resultant date/time occurs twice due to a daylight savings time 0831 * shift, it is set to the first occurrence (before the time shift). 0832 * 0833 * @param time new time value 0834 * @see time(), setDate(), setTimeSpec(), setSecsSinceEpoch() 0835 */ 0836 void setTime(const QTime& time); 0837 0838 /** 0839 * Changes the time specification of the instance. 0840 * 0841 * Any previous time zone is forgotten. The stored date/time component of 0842 * the instance is left unchanged (except that its Qt::TimeSpec setting is 0843 * set to correspond with @p spec). Usually this method will change the 0844 * absolute time which this instance represents. 0845 * 0846 * @param spec new time specification 0847 * @see timeSpec(), timeZone() 0848 */ 0849 void setTimeSpec(const Spec& spec); 0850 0851 /** 0852 * Sets whether the date/time is the second occurrence of this time. This 0853 * is only applicable to a date/time expressed in terms of a time zone (type 0854 * @c TimeZone or @c LocalZone), around the time of change from daylight 0855 * savings to standard time. 0856 * 0857 * When a shift from daylight savings time to standard time occurs, the local 0858 * times (typically the previous hour) immediately preceding the shift occur 0859 * twice. For example, if a time shift of 1 hour happens at 03:00, the clock 0860 * jumps backwards to 02:00, so the local times between 02:00:00 and 02:59:59 0861 * occur once before the shift, and again after the shift. 0862 * 0863 * For instances which are not of type @c TimeZone, or when the date/time is 0864 * not near to a time shift, calling this method has no effect. 0865 * 0866 * Note that most other setting methods clear the second occurrence indicator, 0867 * so if you want to retain its setting, you must call setSecondOccurrence() 0868 * again after changing the instance's value. 0869 * 0870 * @param second @c true to set as the second occurrence, @c false to set as 0871 * the first occurrence 0872 * @see isSecondOccurrence() 0873 */ 0874 void setSecondOccurrence(bool second); 0875 0876 /** 0877 * Returns a date/time @p msecs milliseconds later than the stored date/time. 0878 * 0879 * The calculation is done in UTC to ensure that the result takes proper 0880 * account of clock changes (e.g. daylight savings) in the time zone. The 0881 * result is expressed using the same time specification as the original 0882 * instance. 0883 * 0884 * If the instance is date-only, @p msecs is rounded down to a whole number 0885 * of days and that value is added to the date to find the result. 0886 * 0887 * @return resultant date/time 0888 * @see addSecs(), addDays(), addMonths(), addYears(), secsTo() 0889 */ 0890 KADateTime addMSecs(qint64 msecs) const; 0891 0892 /** 0893 * Returns a date/time @p secs seconds later than the stored date/time. 0894 * 0895 * The calculation is done in UTC to ensure that the result takes proper 0896 * account of clock changes (e.g. daylight savings) in the time zone. The 0897 * result is expressed using the same time specification as the original 0898 * instance. 0899 * 0900 * If the instance is date-only, @p secs is rounded down to a whole number 0901 * of days and that value is added to the date to find the result. 0902 * 0903 * @return resultant date/time 0904 * @see addMSecs(), addDays(), addMonths(), addYears(), secsTo() 0905 */ 0906 KADateTime addSecs(qint64 secs) const; 0907 0908 /** 0909 * Returns a date/time @p days days later than the stored date/time. 0910 * The result is expressed using the same time specification as the 0911 * original instance. 0912 * 0913 * @return resultant date/time 0914 * @see addSecs(), addMonths(), addYears(), daysTo() 0915 */ 0916 KADateTime addDays(qint64 days) const; 0917 0918 /** 0919 * Returns a date/time @p months months later than the stored date/time. 0920 * The result is expressed using the same time specification as the 0921 * original instance. 0922 * 0923 * @return resultant date/time 0924 * @see addSecs(), addDays(), addYears(), daysTo() 0925 */ 0926 KADateTime addMonths(int months) const; 0927 0928 /** 0929 * Returns a date/time @p years years later than the stored date/time. 0930 * The result is expressed using the same time specification as the 0931 * original instance. 0932 * 0933 * @return resultant date/time 0934 * @see addSecs(), addDays(), addMonths(), daysTo() 0935 */ 0936 KADateTime addYears(int years) const; 0937 0938 /** 0939 * Returns the number of milliseconds from this date/time to the @p other date/time. 0940 * 0941 * Before performing the comparison, the two date/times are converted to UTC 0942 * to ensure that the result is correct if one of the two date/times has 0943 * daylight saving time (DST) and the other doesn't. 0944 * 0945 * If one instance is date-only and the other is date-time, the date-time 0946 * value is first converted to the same time specification as the date-only 0947 * value, and the result is the difference in days between the resultant 0948 * date and the date-only date. 0949 * 0950 * If both instances are date-only, the result is the difference in days 0951 * between the two dates, ignoring time zones. 0952 * 0953 * @param other other date/time 0954 * @return number of milliseconds difference 0955 * @see addSecs(), daysTo() 0956 */ 0957 qint64 msecsTo(const KADateTime& other) const; 0958 0959 /** 0960 * Returns the number of seconds from this date/time to the @p other date/time. 0961 * 0962 * Before performing the comparison, the two date/times are converted to UTC 0963 * to ensure that the result is correct if one of the two date/times has 0964 * daylight saving time (DST) and the other doesn't. 0965 * 0966 * If one instance is date-only and the other is date-time, the date-time 0967 * value is first converted to the same time specification as the date-only 0968 * value, and the result is the difference in days between the resultant 0969 * date and the date-only date. 0970 * 0971 * If both instances are date-only, the result is the difference in days 0972 * between the two dates, ignoring time zones. 0973 * 0974 * @param other other date/time 0975 * @return number of seconds difference 0976 * @see addSecs(), daysTo() 0977 */ 0978 qint64 secsTo(const KADateTime& other) const; 0979 0980 /** 0981 * Calculates the number of days from this date/time to the @p other date/time. 0982 * In calculating the result, @p other is first converted to this instance's 0983 * time zone. The number of days difference is then calculated ignoring 0984 * the time parts of the two date/times. For example, if this date/time 0985 * was 13:00 on 1 January 2000, and @p other was 02:00 on 2 January 2000, 0986 * the result would be 1. 0987 * 0988 * If one instance is date-only and the other is date-time, the date-time 0989 * value is first converted to the same time specification as the date-only 0990 * value, and the result is the difference in days between the resultant 0991 * date and the date-only date. 0992 * 0993 * If both instances are date-only, the calculation ignores time zones. 0994 * 0995 * @param other other date/time 0996 * @return number of days difference 0997 * @see secsTo(), addDays() 0998 */ 0999 qint64 daysTo(const KADateTime& other) const; 1000 1001 /** 1002 * Returns the current date and time, as reported by the system clock, 1003 * expressed in the local system time zone (type LocalZone). 1004 * 1005 * @return current date/time 1006 * @see currentUtcDateTime(), currentDateTime() 1007 */ 1008 static KADateTime currentLocalDateTime(); 1009 1010 /** 1011 * Returns the current date and time, as reported by the system clock, 1012 * expressed in UTC. 1013 * 1014 * @return current date/time 1015 * @see currentLocalDateTime(), currentDateTime(), currentLocalDate(), currentLocalTime() 1016 */ 1017 static KADateTime currentUtcDateTime(); 1018 1019 /** 1020 * Returns the current date and time, as reported by the system clock, 1021 * expressed in a given time specification. 1022 * 1023 * @note To fetch the current date and time expressed in UTC or in the local 1024 * system time zone, it is more efficient to use currentUtcDateTime() or 1025 * currentLocalDateTime(). 1026 * 1027 * @param spec time specification 1028 * @return current date/time 1029 * @see currentUtcDateTime(), currentLocalDateTime() 1030 */ 1031 static KADateTime currentDateTime(const Spec& spec); 1032 1033 /** 1034 * Returns the current date in the local time zone, as reported by the 1035 * system clock. 1036 * 1037 * @return current date 1038 * @see currentLocalDateTime(), currentLocalTime() 1039 */ 1040 static QDate currentLocalDate(); 1041 1042 /** 1043 * Returns the current time of day in the local time zone, as reported 1044 * by the system clock. 1045 * 1046 * @return current date 1047 * @see currentLocalDateTime(), currentLocalDate() 1048 */ 1049 static QTime currentLocalTime(); 1050 1051 /** 1052 * Returns the date/time as a string. The @p format parameter determines the 1053 * format of the result string. The @p format codes used for the date and time 1054 * components follow those used elsewhere in KDE, and are similar but not 1055 * identical to those used by strftime(3). Conversion specifiers are 1056 * introduced by a '\%' character, and are replaced in @p format as follows: 1057 * 1058 * \b Date 1059 * 1060 * - \%y 2-digit year excluding century (00 - 99). Conversion is undefined 1061 * if year < 0. 1062 * - \%Y full year number 1063 * - %:m month number, without leading zero (1 - 12) 1064 * - \%m month number, 2 digits (01 - 12) 1065 * - \%b abbreviated month name in current locale 1066 * - \%B full month name in current locale 1067 * - %:b abbreviated month name in English (Jan, Feb, ...) 1068 * - %:B full month name in English 1069 * - \%e day of the month (1 - 31) 1070 * - \%d day of the month, 2 digits (01 - 31) 1071 * - \%a abbreviated weekday name in current locale 1072 * - \%A full weekday name in current locale 1073 * - %:a abbreviated weekday name in English (Mon, Tue, ...) 1074 * - %:A full weekday name in English 1075 * 1076 * \b Time 1077 * 1078 * - \%H hour in the 24 hour clock, 2 digits (00 - 23) 1079 * - \%k hour in the 24 hour clock, without leading zero (0 - 23) 1080 * - \%I hour in the 12 hour clock, 2 digits (01 - 12) 1081 * - \%l hour in the 12 hour clock, without leading zero (1 - 12) 1082 * - \%M minute, 2 digits (00 - 59) 1083 * - \%S seconds (00 - 59) 1084 * - %:S seconds preceded with ':', but omitted if seconds value is zero 1085 * - %:s milliseconds, 3 digits (000 - 999) 1086 * - \%P "am" or "pm" in the current locale, or if undefined there, in English 1087 * - \%p "AM" or "PM" in the current locale, or if undefined there, in English 1088 * - %:P "am" or "pm" 1089 * - %:p "AM" or "PM" 1090 * 1091 * \b Time zone 1092 * 1093 * - %:u UTC offset of the time zone in hours, e.g. -02. If the offset 1094 * is not a whole number of hours, the output is the same as for '\%U'. 1095 * - \%z UTC offset of the time zone in hours and minutes, e.g. -0200. 1096 * - %:z UTC offset of the time zone in hours and minutes, e.g. +02:00. 1097 * - \%Z time zone abbreviation, e.g. UTC, EDT, GMT. This is not guaranteed 1098 * to be unique among different time zones. If not applicable (i.e. if 1099 * the instance is type OffsetFromUTC), the UTC offset is substituted. 1100 * - %:Z time zone name, e.g. Europe/London. This is system dependent. If 1101 * not applicable (i.e. if the instance is type OffsetFromUTC), the 1102 * UTC offset is substituted. 1103 * 1104 * \b Other 1105 * 1106 * - %% literal '\%' character 1107 * 1108 * If you want to use the current locale's date format, you should call 1109 * QLocale::toString() to format the date part of the KADateTime. 1110 * 1111 * @param format format for the string 1112 * @return formatted string 1113 * @see fromString(), QLocale::toString() 1114 */ 1115 QString toString(const QString& format) const; 1116 1117 /** 1118 * Returns the date/time as a string, formatted according to the @p format 1119 * parameter, with the UTC offset appended. 1120 * 1121 * If the instance is date-only, the time will when @p format permits be 1122 * omitted from the output string. This applies to @p format = QtTextDate 1123 * or LocalDate. For all other cases, a time of 00:00:00 will be output. 1124 * 1125 * For RFC 2822 format, set @p format to RFCDateDay to include the day 1126 * of the week, or to RFCDate to omit it. 1127 * 1128 * @param format format for output string 1129 * @return formatted string 1130 * @see fromString(), QDateTime::toString() 1131 */ 1132 QString toString(TimeFormat format = ISODate) const; 1133 1134 /** 1135 * Returns the KADateTime represented by @p string, using the @p format given. 1136 * 1137 * This method is the inverse of toString(TimeFormat), except that it can 1138 * only return a time specification of UTC or OffsetFromUTC. An actual 1139 * named time zone cannot be returned since an offset from UTC only 1140 * partially specifies a time zone. 1141 * 1142 * The time specification of the result is determined by the UTC offset 1143 * present in the string: 1144 * - if the UTC offset is zero the result is type @c UTC. 1145 * - if the UTC offset is non-zero, the result is type @c OffsetFromUTC. 1146 * - if there is no UTC offset (when @p format permits this), the result is 1147 * by default type @c LocalZone. You can use setFromStringDefault() to 1148 * change this default. 1149 * 1150 * If no time is found in @p string, a date-only value is returned, except 1151 * when the specified @p format does not permit the time to be omitted, in 1152 * which case an error is returned. An error is therefore returned for 1153 * ISODate when @p string includes a time zone specification, and for 1154 * RFCDate in all cases. 1155 * 1156 * For RFC format strings (not RFC 3339), you should normally set @p format 1157 * to RFCDate. Only set it to RFCDateDay if you want to return an error 1158 * when the day of the week is omitted. 1159 * 1160 * @param string string to convert 1161 * @param format format code. LocalDate cannot be used here. 1162 * @param negZero if non-null, the value is set to true if a UTC offset of 1163 * '-0000' is found or, for RFC 2822 format, an unrecognised 1164 * or invalid time zone abbreviation is found, else false. 1165 * @return KADateTime value, or an invalid KADateTime if either parameter is invalid 1166 * @see setFromStringDefault(), toString(), QString::fromString() 1167 */ 1168 static KADateTime fromString(const QString& string, TimeFormat format = ISODate, bool* negZero = nullptr); 1169 1170 /** 1171 * Returns the KADateTime represented by @p string, using the @p format 1172 * given, optionally using a time zone collection @p zones as the source of 1173 * time zone definitions. The @p format codes are basically the same as 1174 * those for toString(), and are similar but not identical to those used by 1175 * strftime(3). 1176 * 1177 * The @p format string consists of the same codes as that for 1178 * toString(). However, some codes which are distinct in toString() have 1179 * the same function as each other here. 1180 * 1181 * Numeric values without a stated number of digits permit, but do not 1182 * require, leading zeroes. The maximum number of digits consumed by a 1183 * numeric code is the minimum needed to cover the possible range of the 1184 * number (e.g. for minutes, the range is 0 - 59, so the maximum number of 1185 * digits consumed is 2). All non-numeric values are case insensitive. 1186 * 1187 * \b Date 1188 * 1189 * - \%y year excluding century (0 - 99). Years 0 - 50 return 2000 - 2050, 1190 * while years 51 - 99 return 1951 - 1999. 1191 * - \%Y full year number (4 digits with optional sign) 1192 * - %:Y full year number (>= 4 digits with optional sign) 1193 * - %:m month number (1 - 12) 1194 * - \%m month number, 2 digits (01 - 12) 1195 * - \%b 1196 * - \%B month name in the current locale or, if no match, in English, 1197 * abbreviated or in full 1198 * - %:b 1199 * - %:B month name in English, abbreviated or in full 1200 * - \%e day of the month (1 - 31) 1201 * - \%d day of the month, 2 digits (01 - 31) 1202 * - \%a 1203 * - \%A weekday name in the current locale or, if no match, in English, 1204 * abbreviated or in full 1205 * - %:a 1206 * - %:A weekday name in English, abbreviated or in full 1207 * 1208 * \b Time 1209 * 1210 * - \%H hour in the 24 hour clock, 2 digits (00 - 23) 1211 * - \%k hour in the 24 hour clock (0 - 23) 1212 * - \%I hour in the 12 hour clock, 2 digits (01 - 12) 1213 * - \%l hour in the 12 hour clock (1 - 12) 1214 * - \%M minute, 2 digits (00 - 59) 1215 * - %:M minute (0 - 59) 1216 * - \%S seconds, 2 digits (00 - 59) 1217 * - \%s seconds (0 - 59) 1218 * - %:S optional seconds value (0 - 59) preceded with ':'. If no colon is 1219 * found in @p string, no input is consumed and the seconds value is 1220 * set to zero. 1221 * - %:s fractional seconds value, preceded with a decimal point (either '.' 1222 * or the locale's decimal point symbol) 1223 * - \%P 1224 * - \%p "am" or "pm", in the current locale or, if no match, in 1225 * English. This format is only useful when used with \%I or \%l. 1226 * - %:P 1227 * - %:p "am" or "pm" in English. This format is only useful when used with 1228 * \%I or \%l. 1229 * 1230 * \b Time zone 1231 * 1232 * - %:u 1233 * - \%z UTC offset of the time zone in hours and optionally minutes, 1234 * e.g. -02, -0200. 1235 * - %:z UTC offset of the time zone in hours and minutes, colon separated, 1236 * e.g. +02:00. 1237 * - \%Z time zone abbreviation, consisting of alphanumeric characters, 1238 * e.g. UTC, EDT, GMT. Use of time zone abbreviations is strongly 1239 * advised against, for reasons given below. 1240 * - %:Z time zone name, e.g. Europe/London. The name may contain any 1241 * characters and is delimited by the following character in the 1242 * @p format string. It will not work if you follow %:Z with another 1243 * escape sequence (except %% or \%t). 1244 * 1245 * \b Other 1246 * 1247 * - \%t matches one or more whitespace characters 1248 * - %% literal '\%' character 1249 * 1250 * Any other character must have a matching character in @p string, except 1251 * that a space will match zero or more whitespace characters in the input 1252 * string. 1253 * 1254 * If any time zone information is present in the string, the function 1255 * attempts to find a matching time zone in the @p zones collection. A time 1256 * zone name (format code %:Z) will provide an unambiguous look up in 1257 * @p zones. Any other type of time zone information (an abbreviated time 1258 * zone code (\%Z) or UTC offset (\%z, %:z, %:u) is searched for in @p zones 1259 * and if only one time zone is found to match, the result is set to that 1260 * zone. Otherwise: 1261 * - If more than one match of a UTC offset is found, the action taken is 1262 * determined by @p offsetIfAmbiguous: if @p offsetIfAmbiguous is true, 1263 * a local time with an offset from UTC (type @c OffsetFromUTC) will be 1264 * returned; if false an invalid KADateTime is returned. 1265 * - If more than one match of a time zone abbreviation is found, the UTC 1266 * offset for each matching time zone is compared and, if the offsets are 1267 * the same, a local time with an offset from UTC (type @c OffsetFromUTC) 1268 * will be returned provided that @p offsetIfAmbiguous is true. Otherwise 1269 * an invalid KADateTime is returned. 1270 * - If a time zone abbreviation does not match any time zone in @p zones, 1271 * or the abbreviation does not apply at the parsed date/time, an 1272 * invalid KADateTime is returned. 1273 * Warning: To avoid unacceptable processing loads, searches for a time 1274 * zone abbreviation are done using QTimeZone::displayName() 1275 * instead of QTimeZone::abbreviation(). However, the two 1276 * methods can return different results, and if this happens, no 1277 * match will be found. For this reason as well as the fact that 1278 * time zone abbreviations are not unique, the use of time zone 1279 * abbreviations is strongly advised against. 1280 * - If a time zone name does not match any time zone in @p zones, an 1281 * invalid KADateTime is returned. 1282 * - If the time zone UTC offset does not match any time zone in @p zones, 1283 * a local time with an offset from UTC (type @c OffsetFromUTC) is 1284 * returned. 1285 * If @p format contains more than one time zone or UTC offset code, an 1286 * error is returned. 1287 * 1288 * If no time zone information is present in the string, by default a local 1289 * time (type @c LocalZone) is returned. You can use setFromStringDefault() 1290 * to change this default. 1291 * 1292 * If no time is found in @p string, a date-only value is returned. 1293 * 1294 * If any inconsistencies are found, i.e. the same item of information 1295 * appears more than once but with different values, the weekday name does 1296 * not tally with the date, an invalid KADateTime is returned. 1297 * 1298 * @param string string to convert 1299 * @param format format string 1300 * @param zones time zone collection, or null for none 1301 * @param offsetIfAmbiguous specifies what to do if more than one zone 1302 * matches the UTC offset found in the 1303 * string. Ignored if @p zones is null. 1304 * @return KADateTime value, or an invalid KADateTime if an error occurs, if 1305 * time zone information doesn't match any in @p zones, or if the 1306 * time zone information is ambiguous and @p offsetIfAmbiguous is 1307 * false 1308 * @see setFromStringDefault(), toString() 1309 */ 1310 static KADateTime fromString(const QString& string, const QString& format, 1311 const QList<QTimeZone>* zones = nullptr, bool offsetIfAmbiguous = true); 1312 1313 /** 1314 * Sets the default time specification for use by fromString() when no time 1315 * zone or UTC offset is found in the string being parsed, or when "-0000" 1316 * is found in an RFC 2822 string. 1317 * 1318 * By default, fromString() returns a local time (type @c LocalZone) when 1319 * no definite zone or UTC offset is found. You can use this method to make 1320 * it return UTC, or whatever you wish. 1321 * 1322 * @param spec the new default time specification 1323 * @see fromString() 1324 */ 1325 static void setFromStringDefault(const Spec& spec); 1326 1327 /** 1328 * Compare this instance with another to determine whether they are 1329 * simultaneous, earlier or later, and in the case of date-only values, 1330 * whether they overlap (i.e. partly coincide but are not wholly 1331 * simultaneous). 1332 * The comparison takes time zones into account: if the two instances have 1333 * different time zones, they are first converted to UTC before comparing. 1334 * 1335 * If both instances are date/time values, this instance is considered to 1336 * be either simultaneous, earlier or later, and does not overlap. 1337 * 1338 * If one instance is date-only and the other is a date/time, this instance 1339 * is either strictly earlier, strictly later, or overlaps. 1340 * 1341 * If both instance are date-only, they are considered simultaneous if both 1342 * their start of day and end of day times are simultaneous with each 1343 * other. (Both start and end of day times need to be considered in case a 1344 * daylight savings change occurs during that day.) Otherwise, this instance 1345 * can be strictly earlier, earlier but overlapping, later but overlapping, 1346 * or strictly later. 1347 * 1348 * @return @c true if the two instances represent the same time, @c false otherwise 1349 * @see operator==(), operator!=(), operator<(), operator<=(), operator>=(), operator>() 1350 */ 1351 Comparison compare(const KADateTime& other) const; 1352 1353 /** 1354 * Check whether this date/time is simultaneous with another. 1355 * The comparison takes time zones into account: if the two instances have 1356 * different time zones, they are first converted to UTC before comparing. 1357 * 1358 * If one instance is date-only and the other is date/time, they are 1359 * considered unequal. 1360 * 1361 * If both instances are date-only, they are considered simultaneous if both 1362 * their start of day and end of day times are simultaneous with each 1363 * other. (Both start and end of day times need to be considered in case a 1364 * daylight saving change occurs during that day.) 1365 * 1366 * @return @c true if the two instances represent the same time, @c false otherwise 1367 * @see compare() 1368 */ 1369 bool operator==(const KADateTime& other) const; 1370 1371 bool operator!=(const KADateTime& other) const 1372 { 1373 return !(*this == other); 1374 } 1375 1376 /** 1377 * Check whether this date/time is earlier than another. 1378 * The comparison takes time zones into account: if the two instances have 1379 * different time zones, they are first converted to UTC before comparing. 1380 * 1381 * If one or both instances are date-only, the comparison returns true if 1382 * this date/time or day, falls wholly before the other date/time or 1383 * day. To achieve this, the time used in the comparison is the end of day 1384 * (if this instance is date-only) or the start of day (if the other 1385 * instance is date-only). 1386 * 1387 * @return @c true if this instance represents an earlier time than @p other, 1388 * @c false otherwise 1389 * @see compare() 1390 */ 1391 bool operator<(const KADateTime& other) const; 1392 1393 bool operator<=(const KADateTime& other) const 1394 { 1395 return !(other < *this); 1396 } 1397 bool operator>(const KADateTime& other) const 1398 { 1399 return other < *this; 1400 } 1401 bool operator>=(const KADateTime& other) const 1402 { 1403 return !(*this < other); 1404 } 1405 1406 /** 1407 * Create a separate copy of this instance's data if it is implicitly shared 1408 * with another instance. 1409 * 1410 * You would normally only call this if you want different copies of the 1411 * same date/time value to cache conversions to different time zones. Because 1412 * only the last conversion to another time zone is cached, and the cached 1413 * value is implicitly shared, judicious use of detach() could improve 1414 * efficiency when handling several time zones. But take care: if used 1415 * inappropriately, it will reduce efficiency! 1416 */ 1417 void detach(); 1418 1419 /** 1420 * Set an adjustment to be applied when fetching the current system time. 1421 * This is applied by all KADateTime methods which return the system date 1422 * and/or time. 1423 * 1424 * The supplied date/time is used as the current simulated time and the 1425 * time adjustment is set to the difference between the real current time 1426 * and @p newTime. If @p newTime has a time zone, that time zone is set 1427 * to be the simulated local system time zone. 1428 * 1429 * To cancel time simulation, supply an invalid @p newTime parameter. 1430 * 1431 * @warning This function is provided only for testing purposes, and should 1432 * not be used in released code. If the library is compiled without 1433 * debug enabled, setSimulatedSystemTime() has no effect. 1434 * To avoid confusion, it is recommended that calls to it should be 1435 * conditionally compiled, e.g.: 1436 * \code 1437 * #ifndef NDEBUG 1438 * KADateTime::simulateSystemTime(kdt); 1439 * #endif 1440 * \endcode 1441 * 1442 * @param newTime the current simulated time, or invalid to cancel simulation 1443 * 1444 * @see currentDateTime(), currentLocalDateTime(), currentUtcDateTime(), 1445 * currentLocalDate(), currentLocalTime() 1446 */ 1447 static void setSimulatedSystemTime(const KADateTime& newTime); 1448 1449 /** 1450 * Return the real (not simulated) system time. 1451 * 1452 * @warning This method is provided only for testing purposes, and should 1453 * not be used in released code. If the library is compiled without 1454 * debug enabled, currentLocalDateTime() and realCurrentLocalDateTime() 1455 * both return the real system time. 1456 * To avoid confusion, it is recommended that calls to 1457 * realCurrentLocalDateTime() should be conditionally compiled, e.g.: 1458 * \code 1459 * #ifndef NDEBUG 1460 * dt = KADateTime::realCurrentLocalDateTime(); 1461 * #endif 1462 * \endcode 1463 * 1464 */ 1465 static KADateTime realCurrentLocalDateTime(); 1466 1467 private: 1468 QSharedDataPointer<KADateTimePrivate> d; 1469 }; 1470 1471 /** Write @p spec to the datastream @p out, in binary format. */ 1472 KALARMCAL_EXPORT QDataStream& operator<<(QDataStream& out, const KADateTime::Spec& spec); 1473 /** Read a KADateTime::Spec object into @p spec from @p in, in binary format. */ 1474 KALARMCAL_EXPORT QDataStream& operator>>(QDataStream& in, KADateTime::Spec& spec); 1475 1476 /** Write @p dateTime to the datastream @p out, in binary format. */ 1477 KALARMCAL_EXPORT QDataStream& operator<<(QDataStream& out, const KADateTime& dateTime); 1478 /** Read a KADateTime object into @p dateTime from @p in, in binary format. */ 1479 KALARMCAL_EXPORT QDataStream& operator>>(QDataStream& in, KADateTime& dateTime); 1480 1481 } // namespace KAlarmCal 1482 1483 Q_DECLARE_METATYPE(KAlarmCal::KADateTime) 1484 Q_DECLARE_METATYPE(KAlarmCal::KADateTime::Spec) 1485 1486 // vim: et sw=4: