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