summaryrefslogtreecommitdiffstats
path: root/src/messaging/win32wce/qmailtimestamp.cpp
blob: 298e11591a392d643d283deffa70c0f619173aa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmailtimestamp.h"

#include <QDate>
#include <QStringList>
#include <QTime>

#include <stdlib.h>


class QMailTimeStampPrivate : public QSharedData
{
public:
    QMailTimeStampPrivate();
    explicit QMailTimeStampPrivate(const QString& timeText);
    explicit QMailTimeStampPrivate(const QDateTime& dateTime);
    QMailTimeStampPrivate(const QMailTimeStampPrivate& other);
    ~QMailTimeStampPrivate();

    const QMailTimeStampPrivate& operator= (const QMailTimeStampPrivate& other);

    QString toString(QMailTimeStamp::OutputFormat format) const;

    QDateTime toLocalTime() const;
    QDateTime toUTC() const;

    bool isNull() const;
    bool isValid() const;

    bool operator== (const QMailTimeStampPrivate& other) const;
    bool operator!= (const QMailTimeStampPrivate& other) const;

    bool operator< (const QMailTimeStampPrivate& other) const;
    bool operator<= (const QMailTimeStampPrivate& other) const;

    bool operator> (const QMailTimeStampPrivate& other) const;
    bool operator>= (const QMailTimeStampPrivate& other) const;

private:
    // Make these private - since they must be accessed through a smart pointer from the
    // owner class, it's better if all access is restricted to this class
    QDateTime time;
    int utcOffset;
};

QMailTimeStampPrivate::QMailTimeStampPrivate()
    : utcOffset(0)
{
}

QMailTimeStampPrivate::QMailTimeStampPrivate(const QString& timeText)
    : utcOffset(0)
{
    int month = -1, day = -1, year = -1;
    bool ok;
    QString timeStr, offsetStr;

    // Remove any comments from the text
    QString uncommented;
    uncommented.reserve( timeText.length() );

    int commentDepth = 0;
    bool escaped = false;

    const QChar* it = timeText.constData();
    const QChar* end = it + timeText.length();
    for ( ; it != end; ++it ) {
        if ( !escaped && ( *it == '\\' ) ) {
            escaped = true;
            continue;
        }

        if ( *it == '(' && !escaped )
            commentDepth += 1;
        else if ( *it == ')' && !escaped && ( commentDepth > 0 ) )
            commentDepth -= 1;
        else if ( commentDepth == 0 ) {
            // Remove characters we don't want
            if ( *it != ',' && *it != '\n' && *it != '\r' )
                uncommented.append( *it );
        }

        escaped = false;
    }

    // Extract the date/time elements
    uncommented = uncommented.trimmed();
    QStringList tokens = uncommented.split(' ', QString::SkipEmptyParts);

    int tokenCount = tokens.count();
    if ( tokenCount > 0 ) {
        static const QString Days("sunmontuewedthufrisat");
        if (Days.indexOf(tokens[0].left(3).toLower()) != -1) {
            tokens.removeAt(0);
            tokenCount -= 1;
        }
        if (tokenCount > 0) {
            int value = tokens[0].toInt(&ok);
            if ( ok )
                day = value;
        }
    }
    if ( tokenCount > 1 ) {
        static const QString Months("janfebmaraprmayjunjulaugsepoctnovdec");
        int value = Months.indexOf( tokens[1].left( 3 ).toLower() );
        if ( value != -1 )
            month = (value + 3) / 3;
    }
    if ( tokenCount > 2 ) {
        int value = tokens[2].toInt(&ok);
        if ( ok ) {
            // Interpret year according to RFC2822
            year = value;
            if ( year < 100 ) {
                year += ( year <= 49 ? 2000 : 1900 );
            }
            else if ( year < 1000 ) {
                year += 1900;
            }
        }
    }
    if ( tokenCount > 3 ) {
        timeStr = tokens[3].trimmed();
    }
    if ( tokenCount > 4 ) {
        offsetStr = tokens[4].trimmed();
    }

    // Parse the text into UTC-adjusted time part and UTC-offset indicator
    if ( (day != -1) && (month != -1) && (year != -1) ) {
        QDate dateComponent(year, month, day);
        QTime timeComponent;

        QTime parsedTime;
        if ( timeStr.length() == 8 ) {
            parsedTime = QTime::fromString( timeStr, "hh:mm:ss" );
        }
        else if ( timeStr.length() == 5 ) {
            // Is this legal?  Either way, it seems desirable for robustness...
            parsedTime = QTime::fromString( timeStr, "hh:mm" );
        }
        if ( parsedTime.isValid() )
            timeComponent = parsedTime;

        time = QDateTime( dateComponent, timeComponent, Qt::UTC );

        if ( offsetStr.length() == 5 ) {
            int h = offsetStr.left(3).toInt(&ok);
            if ( ok ) {
                int m = offsetStr.right(2).toInt(&ok);
                if ( ok ) {
                    utcOffset = ( h * 3600 ) + ( m * 60 * ( h < 0 ? -1 : 1 ) );

                    time = time.addSecs( -utcOffset );
                }
            }
        }
    }
}

