~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/tools/qdatetime.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#ifndef QDATETIME_H
 
30
#define QDATETIME_H
 
31
 
 
32
#include "QtCore/qstring.h"
 
33
#include "QtCore/qnamespace.h"
 
34
 
 
35
class Q_CORE_EXPORT QDate
 
36
{
 
37
public:
 
38
    QDate() { jd = 0; }
 
39
    QDate(int y, int m, int d);
 
40
 
 
41
    bool isNull() const { return jd == 0; }
 
42
    bool isValid() const;
 
43
 
 
44
    int year() const;
 
45
    int month() const;
 
46
    int day() const;
 
47
    int dayOfWeek() const;
 
48
    int dayOfYear() const;
 
49
    int daysInMonth() const;
 
50
    int daysInYear() const;
 
51
    int weekNumber(int *yearNum = 0) const;
 
52
 
 
53
#ifndef QT_NO_TEXTDATE
 
54
#ifdef QT3_SUPPORT
 
55
    static QT3_SUPPORT QString monthName(int month) { return shortMonthName(month); }
 
56
    static QT3_SUPPORT QString dayName(int weekday) { return shortDayName(weekday); }
 
57
#endif
 
58
    static QString shortMonthName(int month);
 
59
    static QString shortDayName(int weekday);
 
60
    static QString longMonthName(int month);
 
61
    static QString longDayName(int weekday);
 
62
#endif // QT_NO_TEXTDATE
 
63
#ifndef QT_NO_DATESTRING
 
64
    QString toString(Qt::DateFormat f = Qt::TextDate) const;
 
65
    QString toString(const QString &format) const;
 
66
#endif
 
67
    bool setYMD(int y, int m, int d);
 
68
 
 
69
    QDate addDays(int days) const;
 
70
    QDate addMonths(int months) const;
 
71
    QDate addYears(int years) const;
 
72
    int daysTo(const QDate &) const;
 
73
 
 
74
    bool operator==(const QDate &other) const { return jd == other.jd; }
 
75
    bool operator!=(const QDate &other) const { return jd != other.jd; }
 
76
    bool operator<(const QDate &other) const { return jd < other.jd; }
 
77
    bool operator<=(const QDate &other) const { return jd <= other.jd; }
 
78
    bool operator>(const QDate &other) const { return jd > other.jd; }
 
79
    bool operator>=(const QDate &other) const { return jd >= other.jd; }
 
80
 
 
81
    static QDate currentDate();
 
82
#ifndef QT_NO_DATESTRING
 
83
    static QDate fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
 
84
#endif
 
85
    static QDate fromString(const QString &s, const QString &format);
 
86
    static bool isValid(int y, int m, int d);
 
87
    static bool isLeapYear(int year);
 
88
#ifdef QT3_SUPPORT
 
89
    inline static QT3_SUPPORT bool leapYear(int year) { return isLeapYear(year); }
 
90
#endif
 
91
 
 
92
    static uint gregorianToJulian(int y, int m, int d);
 
93
    static void julianToGregorian(uint jd, int &y, int &m, int &d);
 
94
 
 
95
#ifdef QT3_SUPPORT
 
96
    static QT3_SUPPORT QDate currentDate(Qt::TimeSpec spec);
 
97
#endif
 
98
 
 
99
    static inline QDate fromJulianDay(int jd) { QDate d; d.jd = jd; return d; }
 
100
    inline int toJulianDay() const { return jd; }
 
101
 
 
102
private:
 
103
    uint jd;
 
104
 
 
105
    friend class QDateTime;
 
106
#ifndef QT_NO_DATASTREAM
 
107
    friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
 
108
    friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
 
109
#endif
 
110
};
 
111
Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE);
 
112
 
 
113
class Q_CORE_EXPORT QTime
 
114
{
 
115
public:
 
116
    QTime() { ds = 0; }
 
117
    QTime(int h, int m, int s = 0, int ms = 0);
 
118
 
 
119
    bool isNull() const { return ds == 0; }
 
120
    bool isValid() const;
 
121
 
 
122
    int hour() const;
 
123
    int minute() const;
 
124
    int second() const;
 
125
    int msec() const;
 
126
#ifndef QT_NO_DATESTRING
 
127
    QString toString(Qt::DateFormat f = Qt::TextDate) const;
 
128
    QString toString(const QString &format) const;
 
129
#endif
 
130
    bool setHMS(int h, int m, int s, int ms = 0);
 
131
 
 
132
    QTime addSecs(int secs) const;
 
133
    int secsTo(const QTime &) const;
 
134
    QTime addMSecs(int ms) const;
 
135
    int msecsTo(const QTime &) const;
 
136
 
 
137
    bool operator==(const QTime &other) const { return ds == other.ds; }
 
138
    bool operator!=(const QTime &other) const { return ds != other.ds; }
 
139
    bool operator<(const QTime &other) const { return ds < other.ds; }
 
140
    bool operator<=(const QTime &other) const { return ds <= other.ds; }
 
141
    bool operator>(const QTime &other) const { return ds > other.ds; }
 
142
    bool operator>=(const QTime &other) const { return ds >= other.ds; }
 
143
 
 
144
    static QTime currentTime();
 
145
#ifndef QT_NO_DATESTRING
 
146
    static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
 
147
#endif
 
148
    static QTime fromString(const QString &s, const QString &format);
 
149
    static bool isValid(int h, int m, int s, int ms = 0);
 
150
 
 
151
#ifdef QT3_SUPPORT
 
152
    static QT3_SUPPORT QTime currentTime(Qt::TimeSpec spec);
 
153
    static QT3_SUPPORT QDate currentDate(Qt::TimeSpec spec);
 
154
#endif
 
155
 
 
156
    void start();
 
157
    int restart();
 
158
    int elapsed() const;
 
159
 
 
160
private:
 
161
    uint ds;
 
162
 
 
163
    friend class QDateTime;
 
164
#ifndef QT_NO_DATASTREAM
 
165
    friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
 
166
    friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
 
167
#endif
 
168
};
 
