KDECore
kdatetime.h
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 2005-2010 David Jarvie <djarvie@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00026 #ifndef _KDATETIME_H_ 00027 #define _KDATETIME_H_ 00028 00029 #include <kdecore_export.h> 00030 #include <ktimezone.h> 00031 00032 #include <QtCore/QMetaType> 00033 #include <QtCore/QSharedDataPointer> 00034 00035 class QDataStream; 00036 class KDateTimePrivate; 00037 class KDateTimeSpecPrivate; 00038 00171 class KDECORE_EXPORT KDateTime //krazy:exclude=dpointer (implicitly shared) 00172 { 00173 public: 00181 enum SpecType 00182 { 00183 Invalid, 00184 UTC, 00185 OffsetFromUTC, 00186 TimeZone, 00191 LocalZone, 00203 ClockTime 00215 }; 00216 00223 class KDECORE_EXPORT Spec 00224 { 00225 public: 00229 Spec(); 00230 00237 Spec(const KTimeZone &tz); // allow implicit conversion 00238 00246 Spec(SpecType type, int utcOffset = 0); // allow implicit conversion 00247 00251 Spec(const Spec& spec); 00252 00256 Spec& operator=(const Spec& spec); 00257 00261 ~Spec(); 00262 00268 bool isValid() const; 00269 00280 KTimeZone timeZone() const; 00281 00291 SpecType type() const; 00292 00300 bool isLocalZone() const; 00301 00308 bool isClockTime() const; 00309 00318 bool isUtc() const; 00319 00327 bool isOffsetFromUtc() const; 00328 00336 int utcOffset() const; 00337 00347 void setType(SpecType type, int utcOffset = 0); 00348 00358 void setType(const KTimeZone &tz); 00359 00366 bool operator==(const Spec &other) const; 00367 00368 bool operator!=(const Spec &other) const { return !operator==(other); } 00369 00383 bool equivalentTo(const Spec &other) const; 00384 00389 static Spec UTC(); 00390 00395 static Spec ClockTime(); 00396 00404 static Spec OffsetFromUTC(int utcOffset); 00405 00412 static Spec LocalZone(); 00413 00414 private: 00415 KDateTimeSpecPrivate* const d; 00416 }; 00417 00419 enum TimeFormat 00420 { 00421 ISODate, 00435 RFCDate, 00442 RFCDateDay, 00445 QtTextDate, 00449 LocalDate 00453 }; 00454 00473 enum Comparison 00474 { 00475 Before = 0x01, 00478 AtStart = 0x02, 00482 Inside = 0x04, 00486 AtEnd = 0x08, 00490 After = 0x10, 00493 Equal = AtStart | Inside | AtEnd, 00496 Outside = Before | AtStart | Inside | AtEnd | After, 00501 StartsAt = AtStart | Inside | AtEnd | After, 00506 EndsAt = Before | AtStart | Inside | AtEnd 00511 }; 00512 00513 00517 KDateTime(); 00518 00536 explicit KDateTime(const QDate &date, const Spec &spec = Spec(LocalZone)); 00537 00555 KDateTime(const QDate &date, const QTime &time, const Spec &spec = Spec(LocalZone)); 00556 00586 KDateTime(const QDateTime &dt, const Spec &spec); 00587 00595 explicit KDateTime(const QDateTime &dt); 00596 00597 KDateTime(const KDateTime &other); 00598 ~KDateTime(); 00599 00600 KDateTime &operator=(const KDateTime &other); 00601 00608 bool isNull() const; 00609 00616 bool isValid() const; 00617 00623 bool isDateOnly() const; 00624 00632 QDate date() const; 00633 00642 QTime time() const; 00643 00655 QDateTime dateTime() const; 00656 00665 KTimeZone timeZone() const; 00666 00674 Spec timeSpec() const; 00675 00685 SpecType timeType() const; 00686 00694 bool isLocalZone() const; 00695 00702 bool isClockTime() const; 00703 00713 bool isUtc() const; 00714 00722 bool isOffsetFromUtc() const; 00723 00731 int utcOffset() const; 00732 00751 bool isSecondOccurrence() const; 00752 00764 KDateTime toUtc() const; 00765 00781 KDateTime toOffsetFromUtc() const; 00782 00795 KDateTime toOffsetFromUtc(int utcOffset) const; 00796 00805 KDateTime toLocalZone() const; 00806 00817 KDateTime toClockTime() const; 00818 00830 KDateTime toZone(const KTimeZone &zone) const; 00831 00843 KDateTime toTimeSpec(const Spec &spec) const; 00844 00856 KDateTime toTimeSpec(const KDateTime &dt) const; 00857 00865 uint toTime_t() const; 00866 00874 void setTime_t(qint64 seconds); 00875 00885 void setDateOnly(bool dateOnly); 00886 00893 void setDate(const QDate &date); 00894 00902 void setTime(const QTime &time); 00903 00918 void setDateTime(const QDateTime &dt); 00919 00931 void setTimeSpec(const Spec &spec); 00932 00956 void setSecondOccurrence(bool second); 00957 00976 KDateTime addMSecs(qint64 msecs) const; 00977 00996 KDateTime addSecs(qint64 secs) const; 00997 01010 KDateTime addDays(int days) const; 01011 01024 KDateTime addMonths(int months) const; 01025 01038 KDateTime addYears(int years) const; 01039 01065 int secsTo(const KDateTime &other) const; 01066 01092 qint64 secsTo_long(const KDateTime &other) const; 01093 01117 int daysTo(const KDateTime &other) const; 01118 01126 static KDateTime currentLocalDateTime(); 01127 01135 static KDateTime currentUtcDateTime(); 01136 01145 static KDateTime currentDateTime(const Spec &spec); 01146 01155 static QDate currentLocalDate(); 01156 01165 static QTime currentLocalTime(); 01166 01234 QString toString(const QString &format) const; 01235 01257 QString toString(TimeFormat format = ISODate) const; 01258 01298 static KDateTime fromString(const QString &string, TimeFormat format = ISODate, bool *negZero = 0); 01299 01437 static KDateTime fromString(const QString &string, const QString &format, 01438 const KTimeZones *zones = 0, bool offsetIfAmbiguous = true); 01439 01452 static void setFromStringDefault(const Spec &spec); 01453 01454 01466 bool outOfRange() const; 01467 01496 Comparison compare(const KDateTime &other) const; 01497 01515 bool operator==(const KDateTime &other) const; 01516 01517 bool operator!=(const KDateTime &other) const { return !(*this == other); } 01518 01538 bool operator<(const KDateTime &other) const; 01539 01540 bool operator<=(const KDateTime &other) const { return !(other < *this); } 01541 bool operator>(const KDateTime &other) const { return other < *this; } 01542 bool operator>=(const KDateTime &other) const { return !(*this < other); } 01543 01555 void detach(); 01556 01587 static void setSimulatedSystemTime(const KDateTime& newTime); 01588 01606 static KDateTime realCurrentLocalDateTime(); 01607 01608 friend QDataStream KDECORE_EXPORT &operator<<(QDataStream &out, const KDateTime &dateTime); 01609 friend QDataStream KDECORE_EXPORT &operator>>(QDataStream &in, KDateTime &dateTime); 01610 01611 private: 01612 QSharedDataPointer<KDateTimePrivate> d; 01613 }; 01614 01615 Q_DECLARE_METATYPE(KDateTime) 01616 Q_DECLARE_METATYPE(KDateTime::Spec) 01617 01619 QDataStream KDECORE_EXPORT &operator<<(QDataStream &out, const KDateTime::Spec &spec); 01621 QDataStream KDECORE_EXPORT &operator>>(QDataStream &in, KDateTime::Spec &spec); 01622 01624 QDataStream KDECORE_EXPORT &operator<<(QDataStream &out, const KDateTime &dateTime); 01626 QDataStream KDECORE_EXPORT &operator>>(QDataStream &in, KDateTime &dateTime); 01627 01628 #endif
KDE 4.6 API Reference