QMailTimeStampPrivate::QMailTimeStampPrivate(const QDateTime& dateTime)
{
    // Store the time as UTC
    if ( dateTime.timeSpec() == Qt::LocalTime ) {
        QDateTime original(dateTime);
        original.setTimeSpec( Qt::UTC );

        time = dateTime.toUTC();

        // Find the difference
        utcOffset = time.secsTo( original );
    }
    else {
        // Time is already in UTC
        time = dateTime;

        // Is this the right thing to do? What does it mean if we get a UTC timestamp?
        utcOffset = 0;
    }

    // Since we can't include milliseconds in our textual representation, and we
    // need to ensure the following works:
    //    assert( someTimeStamp == QMailTimeStamp(someTimeStamp.toString()) )
    // then we need to make sure that we have no sub-second component
    qint64 ms = time.time().msec();
    if (ms != 0)
        time = time.addMSecs(0 - ms);
}

QMailTimeStampPrivate::QMailTimeStampPrivate(const QMailTimeStampPrivate& other)
    : QSharedData(other)
{
    this->operator=(other);
}

QMailTimeStampPrivate::~QMailTimeStampPrivate()
{
}

const QMailTimeStampPrivate& QMailTimeStampPrivate::operator= (const QMailTimeStampPrivate& other)
{
    time = other.time;
    utcOffset = other.utcOffset;
    return *this;
}

QString QMailTimeStampPrivate::toString(QMailTimeStamp::OutputFormat format) const
{
    // We can't use QDateTime to provide day and month names, since they may get localized into UTF-8
    static const char Days[] = "MonTueWedThuFriSatSun";
    static const char Months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";

    if (time.isNull() || !time.isValid())
        return QString();

    QString result;

    QDateTime originalTime = time.addSecs( utcOffset );
    QDate originalDate = originalTime.date();

    int hOffset = utcOffset / 3600;
    int mOffset = ( abs(utcOffset) - abs(hOffset * 3600) ) / 60;

    if (format == QMailTimeStamp::Rfc2822) {
        result = QString( originalTime.toString( "%1, d %2 yyyy hh:mm:ss %3" ) );
        result = result.arg( QString::fromAscii( Days + ( originalDate.dayOfWeek() - 1 ) * 3, 3 ) );
        result = result.arg( QString::fromAscii( Months + ( originalDate.month() - 1 ) * 3, 3 ) );
        result = result.arg( QString().sprintf( "%+.2d%.2d", hOffset, mOffset ) );
    } else if (format == QMailTimeStamp::Rfc3501) {
        result = QString( originalTime.toString( "dd-%1-yyyy hh:mm:ss %2" ) );
        result = result.arg( QString::fromAscii( Months + ( originalDate.month() - 1 ) * 3, 3 ) );
        result = result.arg( QString().sprintf( "%+.2d%.2d", hOffset, mOffset ) );

        // The day number should be space-padded
        if (result[0] == '0') {
            result[0] = ' ';
        }
    } else if (format == QMailTimeStamp::Rfc3339) {
        result = QString( originalTime.toString( "yyyy-MM-ddThh:mm:ss%1" ) );
        result = result.arg( utcOffset == 0 ? QString("Z") : QString().sprintf( "%+.2d:%.2d", hOffset, mOffset ) );
    }

    return result;
}

QDateTime QMailTimeStampPrivate::toLocalTime() const
{
    return time.toLocalTime();
}

QDateTime QMailTimeStampPrivate::toUTC() const
{
    return time;
}

bool QMailTimeStampPrivate::isNull() const
{
    return time.isNull();
}

bool QMailTimeStampPrivate::isValid() const
{
    return time.isValid();
}

bool QMailTimeStampPrivate::operator== (const QMailTimeStampPrivate& other) const
{
    return ( toUTC() == other.toUTC() );
}

bool QMailTimeStampPrivate::operator!= (const QMailTimeStampPrivate& other) const
{
    return !operator==(other);
}

bool QMailTimeStampPrivate::operator< (const QMailTimeStampPrivate& other) const
{
    return ( toUTC() < other.toUTC() );
}

bool QMailTimeStampPrivate::operator<= (const QMailTimeStampPrivate& other) const
{
    return !operator>(other);
}

bool QMailTimeStampPrivate::operator> (const QMailTimeStampPrivate& other) const
{
    return ( toUTC() > other.toUTC() );
}