169
Q_DECLARE_TYPEINFO(QTime, Q_MOVABLE_TYPE);
 
170
 
 
171
class QDateTimePrivate;
 
172
 
 
173
class Q_CORE_EXPORT QDateTime
 
174
{
 
175
public:
 
176
    QDateTime();
 
177
    explicit QDateTime(const QDate &);
 
178
    QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime);
 
179
    QDateTime(const QDateTime &other);
 
180
    ~QDateTime();
 
181
 
 
182
    QDateTime &operator=(const QDateTime &other);
 
183
 
 
184
    bool isNull() const;
 
185
    bool isValid() const;
 
186
 
 
187
    QDate date() const;
 
188
    QTime time() const;
 
189
    Qt::TimeSpec timeSpec() const;
 
190
    uint toTime_t() const;
 
191
    void setDate(const QDate &date);
 
192
    void setTime(const QTime &time);
 
193
    void setTimeSpec(Qt::TimeSpec spec);
 
194
    void setTime_t(uint secsSince1Jan1970UTC);
 
195
#ifndef QT_NO_DATESTRING
 
196
    QString toString(Qt::DateFormat f = Qt::TextDate) const;
 
197
    QString toString(const QString &format) const;
 
198
#endif
 
199
    QDateTime addDays(int days) const;
 
200
    QDateTime addMonths(int months) const;
 
201
    QDateTime addYears(int years) const;
 
202
    QDateTime addSecs(int secs) const;
 
203
    QDateTime toTimeSpec(Qt::TimeSpec spec) const;
 
204
    inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
 
205
    inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
 
206
    int daysTo(const QDateTime &) const;
 
207
    int secsTo(const QDateTime &) const;
 
208
 
 
209
    bool operator==(const QDateTime &other) const;
 
210
    inline bool operator!=(const QDateTime &other) const { return !(*this == other); }
 
211
    bool operator<(const QDateTime &other) const;
 
212
    inline bool operator<=(const QDateTime &other) const { return !(other < *this); }
 
213
    inline bool operator>(const QDateTime &other) const { return other < *this; }
 
214
    inline bool operator>=(const QDateTime &other) const { return !(*this < other); }
 
215
 
 
216
    static QDateTime currentDateTime();
 
217
#ifndef QT_NO_DATESTRING
 
218
    static QDateTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
 
219
#endif
 
220
    static QDateTime fromString(const QString &s, const QString &format);
 
221
 
 
222
#ifdef QT3_SUPPORT
 
223
    inline QT3_SUPPORT void setTime_t(uint secsSince1Jan1970UTC, Qt::TimeSpec spec) {
 
224
        setTime_t(secsSince1Jan1970UTC);
 
225
        if (spec == Qt::UTC)
 
226
            *this = toUTC();
 
227
    }
 
228
    static inline QT3_SUPPORT QDateTime currentDateTime(Qt::TimeSpec spec) {
 
229
        if (spec == Qt::LocalTime)
 
230
            return currentDateTime();
 
231
        else
 
232
            return currentDateTime().toUTC();
 
233
    }
 
234
#endif
 
235
 
 
236
private:
 
237
    void detach();
 
238
    QDateTimePrivate *d;
 
239
 
 
240
#ifndef QT_NO_DATASTREAM
 
241
    friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
 
242
    friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
 
243
#endif
 
244
};
 
245
Q_DECLARE_TYPEINFO(QDateTime, Q_MOVABLE_TYPE);
 
246
 
 
247
#ifdef QT3_SUPPORT
 
248
inline QDate QDate::currentDate(Qt::TimeSpec spec)
 
249
{
 
250
    if (spec == Qt::LocalTime)
 
251
        return currentDate();
 
252
    else
 
253
        return QDateTime::currentDateTime().toUTC().date();
 
254
}
 
255
 
 
256
inline QTime QTime::currentTime(Qt::TimeSpec spec)
 
257
{
 
258
    if (spec == Qt::LocalTime)
 
259
        return currentTime();
 
260
    else
 
261
        return QDateTime::currentDateTime().toUTC().time();
 
262
}
 
263
#endif
 
264
 
 
265
#ifndef QT_NO_DATASTREAM
 
266
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDate &);
 
267
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDate &);
 
268
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &);
 
269
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &);
 
270
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QDateTime &);
 
271
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QDateTime &);
 
272
#endif // QT_NO_DATASTREAM
 
273
 
 
274
#ifndef QT_NO_DEBUG_STREAM
 
275
Q_CORE_EXPORT QDebug operator<<(QDebug, const QDate &);
 
276
Q_CORE_EXPORT QDebug operator<<(QDebug, const QTime &);
 
277
Q_CORE_EXPORT QDebug operator<<(QDebug, const QDateTime &);
 
278
#endif
 
279
 
 
280
#endif // QDATETIME_H