forked from HowardHinnant/date
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtz.html
More file actions
4148 lines (3644 loc) · 134 KB
/
Copy pathtz.html
File metadata and controls
4148 lines (3644 loc) · 134 KB
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</title>
<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>
<address align=right>
<br/>
<br/>
<a href="mailto:howard.hinnant@gmail.com">Howard E. Hinnant</a><br/>
2021-03-22<br/>
</address>
<hr/>
<h1 align=center>Time Zone Database Parser</h1>
<h2>Contents</h2>
<ul>
<li><a href="https://github.com/HowardHinnant/date">github link</a></li>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#Description">Description</a>
<ul>
<li><a href="#current_local">What is the current local time?</a></li>
<li><a href="#somewhere_else">What time is it somewhere <i>else</i> in the world?</a></li>
<li><a href="#convert">How do I convert a <code>time_zone</code> from one time zone to another?</a></li>
<li><a href="#loc_vs_sys"><code>local_time</code> vs <code>sys_time</code></a></li>
<li><a href="#Summary">Summary</a></li>
<li><a href="#Examples">Examples</a></li>
</ul>
</li>
<li><a href="#Reference">Reference</a>
<ul>
<li><a href="#database">The database</a></li>
<li><a href="#choose"><code>choose</code></a></li>
<li><a href="#nonexistent_local_time"><code>nonexistent_local_time</code></a></li>
<li><a href="#ambiguous_local_time"><code>ambiguous_local_time</code></a></li>
<li><a href="#sys_info"><code>sys_info</code></a></li>
<li><a href="#local_info"><code>local_info</code></a></li>
<li><a href="#time_zone"><code>time_zone</code></a></li>
<li><a href="#zoned_traits"><code>zoned_traits</code></a></li>
<li><a href="#zoned_time"><code>zoned_time</code></a></li>
<li><a href="#make_zoned"><code>make_zoned</code></a></li>
<li><a href="#utc_clock"><code>utc_clock</code></a></li>
<li><a href="#tai_clock"><code>tai_clock</code></a></li>
<li><a href="#gps_clock"><code>gps_clock</code></a></li>
<li><a href="#clock_cast"><code>clock_cast</code></a></li>
<li><a href="#leap"><code>leap</code></a></li>
<li><a href="#link"><code>link</code></a></li>
</ul>
</li>
<li><a href="#Installation">Installation</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.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
of 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.html">date</a>
library. This is a separate library from <a href="date.html"><code>date</code></a>
because many clients of <a href="date.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 every few months 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.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="Description"></a><h2>Description</h2>
<p>
Here is an overview of all the types we are going to talk about at some point. They are
all fully covered in the reference section. This link is just there to give you a view
of everything on one quick page so that you don't get lost or overwhelmed. Many of these
types will never need to be explicitly named in typical use cases.
</p>
<blockquote>
<a href="tz_types.jpeg">tz_types.jpeg</a>
</blockquote>
<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="current_local"></a><h3>What is the current local time?</h3>
<p>
One of the first things people want to do is find out what the current local time
it is. Here is a complete program to print out the local time in human readable
format:
</p>
<blockquote><pre>
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
auto t = make_zoned(current_zone(), system_clock::now());
std::cout << t << '\n';
}
</pre></blockquote>
<p>
This just output for me:
</p>
<blockquote><pre>
2016-05-14 18:33:24.205124 EDT
</pre></blockquote>
<p>
There are some noteworthy points about this program:
</p>
<ul>
<li><p>
This is a <code><chrono></code>-based system. The current time is
found with <code>std::chrono::system_clock::now()</code>.
</p></li>
<li><p>
The computer's current local time zone is not assumed. If anything is assumed that
would be UTC, since this is the time zone that <code>system_clock</code> tracks
(unspecified but de facto standard).
</p></li>
<li><p>
Specifying you want to convert <code>system_clock::time_point</code>s to the
current local time zone is as easy as calling <code>date::current_zone()</code>
and <i>pairing</i> that with a <code>system_clock::time_point</code> using
<code>date::make_zoned</code>. This creates a <code>zoned_time</code>.
</p></li>
<li><p>
This <code>zoned_time</code> maintains whatever precision it was given. On my
platform <code>system_clock::now()</code> has microseconds precision, so in this
example, <code>t</code> has microseconds precision as well.
</p></li>
<li><p>
Then <code>t</code> is simply streamed out. By default the output
represents all of the precision it is given.
</p></li>
</ul>
<p>
Everything about the above program can be customized: the precision, the formatting,
and the time zone. But by default, things just work, and don't throw away information.
</p>
<p>
For example let's say we wanted to limit the precision to milliseconds. This can
be done by inserting <code>floor<milliseconds></code> in one place. This
makes <code>t</code> have just a precision of <code>milliseconds</code>
and that is reflected in the streaming operator with no further effort:
</p>
<blockquote><pre>
auto t = make_zoned(current_zone(), <b>floor<milliseconds></b>(system_clock::now()));
std::cout << t << '\n'; // 2016-05-14 18:33:24.205 EDT
</pre></blockquote>
<p>
Seconds precision is just as easy:
</p>
<blockquote><pre>
auto t = make_zoned(current_zone(), <b>floor<seconds></b>(system_clock::now()));
std::cout << t << '\n'; // 2016-05-14 18:33:24 EDT
</pre></blockquote>
<p>
The entire <code>time_get</code> / <code>time_put</code> formatting capability is
also at your fingertips (and at any precision):
</p>
<blockquote><pre>
auto t = make_zoned(current_zone(), system_clock::now());
std::cout << <b>format("%a, %b %d, %Y at %I:%M %p %Z", t)</b> << '\n';
// Sat, May 14, 2016 at 06:33 PM EDT
</pre></blockquote>
<p>
Using any <code>std::locale</code> your OS supports:
</p>
<blockquote><pre>
auto t = make_zoned(current_zone(), floor<seconds>(system_clock::now()));
std::cout << format(<b>locale("de_DE")</b>, "%a, %b %d, %Y at %T %Z", t) << '\n';
// Sa, Mai 14, 2016 at 18:33:24 EDT
</pre></blockquote>
<a name="somewhere_else"></a><h3>What time is it somewhere <i>else</i> in the world?</h3>
<p>
From the previous section:
</p>
<blockquote>
<p>
Hmm... German locale in an American time zone.
</p>
</blockquote>
<p>
We can fix that easily too:
</p>
<blockquote><pre>
<b>auto zone = locate_zone("Europe/Berlin");</b>
auto t = make_zoned(<b>zone</b>, floor<seconds>(system_clock::now()));
std::cout << format(locale("de_DE"), "%a, %b %d, %Y at %T %Z", t) << '\n';
// So, Mai 15, 2016 at 00:33:24 CEST
</pre></blockquote>
<p>
The <code>date::locate_zone()</code> function looks up the IANA time zone with the name
"Europe/Berlin" and returns a <code>const time_zone*</code> which has no ownership
issues and can be freely and cheaply copied around. It is not possible for
<code>locate_zone()</code> to return <code>nullptr</code>, though it might throw
an exception if pushed far enough (e.g. <code>locate_zone("Disney/Mickey_Mouse")</code>).
</p>
<p>
You can also call <code>make_zoned</code> with the time zone name right in the call:
</p>
<blockquote><pre>
auto t = make_zoned(<b>"Europe/Berlin"</b>, floor<seconds>(system_clock::now()));
</pre></blockquote>
<p>
The first way is very slightly more efficient if you plan on using <code>zone</code>
multiple times since it then only has to be looked up once.
</p>
<a name="convert"></a><h3>How do I convert a <code>time_zone</code> from one time zone to another?</h3>
<p>
So far we've only looked at converting from <code>system_clock::now()</code> to
a local, or specific time zone. We've used <code>make_zoned</code> with the
first argument being either <code>current_zone()</code> or a specification for
some other time zone, and the second argument being a
<code>system_clock::time_point</code>. So far so good.
</p>
<p>
But now I have a video-conference meeting on the first Monday of May, 2016 at
9am New York time. I need to communicate that meeting with partners in London
and Sydney. And the computation is taking place on a computer in New Zealand (or some other
unrelated time zone). What does that look like?
</p>
<blockquote><pre>
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace date::literals;
using namespace std::chrono_literals;
auto meet_nyc = make_zoned("America/New_York", date::local_days{Monday[1]/May/2016} + 9h);
auto meet_lon = make_zoned("Europe/London", meet_nyc);
auto meet_syd = make_zoned("Australia/Sydney", meet_nyc);
std::cout << "The New York meeting is " << meet_nyc << '\n';
std::cout << "The London meeting is " << meet_lon << '\n';
std::cout << "The Sydney meeting is " << meet_syd << '\n';
}
</pre></blockquote>
<p>
The output is the following. But before you forward it, send a generous bonus
to the guys in Australia.
</p>
<blockquote><pre>
The New York meeting is 2016-05-02 09:00:00 EDT
The London meeting is 2016-05-02 14:00:00 BST
The Sydney meeting is 2016-05-02 23:00:00 AEST
</pre></blockquote>
<p>
The first time, <code>meet_nyc</code> is a pairing of a time zone ("America/New_York")
with a <i>local time</i> (<code>Monday[1]/May/2016</code> at 09:00). Note that this
input is exactly reflected in the output:
</p>
<blockquote><pre>
The New York meeting is 2016-05-02 09:00:00 EDT
</pre></blockquote>
<p>
The next line creates <code>meet_lon</code> with the <code>zoned_time</code>
<code>meet_nyc</code> and a new time zone: "Europe/London". The effect of this pairing
is to create a <code>time_point</code> with the exact same UTC time point, but
associated with a different <code>time_zone</code> for localization purposes. That is,
after this "converting construction", an invariant is that
<code>meet_lon.get_sys_time() == meet_nyc.get_sys_time()</code>, even though these
two objects refer to different time zones.
</p>
<p>
The same recipe is followed for creating <code>meet_syd</code>. The default formatting
for these <code>zoned_time</code>s is to output the <i>local</i> date and time followed
by the current time zone abbreviation.
</p>
<p>
Summary: <code>zoned_time</code> is a pairing of local or UTC time with a <code>time_zone</code>.
The result is a well-specified point in time. And it carries with it the ability to
serve as a translator to any other <code>time_point</code> which carries time zone
information (to any precision).
</p>
<a name="loc_vs_sys"></a><h3><code>local_time</code> vs <code>sys_time</code></h3>
<p>
Let's say I want to refer to the New Years Day party at 2017-01-01 00:00:00. I don't
want to refer to a specific party at some geographical location. I want to refer to
the fact that this moment is celebrated in different parts of the world according to
local times. This is called a <code>local_time</code>.
</p>
<blockquote><pre>
auto new_years = local_time<days>{2017_y/January/1} + 0h + 0m + 0s;
</pre></blockquote>
<p>
A <code>local_time<D></code> can be created with any duration <code>D</code> and
is a <code>std::chrono::time_point</code> except that
<code>local_time<D>::clock</code> has no <code>now()</code> function. There is
no time zone associated with <code>local_time</code>.
</p>
<blockquote>
<p>
<code>local_time</code> <b>is not</b> the time associated with the current local time
the computer is set to.
</p>
</blockquote>
<p>
<code>local_time</code> <i>is</i> a time associated with an <i>as yet</i>
unspecified time zone. Only when you pair a <code>local_time</code> with a
<code>time_zone</code> do you get a concrete point in time that can be converted
to UTC and other time zones: a <code>zoned_time</code>.
</p>
<p>
There also exist convenience type aliases:
</p>
<blockquote><pre>
using local_seconds = local_time<std::chrono::seconds>;
using local_days = local_time<days>;
</pre></blockquote>
<p>
In summary: When is 1min after New Years 2017?
</p>
<blockquote><pre>
auto t = local_days{January/1/2017} + 1min;
cout << t << '\n'; // 2017-01-01 00:01
</pre></blockquote>
<p>
When is 1min after New Years 2017 UTC?
</p>
<blockquote><pre>
auto t = sys_days{January/1/2017} + 1min;
cout << t << '\n'; // 2017-01-01 00:01
</pre></blockquote>
<p>
This effectively means that <code>year_month_day</code> is also ambiguous as to
whether it refers to a local (timezone-less) time or to UTC. You have to
specify which when you use it. But that is the nature of how people use dates
(points in time with days precision). "There will be a celebration on New Years."
In many contexts the time zone is intentionally left unspecified.
</p>
<p>
When is 1min after New Years 2017 in New York?
</p>
<blockquote><pre>
zoned_seconds t{"America/New_York", local_days{January/1/2017} + 1min};
cout << t << '\n'; // 2017-01-01 00:01:00 EST
</pre></blockquote>
<p>
What time will it be in New York when it is 1min after New Years 2017 UTC?
</p>
<blockquote><pre>
zoned_seconds t{"America/New_York", sys_days{January/1/2017} + 1min};
cout << t << '\n'; // 2016-12-31 19:01:00 EST
</pre></blockquote>
<a name="Summary"></a><h3>Summary</h3>
<p>
We now have 5 concepts and their associated types:
</p>
<ol>
<li><p>
Calendars: These are day-precision time points that are typically field structures
(multiple fields that create a unique "name" for a day).
</p>
<p>
Example calendars include <code>year_month_day</code> and
<code>year_month_weekday</code>. Other examples could include the ISO
week-based calendar, the Julian calendar, the Islamic calendar, the Hebrew
calendar, the Chinese calendar, the Mayan calendar, etc.
</p>
<p>
Calendars can convert to and from both <code>sys_days</code> and
<code>local_days</code>. These two conversions involve identical arithmetic, but
have semantic differences.
</p>
<p>
Once these conversions are implemented, the calendars are not only interoperable
with <code>zoned_time</code>, but are also interoperable with each other. That
is dates in the Chinese calendar can easily be converted to or from dates in the
Mayan calendar even though these two calendars have no knowledge of the other.
</p>
<p>
Disclaimer: "date.h" provides only the <code>year_month_day</code> and
<code>year_month_weekday</code> calendars.
</p></li>
<li><p>
<code>sys_time</code>: This is a serial time point and a
<code>std::chrono::time_point</code> of arbitrary precision. It has
<code>sys_seconds</code> and <code>sys_days</code> convenience precisions.
</p>
<p>
<code>sys_time</code> is a <code>time_point</code> associated with the return of
<code>system_clock::now()</code> and represents
<a href="https://en.wikipedia.org/wiki/Unix_time">Unix Time</a> which very
closely approximates UTC.
</p></li>
<li><p>
<code>local_time</code>: This is a serial time point and a
<code>std::chrono::time_point</code> of arbitrary precision. It has
<code>local_seconds</code> and <code>local_days</code> convenience precisions.
</p>
<p>
<code>local_time</code> is a <code>time_point</code> associated with no time
zone, and no <code>clock::now()</code>. It is the <code>void*</code> of
<code>time_point</code>s.
</p></li>
<li><p>
<code>time_zone</code>: This represents a specific geographical area, and all
time zone related information for this area over all time. This includes a
name for the area, and for any specific point in time, the UTC offset, the
abbreviation, and additional information.
</p></li>
<li><p>
<code>zoned_time</code>: This is a pairing of a <code>time_zone</code> and a
<code>sys_time</code> (of precision seconds or finer). It can also be
equivalently viewed as a pairing of a <code>time_zone</code> and a
<code>local_time</code>. Once constructed it represents a valid point in time,
and the <code>time_zone</code>, <code>sys_time</code> and
<code>local_time</code> can all be extracted. There exists a
<code>zoned_seconds</code> convenience precision.
</p></li>
</ol>
<p>
<code>time_zone</code>s are retrieved from a time zone database. The database
also holds information about leap seconds. To make computing with leap
seconds easier, there is a clock that takes leap seconds into account:
<code>utc_clock</code>. This clock has an associated family of time points
called <code>utc_time</code>.
</p>
<p>
Full formatting and parsing facilities are available with <code>time_put</code>-like
formatting strings.
</p>
<a name="Examples"></a><h3>Examples</h3>
<h4>Flight time</h4>
<p>
Interesting things can happen to the apparent time when you travel across the globe
at high speeds. So departure and arrival times of airplane flights make for good
examples involving time zone arithmetic.
</p>
<blockquote><pre>
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace std::chrono_literals;
using namespace date;
auto departure = make_zoned("America/New_York", local_days{December/30/1978} + 12h + 1min);
auto flight_length = 14h + 44min;
auto arrival = make_zoned("Asia/Tehran", departure.get_sys_time() + flight_length);
std::cout << "departure NYC time: " << departure << '\n';
std::cout << "flight time is " << make_time(flight_length) << '\n';
std::cout << "arrival Tehran time: " << arrival << '\n';
}
</pre></blockquote>
<p>
The output of the above program is:
</p>
<blockquote><pre>
departure NYC time: 1978-12-30 12:01:00 EST
flight time is 14:44
arrival Tehran time: 1978-12-31 11:45:00 IRST
</pre></blockquote>
<p>
The departure time is formed by transforming the local calendar date time into a
<code>local_time</code> and pairing that with the "America/New_York"
<code>time_zone</code> to form a <code>zoned_time</code>. The flight time is
just an ordinary <code>chrono::duration</code>.
</p>
<p>
The arrival time is formed by retrieving the departure time in terms of
<code>sys_time</code>, adding the length of the flight, and pairing that
<code>sys_time</code> with the "Asia/Tehran" <code>time_zone</code> to form a
<code>zoned_time</code>.
</p>
<p>
By doing the arithmetic (addition of the flight time) in the UTC (well system)
time zone, we do not have to worry about things like daylight savings time, or
other political changes to the either UTC offset. For example if we change one
line to look at the same flight 24 hours later:
</p>
<blockquote><pre>
auto departure = make_zoned("America/New_York", local_days{December/<b>31</b>/1978} + 12h + 1min);
</pre></blockquote>
<p>
Then the output changes to:
</p>
<blockquote><pre>
departure NYC time: 1978-12-<b>31</b> 12:01:00 EST
flight time is 14:44
arrival Tehran time: 1979-01-01 11:<b>15</b>:00 IRST
</pre></blockquote>
<p>
Now we have the flight arriving 30min earlier. This is because the time zone
"Asia/Tehran" undergoes an offset change while the plane is in the air, shifting its UTC
offset to 30min earlier. Is this the final word on this example? Almost. If accuracy
down to the second is required (it is not for a flight arrival), then additional effort
needs to be expended. Because there was also a leap second insertion while the plane
was in the air. This can be taken into account with the following code:
</p>
<blockquote><pre>
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace std::chrono;
using namespace date;
auto departure = make_zoned("America/New_York", local_days{December/31/1978} + 12h + 1min);
<b>auto departure_utc = clock_cast<utc_clock>(departure.get_sys_time());</b>
auto flight_length = 14h + 44min;
auto arrival = make_zoned("Asia/Tehran", <b>clock_cast<system_clock>(departure_utc + flight_length)</b>);
std::cout << "departure NYC time: " << departure << '\n';
std::cout << "flight time is " << make_time(flight_length) << '\n';
std::cout << "arrival Tehran time: " << arrival << '\n';
}
</pre></blockquote>
<p>
This is just like the previous example except that the arithmetic (departure
time + flight length) is done in <code>utc_time</code> instead of
<code>sys_time</code>. To accomplish this, there is a conversion from
<code>sys_time</code> to <code>utc_time</code> before the arithmetic, and
another conversion from <code>utc_time</code> to <code>sys_time</code> after the
arithmetic. And the result changes to:
</p>
<blockquote><pre>
departure NYC time: 1978-12-31 12:01:00 EST
flight time is 14:44
arrival Tehran time: 1979-01-01 11:<b>14:59</b> IRST
</pre></blockquote>
<h4>Format conversion</h4>
<p>
A common task in dealing with dates and times is converting from one string format
to another. This library is extremely flexible in handling this task. As an
example, let's say that you need to convert strings that look like this:
</p>
<blockquote><pre>
Sun Sep 16 01:03:52 -0500 1973
</pre></blockquote>
<p>
Into strings that look like this:
</p>
<blockquote><pre>
1973-09-16T06:03:52.000Z
</pre></blockquote>
<p>
That is, given a local time with UTC offset, you need to not only update the format
to something more modern, but it also has to be converted to the UTC timezone and to
a precision of milliseconds. The code to do this is quite straight forward:
</p>
<blockquote><pre>
std::string
convert(const std::string& input)
{
using namespace std;
using namespace std::chrono;
using namespace date;
istringstream stream{input};
sys_time<milliseconds> t;
stream >> parse("%a %b %d %T %z %Y", t);
if (stream.fail())
throw runtime_error("failed to parse " + input);
return format("%FT%TZ", t);
}
</pre></blockquote>
<p>
Let's walk through this:
</p>
<p>
First, <code>date::parse</code> works with <code>istream</code>s so you can parse from
files, from strings, or anything else that is an <code>istream</code>.
</p>
<p>
Second, while we don't need to parse to a precision of milliseconds, we need to
format to that precision. It is easy just to parse into a
milliseconds-precision <code>sys_time</code> so that we can then just format it
back out with no change. If we needed to parse at finer precision than
formatting, then we would need to parse at the higher precision, truncate it (by
some rounding mode — <code>truncate</code>, <code>floor,</code>
<code>ceil</code> or <code>round</code>), and then format the truncated value.
</p>
<p>
To have the <code>parse</code> interpret the string as a local time offset by the
UTC offset, we need to ask for a <code>sys_time</code> to be parsed, and use
the <code>%z</code> in the proper location. The <code>parse</code> function will
then subtract the UTC offset to give us the proper <code>sys_time</code> value.
</p>
<p>
If <code>parse</code> fails to find <i>everything</i> in the parse/format string,
exactly as specified, it will set <code>failbit</code> in the <code>istream</code>.
</p>
<p>
Finally, once we know we have a successfully parsed
<code>sys_time<milliseconds></code> it is a very simple matter to format
it back out in whatever format is desired. As confirmed in the
<a href="#Reference">Reference</a>, <code>%S</code> and <code>%T</code> are
sensitive to the precision of the time point argument, and so there is no need
for extension formatting flags to indicate fractional seconds. <code>%S</code>
and <code>%T</code> just work.
</p>
<h4>Custom time zone</h4>
<p>
Occasionally the IANA time zone database doesn't quite do <i>everything</i> you want.
This library allows you to use <code>zoned_time</code> with a time zone and/or pointer
to time zone of your own making. One common example is the need to have a time zone
that has a fixed offset from UTC, but for which that offset isn't known until run time.
Below is an example which supplies a custom time zone called <code>OffsetZone</code>
which can hold a UTC offset with minutes precision.
</p>
<blockquote><pre>
#include "date/tz.h"
#include <iostream>
#include <type_traits>
class OffsetZone
{
std::chrono::minutes offset_;
public:
explicit OffsetZone(std::chrono::minutes offset)
: offset_{offset}
{}
template <class Duration>
auto
to_local(date::sys_time<Duration> tp) const
{
using namespace date;
using namespace std;
using namespace std::chrono;
using LT = local_time<common_type_t<Duration, minutes>>;
return LT{(tp + offset_).time_since_epoch()};
}
template <class Duration>
auto
to_sys(date::local_time<Duration> tp) const
{
using namespace date;
using namespace std;
using namespace std::chrono;
using ST = sys_time<common_type_t<Duration, minutes>>;
return ST{(tp - offset_).time_since_epoch()};
}
};
int
main()
{
using namespace date;
using namespace std::chrono;
OffsetZone p3_45{3h + 45min};
zoned_time<milliseconds, OffsetZone*> zt{&p3_45, floor<milliseconds>(system_clock::now())};
std::cout << zt.get_sys_time() << '\n';
std::cout << zt.get_local_time() << '\n';
}
</pre></blockquote>
<p>
This just output for me:
</p>
<blockquote><pre>
2017-09-16 17:34:47.560
2017-09-16 21:19:47.560
</pre></blockquote>
<p>
The second template parameter to <code>zoned_time</code> is a pointer to a time zone.
This example simply creates a <code>OffsetZone</code> with a UTC offset of 3:45, and
constructs a <code>OffsetZone</code> which points to that custom time zone and
supplies the current time to the desired precision (whatever that may be).
</p>
<p>
You don't have to use a built-in pointer to your time zone. You could just as easily use
<code>unique_ptr</code>, <code>shared_ptr</code>, or whatever smart pointer is right for
your application. And in C++17, you won't need to supply the template parameters for
<code>zoned_time</code> (though you still can if you want to). That is, the construction
of <code>zt</code> above could be simplified down to just this:
</p>
<blockquote><pre>
zoned_time zt{&p3_45, floor<milliseconds>(system_clock::now())};
</pre></blockquote>
<p>
One can <i>even</i> have <code>OffsetZone</code> serve as its <i>own</i> smart pointer by
giving it a member <code>operator->()</code> that returns itself:
</p>
<blockquote><pre>
const OffsetZone* operator->() const {return this;}
</pre></blockquote>
<p>
This allows you to embed the <code>OffsetZone</code> <i>directly</i> into the
<code>zoned_time</code> instead of pointing to an externally held <code>OffsetZone</code>:
</p>
<blockquote><pre>
zoned_time<milliseconds, OffsetZone> zt{OffsetZone{3h + 45min}, floor<milliseconds>(system_clock::now())};
</pre></blockquote>
<p>
As it stands, <code>zoned<Duration, OffsetZone></code> can't be streamed with
<code>operator<<</code> or formatted with <code>format</code>. But that can be
fixed too: Just give <code>OffsetZone</code> a member <code>get_info</code> which
takes a <code>sys_time</code> and returns a
<a href="#sys_info"><code>sys_info</code></a>:
</p>
<blockquote><pre>
template <class Duration>
date::sys_info
get_info(date::sys_time<Duration>) const
{
using namespace date;
using namespace std::chrono;
return {sys_seconds::min(), sys_seconds::max(), offset_,
minutes{0}, offset_ >= minutes{0}
? "+" + date::format("%H%M", offset_)
: "-" + date::format("%H%M", -offset_)};
}
</pre></blockquote>
<p>
Above I've chosen to make the abbreviation for <code>OffsetZone</code> equivalent to
<code>%z</code>, but I could have installed any <code>std::string</code> I wanted to.
This allows me to say:
</p>
<blockquote><pre>
std::cout << zt << '\n';
</pre></blockquote>
<p>
which just output for me:
</p>
<blockquote><pre>
2017-09-16 21:36:10.913 +0345
</pre></blockquote>
<p>
If I want to make <code>zoned_time<Duration, OffsetZone></code> default constructible,
then I need to specialize <code>zoned_traits<OffsetZone></code> with
<code>default_zone()</code>:
</p>
<blockquote><pre>
namespace date
{
template <>
struct zoned_traits<OffsetZone>
{
static
OffsetZone
default_zone()
{
using namespace std::chrono;
return OffsetZone{minutes{0}};
}
};
} // namespace date
</pre></blockquote>
<p>
Now this:
</p>
<blockquote><pre>
zoned_time<milliseconds, OffsetZone> zt;
std::cout << zt << '\n';
</pre></blockquote>
<p>
outputs:
</p>
<blockquote><pre>
1970-01-01 00:00:00.000 +0000
</pre></blockquote>
<p>
And if I wanted to construct a <code>zoned_time<Duration, OffsetZone></code> from
a <code>string</code>, I need to add
<code>static OffsetZone locate_zone(string name)</code> to my <code>zoned_traits</code>
specialization.
</p>
<blockquote><pre>
namespace date
{
template <>
struct zoned_traits<OffsetZone>
{
static
OffsetZone
default_zone()
{
using namespace std::chrono;
return OffsetZone{minutes{0}};
}
static
OffsetZone
locate_zone(const std::string& name)
{
using namespace std::chrono;
if (name == "UTC")
return OffsetZone{minutes{0}};
throw std::runtime_error{"OffsetZone can't handle anything but " + name};
}
};
} // namespace date
</pre></blockquote>
<p>
Now this:
</p>
<blockquote><pre>
zoned_time<seconds, OffsetZone> zt{"UTC", floor<seconds>(system_clock::now())};
std::cout << zt << '\n';
</pre></blockquote>
<p>
outputs:
</p>