forked from HowardHinnant/date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tz.h
1566 lines (1352 loc) · 45.7 KB
/
tz.h
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
991
992
993
994
995
996
997
998
999
1000
#ifndef TZ_H
#define TZ_H
// The MIT License (MIT)
//
// Copyright (c) 2015, 2016, 2017 Howard Hinnant
// Copyright (c) 2017 Jiangang Zhuang
// Copyright (c) 2017 Aaron Bishop
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Our apologies. When the previous paragraph was written, lowercase had not yet
// been invented (that would involve another several millennia of evolution).
// We did not mean to shout.
// Get more recent database at http://www.iana.org/time-zones
// The notion of "current timezone" is something the operating system is expected to "just
// know". How it knows this is system specific. It's often a value set by the user at OS
// installation time and recorded by the OS somewhere. On Linux and Mac systems the current
// timezone name is obtained by looking at the name or contents of a particular file on
// disk. On Windows the current timezone name comes from the registry. In either method,
// there is no guarantee that the "native" current timezone name obtained will match any
// of the "Standard" names in this library's "database". On Linux, the names usually do
// seem to match so mapping functions to map from native to "Standard" are typically not
// required. On Windows, the names are never "Standard" so mapping is always required.
// Technically any OS may use the mapping process but currently only Windows does use it.
#ifndef USE_OS_TZDB
# define USE_OS_TZDB 0
#endif
#ifndef HAS_REMOTE_API
# if USE_OS_TZDB == 0
# ifdef _WIN32
# define HAS_REMOTE_API 0
# else
# define HAS_REMOTE_API 1
# endif
# else // HAS_REMOTE_API makes no since when using the OS timezone database
# define HAS_REMOTE_API 0
# endif
#endif
static_assert(!(USE_OS_TZDB && HAS_REMOTE_API),
"USE_OS_TZDB and HAS_REMOTE_API can not be used together");
#ifndef AUTO_DOWNLOAD
# define AUTO_DOWNLOAD HAS_REMOTE_API
#endif
static_assert(HAS_REMOTE_API == 0 ? AUTO_DOWNLOAD == 0 : true,
"AUTO_DOWNLOAD can not be turned on without HAS_REMOTE_API");
#ifndef USE_SHELL_API
# define USE_SHELL_API 1
#endif
#if USE_OS_TZDB
# ifdef _WIN32
# error "USE_OS_TZDB can not be used on Windows"
# endif
# ifndef MISSING_LEAP_SECONDS
# ifdef __APPLE__
# define MISSING_LEAP_SECONDS 1
# else
# define MISSING_LEAP_SECONDS 0
# endif
# endif
#else
# define MISSING_LEAP_SECONDS 0
#endif
#ifndef HAS_DEDUCTION_GUIDES
# if __cplusplus >= 201703
# define HAS_DEDUCTION_GUIDES 1
# else
# define HAS_DEDUCTION_GUIDES 0
# endif
#endif // HAS_DEDUCTION_GUIDES
#include "date.h"
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#include "tz_private.h"
#endif
#include <algorithm>
#include <cassert>
#include <chrono>
#include <istream>
#include <locale>
#include <memory>
#include <mutex>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#ifdef _WIN32
# ifdef DATE_BUILD_DLL
# define DATE_API __declspec(dllexport)
# elif defined(DATE_BUILD_LIB)
# define DATE_API
# else
# define DATE_API __declspec(dllimport)
# endif
#else
# define DATE_API
#endif
namespace date
{
enum class choose {earliest, latest};
namespace detail
{
struct undocumented;
}
class nonexistent_local_time
: public std::runtime_error
{
public:
template <class Duration>
nonexistent_local_time(local_time<Duration> tp, local_seconds first,
const std::string& first_abbrev, local_seconds last,
const std::string& last_abbrev, sys_seconds time_sys);
private:
template <class Duration>
static
std::string
make_msg(local_time<Duration> tp,
local_seconds first, const std::string& first_abbrev,
local_seconds last, const std::string& last_abbrev,
sys_seconds time_sys);
};
template <class Duration>
inline
nonexistent_local_time::nonexistent_local_time(local_time<Duration> tp,
local_seconds begin,
const std::string& first_abbrev,
local_seconds end,
const std::string& last_abbrev,
sys_seconds time_sys)
: std::runtime_error(make_msg(tp, begin, first_abbrev, end, last_abbrev, time_sys))
{}
template <class Duration>
std::string
nonexistent_local_time::make_msg(local_time<Duration> tp, local_seconds begin,
const std::string& first_abbrev, local_seconds end,
const std::string& last_abbrev, sys_seconds time_sys)
{
using namespace date;
std::ostringstream os;
os << tp << " is in a gap between\n"
<< begin << ' ' << first_abbrev << " and\n"
<< end << ' ' << last_abbrev
<< " which are both equivalent to\n"
<< time_sys << " UTC";
return os.str();
}
class ambiguous_local_time
: public std::runtime_error
{
public:
template <class Duration>
ambiguous_local_time(local_time<Duration> tp, std::chrono::seconds first_offset,
const std::string& first_abbrev,
std::chrono::seconds second_offset,
const std::string& second_abbrev);
private:
template <class Duration>
static
std::string
make_msg(local_time<Duration> tp,
std::chrono::seconds first_offset, const std::string& first_abbrev,
std::chrono::seconds second_offset, const std::string& second_abbrev);
};
template <class Duration>
inline
ambiguous_local_time::ambiguous_local_time(
local_time<Duration> tp,
std::chrono::seconds first_offset,
const std::string& first_abbrev,
std::chrono::seconds second_offset,
const std::string& second_abbrev)
: std::runtime_error(make_msg(tp, first_offset, first_abbrev, second_offset,
second_abbrev))
{}
template <class Duration>
std::string
ambiguous_local_time::make_msg(local_time<Duration> tp,
std::chrono::seconds first_offset,
const std::string& first_abbrev,
std::chrono::seconds second_offset,
const std::string& second_abbrev)
{
using namespace date;
std::ostringstream os;
os << tp << " is ambiguous. It could be\n"
<< tp << ' ' << first_abbrev << " == "
<< tp - first_offset << " UTC or\n"
<< tp << ' ' << second_abbrev << " == "
<< tp - second_offset << " UTC";
return os.str();
}
struct sys_info
{
sys_seconds begin;
sys_seconds end;
std::chrono::seconds offset;
std::chrono::minutes save;
std::string abbrev;
};
template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const sys_info& r)
{
os << r.begin << '\n';
os << r.end << '\n';
os << make_time(r.offset) << "\n";
os << make_time(r.save) << "\n";
os << r.abbrev << '\n';
return os;
}
struct local_info
{
enum {unique, nonexistent, ambiguous} result;
sys_info first;
sys_info second;
};
template<class CharT, class Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const local_info& r)
{
if (r.result == local_info::nonexistent)
os << "nonexistent between\n";
else if (r.result == local_info::ambiguous)
os << "ambiguous between\n";
os << r.first;
if (r.result != local_info::unique)
{
os << "and\n";
os << r.second;
}
return os;
}
class time_zone;
DATE_API const time_zone* locate_zone(const std::string& tz_name);
DATE_API const time_zone* current_zone();
template <class Duration>
class zoned_time
{
public:
using duration = typename std::common_type<Duration, std::chrono::seconds>::type;
private:
const time_zone* zone_;
sys_time<duration> tp_;
public:
zoned_time();
zoned_time(const sys_time<Duration>& st);
explicit zoned_time(const time_zone* z);
explicit zoned_time(const std::string& name);
template <class Duration2,
class = typename std::enable_if
<
std::is_convertible<sys_time<Duration2>,
sys_time<Duration>>::value
>::type>
zoned_time(const zoned_time<Duration2>& zt) NOEXCEPT;
zoned_time(const time_zone* z, const local_time<Duration>& tp);
zoned_time(const std::string& name, const local_time<Duration>& tp);
zoned_time(const char* name, const local_time<Duration>& tp);
zoned_time(const time_zone* z, const local_time<Duration>& tp, choose c);
zoned_time(const std::string& name, const local_time<Duration>& tp, choose c);
zoned_time(const char* name, const local_time<Duration>& tp, choose c);
zoned_time(const time_zone* z, const zoned_time<Duration>& zt);
zoned_time(const std::string& name, const zoned_time<Duration>& zt);
zoned_time(const char* name, const zoned_time<Duration>& zt);
zoned_time(const time_zone* z, const zoned_time<Duration>& zt, choose);
zoned_time(const std::string& name, const zoned_time<Duration>& zt, choose);
zoned_time(const char* name, const zoned_time<Duration>& zt, choose);
zoned_time(const time_zone* z, const sys_time<Duration>& st);
zoned_time(const std::string& name, const sys_time<Duration>& st);
zoned_time(const char* name, const sys_time<Duration>& st);
zoned_time& operator=(const sys_time<Duration>& st);
zoned_time& operator=(const local_time<Duration>& ut);
explicit operator sys_time<duration>() const;
explicit operator local_time<duration>() const;
const time_zone* get_time_zone() const;
local_time<duration> get_local_time() const;
sys_time<duration> get_sys_time() const;
sys_info get_info() const;
template <class Duration1, class Duration2>
friend
bool
operator==(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y);
template <class CharT, class Traits, class Duration1>
friend
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration1>& t);
private:
template <class D> friend class zoned_time;
};
using zoned_seconds = zoned_time<std::chrono::seconds>;
#if HAS_DEDUCTION_GUIDES
template <class Duration>
zoned_time(sys_time<Duration>)
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
template <class Zone, class Duration>
zoned_time(Zone, sys_time<Duration>)
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
template <class Zone, class Duration>
zoned_time(Zone, local_time<Duration>, choose = choose::earliest)
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
template <class Zone, class Duration>
zoned_time(Zone, zoned_time<Duration>, choose = choose::earliest)
-> zoned_time<std::common_type_t<Duration, std::chrono::seconds>>;
#endif // HAS_DEDUCTION_GUIDES
template <class Duration1, class Duration2>
inline
bool
operator==(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y)
{
return x.zone_ == y.zone_ && x.tp_ == y.tp_;
}
template <class Duration1, class Duration2>
inline
bool
operator!=(const zoned_time<Duration1>& x, const zoned_time<Duration2>& y)
{
return !(x == y);
}
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
namespace detail
{
# if USE_OS_TZDB
struct transition;
struct expanded_ttinfo;
# else // !USE_OS_TZDB
struct zonelet;
class Rule;
# endif // !USE_OS_TZDB
}
#endif // !defined(_MSC_VER) || (_MSC_VER >= 1900)
class time_zone
{
private:
std::string name_;
#if USE_OS_TZDB
std::vector<detail::transition> transitions_;
std::vector<detail::expanded_ttinfo> ttinfos_;
#else // !USE_OS_TZDB
std::vector<detail::zonelet> zonelets_;
#endif // !USE_OS_TZDB
std::unique_ptr<std::once_flag> adjusted_;
public:
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
time_zone(time_zone&&) = default;
time_zone& operator=(time_zone&&) = default;
#else // defined(_MSC_VER) && (_MSC_VER < 1900)
time_zone(time_zone&& src);
time_zone& operator=(time_zone&& src);
#endif // defined(_MSC_VER) && (_MSC_VER < 1900)
DATE_API explicit time_zone(const std::string& s, detail::undocumented);
const std::string& name() const NOEXCEPT;
template <class Duration> sys_info get_info(sys_time<Duration> st) const;
template <class Duration> local_info get_info(local_time<Duration> tp) const;
template <class Duration>
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys(local_time<Duration> tp) const;
template <class Duration>
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys(local_time<Duration> tp, choose z) const;
template <class Duration>
local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_local(sys_time<Duration> tp) const;
friend bool operator==(const time_zone& x, const time_zone& y) NOEXCEPT;
friend bool operator< (const time_zone& x, const time_zone& y) NOEXCEPT;
friend DATE_API std::ostream& operator<<(std::ostream& os, const time_zone& z);
#if !USE_OS_TZDB
DATE_API void add(const std::string& s);
#endif // !USE_OS_TZDB
private:
DATE_API sys_info get_info_impl(sys_seconds tp) const;
DATE_API local_info get_info_impl(local_seconds tp) const;
template <class Duration>
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys_impl(local_time<Duration> tp, choose z, std::false_type) const;
template <class Duration>
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
to_sys_impl(local_time<Duration> tp, choose, std::true_type) const;
#if USE_OS_TZDB
DATE_API void init() const;
DATE_API void init_impl();
DATE_API sys_info
load_sys_info(std::vector<detail::transition>::const_iterator i) const;
template <class TimeType>
DATE_API void
load_data(std::istream& inf, std::int32_t tzh_leapcnt, std::int32_t tzh_timecnt,
std::int32_t tzh_typecnt, std::int32_t tzh_charcnt);
#else // !USE_OS_TZDB
DATE_API sys_info get_info_impl(sys_seconds tp, int timezone) const;
DATE_API void adjust_infos(const std::vector<detail::Rule>& rules);
DATE_API void parse_info(std::istream& in);
#endif // !USE_OS_TZDB
};
#if defined(_MSC_VER) && (_MSC_VER < 1900)
inline
time_zone::time_zone(time_zone&& src)
: name_(std::move(src.name_))
, zonelets_(std::move(src.zonelets_))
, adjusted_(std::move(src.adjusted_))
{}
inline
time_zone&
time_zone::operator=(time_zone&& src)
{
name_ = std::move(src.name_);
zonelets_ = std::move(src.zonelets_);
adjusted_ = std::move(src.adjusted_);
return *this;
}
#endif // defined(_MSC_VER) && (_MSC_VER < 1900)
inline
const std::string&
time_zone::name() const NOEXCEPT
{
return name_;
}
template <class Duration>
inline
sys_info
time_zone::get_info(sys_time<Duration> st) const
{
using namespace std::chrono;
return get_info_impl(date::floor<seconds>(st));
}
template <class Duration>
inline
local_info
time_zone::get_info(local_time<Duration> tp) const
{
using namespace std::chrono;
return get_info_impl(date::floor<seconds>(tp));
}
template <class Duration>
inline
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
time_zone::to_sys(local_time<Duration> tp) const
{
return to_sys_impl(tp, choose{}, std::true_type{});
}
template <class Duration>
inline
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
time_zone::to_sys(local_time<Duration> tp, choose z) const
{
return to_sys_impl(tp, z, std::false_type{});
}
template <class Duration>
inline
local_time<typename std::common_type<Duration, std::chrono::seconds>::type>
time_zone::to_local(sys_time<Duration> tp) const
{
using LT = local_time<typename std::common_type<Duration, std::chrono::seconds>::type>;
auto i = get_info(tp);
return LT{(tp + i.offset).time_since_epoch()};
}
inline bool operator==(const time_zone& x, const time_zone& y) NOEXCEPT {return x.name_ == y.name_;}
inline bool operator< (const time_zone& x, const time_zone& y) NOEXCEPT {return x.name_ < y.name_;}
inline bool operator!=(const time_zone& x, const time_zone& y) NOEXCEPT {return !(x == y);}
inline bool operator> (const time_zone& x, const time_zone& y) NOEXCEPT {return y < x;}
inline bool operator<=(const time_zone& x, const time_zone& y) NOEXCEPT {return !(y < x);}
inline bool operator>=(const time_zone& x, const time_zone& y) NOEXCEPT {return !(x < y);}
template <class Duration>
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
time_zone::to_sys_impl(local_time<Duration> tp, choose z, std::false_type) const
{
using namespace date;
using namespace std::chrono;
auto i = get_info(tp);
if (i.result == local_info::nonexistent)
{
return i.first.end;
}
else if (i.result == local_info::ambiguous)
{
if (z == choose::latest)
return sys_time<Duration>{tp.time_since_epoch()} - i.second.offset;
}
return sys_time<Duration>{tp.time_since_epoch()} - i.first.offset;
}
template <class Duration>
sys_time<typename std::common_type<Duration, std::chrono::seconds>::type>
time_zone::to_sys_impl(local_time<Duration> tp, choose, std::true_type) const
{
using namespace date;
using namespace std::chrono;
auto i = get_info(tp);
if (i.result == local_info::nonexistent)
{
auto prev_end = local_seconds{i.first.end.time_since_epoch()} +
i.first.offset;
auto next_begin = local_seconds{i.second.begin.time_since_epoch()} +
i.second.offset;
throw nonexistent_local_time(tp, prev_end, i.first.abbrev,
next_begin, i.second.abbrev, i.first.end);
}
else if (i.result == local_info::ambiguous)
{
throw ambiguous_local_time(tp, i.first.offset, i.first.abbrev,
i.second.offset, i.second.abbrev);
}
return sys_time<Duration>{tp.time_since_epoch()} - i.first.offset;
}
#if !USE_OS_TZDB
class link
{
private:
std::string name_;
std::string target_;
public:
DATE_API explicit link(const std::string& s);
const std::string& name() const {return name_;}
const std::string& target() const {return target_;}
friend bool operator==(const link& x, const link& y) {return x.name_ == y.name_;}
friend bool operator< (const link& x, const link& y) {return x.name_ < y.name_;}
friend DATE_API std::ostream& operator<<(std::ostream& os, const link& x);
};
inline bool operator!=(const link& x, const link& y) {return !(x == y);}
inline bool operator> (const link& x, const link& y) {return y < x;}
inline bool operator<=(const link& x, const link& y) {return !(y < x);}
inline bool operator>=(const link& x, const link& y) {return !(x < y);}
#endif // !USE_OS_TZDB
#if !MISSING_LEAP_SECONDS
class leap
{
private:
sys_seconds date_;
public:
#if USE_OS_TZDB
DATE_API explicit leap(const sys_seconds& s, detail::undocumented);
#else
DATE_API explicit leap(const std::string& s, detail::undocumented);
#endif
sys_seconds date() const {return date_;}
friend bool operator==(const leap& x, const leap& y) {return x.date_ == y.date_;}
friend bool operator< (const leap& x, const leap& y) {return x.date_ < y.date_;}
template <class Duration>
friend
bool
operator==(const leap& x, const sys_time<Duration>& y)
{
return x.date_ == y;
}
template <class Duration>
friend
bool
operator< (const leap& x, const sys_time<Duration>& y)
{
return x.date_ < y;
}
template <class Duration>
friend
bool
operator< (const sys_time<Duration>& x, const leap& y)
{
return x < y.date_;
}
friend DATE_API std::ostream& operator<<(std::ostream& os, const leap& x);
};
inline bool operator!=(const leap& x, const leap& y) {return !(x == y);}
inline bool operator> (const leap& x, const leap& y) {return y < x;}
inline bool operator<=(const leap& x, const leap& y) {return !(y < x);}
inline bool operator>=(const leap& x, const leap& y) {return !(x < y);}
template <class Duration>
inline
bool
operator==(const sys_time<Duration>& x, const leap& y)
{
return y == x;
}
template <class Duration>
inline
bool
operator!=(const leap& x, const sys_time<Duration>& y)
{
return !(x == y);
}
template <class Duration>
inline
bool
operator!=(const sys_time<Duration>& x, const leap& y)
{
return !(x == y);
}
template <class Duration>
inline
bool
operator> (const leap& x, const sys_time<Duration>& y)
{
return y < x;
}
template <class Duration>
inline
bool
operator> (const sys_time<Duration>& x, const leap& y)
{
return y < x;
}
template <class Duration>
inline
bool
operator<=(const leap& x, const sys_time<Duration>& y)
{
return !(y < x);
}
template <class Duration>
inline
bool
operator<=(const sys_time<Duration>& x, const leap& y)
{
return !(y < x);
}
template <class Duration>
inline
bool
operator>=(const leap& x, const sys_time<Duration>& y)
{
return !(x < y);
}
template <class Duration>
inline
bool
operator>=(const sys_time<Duration>& x, const leap& y)
{
return !(x < y);
}
#endif // !MISSING_LEAP_SECONDS
#ifdef _WIN32
namespace detail
{
// The time zone mapping is modelled after this data file:
// http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml
// and the field names match the element names from the mapZone element
// of windowsZones.xml.
// The website displays this file here:
// http://www.unicode.org/cldr/charts/latest/supplemental/zone_tzid.html
// The html view is sorted before being displayed but is otherwise the same
// There is a mapping between the os centric view (in this case windows)
// the html displays uses and the generic view the xml file.
// That mapping is this:
// display column "windows" -> xml field "other".
// display column "region" -> xml field "territory".
// display column "tzid" -> xml field "type".
// This structure uses the generic terminology because it could be
// used to to support other os/native name conversions, not just windows,
// and using the same generic names helps retain the connection to the
// origin of the data that we are using.
struct timezone_mapping
{
timezone_mapping(const char* other, const char* territory, const char* type)
: other(other), territory(territory), type(type)
{
}
timezone_mapping() = default;
std::string other;
std::string territory;
std::string type;
};
} // detail
#endif // _WIN32
struct TZ_DB
{
std::string version = "unknown";
std::vector<time_zone> zones;
#if !USE_OS_TZDB
std::vector<link> links;
#endif
#if !MISSING_LEAP_SECONDS
std::vector<leap> leaps;
#endif
#if !USE_OS_TZDB
std::vector<detail::Rule> rules;
#endif
#ifdef _WIN32
std::vector<detail::timezone_mapping> mappings;
#endif
TZ_DB() = default;
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
TZ_DB(TZ_DB&&) = default;
TZ_DB& operator=(TZ_DB&&) = default;
#else // defined(_MSC_VER) && (_MSC_VER < 1900)
TZ_DB(TZ_DB&& src)
: version(std::move(src.version))
, zones(std::move(src.zones))
, links(std::move(src.links))
, leaps(std::move(src.leaps))
, rules(std::move(src.rules))
, mappings(std::move(src.mappings))
{}
TZ_DB& operator=(TZ_DB&& src)
{
version = std::move(src.version);
zones = std::move(src.zones);
links = std::move(src.links);
leaps = std::move(src.leaps);
rules = std::move(src.rules);
mappings = std::move(src.mappings);
return *this;
}
#endif // defined(_MSC_VER) && (_MSC_VER < 1900)
};
DATE_API std::ostream&
operator<<(std::ostream& os, const TZ_DB& db);
DATE_API const TZ_DB& get_tzdb();
#if !USE_OS_TZDB
DATE_API const TZ_DB& reload_tzdb();
DATE_API void set_install(const std::string& install);
#endif // !USE_OS_TZDB
#if HAS_REMOTE_API
DATE_API std::string remote_version();
DATE_API bool remote_download(const std::string& version);
DATE_API bool remote_install(const std::string& version);
#endif
DATE_API const time_zone* locate_zone(const std::string& tz_name);
DATE_API const time_zone* current_zone();
// zoned_time
template <class Duration>
inline
zoned_time<Duration>::zoned_time()
: zone_(locate_zone("UTC"))
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const sys_time<Duration>& st)
: zone_(locate_zone("UTC"))
, tp_(st)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const time_zone* z)
: zone_(z)
{assert(zone_ != nullptr);}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const std::string& name)
: zoned_time(locate_zone(name))
{}
template <class Duration>
template <class Duration2, class>
inline
zoned_time<Duration>::zoned_time(const zoned_time<Duration2>& zt) NOEXCEPT
: zone_(zt.zone_)
, tp_(zt.tp_)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const time_zone* z, const local_time<Duration>& t)
: zone_(z)
, tp_(z->to_sys(t))
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const std::string& name, const local_time<Duration>& t)
: zoned_time(locate_zone(name), t)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const char* name, const local_time<Duration>& t)
: zoned_time(locate_zone(name), t)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const time_zone* z, const local_time<Duration>& t,
choose c)
: zone_(z)
, tp_(z->to_sys(t, c))
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const std::string& name, const local_time<Duration>& t,
choose c)
: zoned_time(locate_zone(name), t, c)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const char* name, const local_time<Duration>& t,
choose c)
: zoned_time(locate_zone(name), t, c)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const time_zone* z, const zoned_time<Duration>& zt)
: zone_(z)
, tp_(zt.tp_)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const std::string& name, const zoned_time<Duration>& zt)
: zoned_time(locate_zone(name), zt)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const char* name, const zoned_time<Duration>& zt)
: zoned_time(locate_zone(name), zt)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const time_zone* z, const zoned_time<Duration>& zt, choose)
: zoned_time(z, zt)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const std::string& name,
const zoned_time<Duration>& zt, choose c)
: zoned_time(locate_zone(name), zt, c)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const char* name,
const zoned_time<Duration>& zt, choose c)
: zoned_time(locate_zone(name), zt, c)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const time_zone* z, const sys_time<Duration>& st)
: zone_(z)
, tp_(st)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const std::string& name, const sys_time<Duration>& st)
: zoned_time(locate_zone(name), st)
{}
template <class Duration>
inline
zoned_time<Duration>::zoned_time(const char* name, const sys_time<Duration>& st)
: zoned_time(locate_zone(name), st)
{}
template <class Duration>
inline
zoned_time<Duration>&
zoned_time<Duration>::operator=(const sys_time<Duration>& st)
{
tp_ = st;
return *this;