-
Notifications
You must be signed in to change notification settings - Fork 3
/
point.hh
1355 lines (1137 loc) · 48.5 KB
/
point.hh
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
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0 AND BSD-3-Clause
// Go's time library was used as a reference for parts of this file (see LICENSE.md).
// Copyright (c) 2009 The Go Authors. All rights reserved.
// # Time point and `now()` function
// Represents a single point in time in UTC.
// The supported range is:
// Unix timestamp Formatted
// Min: -67768100567971200 -2147483648-01-01T00:00:00+00:00
// Max: 67767976233532799 2147483647-12-31T23:59:59+00:00
// Values outside this range might silently overflow (even with integer sanitizer).
#pragma once
#include "snn-core/array.hh"
#include "snn-core/exception.hh"
#include "snn-core/formatter.hh"
#include "snn-core/strcore.hh"
#include "snn-core/chr/common.hh"
#include "snn-core/fmt/context.hh"
#include "snn-core/fn/common.hh"
#include "snn-core/math/common.hh"
#include "snn-core/range/contiguous.hh"
#include "snn-core/time/core.hh"
#include "snn-core/time/duration.hh"
#include "snn-core/time/error.hh"
#include "snn-core/time/unit.hh"
#include "snn-core/time/wall/since_epoch.hh"
#include "snn-core/time/zone/location.hh"
namespace snn::time
{
// ## Constants
// ### Format strings helpers
namespace format_string
{
// #### For use with or without location
inline constexpr char date[] = "f"; // 2001-02-03
inline constexpr char local[] = "f t"; // 2001-02-03 04:05:06
// Quick/readable, will include fraction (trimmed) if any.
inline constexpr char quick[] = "q"; // 2001-02-03 04:05:06[.123456789] +0700
inline constexpr char rfc2822[] = "r"; // Sat, 03 Feb 2001 04:05:06 +0700
// Note: ISO 8601/RFC 3339 are the same here.
inline constexpr char iso8601[] = "c"; // 2001-02-03T04:05:06+07:00
inline constexpr char rfc3339[] = "c"; // 2001-02-03T04:05:06+07:00
// #### For use without location
inline constexpr char iso8601_z[] = "z"; // 2001-02-03T04:05:06Z
inline constexpr char rfc1123[] = "l"; // Sat, 03 Feb 2001 04:05:06 GMT
}
// ## Classes
// ### point
class point final
{
public:
// #### Default constructor
constexpr point() noexcept
: point{i64{}}
{
}
// #### Explicit constructors
constexpr explicit point(const i64 unixtime) noexcept
: sec_{unixtime + unix_to_internal_},
nano_{0}
{
}
constexpr explicit point(const time::duration d) noexcept
: sec_{d.seconds() + unix_to_internal_},
nano_{d.nanoseconds()}
{
}
// Date/time structures are not validated but normalized, e.g. (non-leap year):
// `time::point{time::ymd{2017, 2, 29}} == time::point{2017, 3, 1}`
constexpr explicit point(const year_month_day ymd) noexcept
: point{ymd.y, ymd.m, ymd.d}
{
}
constexpr explicit point(const year_month_day_hour_minute_second ymdhms) noexcept
: point{ymdhms.ymd.y, ymdhms.ymd.m, ymdhms.ymd.d,
ymdhms.hms.h, ymdhms.hms.m, ymdhms.hms.s}
{
}
constexpr explicit point(
const year_month_day_hour_minute_second_nanosecond_offset c) noexcept
: point{c.ymd.y, c.ymd.m, c.ymd.d, c.hms.h, c.hms.m, c.hms.s, c.n, c.o}
{
}
// #### Converting constructors
// Values are normalized, e.g.:
// `time::point{2017, 5, 0} == time::point{2017, 4, 30}`
// `time::point{2017, -1, 15} == time::point{2016, 11, 15}`
constexpr point(i32 year, i32 month, i32 day) noexcept
: sec_{to_unix_(year, month, day, 0, 0, 0) + unix_to_internal_},
nano_{0}
{
}
constexpr point(i32 year, i32 month, i32 day, zone::location& loc) noexcept
: point{year, month, day, 0, 0, 0, loc}
{
}
constexpr point(const year_month_day ymd, zone::location& loc) noexcept
: point{ymd.y, ymd.m, ymd.d, loc}
{
}
constexpr point(i32 year, i32 month, i32 day, i32 hour, i32 min, i32 sec) noexcept
: sec_{to_unix_(year, month, day, hour, min, sec) + unix_to_internal_},
nano_{0}
{
}
constexpr point(i32 year, i32 month, i32 day, i32 hour, i32 min, i32 sec,
zone::location& loc) noexcept
: sec_{0},
nano_{0}
{
i64 unix = to_unix_(year, month, day, hour, min, sec);
i32 offset_sec = loc.offset(unix).seconds();
if (offset_sec != 0)
{
const i64 from = loc.last_offset_from();
const i64 to = loc.last_offset_to();
const i64 utc = unix - offset_sec;
if (utc < from)
{
offset_sec = loc.offset(from - 1).seconds();
}
else if (utc >= to)
{
offset_sec = loc.offset(to).seconds();
}
unix -= offset_sec;
}
sec_ = unix + unix_to_internal_;
}
constexpr point(const year_month_day_hour_minute_second ymdhms,
zone::location& loc) noexcept
: point{ymdhms.ymd.y, ymdhms.ymd.m, ymdhms.ymd.d, ymdhms.hms.h,
ymdhms.hms.m, ymdhms.hms.s, loc}
{
}
constexpr point(i32 year, i32 month, i32 day, i32 hour, i32 min, i32 sec, i64 nano,
i32 offset_sec) noexcept
: sec_{to_unix_(year, month, day, hour, min, sec) - offset_sec + unix_to_internal_},
nano_{0}
{
time::normalize_inplace<1'000'000'000>(sec_, nano);
nano_ = static_cast<u32>(nano);
}
// #### Observers
[[nodiscard]] constexpr year_month_day date() const noexcept
{
return date_(absolute_());
}
[[nodiscard]] constexpr year_month_day date(zone::location& loc) const noexcept
{
return date_(absolute_(loc));
}
[[nodiscard]] constexpr u16 day() const noexcept
{
return date_(absolute_()).d;
}
[[nodiscard]] constexpr u16 day(zone::location& loc) const noexcept
{
return date_(absolute_(loc)).d;
}
[[nodiscard]] constexpr u8 day_of_week() const noexcept
{
return day_of_week_(absolute_());
}
[[nodiscard]] constexpr u8 day_of_week(zone::location& loc) const noexcept
{
return day_of_week_(absolute_(loc));
}
[[nodiscard]] constexpr u16 day_of_year() const noexcept
{
return year_day_of_year_(absolute_()).day_of_year + 1; // 1-366
}
[[nodiscard]] constexpr u16 day_of_year(zone::location& loc) const noexcept
{
return year_day_of_year_(absolute_(loc)).day_of_year + 1; // 1-366
}
[[nodiscard]] constexpr u8 hour() const noexcept
{
return static_cast<u8>((absolute_() % time::seconds_per_day<u64>) /
time::seconds_per_hour<u64>);
}
[[nodiscard]] constexpr u8 hour(zone::location& loc) const noexcept
{
return static_cast<u8>((absolute_(loc) % time::seconds_per_day<u64>) /
time::seconds_per_hour<u64>);
}
[[nodiscard]] constexpr u8 minute() const noexcept
{
return static_cast<u8>((absolute_() % time::seconds_per_hour<u64>) /
time::seconds_per_minute<u64>);
}
[[nodiscard]] constexpr u8 minute(zone::location& loc) const noexcept
{
return static_cast<u8>((absolute_(loc) % time::seconds_per_hour<u64>) /
time::seconds_per_minute<u64>);
}
[[nodiscard]] constexpr u16 month() const noexcept
{
return date_(absolute_()).m;
}
[[nodiscard]] constexpr u16 month(zone::location& loc) const noexcept
{
return date_(absolute_(loc)).m;
}
[[nodiscard]] constexpr u32 nanosecond() const noexcept
{
return nano_;
}
[[nodiscard]] constexpr zone::offset offset(zone::location& loc) const noexcept
{
return loc.offset(sec_ + internal_to_unix_);
}
[[nodiscard]] constexpr u8 second() const noexcept
{
return static_cast<u8>(absolute_() % time::seconds_per_minute<u64>);
}
[[nodiscard]] constexpr u8 second(zone::location& loc) const noexcept
{
return static_cast<u8>(absolute_(loc) % time::seconds_per_minute<u64>);
}
[[nodiscard]] constexpr hour_minute_second time() const noexcept
{
return time_(absolute_());
}
[[nodiscard]] constexpr hour_minute_second time(zone::location& loc) const noexcept
{
return time_(absolute_(loc));
}
[[nodiscard]] constexpr i64 unix() const noexcept
{
return sec_ + internal_to_unix_;
}
[[nodiscard]] constexpr year_week week() const noexcept
{
return week_iso8601_(absolute_());
}
[[nodiscard]] constexpr year_week week(zone::location& loc) const noexcept
{
return week_iso8601_(absolute_(loc));
}
[[nodiscard]] constexpr i32 year() const noexcept
{
return year_day_of_year_(absolute_()).year;
}
[[nodiscard]] constexpr i32 year(zone::location& loc) const noexcept
{
return year_day_of_year_(absolute_(loc)).year;
}
// #### Format
// Format without location.
template <typename Buf>
constexpr void format(const cstrview string, strcore<Buf>& append_to,
promise::no_overlap_t) const
{
snn_should(std::is_constant_evaluated() || !string.overlaps(append_to));
format_(absolute_(), string, zone::offset::utc(), append_to, promise::no_overlap);
}
template <usize N, typename Buf>
constexpr void format(const char (&string)[N], strcore<Buf>& append_to) const
{
format_(absolute_(), string, zone::offset::utc(), append_to, promise::no_overlap);
}
template <any_strcore Str = str>
[[nodiscard]] constexpr Str format(const cstrview string) const
{
Str append_to;
format_(absolute_(), string, zone::offset::utc(), append_to, promise::no_overlap);
return append_to;
}
// Format with location.
template <typename Buf>
constexpr void format(const cstrview string, zone::location& loc, strcore<Buf>& append_to,
promise::no_overlap_t) const
{
snn_should(std::is_constant_evaluated() || !string.overlaps(append_to));
const auto offs = offset(loc);
format_(absolute_(offs.seconds()), string, offs, append_to, promise::no_overlap);
}
template <usize N, typename Buf>
constexpr void format(const char (&string)[N], zone::location& loc,
strcore<Buf>& append_to) const
{
const auto offs = offset(loc);
format_(absolute_(offs.seconds()), string, offs, append_to, promise::no_overlap);
}
template <any_strcore Str = str>
[[nodiscard]] constexpr Str format(const cstrview string, zone::location& loc) const
{
Str append_to;
const auto offs = offset(loc);
format_(absolute_(offs.seconds()), string, offs, append_to, promise::no_overlap);
return append_to;
}
// #### Hash
[[nodiscard]] constexpr usize hash() const noexcept
{
// The hash is currently the unsigned unix time in nanoseconds (this might change in the
// future). This will not overflow for time points between the years 1970 and 2553
// (inclusive).
const u64 unix_in_nano = math::multiply_with_overflow(to_u64(unix()), 1'000'000'000);
return math::add_with_overflow(unix_in_nano, nano_);
}
// #### Addition
// Duration (will never throw).
constexpr point& add(const time::duration d) noexcept
{
time::duration tmp{sec_, nano_, promise::is_valid};
tmp.add(d);
sec_ = tmp.seconds();
nano_ = tmp.nanoseconds();
return *this;
}
constexpr point& operator+=(const time::duration d) noexcept
{
return add(d);
}
[[nodiscard]] constexpr point operator+(const time::duration d) const noexcept
{
return point{*this}.add(d);
}
// Unit (can throw).
template <any_unit Unit>
constexpr point& add(const Unit u)
{
return add(u.duration().value());
}
template <any_unit Unit>
constexpr point& operator+=(const Unit u)
{
return add(u);
}
template <any_unit Unit>
[[nodiscard]] constexpr point operator+(const Unit u) const
{
return point{*this}.add(u);
}
// #### Subtraction
// Duration (will never throw).
constexpr point& subtract(const time::duration d) noexcept
{
time::duration tmp{sec_, nano_, promise::is_valid};
tmp.subtract(d);
sec_ = tmp.seconds();
nano_ = tmp.nanoseconds();
return *this;
}
constexpr point& operator-=(const time::duration d) noexcept
{
return subtract(d);
}
[[nodiscard]] constexpr point operator-(const time::duration d) const noexcept
{
return point{*this}.subtract(d);
}
// Unit (can throw).
template <any_unit Unit>
constexpr point& subtract(const Unit u)
{
return subtract(u.duration().value());
}
template <any_unit Unit>
constexpr point& operator-=(const Unit u)
{
return subtract(u);
}
template <any_unit Unit>
[[nodiscard]] constexpr point operator-(const Unit u) const
{
return point{*this}.subtract(u);
}
// #### Conversion
[[nodiscard]] constexpr time::duration duration() const noexcept
{
return time::duration{sec_ + internal_to_unix_, nano_, promise::is_valid};
}
// ### Comparison
constexpr auto operator<=>(const point&) const noexcept = default;
private:
i64 sec_; // The number of seconds elapsed since January 1, year 1 00:00:00 UTC.
u32 nano_; // Nanoseconds, 0-999'999'999.
// "The unsigned zero year for internal calculations. Must be 1 mod 400, and times before
// it will not compute correctly, but otherwise can be changed at will."
// If this is changed the constants below have to updated as well.
static constexpr i64 abs_zero_year_ = -292277022399;
// The year of the zero time. Assumed by the unix_to_internal_ computation below.
// static constexpr i64 internal_year_ = 1;
// Offsets to convert between internal and absolute times (see calculation below).
static constexpr i64 abs_to_internal_ = -9223371966579724800;
static constexpr i64 internal_to_abs_ = -abs_to_internal_;
// Absolute to internal calculated:
// static constexpr i64 abs_to_internal_ = static_cast<i64>((abs_zero_year_ -
// internal_year_) * 365.2425) * time::seconds_per_day<i64>;
// Offsets to convert between internal and unix times (see calculation below).
static constexpr i64 unix_to_internal_ = 62135596800;
static constexpr i64 internal_to_unix_ = -unix_to_internal_;
// Unix to internal calculated:
// static constexpr i64 unix_to_internal_
// = (1969 * 365 + 1969 / 4 - 1969 / 100 + 1969 / 400) * time::seconds_per_day<i64>;
static constexpr u32 days_per_400_years_ = 146097; // 365 * 400 + 97
static constexpr u32 days_per_100_years_ = 36524; // 365 * 100 + 24
static constexpr u32 days_per_4_years_ = 1461; // 365 * 4 + 1
static constexpr char day_abbr_[7][4]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static constexpr char month_abbr_[12][4]{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct year_day_of_year
{
i32 year; // E.g. 2016.
u16 day_of_year; // 0-365
};
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
static constexpr auto lookup_day_abbr_(const u8 day) noexcept -> const char (&)[4]
{
snn_should(day <= 6);
return day_abbr_[day];
}
static constexpr auto lookup_month_abbr_(const u16 month) noexcept -> const char (&)[4]
{
snn_should(month >= 1 && month <= 12);
return month_abbr_[month - 1];
}
SNN_DIAGNOSTIC_POP
constexpr u64 absolute_() const noexcept
{
// Avoid signed integer overflow, same assembly as:
// to_u64(sec_ + internal_to_abs_)
return math::add_with_overflow(to_u64(sec_), to_u64(internal_to_abs_));
}
constexpr u64 absolute_(const i32 offset_sec) const noexcept
{
// Avoid signed integer overflow, same assembly as:
// to_u64(sec_ + internal_to_abs_ + offset_sec)
return math::add_with_overflow(absolute_(), to_u64(offset_sec));
}
constexpr u64 absolute_(zone::location& loc) const noexcept
{
i64 timestamp = sec_ + internal_to_unix_;
timestamp += loc.offset(timestamp).seconds();
// Avoid signed integer overflow, same assembly as:
// to_u64(timestamp + (unix_to_internal_ + internal_to_abs_));
return to_u64(timestamp) + to_u64(unix_to_internal_ + internal_to_abs_);
}
static constexpr hour_minute_second time_(const u64 abs) noexcept
{
u64 sec = abs % time::seconds_per_day<u64>;
const u64 hour = sec / time::seconds_per_hour<u64>;
sec -= hour * time::seconds_per_hour<u64>;
const u64 min = sec / time::seconds_per_minute<u64>;
sec -= min * time::seconds_per_minute<u64>;
return hour_minute_second{static_cast<u16>(hour), static_cast<u8>(min),
static_cast<u8>(sec)};
}
static constexpr year_month_day date_(const u64 abs) noexcept
{
const auto ydoy = year_day_of_year_(abs);
return year_month_day_(ydoy.year, ydoy.day_of_year);
}
static constexpr u8 day_of_week_(const u64 abs) noexcept
{
// "January 1 of the absolute year, like January 1 of 2001, was a Monday."
// Seconds per day here is seconds after monday.
const u64 sec = (abs + time::seconds_per_day<u64>) % time::seconds_per_week<u64>;
return static_cast<u8>(sec / time::seconds_per_day<u64>);
}
template <typename Buf>
constexpr void format_(const u64 abs, const cstrview string, const zone::offset offs,
strcore<Buf>& append_to, promise::no_overlap_t) const
{
const auto ymd = date_(abs);
const auto hms = time_(abs);
auto rng = string.range();
while (rng)
{
const char c = rng.pop_front(promise::not_empty);
usize repeat_count = 1;
if (chr::is_alpha(c))
{
repeat_count += rng.pop_front_while(fn::is{fn::equal_to{}, c}).count();
}
switch (c)
{
// Cases are ASCII-sorted: A-Z, [backslash], a-z, default.
// AA - Ante meridiem (AM) or post meridiem (PM), abbreviated, upper case.
case 'A':
if (repeat_count == 2)
{
const char prefix = hms.is_am() ? 'A' : 'P';
append_to.append({prefix, 'M'});
break;
}
throw_or_abort(error::invalid_format_string);
// DD - Day of the month, space padded.
case 'D':
if (repeat_count == 2)
{
char* dest = append_to.append_uninitialized(2).begin();
dest = format_2_digits_(' ', ymd.d, dest);
snn_should(dest == append_to.end());
break;
}
throw_or_abort(error::invalid_format_string);
// N - Nanosecond fraction, 1-digit, trimmed. (Tenths of a second.)
// NN - Nanosecond fraction, 2-digit, trimmed. (Hundredths of a second.)
// NNN...
// NNNNNNNNN - Nanosecond fraction, 9-digit, trimmed.
case 'N':
if (repeat_count <= 9)
{
constexpr bool trim = true;
format_nano_(repeat_count, trim, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// OOO - Time zone abbreviation, e.g. "GMT", "CEST" etc.
case 'O':
if (repeat_count == 3)
{
append_to.append(offs.abbr().view());
break;
}
throw_or_abort(error::invalid_format_string);
// Escape, append character after '\', if any.
case '\\':
if (rng)
{
const char escaped = rng.pop_front(promise::not_empty);
append_to.append(escaped);
}
break;
// aa - Ante meridiem (am) or post meridiem (pm), abbreviated, lower case.
case 'a':
if (repeat_count == 2)
{
const char prefix = hms.is_am() ? 'a' : 'p';
append_to.append({prefix, 'm'});
break;
}
throw_or_abort(error::invalid_format_string);
// c - ISO 8601/RFC 3339 (without fraction).
case 'c':
if (repeat_count == 1)
{
append_to.reserve_append(string_size("2001-02-03T04:05:06+07:00"));
format_full_date_(ymd, append_to);
append_to.append('T');
format_full_time_(hms, append_to);
format_full_offset_(offs.seconds(), append_to, ':');
break;
}
throw_or_abort(error::invalid_format_string);
// d - Day of the month.
// dd - Day of the month, zero padded.
case 'd':
if (repeat_count <= 2)
{
format_1_or_2_digits_(repeat_count, '0', ymd.d, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// e - Day of the week (single digit)
// eee - Day of the week (abbreviated name).
case 'e':
if (repeat_count == 3) // Most common.
{
append_to.append(lookup_day_abbr_(day_of_week_(abs)));
break;
}
if (repeat_count == 1)
{
append_to.append(static_cast<char>(day_of_week_(abs) + '0'));
break;
}
throw_or_abort(error::invalid_format_string);
// f - Full date.
case 'f':
if (repeat_count == 1)
{
format_full_date_(ymd, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// g - Hour (12-hour clock).
// gg - Hour (12-hour clock), zero padded.
case 'g':
if (repeat_count <= 2)
{
format_1_or_2_digits_(repeat_count, '0', hms.hour_12(), append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// h - Hour (24-hour clock).
// hh - Hour (24-hour clock), zero padded.
case 'h':
if (repeat_count <= 2)
{
format_1_or_2_digits_(repeat_count, '0', hms.h, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// i - Minute
// ii - Minute, zero padded.
case 'i':
if (repeat_count <= 2)
{
format_1_or_2_digits_(repeat_count, '0', hms.m, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// l - RFC 1123 (time zone abbreviation is always "GMT").
case 'l':
if (repeat_count == 1)
{
append_to.reserve_append(string_size("Sat, 03 Feb 2001 04:05:06 GMT"));
// Day of week.
append_to.append(lookup_day_abbr_(day_of_week_(abs)));
// Separator
append_to.append(", ");
// Day of month.
char* dest = append_to.append_uninitialized(2).begin();
dest = format_2_digits_('0', ymd.d, dest);
snn_should(dest == append_to.end());
// Separator
append_to.append(' ');
// Month abbreviation.
append_to.append(lookup_month_abbr_(ymd.m));
// Separator
append_to.append(' ');
// Year
format_year_(ymd.y, append_to);
// Separator
append_to.append(' ');
// Full time.
format_full_time_(hms, append_to);
// Separator & time zone abbreviation.
append_to.append(" GMT");
break;
}
throw_or_abort(error::invalid_format_string);
// m - Month
// mm - Month, zero padded.
// mmm - Month (abbreviated name).
case 'm':
if (repeat_count <= 2)
{
format_1_or_2_digits_(repeat_count, '0', ymd.m, append_to);
break;
}
if (repeat_count == 3)
{
append_to.append(lookup_month_abbr_(ymd.m));
break;
}
throw_or_abort(error::invalid_format_string);
// n - Nanosecond fraction, 1-digit, fixed. (Tenths of a second.)
// nn - Nanosecond fraction, 2-digit, fixed. (Hundredths of a second.)
// nnn...
// nnnnnnnnn - Nanosecond fraction, 9-digit, fixed.
case 'n':
if (repeat_count <= 9)
{
constexpr bool trim = false;
format_nano_(repeat_count, trim, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// o - Time offset in seconds.
// oooo - Time offset in +/-hours-minutes, e.g. "+0700".
// ooooo - Time offset in +/-hours-colon-minutes, e.g. "+07:00".
case 'o':
if (repeat_count == 4)
{
format_full_offset_(offs.seconds(), append_to);
break;
}
if (repeat_count == 5)
{
format_full_offset_(offs.seconds(), append_to, ':');
break;
}
if (repeat_count == 1)
{
append_to.append_integral(offs.seconds());
break;
}
throw_or_abort(error::invalid_format_string);
// q - Quick/readable with optional fraction.
case 'q':
if (repeat_count == 1)
{
append_to.reserve_append(
string_size("2001-02-03 04:05:06.123456789 +0700"));
format_full_date_(ymd, append_to);
append_to.append(' ');
format_full_time_(hms, append_to);
if (nano_)
{
append_to.append('.');
constexpr bool trim = true;
format_nano_(9, trim, append_to);
}
append_to.append(' ');
format_full_offset_(offs.seconds(), append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// r - RFC 2822.
case 'r':
if (repeat_count == 1)
{
append_to.reserve_append(
string_size("Sat, 03 Feb 2001 04:05:06 +0700"));
// Day of week.
append_to.append(lookup_day_abbr_(day_of_week_(abs)));
// Separator
append_to.append(", ");
// Day of month.
char* dest = append_to.append_uninitialized(2).begin();
dest = format_2_digits_('0', ymd.d, dest);
snn_should(dest == append_to.end());
// Separator
append_to.append(' ');
// Month abbreviation.
append_to.append(lookup_month_abbr_(ymd.m));
// Separator
append_to.append(' ');
// Year
format_year_(ymd.y, append_to);
// Separator
append_to.append(' ');
// Full time.
format_full_time_(hms, append_to);
// Separator
append_to.append(' ');
// Time zone offset.
format_full_offset_(offs.seconds(), append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// s - Second
// ss - Second, zero padded.
case 's':
if (repeat_count <= 2)
{
format_1_or_2_digits_(repeat_count, '0', hms.s, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// t - Full 24-hour time.
case 't':
if (repeat_count == 1)
{
format_full_time_(hms, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// v - Daylight saving time.
case 'v':
if (repeat_count == 1)
{
const char dst = '0' + char{offs.is_dst()};
append_to.append(dst);
break;
}
throw_or_abort(error::invalid_format_string);
// yyyy - Year, 4-digit (minimum), zero padded.
case 'y':
if (repeat_count == 4)
{
format_year_(ymd.y, append_to);
break;
}
throw_or_abort(error::invalid_format_string);
// z - ISO 8601/RFC 3339 with 'Z' timezone (without fraction).
case 'z':
if (repeat_count == 1)
{
append_to.reserve_append(string_size("2001-02-03T04:05:06Z"));
format_full_date_(ymd, append_to);
append_to.append('T');
format_full_time_(hms, append_to);
append_to.append('Z');
break;
}
throw_or_abort(error::invalid_format_string);
default:
if (!chr::is_alpha(c))
{
append_to.append(c);
break;
}
throw_or_abort(error::unescaped_alpha_character);
}
}
}
SNN_DIAGNOSTIC_PUSH
SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
template <typename Buf>
constexpr void format_full_date_(const year_month_day ymd, strcore<Buf>& append_to) const
{
format_year_(ymd.y, append_to);
char* dest = append_to.append_uninitialized(string_size("-02-03")).begin();
*(dest++) = '-';
dest = format_2_digits_('0', ymd.m, dest);
*(dest++) = '-';
dest = format_2_digits_('0', ymd.d, dest);
snn_should(dest == append_to.end());
}
template <typename Buf>
constexpr void format_full_time_(const hour_minute_second hms,
strcore<Buf>& append_to) const
{
char* dest = append_to.append_uninitialized(string_size("04:05:06")).begin();
dest = format_2_digits_('0', hms.h, dest);
*(dest++) = ':';
dest = format_2_digits_('0', hms.m, dest);
*(dest++) = ':';
dest = format_2_digits_('0', hms.s, dest);
snn_should(dest == append_to.end());
}
SNN_DIAGNOSTIC_POP
template <typename Buf>
constexpr void format_nano_(const usize digit_count, const bool trim,
strcore<Buf>& append_to) const
{
array<char, 9> tmp;
tmp.fill('0');
if (nano_)
{
tmp.each_in_reverse([n = nano_](char& c) mutable {
c = static_cast<char>((n % 10) + '0');
n /= 10;
});
}
cstrview formatted = tmp.view(0, digit_count);
if (trim)
{
while (formatted.has_back('0'))
{
formatted.drop_back_n(1);
}
if (formatted.is_empty())
{
if (append_to.has_back('.'))