-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtz.html
2113 lines (1779 loc) · 73.4 KB
/
tz.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Time Zone Database Parser redirect</title>
<META http-equiv="refresh" content="5;URL=http://howardhinnant.github.io/date/tz.html">
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {color:#00A000}
del {color:#A00000}
code {white-space:pre;}
</style>
</head>
<body>
<center>
The contents you are looking for have moved. You will be redirected to the new
location automatically in 5 seconds. Please bookmark the correct page at
<a href="http://howardhinnant.github.io/date/tz.html">http://howardhinnant.github.io/date/tz.html</a>
</center>
<!--
<address align=right>
<br/>
<br/>
<a href="mailto:howard.hinnant@gmail.com">Howard E. Hinnant</a><br/>
2016-04-09<br/>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"> <img alt="Creative
Commons License" style="border-width:0"
src="http://i.creativecommons.org/l/by/4.0/80x15.png" /></a><br /> This work is licensed
under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative
Commons Attribution 4.0 International License</a>.
</address>
<hr/>
<h1 align=center>Time Zone Database Parser</h1>
<h2>Contents</h2>
<ul>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#Overview">Overview</a></li>
<li><a href="#Synopsis">Synopsis</a></li>
<li><a href="#Description">Description</a>
<ul>
<li><a href="#TheDatabase">The Database</a></li>
<li><a href="#remote">The remote API</a></li>
<li><a href="#Zone">Zone</a>
<ul>
<li><a href="#Infrastructure">Infrastructure</a></li>
<li><a href="#ZoneContinued">Zone continued</a></li>
</ul>
</li>
<li><a href="#flightexample1">Flight Example</a></li>
<li><a href="#utc_clock">utc_clock</a></li>
<li><a href="#flightexample2">Flight Example with leap seconds</a></li>
<li><a href="#Formatting">Formatting</a></li>
<li><a href="#Parsing">Parsing</a></li>
<li><a href="#ThreadSafety">Thread Safety</a></li>
</ul>
</li>
<li><a href="#Installation">Installation — this is where you find the implementation</a></li>
<li><a href="#Acknowledgements">Acknowledgements</a></li>
</ul>
<a name="Introduction"></a><h2>Introduction</h2>
<p>
I had just completed writing <a href="date_v2.html"><code>date</code></a>, which is a
library for extending <code><chrono></code> into the realm of calendars, and I was
looking around for the most challenging date time problem I could find with which I could
demonstrate the power of this new library. "I know," I said to myself, "I'll handle all
the world's time zones, and maybe even leap seconds!" Thus began my journey into a
rabbit hole which I knew existed, but had never truly appreciated the intricacies of.
</p>
<p>
This library adds timezone and leap second support to this <a href="date_v2.html">date</a>
library. This is a separate library from <a href="date_v2.html"><code>date</code></a>
because many clients of <a href="date_v2.html"><code>date</code></a> do not need timezone
nor leap second support, and this support does not come for free (though the cost is quite
reasonable).
</p>
<p>
This library is a <b>complete</b> parser of the <a
href="http://www.iana.org/time-zones">IANA Time Zone Database</a>. This database contains
timezone information that represents the history of local time for many representative
locations around the globe. It is updated periodically to reflect changes made by
political bodies to time zone boundaries, UTC offsets, and daylight-saving rules. The
database also maintains a list of leap seconds from 1972 through the present.
</p>
<p>
The <a href="http://www.iana.org/time-zones">IANA Time Zone Database</a> contains four
specific types of data:
</p>
<ol>
<li><p>
Zone: A geographic location with a human-readable name (e.g. "America/New_York") which
specifies the offset from UTC and an abbreviation for the zone. This data includes
daylight saving rules, if applicable, for the zone. This data is not only the rules
currently in effect for the region, but also includes specifications dating back to at
least 1970, and in most cases dating back to the mid 1800's (when uniform time was
first introduced across regions larger than individual towns and cities).
</p></li>
<li><p>
Rule: A specification for a single daylight-saving rule. This helps implement and
consolidate the specifications of Zones.
</p></li>
<li><p>
Link: This is an alternative name for a Zone.
</p></li>
<li><p>
Leap: The date of the insertion of a leap second.
</p></li>
</ol>
<p>
The library documented herein provides access to <i>all</i> of this data, and offers
efficient and convenient ways to compute with it. And this is all done based on the <a
href="date_v2.html"><code>date</code></a> library, which in turn is based on the C++11/14
<code><chrono></code> library. So once you've learned those fundamental libraries,
the learning curve for this library is greatly eased.
</p>
<a name="Overview"></a><h2>Overview</h2>
<p>
Like <a href="date_v2.html"><code>date</code></a>, this library revolves around
<code>std::chrono::system_clock</code>. Thus it is important to understand the properties
of this foundation. <code>std::chrono::system_clock</code> has nested types including
<code>time_point</code> and <code>duration</code>, and a static function called
<code>now</code> which returns a <code>time_point</code>. This <code>time_point</code>
measures time against some unspecified epoch using the units of <code>duration</code>.
</p>
<blockquote><pre>
namespace std { namespace chrono {
class system_clock
{
public:
using duration = microseconds;
using time_point = chrono::time_point<system_clock>;
// other types ...
static time_point now() noexcept;
// other member functions ...
};
}} // namespace std::chrono
</pre></blockquote>
<p>
The use of <code>microseconds</code> above for the <code>duration</code> type is just
an example. This is what is used for <a href="http://libcxx.llvm.org">libc++</a> on
<a href="http://www.apple.com/osx/">OS X</a>. Other implementations may use
other units such as <code>nanoseconds</code>. But one unspecified property that all
implementations of <code>std::chrono::system_clock</code> have in common is that they
all model <a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a>.
</p>
<p>
<a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a> counts the number of
seconds since 1970-01-01 00:00:00 UTC, except that leap seconds are ignored in the count.
An interesting and useful property of
<a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a> is that it treats leap
seconds as nothing more than a typical clock correction. That is, no clock keeps perfect
time, not even the one in your computer. And several times a day your computer will ask
another computer what time it is, and typically correct itself by a small fraction of a
second.
</p>
<p>
When a leap second occurs, instead of counting an extra second,
<a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a> reacts by saying, oh, I'm
off by a second, I'll slow down or speed up to correct. Companies such as Google will
"smear" the application of a leap second over a period of hours, letting their
<a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a> counters adjust by
milliseconds, or even microseconds at a time, so that the insertion of a leap second is
virtually undetectable to applications running on the computer.
</p>
<p>
It is this de-facto standard based on
<a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a> that allows
<a href="date_v2.html"><code>date</code></a> to portably convert a count of microseconds
(for example) into field-based structures containing <code>year/month/day</code> and
<code>hours::minutes::seconds.microseconds</code>. And when you need to further convert
the <code>system_clock::time_point</code> into a local time, and/or take leap seconds into
account, the library documented herein handles it by accessing your local copy of the
<a href="http://www.iana.org/time-zones">IANA Time Zone Database</a>.
</p>
<a name="Synopsis"></a><h2>Synopsis</h2>
<p>
This synopsis provides nothing but a quick overview of the documented API of this library.
If you don't see it in the synopsis below, it is not a documented part of this library.
</p>
<blockquote><pre>
namespace date
{
using second_point = std::chrono::time_point<std::chrono::system_clock,
std::chrono::seconds>;
struct Info
{
second_point begin;
second_point end;
std::chrono::seconds offset;
std::chrono::minutes save;
std::string abbrev;
};
std::ostream&
operator<<(std::ostream& os, const Info& r);
enum class tz {utc, local};
enum class choose {earliest, latest};
class nonexistent_local_time
: public std::runtime_error
{
public:
};
class ambiguous_local_time
: public std::runtime_error
{
public:
};
class Zone
{
public:
const std::string& name() const;
template <class Rep, class Period>
std::pair
<
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>,
std::string
>
to_local(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp) const;
template <class Rep, class Period>
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>
to_sys(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp) const;
template <class Rep, class Period>
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>
to_sys(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp,
choose z) const;
template <class Rep, class Period>
Info
get_info(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp,
tz timezone) const;
};
const Zone* locate_zone(const std::string& tz_name);
const Zone* current_zone();
bool operator==(const Zone& x, const Zone& y);
bool operator!=(const Zone& x, const Zone& y);
bool operator< (const Zone& x, const Zone& y);
bool operator> (const Zone& x, const Zone& y);
bool operator<=(const Zone& x, const Zone& y);
bool operator>=(const Zone& x, const Zone& y);
std::ostream& operator<<(std::ostream& os, const Zone& z);
class Link
{
public:
const std::string& name() const;
const std::string& target() const;
};
bool operator==(const Link& x, const Link& y);
bool operator!=(const Link& x, const Link& y);
bool operator< (const Link& x, const Link& y);
bool operator> (const Link& x, const Link& y);
bool operator<=(const Link& x, const Link& y);
bool operator>=(const Link& x, const Link& y);
std::ostream& operator<<(std::ostream& os, const Link& x);
class Leap
{
public:
second_point date() const;
};
bool operator==(const Leap& x, const Leap& y);
bool operator!=(const Leap& x, const Leap& y);
bool operator< (const Leap& x, const Leap& y);
bool operator> (const Leap& x, const Leap& y);
bool operator<=(const Leap& x, const Leap& y);
bool operator>=(const Leap& x, const Leap& y);
std::ostream& operator<<(std::ostream& os, const Leap& x);
template <class Duration>
bool
operator==(const Leap& x,
const std::chrono::time_point<std::chrono::system_clock, Duration>& y);
template <class Duration>
bool
operator==(const std::chrono::time_point<std::chrono::system_clock, Duration>& x,
const Leap& y);
template <class Duration>
bool
operator!=(const Leap& x,
const std::chrono::time_point<std::chrono::system_clock, Duration>& y);
template <class Duration>
bool
operator!=(const std::chrono::time_point<std::chrono::system_clock, Duration>& x,
const Leap& y);
template <class Duration>
bool
operator< (const Leap& x,
const std::chrono::time_point<std::chrono::system_clock, Duration>& y);
template <class Duration>
bool
operator< (const std::chrono::time_point<std::chrono::system_clock, Duration>& x,
const Leap& y);
template <class Duration>
bool
operator> (const Leap& x,
const std::chrono::time_point<std::chrono::system_clock, Duration>& y);
template <class Duration>
bool
operator> (const std::chrono::time_point<std::chrono::system_clock, Duration>& x,
const Leap& y);
template <class Duration>
bool
operator<=(const Leap& x,
const std::chrono::time_point<std::chrono::system_clock, Duration>& y);
template <class Duration>
bool
operator<=(const std::chrono::time_point<std::chrono::system_clock, Duration>& x,
const Leap& y);
template <class Duration>
bool
operator>=(const Leap& x,
const std::chrono::time_point<std::chrono::system_clock, Duration>& y);
template <class Duration>
bool
operator>=(const std::chrono::time_point<std::chrono::system_clock, Duration>& x,
const Leap& y);
class Rule;
struct TZ_DB
{
std::string version;
std::vector<Zone> zones;
std::vector<Link> links;
std::vector<Leap> leaps;
std::vector<Rule> rules;
};
std::ostream& operator<<(std::ostream& os, const TZ_DB& db);
const TZ_DB& get_tzdb();
const TZ_DB& reload_tzdb();
#if HAS_REMOTE_API
std::string remote_version();
bool remote_download(const std::string& version);
bool remote_install(const std::string& version);
#endif
class utc_clock
{
public:
using duration = std::chrono::system_clock::duration;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<utc_clock>;
static constexpr bool is_steady = true;
static time_point now() noexcept;
template <class Duration>
static
std::chrono::time_point<utc_clock,
typename std::common_type<Duration, std::chrono::seconds>::type>
sys_to_utc(std::chrono::time_point<std::chrono::system_clock, Duration> t);
template <class Duration>
static
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<Duration, std::chrono::seconds>::type>
utc_to_sys(std::chrono::time_point<utc_clock, Duration> t);
};
template <class Duration>
std::string
format(const std::locale& loc, std::string format,
std::chrono::time_point<std::chrono::system_clock, Duration> tp,
const Zone* zone = nullptr);
std::string
format(const std::locale& loc, std::string format, day_point tp,
const Zone* zone = nullptr);
template <class Duration>
std::string
format(std::string format,
std::chrono::time_point<std::chrono::system_clock, Duration> tp,
const Zone* zone = nullptr);
std::string
format(std::string format, day_point tp, const Zone* zone = nullptr);
template <class Duration>
void
parse(std::istream& is, const std::string& format,
std::chrono::time_point<std::chrono::system_clock, Duration>& tp);
template <class Duration>
void
parse(std::istream& is, const std::string& format,
std::chrono::time_point<std::chrono::system_clock, Duration>& tp,
std::string& abbrev);
} // namespace date
</pre></blockquote>
<a name="Description"></a><h2>Description</h2>
<p>
Everything documented below is in <code>namespace date</code>. Explicit references to
this namespace in example code below is intentionally omitted in the hopes of reducing
verbosity.
</p>
<a name="TheDatabase"></a><h3>The Database</h3>
<p>
The database is represented with the type <code>TZ_DB</code>:
</p>
<blockquote><pre>
struct TZ_DB
{
std::string version;
std::vector<Zone> zones;
std::vector<Link> links;
std::vector<Leap> leaps;
std::vector<Rule> rules;
};
</pre></blockquote>
<p>
This is a singleton class. You can get a <code>const TZ_DB&</code> to the singleton
using this function:
</p>
<blockquote><pre>
const TZ_DB& get_tzdb();
</pre></blockquote>
<p>
The first call to <code>get_tzdb()</code> will initialize the database from your local
copy of the <a href="http://www.iana.org/time-zones">IANA Time Zone Database</a> located
at <code>install</code> (a file-scope variable of type <code>std::string</code> in
<a href="https://github.com/HowardHinnant/date/blob/master/tz.cpp"><code>tz.cpp</code></a>).
You will need to catch the return of this function by <code>const&</code> as the
<code>TZ_DB</code> is not constructible from a <code>const TZ_DB</code>. This can be done
with the following example code:
</p>
<blockquote><pre>
auto& db = get_tzdb();
</pre></blockquote>
<p>
With a reference to the database in hand, you have read-only access to the entire
database, which is nothing more than sorted <code>vector</code>s for the four types of
data contained in the database. With such a reference you could (for example) print the
names of all the Zones in the database:
</p>
<blockquote><pre>
for (auto& z : db.zones)
std::cout << z.name() << '\n';
</pre></blockquote>
<p>
There are currently 377 zones in the database.
</p>
<p>
Or you could output the 89 <code>Link</code>s, including their <code>name()</code> and
<code>target()</code>:
</p>
<blockquote><pre>
for (auto& link : db.links)
std::cout << link << '\n';
</pre></blockquote>
<p>
If you aren't happy with the format this outputs in, <code>Link</code> has public member
functions <code>name()</code> and <code>target()</code> so that you can achieve whatever
format you desire.
</p>
<p>
If needed, <code>db.version</code> is a <code>std::string</code> containing the
<a href="http://www.iana.org/time-zones">IANA Time Zone Database</a> version of the
database you are reading. For example the current version when this sentence
was written was "2016a".
</p>
<p>
You can even print the entire database out in a semi-human-readable format if desired:
</p>
<blockquote><pre>
std::cout << db << '\n';
</pre></blockquote>
<p>
If you constrain the geography or history of the database during installation, those
constraints will be reflected in these examples.
</p>
<p>
If you decide you need to reload the database say, because you want to install a new
version of the <a href="http://www.iana.org/time-zones">IANA Time Zone Database</a>
without stopping your program, you can use this function:
</p>
<blockquote><pre>
const TZ_DB& reload_tzdb();
</pre></blockquote>
<p>
This re-initializes the database by reading from the <code>install</code> location you
customized on installation. The use of the <code>reload_tzdb</code> function is not
pain-free, and not for every application (not for most of them I'm guessing). For example
see the <b>Thread Safety</b> section for issues related to the use of these functions.
</p>
<a name="remote"></a><h3>The remote API</h3>
<p>
The remote API is enabled only if <code>HAS_REMOTE_API</code> is set to 1 during
compilation. See <a href="#Installation">Installation</a> for more details.
</p>
<blockquote><pre>
std::string remote_version();
</pre></blockquote>
<p>
This function will query the
<a href="http://www.iana.org/time-zones">IANA Time Zone Database website</a> for the
latest version number of the IANA database, and return it as a <code>std::string</code>.
If an internet connection can not be made, an empty <code>string</code> is returned.
This string can be compared against the version of your local copy of the database:
<code>get_tzdb().version</code>.
</p>
<blockquote><pre>
bool remote_download(const std::string& version);
</pre></blockquote>
<p>
This function will attempt to download the database with the version <code>version</code>
from the <a href="http://www.iana.org/time-zones">IANA Time Zone Database website</a>.
If successful, <code>true</code> is returned and a file named
<code>version + ".tar.gz"</code> will be stored at the location <code>install</code>.
If not successful, <code>false</code> is returned.
</p>
<blockquote><pre>
bool remote_install(const std::string& version);
</pre></blockquote>
<p>
This function will attempt to uncompress the tar file downloaded by
<code>remote_download(version)</code> and replace any existing database with
the result. It will then delete the tar file. If the tar file doesn't exist,
<code>remote_install</code> will do nothing. Returns <code>true</code> on
success, else returns <code>false</code>.
</p>
<a name="Zone"></a><h3>Zone</h3>
<p>
The <code>Zone</code> class is the most important type in this library. It provides the
main access to the functionality provided by this library. Each <code>Zone</code> is
named, represents a geographic area, and provides a mapping between UTC and the local
time, in both directions. This mapping from local time to UTC is in general not one to
one. The mapping, and even the specific rule, depends upon the input
<code>time_point</code>, which can represent either UTC or local time.
</p>
<p>
The detailed API of the <code>Zone</code> class depends upon a small amount of
infrastructure which is introduced first.
</p>
<a name="Infrastructure"></a><h4>Infrastructure</h4>
<blockquote><pre>
using second_point = std::chrono::time_point<std::chrono::system_clock,
std::chrono::seconds>;
</pre></blockquote>
<p>
<code>second_point</code> is a <code>std::chrono::time_point</code> based on
<code>system_clock</code> but with the precision of <code>seconds</code>. This library
will interoperate with <code>system_clock::time_point</code>s of <i>any</i> precision.
However the data in the database is largely based on <code>second_point</code>, and
some of the data which is presented, such as that in the <code>Info</code> class, uses
this type alias as a convenience, and to reduce verbosity. <code>second_point</code>
will implicitly convert to <code>system_clock::time_point</code>. And coarser
<code>time_point</code>s such as the <code>day_point</code> from the
<a href="date_v2.html"><code>date</code></a> library will implicitly convert to
<code>second_point</code>.
</p>
<blockquote><pre>
struct Info
{
second_point begin;
second_point end;
std::chrono::seconds offset;
std::chrono::minutes save;
std::string abbrev;
};
</pre></blockquote>
<p>
The <code>Info</code> struct is the return type of the <code>get_info</code> member
function of the <code>Zone</code> class. It contains very detailed information about the
<code>Zone</code> at the <code>time_point</code> (UTC or local) input into this member
function. <code>Info</code> contains no pointers or references into the database.
Therefore clients do not need to be concerned about holding on to <code>Info</code>s
during a call to <code>reload_tzdb()</code>. Though a call to <code>reload_tzdb()</code>
could potentially make the data in an outstanding <code>Info</code> obsolete. See
<code>Zone::get_info</code> for more details.
</p>
<blockquote><pre>
enum class tz {utc, local};
enum class choose {earliest, latest};
</pre></blockquote>
<p>
These <code>enum</code>s are used as input to some of the <code>Zone</code> member
functions. <code>tz::utc</code> indicates that a <code>time_point</code> represents a
time in the UTC time zone. <code>tz::local</code> indicates that a
<code>time_point</code> represents a time in the <code>Zone</code>'s local time zone.
The <code>choose enum</code> allows a client to specify how a mapping from local to UTC
should behave when the mapping is not one to one. Alternatively one can not specify
a policy in the mapping, and if the mapping is not unique, an exception will be thrown.
</p>
<blockquote><pre>
class nonexistent_local_time
: public std::runtime_error
{
public:
const char* what() const override;
};
class ambiguous_local_time
: public std::runtime_error
{
public:
const char* what() const override;
};
</pre></blockquote>
<p>
These are the exception classes thrown by the local to UTC mapping. In addition to their
type indicating the nature of the exceptional circumstance, they also sport a
<code>what()</code> member function that will contain a very detailed explanation
including specific times for the specific <code>time_point</code>s involved in the
attempted mapping.
</p>
<p>
If in a call to <code>Zone::to_sys</code> the local <code>time_point</code> falls into a
"gap" for which no local time exists, a <code>nonexistent_local_time</code> exception is
thrown.
</p>
<p>
If in a call to <code>Zone::to_sys</code> the local <code>time_point</code> has an
ambiguous mapping to UTC, a <code>ambiguous_local_time</code> exception is thrown.
</p>
<p>
Either exceptional situation can be circumvented with the use of
<code>choose::earliest</code> or <code>choose::latest</code> in the call to
<code>to_sys</code>.
</p>
<a name="ZoneContinued"></a><h4>Zone continued</h4>
<blockquote><pre>
class Zone
{
public:
const std::string& name() const;
template <class Rep, class Period>
std::pair
<
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>,
std::string
>
to_local(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp) const;
template <class Rep, class Period>
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>
to_sys(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp) const;
template <class Rep, class Period>
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>
to_sys(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp,
choose z) const;
template <class Rep, class Period>
Info
get_info(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp,
tz timezone) const;
};
const Zone* locate_zone(const std::string& tz_name);
const Zone* current_zone();
bool operator==(const Zone& x, const Zone& y);
bool operator!=(const Zone& x, const Zone& y);
bool operator< (const Zone& x, const Zone& y);
bool operator> (const Zone& x, const Zone& y);
bool operator<=(const Zone& x, const Zone& y);
bool operator>=(const Zone& x, const Zone& y);
std::ostream& operator<<(std::ostream& os, const Zone& z);
</pre></blockquote>
<p>
The entire public API of the <code>Zone</code> is <code>const</code>. Once the database
is initialized (or reloaded), <code>Zone</code>s are set in concrete.
</p>
<hr>
<p>
The current time zone associated with your computer can be retrieved with the namespace
scope function <code>current_zone()</code>. For example:
</p>
<blockquote><pre>
std::cout << current_zone()->name() << '\n';
</pre></blockquote>
<p>
For me the above currently outputs <code>America/New_York</code>.
</p>
<hr>
<blockquote><pre>
const Zone* locate_zone(const std::string& tz_name);
</pre></blockquote>
<p>
<code>locate_zone</code> returns a pointer to a <code>Zone</code> in the database
associated with <code>tz_name</code>. If it can't find a <code>Zone</code> named
<code>tz_name</code>, the implementation will search for a <code>Link</code> named
<code>tz_name</code>, and then return the <code>Zone</code> associated with the
<code>Link</code>'s <code>target()</code>. If <code>tz_name</code> can not be found in the
database, a <code>std::runtime_error</code> is thrown.
</p>
<p>
Example:
</p>
<blockquote><pre>
try
{
cout << locate_zone("Europe/London")->name() << '\n'; // A Zone
cout << locate_zone("Europe/Jersey")->name() << '\n'; // A Link to a Zone
cout << locate_zone("Europe/New_Jersey")->name() << '\n'; // Doesn't exist
}
catch (const exception& e)
{
cout << e.what() << '\n';
}
</pre></blockquote>
<p>
Which outputs:
</p>
<blockquote><pre>
Europe/London
Europe/London
Europe/New_Jersey not found in timezone database
</pre></blockquote>
<p>
Note that <code>locate_zone</code> never returns <code>nullptr</code>. Also note that
the first call to <code>locate_zone</code> may implicitly initialize the database.
</p>
<hr>
<blockquote><pre>
template <class Rep, class Period>
std::pair
<
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>,
std::string
>
to_local(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp) const;
</pre></blockquote>
<p>
<code>to_local</code> maps a <code>system_clock</code>-associated <code>time_point</code>
from UTC to local time, returning both the mapped <code>time_point</code> and an
abbreviation for the local time zone. This member function accepts any precision
<code>time_point</code>, but returns a <code>time_point</code> with a precision of
<code>seconds</code> or finer. This is done because it is possible that some of the
mappings returned by the database need the precision of a second.
</p>
<p>
There are only two ways this function can fail:
</p>
<ol>
<li><p>
Out of memory error. Not bloody likely. The only memory that possibly could be
allocated is for the abbreviation stored in a <code>std::string</code> and all known
implementations will fit all known abbreviations into their short string buffer.
</p></li>
<li><p>
If you curtailed history during installation, a <code>runtime_error</code> will be thrown
if <code>tp</code> refers to a <code>time_point</code> outside of the range
<code>min_year/jan/1 00:00:00</code> to <code>max_year/dec/31 23:59:59</code>. This can
not happen with the default settings of <code>min_year</code> and <code>max_year</code>.
</p></li>
</ol>
<p>
Example:
</p>
<blockquote><pre>
auto local = current_zone()->to_local(system_clock::now());
cout << local.first << ' ' << local.second << '\n';
</pre></blockquote>
<p>
Which just output for me:
</p>
<blockquote><pre>
2015-07-12 16:57:14.430467 EDT
</pre></blockquote>
<p>
Not quite 5pm in the US Eastern timezone during daylight saving time.
</p>
<p>
And for a historical example:
</p>
<blockquote><pre>
auto distant_past = locate_zone("America/New_York")->to_local(day_point(feb/9/1942) + 7h);
cout << distant_past.first << ' ' << distant_past.second << '\n';
</pre></blockquote>
<p>
Which outputs:
</p>
<blockquote><pre>
1942-02-09 03:00:00 EWT
</pre></blockquote>
<p>
The US shifted to "War Time."
</p>
<hr>
<p>
If you want to go the other direction (from local time to UTC) use:
</p>
<blockquote><pre>
template <class Rep, class Period>
std::chrono::time_point<std::chrono::system_clock,
typename std::common_type<std::chrono::duration<Rep, Period>,
std::chrono::seconds>::type>
to_sys(std::chrono::time_point<std::chrono::system_clock,
std::chrono::duration<Rep, Period>> tp) const;
</pre></blockquote>
<p>
For example:
</p>
<blockquote><pre>
auto distant_past = locate_zone("America/New_York")->to_sys(day_point(feb/9/1942) + 3h);
cout << distant_past << ' ' << " UTC\n";
</pre></blockquote>
<p>
Which outputs:
</p>
<blockquote><pre>
1942-02-09 07:00:00 UTC
</pre></blockquote>
<p>
This function will throw an exception of type <code>nonexistent_local_time</code> if the
local time does not exist. This can happen when the local clock is discontinuously set
forward, such as when moving from standard time to daylight savings time.
</p>
<p>
For example:
</p>
<blockquote><pre>
try
{
auto distant_past = locate_zone("America/New_York")->to_sys(day_point(feb/9/1942) + 3h - 1ms);
cout << distant_past << ' ' << " UTC\n";
}
catch (const exception& e)
{
cout << e.what() << '\n';
}
</pre></blockquote>
<p>