qdatetime.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QDATETIME_H
00043 #define QDATETIME_H
00044 
00045 #include <QtCore/qstring.h>
00046 #include <QtCore/qnamespace.h>
00047 #include <QtCore/qsharedpointer.h>
00048 
00049 QT_BEGIN_HEADER
00050 
00051 QT_BEGIN_NAMESPACE
00052 
00053 QT_MODULE(Core)
00054 
00055 class Q_CORE_EXPORT QDate
00056 {
00057 public:
00058     enum MonthNameType {
00059         DateFormat = 0,
00060         StandaloneFormat
00061     };
00062 public:
00063     QDate() { jd = 0; }
00064     QDate(int y, int m, int d);
00065 
00066     bool isNull() const { return jd == 0; }
00067     bool isValid() const;
00068 
00069     int year() const;
00070     int month() const;
00071     int day() const;
00072     int dayOfWeek() const;
00073     int dayOfYear() const;
00074     int daysInMonth() const;
00075     int daysInYear() const;
00076     int weekNumber(int *yearNum = 0) const;
00077 
00078 #ifndef QT_NO_TEXTDATE
00079 #ifdef QT3_SUPPORT
00080     static QT3_SUPPORT QString monthName(int month) { return shortMonthName(month); }
00081     static QT3_SUPPORT QString dayName(int weekday) { return shortDayName(weekday); }
00082 #endif
00083     // ### Qt 5: merge these functions.
00084     static QString shortMonthName(int month);
00085     static QString shortMonthName(int month, MonthNameType type);
00086     static QString shortDayName(int weekday);
00087     static QString shortDayName(int weekday, MonthNameType type);
00088     static QString longMonthName(int month);
00089     static QString longMonthName(int month, MonthNameType type);
00090     static QString longDayName(int weekday);
00091     static QString longDayName(int weekday, MonthNameType type);
00092 #endif // QT_NO_TEXTDATE
00093 #ifndef QT_NO_DATESTRING
00094     QString toString(Qt::DateFormat f = Qt::TextDate) const;
00095     QString toString(const QString &format) const;
00096 #endif
00097     bool setYMD(int y, int m, int d);
00098     bool setDate(int year, int month, int day);
00099 
00100     void getDate(int *year, int *month, int *day);
00101 
00102     QDate addDays(int days) const;
00103     QDate addMonths(int months) const;
00104     QDate addYears(int years) const;
00105     int daysTo(const QDate &) const;
00106 
00107     bool operator==(const QDate &other) const { return jd == other.jd; }
00108     bool operator!=(const QDate &other) const { return jd != other.jd; }
00109     bool operator<(const QDate &other) const { return jd < other.jd; }
00110     bool operator<=(const QDate &other) const { return jd <= other.jd; }
00111     bool operator>(const QDate &other) const { return jd > other.jd; }
00112     bool operator>=(const QDate &other) const { return jd >= other.jd; }
00113 
00114     static QDate currentDate();
00115 #ifndef QT_NO_DATESTRING
00116     static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
00117     static QDate fromString(const QString &s, const QString &format);
00118 #endif
00119     static bool isValid(int y, int m, int d);
00120     static bool isLeapYear(int year);
00121 #ifdef QT3_SUPPORT
00122     inline static QT3_SUPPORT bool leapYear(int year) { return isLeapYear(year); }
00123 #endif
00124 
00125     // ### Qt 5: remove these two functions
00126     static uint gregorianToJulian(int y, int m, int d);
00127     static void julianToGregorian(uint jd, int &y, int &m, int &d);
00128 
00129 #ifdef QT3_SUPPORT
00130     static QT3_SUPPORT QDate currentDate(Qt::TimeSpec spec);
00131 #endif
00132 
00133     static inline QDate fromJulianDay(int jd) { QDate d; d.jd = jd; return d; }
00134     inline int toJulianDay() const { return jd; }
00135 
00136 private:
00137     uint jd;
00138 
00139     friend class QDateTime;
00140     friend class QDateTimePrivate;
00141 #ifndef QT_NO_DATASTREAM
00142     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
00143     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
00144 #endif
00145 };
00146 Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
00147 
00148 class Q_CORE_EXPORT QTime
00149 {
00150 public:
00151     QTime(): mds(NullTime)
00152 #if defined(Q_OS_WINCE)
00153         , startTick(NullTime)
00154 #endif
00155     {}
00156     QTime(int h, int m, int s = 0, int ms = 0);
00157 
00158     bool isNull() const { return mds == NullTime; }
00159     bool isValid() const;
00160 
00161     int hour() const;
00162     int minute() const;
00163     int second() const;
00164     int msec() const;
00165 #ifndef QT_NO_DATESTRING
00166     QString toString(Qt::DateFormat f = Qt::TextDate) const;
00167     QString toString(const QString &format) const;
00168 #endif
00169     bool setHMS(int h, int m, int s, int ms = 0);
00170 
00171     QTime addSecs(int secs) const;
00172     int secsTo(const QTime &) const;
00173     QTime addMSecs(int ms) const;
00174     int msecsTo(const QTime &) const;
00175 
00176     bool operator==(const QTime &other) const { return mds == other.mds; }
00177     bool operator!=(const QTime &other) const { return mds != other.mds; }
00178     bool operator<(const QTime &other) const { return mds < other.mds; }
00179     bool operator<=(const QTime &other) const { return mds <= other.mds; }
00180     bool operator>(const QTime &other) const { return mds > other.mds; }
00181     bool operator>=(const QTime &other) const { return mds >= other.mds; }
00182 
00183     static QTime currentTime();
00184 #ifndef QT_NO_DATESTRING
00185     static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
00186     static QTime fromString(const QString &s, const QString &format);
00187 #endif
00188     static bool isValid(int h, int m, int s, int ms = 0);
00189 
00190 #ifdef QT3_SUPPORT
00191     static QT3_SUPPORT QTime currentTime(Qt::TimeSpec spec);
00192 #endif
00193 
00194     void start();
00195     int restart();
00196     int elapsed() const;
00197 private:
00198     enum TimeFlag { NullTime = -1 };
00199     inline int ds() const { return mds == -1 ? 0 : mds; }
00200     int mds;
00201 #if defined(Q_OS_WINCE)
00202     int startTick;
00203 #endif
00204 
00205     friend class QDateTime;
00206     friend class QDateTimePrivate;
00207 #ifndef QT_NO_DATASTREAM
00208     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
00209     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
00210 #endif
00211 };
00212 Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
00213 
00214 class QDateTimePrivate;
00215 
00216 class Q_CORE_EXPORT QDateTime
00217 {
00218 public:
00219     QDateTime();
00220     explicit QDateTime(const QDate &);
00221     QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
00222     QDateTime(const QDateTime &other);
00223     ~QDateTime();
00224 
00225     QDateTime &operator=(const QDateTime &other);
00226 
00227     bool isNull() const;
00228     bool isValid() const;
00229 
00230     QDate date() const;
00231     QTime time() const;
00232     Qt::TimeSpec timeSpec() const;
00233     qint64 toMSecsSinceEpoch() const;
00234     uint toTime_t() const;
00235     void setDate(const QDate &date);
00236     void setTime(const QTime &time);
00237     void setTimeSpec(Qt::TimeSpec spec);
00238     void setMSecsSinceEpoch(qint64 msecs);
00239     void setTime_t(uint secsSince1Jan1970UTC);
00240 #ifndef QT_NO_DATESTRING
00241     QString toString(Qt::DateFormat f = Qt::TextDate) const;
00242     QString toString(const QString &format) const;
00243 #endif
00244     QDateTime addDays(int days) const;
00245     QDateTime addMonths(int months) const;
00246     QDateTime addYears(int years) const;
00247     QDateTime addSecs(int secs) const;
00248     QDateTime addMSecs(qint64 msecs) const;
00249     QDateTime toTimeSpec(Qt::TimeSpec spec) const;
00250     inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
00251     inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
00252     int daysTo(const QDateTime &) const;
00253     int secsTo(const QDateTime &) const;
00254     qint64 msecsTo(const QDateTime &) const;
00255 
00256     bool operator==(const QDateTime &other) const;
00257     inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
00258     bool operator<(const QDateTime &other) const;
00259     inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
00260     inline bool operator>(const QDateTime &other) const { return other < *this; }
00261     inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
00262 
00263     void setUtcOffset(int seconds);
00264     int utcOffset() const;
00265 
00266     static QDateTime currentDateTime();
00267     static QDateTime currentDateTimeUtc();
00268 #ifndef QT_NO_DATESTRING
00269     static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
00270     static QDateTime fromString(const QString &s, const QString &format);
00271 #endif
00272     static QDateTime fromTime_t(uint secsSince1Jan1970UTC);
00273     static QDateTime fromMSecsSinceEpoch(qint64 msecs);
00274     static qint64 currentMSecsSinceEpoch();
00275 
00276 #ifdef QT3_SUPPORT
00277     inline QT3_SUPPORT void setTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec) {
00278         setTime_t(secsSince1Jan1970UTC);
00279         if (spec == Qt::UTC)
00280             *this = toUTC();
00281     }
00282     static inline QT3_SUPPORT QDateTime currentDateTime(Qt::TimeSpec spec) {
00283         if (spec == Qt::LocalTime)
00284             return currentDateTime();
00285         else
00286             return currentDateTime().toUTC();
00287     }
00288     
00289 #endif
00290 
00291 private:
00292     friend class QDateTimePrivate;
00293     void detach();
00294     QExplicitlySharedDataPointer<QDateTimePrivate> d;
00295 
00296 #ifndef QT_NO_DATASTREAM
00297     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
00298     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
00299 #endif
00300 };
00301 Q_DECLARE_TYPEINFO(QDateTime, Q_MOVABLE_TYPE);
00302 
00303 #ifdef QT3_SUPPORT
00304 inline QDate QDate::currentDate(Qt::TimeSpec spec)
00305 {
00306     if (spec == Qt::LocalTime)
00307         return currentDate();
00308     else
00309         return QDateTime::currentDateTime().toUTC().date();
00310 }
00311 
00312 inline QTime QTime::currentTime(Qt::TimeSpec spec)
00313 {
00314     if (spec == Qt::LocalTime)
00315         return currentTime();
00316     else
00317         return QDateTime::currentDateTime().toUTC().time();
00318 }
00319 #endif
00320 
00321 #ifndef QT_NO_DATASTREAM
00322 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
00323 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
00324 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
00325 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
00326 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
00327 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
00328 #endif // QT_NO_DATASTREAM
00329 
00330 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_NO_DATESTRING)
00331 Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
00332 Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
00333 Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
00334 #endif
00335 
00336 QT_END_NAMESPACE
00337 
00338 QT_END_HEADER
00339 
00340 #endif // QDATETIME_H