summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qtimezone.cpp
blob: b6160a3fd85e2985cd302fd4b4bfb8ff0bea5d01 (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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/****************************************************************************
**
** Copyright (C) 2013 John Layt <jlayt@kde.org>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include "qtimezone.h"
#include "qtimezoneprivate_p.h"

#include <QtCore/qdatastream.h>
#include <QtCore/qdatetime.h>

#include <qdebug.h>

#include <algorithm>

QT_BEGIN_NAMESPACE

// Create default time zone using appropriate backend
static QTimeZonePrivate *newBackendTimeZone()
{
#ifdef QT_NO_SYSTEMLOCALE
#ifdef QT_USE_ICU
    return new QIcuTimeZonePrivate();
#else
    return new QUtcTimeZonePrivate();
#endif // QT_USE_ICU
#else
#if defined Q_OS_MAC
    return new QMacTimeZonePrivate();
#elif defined Q_OS_ANDROID
    return new QAndroidTimeZonePrivate();
#elif defined Q_OS_UNIX
    return new QTzTimeZonePrivate();
    // Registry based timezone backend not available on WinRT
#elif defined Q_OS_WIN && !defined Q_OS_WINRT
    return new QWinTimeZonePrivate();
#elif defined QT_USE_ICU
    return new QIcuTimeZonePrivate();
#else
    return new QUtcTimeZonePrivate();
#endif // System Locales
#endif // QT_NO_SYSTEMLOCALE
}

// Create named time zone using appropriate backend
static QTimeZonePrivate *newBackendTimeZone(const QByteArray &ianaId)
{
#ifdef QT_NO_SYSTEMLOCALE
#ifdef QT_USE_ICU
    return new QIcuTimeZonePrivate(ianaId);
#else
    return new QUtcTimeZonePrivate(ianaId);
#endif // QT_USE_ICU
#else
#if defined Q_OS_MAC
    return new QMacTimeZonePrivate(ianaId);
#elif defined Q_OS_ANDROID
    return new QAndroidTimeZonePrivate(ianaId);
#elif defined Q_OS_UNIX
    return new QTzTimeZonePrivate(ianaId);
    // Registry based timezone backend not available on WinRT
#elif defined Q_OS_WIN && !defined Q_OS_WINRT
    return new QWinTimeZonePrivate(ianaId);
#elif defined QT_USE_ICU
    return new QIcuTimeZonePrivate(ianaId);
#else
    return new QUtcTimeZonePrivate(ianaId);
#endif // System Locales
#endif // QT_NO_SYSTEMLOCALE
}

class QTimeZoneSingleton
{
public:
    QTimeZoneSingleton() : backend(newBackendTimeZone()) {}

    // The backend_tz is the tz to use in static methods such as availableTimeZoneIds() and
    // isTimeZoneIdAvailable() and to create named IANA time zones.  This is usually the host
    // system, but may be different if the host resources are insufficient or if
    // QT_NO_SYSTEMLOCALE is set.  A simple UTC backend is used if no alternative is available.
    QSharedDataPointer<QTimeZonePrivate> backend;
};

Q_GLOBAL_STATIC(QTimeZoneSingleton, global_tz);

/*!
    \class QTimeZone
    \inmodule QtCore
    \since 5.2
    \brief The QTimeZone class converts between between UTC and local time in a
           specific time zone.

    \threadsafe

    This class provides a stateless calculator for time zone conversions
    between UTC and the local time in a specific time zone.  By default it uses
    the host system time zone data to perform these conversions.

    This class is primarily designed for use in QDateTime; most applications
    will not need to access this class directly and should instead use
    QDateTime with a Qt::TimeSpec of Qt::TimeZone.

    \note For consistency with QDateTime, QTimeZone does not account for leap
    seconds.

    \section1

    \section2 IANA Time Zone IDs

    QTimeZone uses the IANA time zone IDs as defined in the IANA Time Zone
    Database (http://www.iana.org/time-zones). This is to ensure a standard ID
    across all supported platforms.  Most platforms support the IANA IDs
    and the IANA Database natively, but for Windows a mapping is required to
    the native IDs.  See below for more details.

    The IANA IDs can and do change on a regular basis, and can vary depending
    on how recently the host system data was updated.  As such you cannot rely
    on any given ID existing on any host system.  You must use
    availableTimeZoneIds() to determine what IANA IDs are available.

    The IANA IDs and database are also know as the Olson IDs and database,
    named after their creator.

    \section2 UTC Offset Time Zones

    A default UTC time zone backend is provided which is always guaranteed to
    be available.  This provides a set of generic Offset From UTC time zones
    in the range UTC-14:00 to UTC+14:00.  These time zones can be created
    using either the standard ISO format names "UTC+00:00" as listed by
    availableTimeZoneIds(), or using the number of offset seconds.

    \section2 Windows Time Zones

    Windows native time zone support is severely limited compared to the
    standard IANA TZ Database.  Windows time zones cover larger geographic
    areas and are thus less accurate in their conversions.  They also do not
    support as much historic conversion data and so may only be accurate for
    the current year.

    QTimeZone uses a conversion table derived form the Unicode CLDR data to map
    between IANA IDs and Windows IDs.  Depending on your version of Windows
    and Qt, this table may not be able to provide a valid conversion, in which
    "UTC" will be returned.

    QTimeZone provides a public API to use this conversion table.  The Windows ID
    used is the Windows Registry Key for the time zone which is also the MS
    Exchange EWS ID as well, but is different to the Time Zone Name (TZID) and
    COD code used by MS Exchange in versions before 2007.

    \section2 System Time Zone

    QTimeZone does not support any concept of a system or default time zone.
    If you require a QDateTime that uses the current system time zone at any
    given moment then you should use a Qt::TimeSpec of Qt::LocalTime.

    The method systemTimeZoneId() returns the current system IANA time zone
    ID which on OSX and Linux will always be correct.  On Windows this ID is
    translated from the Windows system ID using an internal translation
    table and the user's selected country.  As a consequence there is a small
    chance any Windows install may have IDs not known by Qt, in which case
    "UTC" will be returned.

    Creating a new QTimeZone instance using the system time zone ID will only
    produce a fixed named copy of the time zone, it will not change if the
    system time zone changes.

    \section2 Time Zone Offsets

    The difference between UTC and the local time in a time zone is expressed
    as an offset in seconds from UTC, i.e. the number of seconds to add to UTC
    to obtain the local time.  The total offset is comprised of two component
    parts, the standard time offset and the daylight time offset.  The standard
    time offset is the number of seconds to add to UTC to obtain standard time
    in the time zone.  The daylight time offset is the number of seconds to add
    to the standard time offset to obtain daylight time in the time zone.

    Note that the standard and daylight offsets for a time zone may change over
    time as countries have changed daylight time laws or even their standard
    time offset.

    \section2 License

    This class includes data obtained from the CLDR data files under the terms
    of the Unicode license.

    \legalese
    COPYRIGHT AND PERMISSION NOTICE

    Copyright © 1991-2012 Unicode, Inc. All rights reserved. Distributed under
    the Terms of Use in http://www.unicode.org/copyright.html.

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of the Unicode data files and any associated documentation (the "Data
    Files") or Unicode software and any associated documentation (the "Software")
    to deal in the Data Files or Software without restriction, including without
    limitation the rights to use, copy, modify, merge, publish, distribute, and/or
    sell copies of the Data Files or Software, and to permit persons to whom the
    Data Files or Software are furnished to do so, provided that (a) the above
    copyright notice(s) and this permission notice appear with all copies of the
    Data Files or Software, (b) both the above copyright notice(s) and this
    permission notice appear in associated documentation, and (c) there is clear
    notice in each modified Data File or in the Software as well as in the
    documentation associated with the Data File(s) or Software that the data or
    software has been modified.
    \endlegalese

    \sa QDateTime
*/

/*!
    \enum QTimeZone::TimeType

    The type of time zone time, for example when requesting the name.  In time
    zones that do not apply daylight time, all three values may return the
    same result.

    \value StandardTime
           The standard time in a time zone, i.e. when Daylight Savings is not
           in effect.
           For example when formatting a display name this will show something
           like "Pacific Standard Time".
    \value DaylightTime
           A time when Daylight Savings is in effect.
           For example when formatting a display name this will show something
           like "Pacific daylight time".
    \value GenericTime
           A time which is not specifically Standard or Daylight time, either
           an unknown time or a neutral form.
           For example when formatting a display name this will show something
           like "Pacific Time".
*/

/*!
    \enum QTimeZone::NameType

    The type of time zone name.

    \value DefaultName
           The default form of the time zone name, e.g. LongName, ShortName or OffsetName
    \value LongName
           The long form of the time zone name, e.g. "Central European Time"
    \value ShortName
           The short form of the time zone name, usually an abbreviation, e.g. "CET"
    \value OffsetName
           The standard ISO offset form of the time zone name, e.g. "UTC+01:00"
*/

/*!
    \class QTimeZone::OffsetData
    \inmodule QtCore

    The time zone offset data for a given moment in time, i.e. the time zone
    offsets and abbreviation to use at that moment in time.

    \list
    \li OffsetData::atUtc  The datetime of the offset data in UTC time.
    \li OffsetData::offsetFromUtc  The total offset from UTC in effect at the datetime.
    \li OffsetData::standardTimeOffset  The standard time offset component of the total offset.
    \li OffsetData::daylightTimeOffset  The daylight time offset component of the total offset.
    \li OffsetData::abbreviation  The abbreviation in effect at the datetime.
    \endlist

    For example, for time zone "Europe/Berlin" the OffsetDate in standard and daylight time might be:

    \list
    \li atUtc = QDateTime(QDate(2013, 1, 1), QTime(0, 0, 0), Qt::UTC)
    \li offsetFromUtc = 3600
    \li standardTimeOffset = 3600
    \li daylightTimeOffset = 0
    \li abbreviation = "CET"
    \endlist

    \list
    \li atUtc = QDateTime(QDate(2013, 6, 1), QTime(0, 0, 0), Qt::UTC)
    \li offsetFromUtc = 7200
    \li standardTimeOffset = 3600
    \li daylightTimeOffset = 3600
    \li abbreviation = "CEST"
    \endlist
*/

/*!
    \typedef QTimeZone::OffsetDataList
    \relates QTimeZone

    Synonym for QList<OffsetData>.
*/

/*!
    Create a null/invalid time zone instance.
*/

QTimeZone::QTimeZone()
    : d(0)
{
}

/*!
    Creates an instance of the requested time zone \a ianaId.

    The ID must be one of the available system IDs otherwise an invalid
    time zone will be returned.

    \sa availableTimeZoneIds()
*/

QTimeZone::QTimeZone(const QByteArray &ianaId)
{
    // Try and see if it's a valid UTC offset ID, just as quick to try create as look-up
    d = new QUtcTimeZonePrivate(ianaId);
    // If not a valid UTC offset ID then try create it with the system backend
    // Relies on backend not creating valid tz with invalid name
    if (!d->isValid())
        d = newBackendTimeZone(ianaId);
}

/*!
    Creates an instance of a time zone with the requested Offset from UTC of
    \a offsetSeconds.

    The \a offsetSeconds from UTC must be in the range -14 hours to +14 hours
    otherwise an invalid time zone will be returned.
*/

QTimeZone::QTimeZone(int offsetSeconds)
{
    // offsetSeconds must fall between -14:00 and +14:00 hours
    if (offsetSeconds >= -50400 && offsetSeconds <= 50400)
        d = new QUtcTimeZonePrivate(offsetSeconds);
    else
        d = 0;
}

/*!
    Creates a custom time zone with an ID of \a ianaId and an offset from UTC
    of \a offsetSeconds.  The \a name will be the name used by displayName()
    for the LongName, the \a abbreviation will be used by displayName() for the
    ShortName and by abbreviation(), and the optional \a country will be used
    by country().  The \a comment is an optional note that may be displayed in
    a GUI to assist users in selecting a time zone.

    The \a ianaId must not be one of the available system IDs returned by
    availableTimeZoneIds().  The \a offsetSeconds from UTC must be in the range
    -14 hours to +14 hours.

    If the custom time zone does not have a specific country then set it to the
    default value of QLocale::AnyCountry.
*/

QTimeZone::QTimeZone(const QByteArray &ianaId, int offsetSeconds, const QString &name,
                     const QString &abbreviation, QLocale::Country country, const QString &comment)
{
    // ianaId must be a valid ID and must not clash with the standard system names
    if (QTimeZonePrivate::isValidId(ianaId) && !availableTimeZoneIds().contains(ianaId))
        d = new QUtcTimeZonePrivate(ianaId, offsetSeconds, name, abbreviation, country, comment);
    else
        d = 0;
}

/*!
    \internal

    Private. Create time zone with given private backend
*/

QTimeZone::QTimeZone(QTimeZonePrivate &dd)
    : d(&dd)
{
}

/*!
    Copy constructor, copy \a other to this.
*/

QTimeZone::QTimeZone(const QTimeZone &other)
    : d(other.d)
{
}

/*!
    Destroys the time zone.
*/

QTimeZone::~QTimeZone()
{
}

/*!
    \fn QTimeZone::swap(QTimeZone &other)

    Swaps this time zone instance with \a other. This function is very
    fast and never fails.
*/

/*!
    Assignment operator, assign \a other to this.
*/

QTimeZone &QTimeZone::operator=(const QTimeZone &other)
{
    d = other.d;
    return *this;
}

/*
    \fn void QTimeZone::swap(QTimeZone &other)

    Swaps this timezone with \a other. This function is very fast and
    never fails.
*/

/*!
    \fn QTimeZone &QTimeZone::operator=(QTimeZone &&other)

    Move-assigns \a other to this QTimeZone instance, transferring the
    ownership of the managed pointer to this instance.
*/

/*!
    Returns \c true if this time zone is equal to the \a other time zone.
*/

bool QTimeZone::operator==(const QTimeZone &other) const
{
    if (d && other.d)
        return (*d == *other.d);
    else
        return (d == other.d);
}

/*!
    Returns \c true if this time zone is not equal to the \a other time zone.
*/

bool QTimeZone::operator!=(const QTimeZone &other) const
{
    if (d && other.d)
        return (*d != *other.d);
    else
        return (d != other.d);
}

/*!
    Returns \c true if this time zone is valid.
*/

bool QTimeZone::isValid() const
{
    if (d)
        return d->isValid();
    else
        return false;
}

/*!
    Returns the IANA ID for the time zone.

    IANA IDs are used on all platforms.  On Windows these are translated
    from the Windows ID into the closest IANA ID for the time zone and country.
*/

QByteArray QTimeZone::id() const
{
    if (d)
        return d->id();
    else
        return QByteArray();
}

/*!
    Returns the country for the time zone.
*/

QLocale::Country QTimeZone::country() const
{
    if (isValid())
        return d->country();
    else
        return QLocale::AnyCountry;
}

/*!
    Returns any comment for the time zone.

    A comment may be provided by the host platform to assist users in
    choosing the correct time zone.  Depending on the platform this may not
    be localized.
*/

QString QTimeZone::comment() const
{
    if (isValid())
        return d->comment();
    else
        return QString();
}

/*!
    Returns the localized time zone display name at the given \a atDateTime
    for the given \a nameType in the given \a locale.  The \a nameType and
    \a locale requested may not be supported on all platforms, in which case
    the best available option will be returned.

    If the \a locale is not provided then the application default locale will
    be used.

    The display name may change depending on daylight time or historical
    events.

    \sa abbreviation()
*/

QString QTimeZone::displayName(const QDateTime &atDateTime, NameType nameType,
                               const QLocale &locale) const
{
    if (isValid())
        return d->displayName(atDateTime.toMSecsSinceEpoch(), nameType, locale);
    else
        return QString();
}

/*!
    Returns the localized time zone display name for the given \a timeType
    and \a nameType in the given \a locale. The \a nameType and \a locale
    requested may not be supported on all platforms, in which case the best
    available option will be returned.

    If the \a locale is not provided then the application default locale will
    be used.

    Where the time zone display names have changed over time then the most
    recent names will be used.

    \sa abbreviation()
*/

QString QTimeZone::displayName(TimeType timeType, NameType nameType,
                               const QLocale &locale) const
{
    if (isValid())
        return d->displayName(timeType, nameType, locale);
    else
        return QString();
}

/*!
    Returns the time zone abbreviation at the given \a atDateTime.  The
    abbreviation may change depending on daylight time or even
    historical events.

    Note that the abbreviation is not guaranteed to be unique to this time zone
    and should not be used in place of the ID or display name.

    \sa displayName()
*/

QString QTimeZone::abbreviation(const QDateTime &atDateTime) const
{
    if (isValid())
        return d->abbreviation(atDateTime.toMSecsSinceEpoch());
    else
        return QString();
}

/*!
    Returns the total effective offset at the given \a atDateTime, i.e. the
    number of seconds to add to UTC to obtain the local time.  This includes
    any daylight time offset that may be in effect, i.e. it is the sum of
    standardTimeOffset() and daylightTimeOffset() for the given datetime.

    For example, for the time zone "Europe/Berlin" the standard time offset is
    +3600 seconds and the daylight time offset is +3600 seconds.  During standard
    time offsetFromUtc() will return +3600 (UTC+01:00), and during daylight time
    it will return +7200 (UTC+02:00).

    \sa standardTimeOffset(), daylightTimeOffset()
*/

int QTimeZone::offsetFromUtc(const QDateTime &atDateTime) const
{
    if (isValid())
        return d->offsetFromUtc(atDateTime.toMSecsSinceEpoch());
    else
        return 0;
}

/*!
    Returns the standard time offset at the given \a atDateTime, i.e. the
    number of seconds to add to UTC to obtain the local Standard Time.  This
    excludes any daylight time offset that may be in effect.

    For example, for the time zone "Europe/Berlin" the standard time offset is
    +3600 seconds.  During both standard and daylight time offsetFromUtc() will
    return +3600 (UTC+01:00).

    \sa offsetFromUtc(), daylightTimeOffset()
*/

int QTimeZone::standardTimeOffset(const QDateTime &atDateTime) const
{
    if (isValid())
        return d->standardTimeOffset(atDateTime.toMSecsSinceEpoch());
    else
        return 0;
}

/*!
    Returns the daylight time offset at the given \a atDateTime, i.e. the
    number of seconds to add to the standard time offset to obtain the local
    daylight time.

    For example, for the time zone "Europe/Berlin" the daylight time offset
    is +3600 seconds.  During standard time daylightTimeOffset() will return
    0, and during daylight time it will return +3600.

    \sa offsetFromUtc(), standardTimeOffset()
*/

int QTimeZone::daylightTimeOffset(const QDateTime &atDateTime) const
{
    if (hasDaylightTime())
        return d->daylightTimeOffset(atDateTime.toMSecsSinceEpoch());
    else
        return 0;
}

/*!
    Returns \c true if the time zone has observed daylight time at any time.

    \sa isDaylightTime(), daylightTimeOffset()
*/

bool QTimeZone::hasDaylightTime() const
{
    if (isValid())
        return d->hasDaylightTime();
    else
        return false;
}

/*!
    Returns \c true if the given \a atDateTime is in daylight time.

    \sa hasDaylightTime(), daylightTimeOffset()
*/

bool QTimeZone::isDaylightTime(const QDateTime &atDateTime) const
{
    if (hasDaylightTime())
        return d->isDaylightTime(atDateTime.toMSecsSinceEpoch());
    else
        return false;
}

/*!
    Returns the effective offset details at the given \a forDateTime. This is
    the equivalent of calling offsetFromUtc(), abbreviation(), etc individually but is
    more efficient.

    \sa offsetFromUtc(), standardTimeOffset(), daylightTimeOffset(), abbreviation()
*/

QTimeZone::OffsetData QTimeZone::offsetData(const QDateTime &forDateTime) const
{
    if (hasTransitions())
        return d->toOffsetData(d->data(forDateTime.toMSecsSinceEpoch()));
    else
        return d->invalidOffsetData();
}

/*!
    Returns \c true if the system backend supports obtaining transitions.
*/

bool QTimeZone::hasTransitions() const
{
    if (isValid())
        return d->hasTransitions();
    else
        return false;
}

/*!
    Returns the first time zone Transition after the given \a afterDateTime.
    This is most useful when you have a Transition time and wish to find the
    Transition after it.

    If there is no transition after the given \a afterDateTime then an invalid
    OffsetData will be returned with an invalid QDateTime.

    The given \a afterDateTime is exclusive.

    \sa hasTransitions(), previousTransition(), transitions()
*/

QTimeZone::OffsetData QTimeZone::nextTransition(const QDateTime &afterDateTime) const
{
    if (hasTransitions())
        return d->toOffsetData(d->nextTransition(afterDateTime.toMSecsSinceEpoch()));
    else
        return d->invalidOffsetData();
}

/*!
    Returns the first time zone Transition before the given \a beforeDateTime.
    This is most useful when you have a Transition time and wish to find the
    Transition before it.

    If there is no transition before the given \a beforeDateTime then an invalid
    OffsetData will be returned with an invalid QDateTime.

    The given \a beforeDateTime is exclusive.

    \sa hasTransitions(), nextTransition(), transitions()
*/

QTimeZone::OffsetData QTimeZone::previousTransition(const QDateTime &beforeDateTime) const
{
    if (hasTransitions())
        return d->toOffsetData(d->previousTransition(beforeDateTime.toMSecsSinceEpoch()));
    else
        return d->invalidOffsetData();
}

/*!
    Returns a list of all time zone transitions between the given datetimes.

    The given \a fromDateTime and \a toDateTime are inclusive.

    \sa hasTransitions(), nextTransition(), previousTransition()
*/

QTimeZone::OffsetDataList QTimeZone::transitions(const QDateTime &fromDateTime,
                                                 const QDateTime &toDateTime) const
{
    OffsetDataList list;
    if (hasTransitions()) {
        QTimeZonePrivate::DataList plist = d->transitions(fromDateTime.toMSecsSinceEpoch(),
                                                          toDateTime.toMSecsSinceEpoch());
        foreach (const QTimeZonePrivate::Data &pdata, plist)
            list.append(d->toOffsetData(pdata));
    }
    return list;
}

// Static methods

/*!
    Returns the current system time zone IANA ID.

    On Windows this ID is translated from the Windows ID using an internal
    translation table and the user's selected country.  As a consequence there
    is a small chance any Windows install may have IDs not known by Qt, in
    which case "UTC" will be returned.
*/

QByteArray QTimeZone::systemTimeZoneId()
{
    return global_tz->backend->systemTimeZoneId();
}

/*!
    \since 5.5
    Returns a QTimeZone object that refers to the local system time, as
    specified by systemTimeZoneId().

    \sa utc()
*/
QTimeZone QTimeZone::systemTimeZone()
{
    return QTimeZone(QTimeZone::systemTimeZoneId());
}

/*!
    \since 5.5
    Returns a QTimeZone object that refers to UTC (Universal Time Coordinated).

    \sa systemTimeZone()
*/
QTimeZone QTimeZone::utc()
{
    return QTimeZone(QTimeZonePrivate::utcQByteArray());
}

/*!
    Returns \c true if a given time zone \a ianaId is available on this system.

    \sa availableTimeZoneIds()
*/

bool QTimeZone::isTimeZoneIdAvailable(const QByteArray &ianaId)
{
    // isValidId is not strictly required, but faster to weed out invalid
    // IDs as availableTimeZoneIds() may be slow
    return (QTimeZonePrivate::isValidId(ianaId) && (availableTimeZoneIds().contains(ianaId)));
}

/*!
    Returns a list of all available IANA time zone IDs on this system.

    \sa isTimeZoneIdAvailable()
*/

QList<QByteArray> QTimeZone::availableTimeZoneIds()
{
    QSet<QByteArray> set = QUtcTimeZonePrivate().availableTimeZoneIds()
                           + global_tz->backend->availableTimeZoneIds();
    QList<QByteArray> list = set.toList();
    std::sort(list.begin(), list.end());
    return list;
}

/*!
    Returns a list of all available IANA time zone IDs for a given \a country.

    As a special case, a \a country of Qt::AnyCountry returns those time zones
    that do not have any country related to them, such as UTC.  If you require
    a list of all time zone IDs for all countries then use the standard
    availableTimeZoneIds() method.

    \sa isTimeZoneIdAvailable()
*/

QList<QByteArray> QTimeZone::availableTimeZoneIds(QLocale::Country country)
{
    QSet<QByteArray> set = QUtcTimeZonePrivate().availableTimeZoneIds(country)
                           + global_tz->backend->availableTimeZoneIds(country);
    QList<QByteArray> list = set.toList();
    std::sort(list.begin(), list.end());
    return list;
}

/*!
    Returns a list of all available IANA time zone IDs with a given standard
    time offset of \a offsetSeconds.

    \sa isTimeZoneIdAvailable()
*/

QList<QByteArray> QTimeZone::availableTimeZoneIds(int offsetSeconds)
{
    QSet<QByteArray> set = QUtcTimeZonePrivate().availableTimeZoneIds(offsetSeconds)
                           + global_tz->backend->availableTimeZoneIds(offsetSeconds);
    QList<QByteArray> list = set.toList();
    std::sort(list.begin(), list.end());
    return list;
}

/*!
    Returns the Windows ID equivalent to the given \a ianaId.

    \sa windowsIdToDefaultIanaId(), windowsIdToIanaIds()
*/

QByteArray QTimeZone::ianaIdToWindowsId(const QByteArray &ianaId)
{
    return QTimeZonePrivate::ianaIdToWindowsId(ianaId);
}

/*!
    Returns the default IANA ID for a given \a windowsId.

    Because a Windows ID can cover several IANA IDs in several different
    countries, this function returns the most frequently used IANA ID with no
    regard for the country and should thus be used with care.  It is usually
    best to request the default for a specific country.

    \sa ianaIdToWindowsId(), windowsIdToIanaIds()
*/

QByteArray QTimeZone::windowsIdToDefaultIanaId(const QByteArray &windowsId)
{
    return QTimeZonePrivate::windowsIdToDefaultIanaId(windowsId);
}

/*!
    Returns the default IANA ID for a given \a windowsId and \a country.

    Because a Windows ID can cover several IANA IDs within a given country,
    the most frequently used IANA ID in that country is returned.

    As a special case, QLocale::AnyCountry returns the default of those IANA IDs
    that do not have any specific country.

    \sa ianaIdToWindowsId(), windowsIdToIanaIds()
*/

QByteArray QTimeZone::windowsIdToDefaultIanaId(const QByteArray &windowsId,
                                                QLocale::Country country)
{
    return QTimeZonePrivate::windowsIdToDefaultIanaId(windowsId, country);
}

/*!
    Returns all the IANA IDs for a given \a windowsId.

    The returned list is sorted alphabetically.

    \sa ianaIdToWindowsId(), windowsIdToDefaultIanaId()
*/

QList<QByteArray> QTimeZone::windowsIdToIanaIds(const QByteArray &windowsId)
{
    return QTimeZonePrivate::windowsIdToIanaIds(windowsId);
}

/*!
    Returns all the IANA IDs for a given \a windowsId and \a country.

    As a special case QLocale::AnyCountry returns those IANA IDs that do
    not have any specific country.

    The returned list is in order of frequency of usage, i.e. larger zones
    within a country are listed first.

    \sa ianaIdToWindowsId(), windowsIdToDefaultIanaId()
*/

QList<QByteArray> QTimeZone::windowsIdToIanaIds(const QByteArray &windowsId,
                                                    QLocale::Country country)
{
    return QTimeZonePrivate::windowsIdToIanaIds(windowsId, country);
}

#ifndef QT_NO_DATASTREAM
QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz)
{
    tz.d->serialize(ds);
    return ds;
}

QDataStream &operator>>(QDataStream &ds, QTimeZone &tz)
{
    QString ianaId;
    ds >> ianaId;
    if (ianaId == QLatin1String("OffsetFromUtc")) {
        int utcOffset;
        QString name;
        QString abbreviation;
        int country;
        QString comment;
        ds >> ianaId >> utcOffset >> name >> abbreviation >> country >> comment;
        tz = QTimeZone(ianaId.toUtf8(), utcOffset, name, abbreviation, (QLocale::Country) country, comment);
    } else {
        tz = QTimeZone(ianaId.toUtf8());
    }
    return ds;
}
#endif // QT_NO_DATASTREAM

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QTimeZone &tz)
{
    QDebugStateSaver saver(dbg);
    //TODO Include backend and data version details?
    dbg.nospace() << "QTimeZone(" << QString::fromUtf8(tz.id()) << ')';
    return dbg;
}
#endif

QT_END_NAMESPACE