bool QMailTimeStampPrivate::operator>= (const QMailTimeStampPrivate& other) const
{
    return !operator<(other);
}


/*!
    \class QMailTimeStamp

    \brief The QMailTimeStamp class manages message time stamps.
    \ingroup messaginglibrary
    \since 1.1

    QMailTimeStamp provides functions for creating and manipulating the time stamps of messages.
    QMailTimeStamp can be created from time stamp strings, or from QDateTime objects.  The
    time stamp information can be extracted in UTC time, local time, or as a formatted
    string.

    QMailTimeStamp maintains the timezone information of a time stamp, so it can be used to
    convert time stamp information between UTC time and localized time values.

    \sa QDateTime, QMailMessage
*/

/*!
    \enum QMailTimeStamp::OutputFormat

    This enum type is used to select a format for timestamp output.

    \value Rfc2822  The format used in SMTP message format; example: "Wed, 17 May 2006 20:45:00 +0100".
    \value Rfc3501  The format used in IMAP message append; example: "17-May-2006 20:45:00 +0100".
    \value Rfc3339  The format specified for future protocols (a variant of ISO 8601); example: "2006-05-17T20:45:00+01:00".
*/

/*!
    Returns a QMailTimeStamp object initialised to contain the current
    date and time, in the local time zone.
*/
QMailTimeStamp QMailTimeStamp::currentDateTime()
{
    return QMailTimeStamp(QDateTime::currentDateTime());
}

/*!
    Constructs a null QMailTimeStamp object. A null timestamp is invalid.
*/
QMailTimeStamp::QMailTimeStamp()
{
    d = new QMailTimeStampPrivate();
}

/*! \internal */
QMailTimeStamp::QMailTimeStamp(const QMailTimeStamp& other)
{
    this->operator=(other);
}

/*!
    Constructs a QMailTimeStamp object by parsing \a timeText.
*/
QMailTimeStamp::QMailTimeStamp(const QString& timeText)
{
    d = new QMailTimeStampPrivate(timeText);
}

/*!
    Constructs a QMailTimeStamp object from the given \a dateTime.
*/
QMailTimeStamp::QMailTimeStamp(const QDateTime& dateTime)
{
    d = new QMailTimeStampPrivate(dateTime);
}

/*!
    Destroys a QMailTimeStamp object.
*/
QMailTimeStamp::~QMailTimeStamp()
{
}

/*! \internal */
const QMailTimeStamp& QMailTimeStamp::operator= (const QMailTimeStamp& other)
{
    d = other.d;
    return *this;
}

/*!
    Returns the time stamp information in the format specified by \a format.
*/
QString QMailTimeStamp::toString(QMailTimeStamp::OutputFormat format) const
{
    return d->toString(format);
}

/*!
    Returns a QDateTime containing the time stamp converted to the local time zone.
*/
QDateTime QMailTimeStamp::toLocalTime() const
{
    return d->toLocalTime();
}

/*!
    Returns a QDateTime containing the time stamp in UTC time.
*/
QDateTime QMailTimeStamp::toUTC() const
{
    return d->toUTC();
}

/*!
    Returns true if the timestamp has not been initialized to contain a value.
*/
bool QMailTimeStamp::isNull() const
{
    return d->isNull();
}

/*!
    Returns true if the timestamp is valid; otherwise returns false;
*/
bool QMailTimeStamp::isValid() const
{
    return d->isValid();
}

/*!
    Returns true if this time stamp is equal to \a other; otherwise returns false.

    \sa operator!=()
*/
bool QMailTimeStamp::operator== (const QMailTimeStamp& other) const
{
    return d->operator==(*other.d);
}

/*!
    Returns true if this time stamp is different from \a other; otherwise returns false.

    Two time stamps are different if either the date, the time, or the time zone components are different.

    \sa operator==()
*/
bool QMailTimeStamp::operator!= (const QMailTimeStamp& other) const
{
    return d->operator!=(*other.d);
}

/*!
    Returns true if this time stamp is earlier than \a other; otherwise returns false.
*/
bool QMailTimeStamp::operator< (const QMailTimeStamp& other) const
{
    return d->operator<(*other.d);
}

/*!
    Returns true if this time stamp is earlier than or equal to \a other; otherwise returns false.
*/
bool QMailTimeStamp::operator<= (const QMailTimeStamp& other) const
{
    return d->operator<=(*other.d);
}

/*!
    Returns true if this time stamp is later than \a other; otherwise returns false.
*/
bool QMailTimeStamp::operator> (const QMailTimeStamp& other) const
{
    return d->operator>(*other.d);
}

/*!
    Returns true if this time stamp is later than or equal to \a other; otherwise returns false.
*/
bool QMailTimeStamp::operator>= (const QMailTimeStamp& other) const
{
    return d->operator>=(*other.d);
}