KDECore
ktimezone.h
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 2005-2007 David Jarvie <djarvie@kde.org> 00004 Copyright (c) 2005 S.R.Haque <srhaque@iee.org>. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00028 #ifndef _KTIMEZONES_H 00029 #define _KTIMEZONES_H 00030 00031 #include <kdecore_export.h> 00032 00033 #include <sys/time.h> 00034 #include <ctime> 00035 00036 #include <QtCore/QDateTime> 00037 #include <QtCore/QMap> 00038 #include <QtCore/QList> 00039 #include <QtCore/QString> 00040 #include <QtCore/QByteArray> 00041 #include <QtCore/QSharedDataPointer> 00042 00043 class KTimeZone; 00044 class KTimeZoneBackend; 00045 class KTimeZoneData; 00046 class KTimeZoneSource; 00047 class KTimeZonesPrivate; 00048 class KTimeZonePrivate; 00049 class KTimeZoneSourcePrivate; 00050 class KTimeZoneDataPrivate; 00051 class KTimeZoneTransitionPrivate; 00052 class KTimeZoneLeapSecondsPrivate; 00053 00308 class KDECORE_EXPORT KTimeZones 00309 { 00310 public: 00311 KTimeZones(); 00312 ~KTimeZones(); 00313 00320 KTimeZone zone(const QString &name) const; 00321 00322 typedef QMap<QString, KTimeZone> ZoneMap; 00323 00329 const ZoneMap zones() const; 00330 00339 bool add(const KTimeZone &zone); 00340 00348 KTimeZone remove(const KTimeZone &zone); 00349 00357 KTimeZone remove(const QString &name); 00358 00364 void clear(); 00365 00366 private: 00367 KTimeZones(const KTimeZones &); // prohibit copying 00368 KTimeZones &operator=(const KTimeZones &); // prohibit copying 00369 00370 KTimeZonesPrivate * const d; 00371 }; 00372 00373 00415 class KDECORE_EXPORT KTimeZone //krazy:exclude=dpointer (has non-const d-pointer to Backend class) 00416 { 00417 public: 00418 00419 /* 00420 * Time zone phase. 00421 * 00422 * A phase can be daylight savings time or standard time. It holds the 00423 * UTC offset and time zone abbreviation (e.g. EST, GMT). 00424 * 00425 * @short Time zone phase 00426 * @author David Jarvie <djarvie@kde.org>. 00427 */ 00428 class KDECORE_EXPORT Phase 00429 { 00430 public: 00431 Phase(); 00432 00442 Phase(int utcOffset, const QByteArray &abbreviations, bool dst, 00443 const QString &comment = QString()); 00444 00453 Phase(int utcOffset, const QList<QByteArray> &abbreviations, bool dst, 00454 const QString &comment = QString()); 00455 00456 Phase(const Phase &rhs); 00457 ~Phase(); 00458 Phase &operator=(const Phase &rhs); 00459 bool operator==(const Phase &rhs) const; 00460 bool operator!=(const Phase &rhs) const { return !operator==(rhs); } 00461 00469 int utcOffset() const; 00470 00478 QList<QByteArray> abbreviations() const; 00479 00485 bool isDst() const; 00486 00492 QString comment() const; 00493 00494 private: 00495 QSharedDataPointer<class KTimeZonePhasePrivate> d; 00496 }; 00497 00498 00499 /* 00500 * Time zone daylight saving time transition. 00501 * 00502 * A Transition instance holds details of a transition to daylight saving time or 00503 * standard time, including the UTC time of the change. 00504 * 00505 * @short Time zone transition 00506 * @author David Jarvie <djarvie@kde.org>. 00507 */ 00508 class KDECORE_EXPORT Transition 00509 { 00510 public: 00511 Transition(); 00512 Transition(const QDateTime &dt, const Phase &phase); 00513 Transition(const KTimeZone::Transition &t); 00514 ~Transition(); 00515 Transition &operator=(const KTimeZone::Transition &t); 00516 00522 QDateTime time() const; 00523 00529 Phase phase() const; 00530 00537 bool operator<(const Transition &rhs) const; 00538 00539 private: 00540 KTimeZoneTransitionPrivate *const d; 00541 }; 00542 00543 00544 /* 00545 * Leap seconds adjustment for a time zone. 00546 * 00547 * This class defines a leap seconds adjustment for a time zone by its UTC time of 00548 * occurrence and the cumulative number of leap seconds to be added at that time. 00549 * 00550 * @short Leap seconds adjustment for a time zone 00551 * @see KTimeZone, KTimeZoneData 00552 * @ingroup timezones 00553 * @author David Jarvie <djarvie@kde.org>. 00554 */ 00555 class KDECORE_EXPORT LeapSeconds 00556 { 00557 public: 00558 LeapSeconds(); 00559 LeapSeconds(const QDateTime &utcTime, int leapSeconds, const QString &comment = QString()); 00560 LeapSeconds(const LeapSeconds &c); 00561 ~LeapSeconds(); 00562 LeapSeconds &operator=(const LeapSeconds &c); 00563 bool operator<(const LeapSeconds &c) const; // needed by qSort() 00564 00570 bool isValid() const; 00571 00577 QDateTime dateTime() const; 00578 00585 int leapSeconds() const; 00586 00592 QString comment() const; 00593 00594 private: 00595 KTimeZoneLeapSecondsPrivate *const d; 00596 }; 00597 00598 00604 KTimeZone(); 00605 00611 explicit KTimeZone(const QString &name); 00612 00613 KTimeZone(const KTimeZone &tz); 00614 KTimeZone &operator=(const KTimeZone &tz); 00615 00616 virtual ~KTimeZone(); 00617 00627 bool operator==(const KTimeZone &rhs) const; 00628 bool operator!=(const KTimeZone &rhs) const { return !operator==(rhs); } 00629 00637 QByteArray type() const; 00638 00644 bool isValid() const; 00645 00653 QString name() const; 00654 00660 QString countryCode() const; 00661 00667 float latitude() const; 00668 00674 float longitude() const; 00675 00681 QString comment() const; 00682 00691 QList<QByteArray> abbreviations() const; 00692 00701 QByteArray abbreviation(const QDateTime &utcDateTime) const; 00702 00717 QList<int> utcOffsets() const; 00718 00729 QDateTime convert(const KTimeZone &newZone, const QDateTime &zoneDateTime) const; 00730 00744 QDateTime toUtc(const QDateTime &zoneDateTime) const; 00745 00760 QDateTime toZoneTime(const QDateTime &utcDateTime, bool *secondOccurrence = 0) const; 00761 00775 int currentOffset(Qt::TimeSpec basis = Qt::UTC) const; 00776 00797 virtual int offsetAtZoneTime(const QDateTime &zoneDateTime, int *secondOffset = 0) const; 00798 00818 virtual int offsetAtUtc(const QDateTime &utcDateTime) const; 00819 00834 virtual int offset(time_t t) const; 00835 00852 virtual bool isDstAtUtc(const QDateTime &utcDateTime) const; 00853 00865 virtual bool isDst(time_t t) const; 00866 00877 QList<Phase> phases() const; 00878 00887 virtual bool hasTransitions() const; 00888 00906 QList<KTimeZone::Transition> transitions(const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime()) const; 00907 00928 const KTimeZone::Transition *transition(const QDateTime &dt, const Transition **secondTransition = 0, bool *validTime = 0) const; 00929 00952 int transitionIndex(const QDateTime &dt, int *secondIndex = 0, bool *validTime = 0) const; 00953 00973 QList<QDateTime> transitionTimes(const Phase &phase, const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime()) const; 00974 00984 QList<LeapSeconds> leapSecondChanges() const; 00985 00991 KTimeZoneSource *source() const; 00992 00998 bool parse() const; 00999 01008 const KTimeZoneData *data(bool create = false) const; 01009 01026 bool updateBase(const KTimeZone &other); 01027 01037 static QDateTime fromTime_t(time_t t); 01038 01051 static time_t toTime_t(const QDateTime &utcDateTime); 01052 01063 static KTimeZone utc(); 01064 01068 static const int InvalidOffset; 01069 01072 static const time_t InvalidTime_t; 01073 01078 static const float UNKNOWN; 01079 01080 protected: 01081 KTimeZone(KTimeZoneBackend *impl); 01082 01092 void setData(KTimeZoneData *data, KTimeZoneSource *source = 0); 01093 01094 private: 01095 KTimeZoneBackend *d; 01096 }; 01097 01098 01115 class KDECORE_EXPORT KTimeZoneBackend //krazy:exclude=dpointer (non-const d-pointer for KTimeZoneBackend-derived classes) 01116 { 01117 public: 01119 KTimeZoneBackend(); 01121 explicit KTimeZoneBackend(const QString &name); 01122 01123 KTimeZoneBackend(const KTimeZoneBackend &other); 01124 KTimeZoneBackend &operator=(const KTimeZoneBackend &other); 01125 virtual ~KTimeZoneBackend(); 01126 01134 virtual KTimeZoneBackend *clone() const; 01135 01145 virtual QByteArray type() const; 01146 01152 virtual int offsetAtZoneTime(const KTimeZone* caller, const QDateTime &zoneDateTime, int *secondOffset) const; 01158 virtual int offsetAtUtc(const KTimeZone* caller, const QDateTime &utcDateTime) const; 01164 virtual int offset(const KTimeZone* caller, time_t t) const; 01170 virtual bool isDstAtUtc(const KTimeZone* caller, const QDateTime &utcDateTime) const; 01176 virtual bool isDst(const KTimeZone* caller, time_t t) const; 01182 virtual bool hasTransitions(const KTimeZone* caller) const; 01183 01184 protected: 01197 KTimeZoneBackend(KTimeZoneSource *source, const QString &name, 01198 const QString &countryCode = QString(), float latitude = KTimeZone::UNKNOWN, 01199 float longitude = KTimeZone::UNKNOWN, const QString &comment = QString()); 01200 01201 private: 01202 KTimeZonePrivate *d; // non-const 01203 friend class KTimeZone; 01204 }; 01205 01225 class KDECORE_EXPORT KTimeZoneSource 01226 { 01227 public: 01228 KTimeZoneSource(); 01229 virtual ~KTimeZoneSource(); 01230 01247 virtual KTimeZoneData *parse(const KTimeZone &zone) const; 01248 01256 bool useZoneParse() const; 01257 01258 protected: 01276 explicit KTimeZoneSource(bool useZoneParse); 01277 01278 private: 01279 KTimeZoneSourcePrivate * const d; 01280 }; 01281 01282 01297 class KDECORE_EXPORT KTimeZoneData 01298 { 01299 friend class KTimeZone; 01300 01301 public: 01302 KTimeZoneData(); 01303 KTimeZoneData(const KTimeZoneData &c); 01304 virtual ~KTimeZoneData(); 01305 KTimeZoneData &operator=(const KTimeZoneData &c); 01306 01315 virtual KTimeZoneData *clone() const; 01316 01325 virtual QList<QByteArray> abbreviations() const; 01326 01335 virtual QByteArray abbreviation(const QDateTime &utcDateTime) const; 01336 01346 virtual QList<int> utcOffsets() const; 01347 01353 int previousUtcOffset() const; 01354 01365 QList<KTimeZone::Phase> phases() const; 01366 01375 virtual bool hasTransitions() const; 01376 01394 QList<KTimeZone::Transition> transitions(const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime()) const; 01395 01416 const KTimeZone::Transition *transition(const QDateTime &dt, const KTimeZone::Transition **secondTransition = 0, bool *validTime = 0) const; 01417 01440 int transitionIndex(const QDateTime &dt, int *secondIndex = 0, bool *validTime = 0) const; 01441 01461 QList<QDateTime> transitionTimes(const KTimeZone::Phase &phase, const QDateTime &start = QDateTime(), const QDateTime &end = QDateTime()) const; 01462 01472 QList<KTimeZone::LeapSeconds> leapSecondChanges() const; 01473 01481 KTimeZone::LeapSeconds leapSecondChange(const QDateTime &utc) const; 01482 01483 protected: 01492 void setPhases(const QList<KTimeZone::Phase> &phases, int previousUtcOffset); 01493 01500 void setTransitions(const QList<KTimeZone::Transition> &transitions); 01501 01508 void setLeapSecondChanges(const QList<KTimeZone::LeapSeconds> &adjusts); 01509 01510 private: 01511 KTimeZoneDataPrivate * const d; 01512 }; 01513 01514 #endif
KDE 4.6 API Reference