/
date_time_formatter.ex
3705 lines (2742 loc) · 126 KB
/
date_time_formatter.ex
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
defmodule Cldr.DateTime.Formatter do
@moduledoc """
Functions that implement the formatting for each specific
format symbol.
Each format symbol is an ASCII character in the
range `a-zA-z`. Although not all characters are used as
format symbols, all characters are reserved for that use
requiring that literals be enclosed in single quote
characters, for example `'a literal'`.
Variations of each format are defined by repeating the
format symbol one or more times. CLDR typically defines
an `:abbreviated`, `:wide` and `:narrow` format that is
reprented by a sequence of 3, 4 or 5 format symbols but
this can vary depending on the format symbol.
The [CLDR standard](http://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
defines a wide range of format symbols. Most - but not
all - of these symbols are supported in `Cldr`. The supported
symbols are described below.
## Format Symbol Table
| Element | Symbol | Example | Cldr Format |
| :-------------------- | :-------- | :-------------- | :--------------------------------- |
| Era | G, GG, GGG | "AD" | Abbreviated |
| | GGGG | "Anno Domini" | Wide |
| | GGGGG | "A" | Narrow |
| Year | y | 7 | Minimum necessary digits |
| | yy | "17" | Least significant 2 digits |
| | yyy | "017", "2017" | Padded to at least 3 digits |
| | yyyy | "2017" | Padded to at least 4 digits |
| | yyyyy | "02017" | Padded to at least 5 digits |
| ISOWeek Year | Y | 7 | Minimum necessary digits |
| | YY | "17" | Least significant 2 digits |
| | YYY | "017", "2017" | Padded to at least 3 digits |
| | YYYY | "2017" | Padded to at least 4 digits |
| | YYYYY | "02017" | Padded to at least 5 digits |
| Related Gregorian Year | r, rr, rr+ | 2017 | Minimum necessary digits |
| Cyclic Year | U, UU, UUU | "甲子" | Abbreviated |
| | UUUU | "甲子" (for now) | Wide |
| | UUUUU | "甲子" (for now) | Narrow |
| Extended Year | u+ | 4601 | Minimim necessary digits |
| Quarter | Q | 2 | Single digit |
| | QQ | "02" | Two digits |
| | QQQ | "Q2" | Abbreviated |
| | QQQQ | "2nd quarter" | Wide |
| | QQQQQ | "2" | Narrow |
| Standalone Quarter | q | 2 | Single digit |
| | qq | "02" | Two digits |
| | qqq | "Q2" | Abbreviated |
| | qqqq | "2nd quarter" | Wide |
| | qqqqq | "2" | Narrow |
| Month | M | 9 | Single digit |
| | MM | "09" | Two digits |
| | MMM | "Sep" | Abbreviated |
| | MMMM | "September" | Wide |
| | MMMMM | "S" | Narrow |
| Standalone Month | L | 9 | Single digit |
| | LL | "09" | Two digits |
| | LLL | "Sep" | Abbreviated |
| | LLLL | "September" | Wide |
| | LLLLL | "S" | Narrow |
| Week of Year | w | 2, 22 | Single digit |
| | ww | 02, 22 | Two digits, zero padded |
| Week of Month | W | 2 | Single digit |
| Day of Year | D | 3, 33, 333 | Minimum necessary digits |
| | DD | 03, 33, 333 | Minimum of 2 digits, zero padded |
| | DDD | 003, 033, 333 | Minimum of 3 digits, zero padded |
| Day of Month | d | 2, 22 | Minimum necessary digits |
| | dd | 02, 22 | Two digits, zero padded |
| | ddd | 2nd, 25th | Ordinal day of month |
| Day of Week | E, EE, EEE | "Tue" | Abbreviated |
| | EEEE | "Tuesday" | Wide |
| | EEEEE | "T" | Narrow |
| | EEEEEE | "Tu" | Short |
| | e | 2 | Single digit |
| | ee | "02" | Two digits |
| | eee | "Tue" | Abbreviated |
| | eeee | "Tuesday" | Wide |
| | eeeee | "T" | Narrow |
| | eeeeee | "Tu" | Short |
| Standalone Day of Week | c, cc | 2 | Single digit |
| | ccc | "Tue" | Abbreviated |
| | cccc | "Tuesday" | Wide |
| | ccccc | "T" | Narrow |
| | cccccc | "Tu" | Short |
| AM or PM | a, aa, aaa | "am." | Abbreviated |
| | aaaa | "am." | Wide |
| | aaaaa | "am" | Narrow |
| Noon, Mid, AM, PM | b, bb, bbb | "mid." | Abbreviated |
| | bbbb | "midnight" | Wide |
| | bbbbb | "md" | Narrow |
| Flexible time period | B, BB, BBB | "at night" | Abbreviated |
| | BBBB | "at night" | Wide |
| | BBBBB | "at night" | Narrow |
| Hour | h, K, H, k | | See the table below |
| Minute | m | 3, 10 | Minimim digits of minutes |
| | mm | "03", "12" | Two digits, zero padded |
| Second | s | 3, 48 | Minimim digits of seconds |
| | ss | "03", "48" | Two digits, zero padded |
| Fractional Seconds | S | 3, 48 | Minimim digits of fractional seconds |
| | SS | "03", "48" | Two digits, zero padded |
| Milliseconds | A+ | 4000, 63241 | Minimim digits of milliseconds since midnight |
| Generic non-location TZ | v | "Etc/UTC" | `:time_zone` key, unlocalised |
| | vvvv | "unk" | Generic timezone name. Currently returns only "unk" |
| Specific non-location TZ | z..zzz | "UTC" | `:zone_abbr` key, unlocalised |
| | zzzz | "GMT" | Delegates to `zone_gmt/4` |
| Timezone ID | V | "unk" | `:zone_abbr` key, unlocalised |
| | VV | "Etc/UTC | Delegates to `zone_gmt/4` |
| | VVV | "Unknown City" | Exemplar city. Not supported. |
| | VVVV | "GMT" | Delegates to `zone_gmt/4 |
| ISO8601 Format | Z..ZZZ | "+0100" | ISO8601 Basic Format with hours and minutes |
| | ZZZZ | "+01:00" | Delegates to `zone_gmt/4 |
| | ZZZZZ | "+01:00:10" | ISO8601 Extended format with optional seconds |
| ISO8601 plus Z | X | "+01" | ISO8601 Basic Format with hours and optional minutes or "Z" |
| | XX | "+0100" | ISO8601 Basic Format with hours and minutes or "Z" |
| | XXX | "+0100" | ISO8601 Basic Format with hours and minutes, optional seconds or "Z" |
| | XXXX | "+010059" | ISO8601 Basic Format with hours and minutes, optional seconds or "Z" |
| | XXXXX | "+01:00:10" | ISO8601 Extended Format with hours and minutes, optional seconds or "Z" |
| ISO8601 minus Z | x | "+0100" | ISO8601 Basic Format with hours and optional minutes |
| | xx | "-0800" | ISO8601 Basic Format with hours and minutes |
| | xxx | "+01:00" | ISO8601 Extended Format with hours and minutes |
| | xxxx | "+010059" | ISO8601 Basic Format with hours and minutes, optional seconds |
| | xxxxx | "+01:00:10" | ISO8601 Extended Format with hours and minutes, optional seconds |
| GMT Format | O | "+0100" | Short localised GMT format |
| | OOOO | "+010059" | Long localised GMT format |
## Formatting symbols for hour of day
The hour of day can be formatted differently depending whether
a 12- or 24-hour day is being represented and depending on the
way in which midnight and noon are represented. The following
table illustrates the differences:
| Symbol | Midn. | Morning | Noon | Afternoon | Midn. |
| :----: | :---: | :-----: | :--: | :--------: | :---: |
| h | 12 | 1...11 | 12 | 1...11 | 12 |
| K | 0 | 1...11 | 0 | 1...11 | 0 |
| H | 0 | 1...11 | 12 | 13...23 | 0 |
| k | 24 | 1...11 | 12 | 13...23 | 24 |
"""
alias Cldr.DateTime.Timezone
alias Cldr.Calendar.Gregorian
alias Cldr.Locale
@default_format 1
@doc """
Returns a formatted date.
DateTime formats are defined in CLDR using substitution rules whereby
the Date and/or Time are substituted into a format string. Therefore
this function crafts a date format string which is then inserted into
the overall format being requested.
"""
@spec date(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def date(date, n \\ @default_format, options \\ [])
def date(date, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
date(date, @default_format, locale, backend, options)
end
def date(date, n, options) do
{locale, backend} = extract_locale!(options)
date(date, n, locale, backend, options)
end
@spec date(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), Keyword.t() | map()) ::
String.t() | {:error, {module(), String.t()}}
def date(date, _n, _locale, backend, options) when is_list(options) do
with {:ok, date_string} <- Cldr.Date.to_string(date, backend, options) do
date_string
end
end
def date(date, n, locale, backend, options) when is_map(options) do
date(date, n, locale, backend, Map.to_list(options))
end
@doc """
Returns a formatted time.
DateTime formats are defined in CLDR using substitution rules whereby
the Date and/or Time are substituted into a format string. Therefore
this function crafts a time format string which is then inserted into
the overall format being requested.
"""
@spec time(Calendar.time(), integer, Keyword.t()) ::
String.t() | {:error, {module(), String.t()}}
def time(time, n \\ @default_format, options \\ [])
def time(time, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
time(time, @default_format, locale, backend, options)
end
def time(time, n, options) do
{locale, backend} = extract_locale!(options)
time(time, n, locale, backend, options)
end
@spec time(Calendar.time(), integer, Locale.locale_reference(), Cldr.backend(), Keyword.t() | map()) ::
String.t() | {:error, String.t()}
def time(time, _n, _locale, backend, options) when is_list(options) do
with {:ok, time_string} <- Cldr.Time.to_string(time, backend, options) do
time_string
end
end
def time(time, n, locale, backend, options) when is_map(options) do
time(time, n, locale, backend, Map.to_list(options))
end
@doc """
Returns the `era` (format symbol `G`) of a date
for given locale.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the year
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. The only applicable
option is `:era` with a value of either `nil` (the default) or
`:variant` which will return the variant form of an era if one
is available.
## Format Symbol
The representation of the era is made in accordance
with the following table:
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :---------------------------|
| G, GG, GGG | "AD" | Abbreviated |
| GGGG | "Anno Domini | Wide |
| GGGGG | "A" | Narrow |
## Examples
iex> Cldr.DateTime.Formatter.era ~D[2017-12-01], 1
"AD"
iex> Cldr.DateTime.Formatter.era ~D[2017-12-01], 4, "fr", MyApp.Cldr
"après Jésus-Christ"
"""
@spec era(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def era(era, n \\ @default_format, options \\ [])
def era(era, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
era(era, @default_format, locale, backend, Map.new(options))
end
def era(era, n, options) do
{locale, backend} = extract_locale!(options)
era(era, n, locale, backend, Map.new(options))
end
@spec era(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def era(date, n, locale, backend, options \\ %{})
def era(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> era(n, locale, backend, options)
end
def era(date, n, locale, backend, options) when n in 1..3 do
Cldr.Calendar.localize(date, :era, :format, :abbreviated, backend, locale)
|> maybe_wrap(:era, options)
end
def era(date, 4, locale, backend, options) do
Cldr.Calendar.localize(date, :era, :format, :wide, backend, locale)
|> maybe_wrap(:era, options)
end
def era(date, 5, locale, backend, options) do
Cldr.Calendar.localize(date, :era, :format, :narrow, backend, locale)
|> maybe_wrap(:era, options)
end
def era(date, _n, _locale, _backend, _options) do
error_return(date, "G", [:year, :month, :day, :calendar])
end
@doc """
Returns the `year` (format symbol `y`) of a date
as an integer. The `y` format returns the year
as a simple integer in string format.
The format `yy` is a special case which requests just
the two low-order digits of the year, zero-padded
as necessary. For most use cases, `y` or `yy` should
be adequate.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the year
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. There are no options
used in `year/4`
## Format Symbol
The representation of the quarter is made in accordance
with the following table:
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :---------------------------|
| y | 7 | Minimum necessary digits |
| yy | "17" | Least significant 2 digits |
| yyy | "017", "2017" | Padded to at least 3 digits |
| yyyy | "2017" | Padded to at least 4 digits |
| yyyyy | "02017" | Padded to at least 5 digits |
In most cases the length of the `y` field specifies
the minimum number of digits to display, zero-padded
as necessary; more digits will be displayed if needed
to show the full year.
## Examples
iex> Cldr.DateTime.Formatter.year %{year: 2017, calendar: Calendar.ISO}, 1
2017
iex> Cldr.DateTime.Formatter.year %{year: 2017, calendar: Calendar.ISO}, 2
"17"
iex> Cldr.DateTime.Formatter.year %{year: 2017, calendar: Calendar.ISO}, 3
"2017"
iex> Cldr.DateTime.Formatter.year %{year: 2017, calendar: Calendar.ISO}, 4
"2017"
iex> Cldr.DateTime.Formatter.year %{year: 2017, calendar: Calendar.ISO}, 5
"02017"
"""
@spec year(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def year(year, n \\ @default_format, options \\ [])
def year(year, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
year(year, @default_format, locale, backend, Map.new(options))
end
def year(year, n, options) do
{locale, backend} = extract_locale!(options)
year(year, n, locale, backend, Map.new(options))
end
@spec year(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def year(date, n, locale, backend, options \\ %{})
def year(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> year(n, locale, backend, options)
end
def year(%{year: _year} = date, 1, _locale, backend, options) do
date
|> Cldr.Calendar.calendar_year()
|> transliterate("y", backend, options)
|> maybe_wrap(:year, options)
end
def year(%{year: _year} = date, 2 = n, _locale, backend, options) do
date
|> Cldr.Calendar.calendar_year()
|> rem(100)
|> transliterate("y", backend, options)
|> pad(n)
|> maybe_wrap(:year, options)
end
def year(%{year: _year} = date, n, _locale, backend, options) do
date
|> Cldr.Calendar.calendar_year()
|> transliterate("y", backend, options)
|> pad(n)
|> maybe_wrap(:year, options)
end
def year(date, _n, _locale, _backend, _options) do
error_return(date, "y", [:year])
end
@doc """
Returns the `year` (format symbol `Y`) in “Week of Year”
based calendars in which the year transition occurs
on a week boundary.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the year
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. There are no options
used in `weeK_aligned_year/4`
## Format Symbol
The representation of the year is made in accordance
with the following table:
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :---------------------------|
| Y | 7 | Minimum necessary digits |
| YY | "17" | Least significant 2 digits |
| YYY | "017", "2017" | Padded to at least 3 digits |
| YYYY | "2017" | Padded to at least 4 digits |
| YYYYY | "02017" | Padded to at least 5 digits |
The result may differ from calendar year ‘y’ near
a year transition. This numeric year designation
is used in conjunction with pattern character ‘w’
in the ISO year-week calendar as defined
by ISO 8601, but can be used in non-Gregorian based
calendar systems where week date processing is desired.
The field length is interpreted in the same was as for
`y`; that is, `yy` specifies use of the two low-order
year digits, while any other field length specifies a
minimum number of digits to display.
## Examples
iex> Cldr.DateTime.Formatter.week_aligned_year ~D[2017-01-04], 1
"2017"
iex> Cldr.DateTime.Formatter.week_aligned_year ~D[2017-01-04], 2
"17"
iex> Cldr.DateTime.Formatter.week_aligned_year ~D[2017-01-04], 3
"2017"
iex> Cldr.DateTime.Formatter.week_aligned_year ~D[2017-01-04], 4
"2017"
iex> Cldr.DateTime.Formatter.week_aligned_year ~D[2017-01-04], 5
"02017"
"""
@spec week_aligned_year(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def week_aligned_year(week_aligned_year, n \\ @default_format, options \\ [])
def week_aligned_year(week_aligned_year, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
week_aligned_year(week_aligned_year, @default_format, locale, backend, Map.new(options))
end
def week_aligned_year(week_aligned_year, n, options) do
{locale, backend} = extract_locale!(options)
week_aligned_year(week_aligned_year, n, locale, backend, Map.new(options))
end
@spec week_aligned_year(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def week_aligned_year(date, n, locale, backend, options \\ %{})
def week_aligned_year(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> week_aligned_year(n, locale, backend, options)
end
def week_aligned_year(date, 1, _locale, _backend, options) do
{year, _week} = Cldr.Calendar.week_of_year(date)
to_string(year)
|> maybe_wrap(:week_aligned_year, options)
end
def week_aligned_year(date, 2 = n, _locale, _backend, options) do
{year, _week} = Cldr.Calendar.week_of_year(date)
year
|> rem(100)
|> pad(n)
|> maybe_wrap(:week_aligned_year, options)
end
def week_aligned_year(date, n, _locale, _backend, options) when n in 3..5 do
{year, _week} = Cldr.Calendar.week_of_year(date)
pad(year, n)
|> maybe_wrap(:week_aligned_year, options)
end
def week_aligned_year(date, _n, _locale, _backend, _options) do
error_return(date, "Y", [:year, :month, :day, :calendar])
end
@doc """
Returns the Extended year (format symbol `u`).
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the year
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. There are no options
used in `weeK_aligned_year/4`
**NOTE: This current implementation always returns
the year provided in the supplied date. This means
`u` returns the same result as the format `y`.**
## Format Symbol
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :------------------------ |
| u+ | 4601 | Minimim necessary digits |
This is a single number designating the year of this
calendar system, encompassing all supra-year fields.
For example, for the Julian calendar system, year
numbers are positive, with an era of BCE or CE. An
extended year value for the Julian calendar system
assigns positive values to CE years and negative
values to BCE years, with 1 BCE being year 0.
For `u`, all field lengths specify a minimum number of
digits; there is no special interpretation for `uu`.
"""
@spec extended_year(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def extended_year(extended_year, n \\ @default_format, options \\ [])
def extended_year(extended_year, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
extended_year(extended_year, @default_format, locale, backend, Map.new(options))
end
def extended_year(extended_year, n, options) do
{locale, backend} = extract_locale!(options)
extended_year(extended_year, n, locale, backend, Map.new(options))
end
@spec extended_year(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def extended_year(date, n, locale, backend, options \\ %{})
def extended_year(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> extended_year(n, locale, backend, options)
end
def extended_year(%{year: year, calendar: Calendar.ISO}, n, _locale, _backend, options) do
year
|> pad(n)
|> maybe_wrap(:extended_year, options)
end
def extended_year(%{year: year}, n, _locale, _backend, options) do
year
|> pad(n)
|> maybe_wrap(:extended_year, options)
end
def extended_year(date, _n, _locale, _backend, _options) do
error_return(date, "u", [:year, :calendar])
end
@doc """
Returns the cyclic year (format symbol `U`) name for
non-gregorian calendars.
**NOTE: In the current implementation, the cyclic year is
delegated to `Cldr.DateTime.Formatter.year/3`
(format symbol `y`) and does not return a localized
cyclic year.**
## Format Symbol
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :-------------- |
| U, UU, UUU | "甲子" | Abbreviated |
| UUUU | "甲子" (for now) | Wide |
| UUUUU | "甲子" (for now) | Narrow |
Calendars such as the Chinese lunar
calendar (and related calendars) and the Hindu calendars
use 60-year cycles of year names. If the calendar does
not provide cyclic year name data, or if the year value
to be formatted is out of the range of years for which
cyclic name data is provided, then numeric formatting
is used (behaves like format symbol `y`).
Currently the CLDR data only provides abbreviated names,
which will be used for all requested name widths.
"""
@spec cyclic_year(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def cyclic_year(cyclic_year, n \\ @default_format, options \\ [])
def cyclic_year(cyclic_year, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
cyclic_year(cyclic_year, @default_format, locale, backend, Map.new(options))
end
def cyclic_year(cyclic_year, n, options) do
{locale, backend} = extract_locale!(options)
cyclic_year(cyclic_year, n, locale, backend, Map.new(options))
end
@spec cyclic_year(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def cyclic_year(date, n, locale, backend, options \\ %{})
def cyclic_year(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> cyclic_year(n, locale, backend, options)
end
def cyclic_year(%{year: year}, _n, _locale, _backend, options) do
maybe_wrap(year, :cyclic_year, options)
end
def cyclic_year(date, _n, _locale, _backend, _options) do
error_return(date, "U", [:year])
end
@doc """
Returns the related gregorian year (format symbol `r`)
of a date for given locale.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the quarter
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. There are no options
used in `related_year/4`
## Format Symbol
The representation of the related year is made in accordance
with the following table:
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :-------------- |
| r+ | 2017 | |
This corresponds to the extended Gregorian year
in which the calendar’s year begins. Related
Gregorian years are often displayed, for example,
when formatting dates in the Japanese calendar —
e.g. “2012(平成24)年1月15日” — or in the Chinese
calendar — e.g. “2012壬辰年腊月初四”. The related
Gregorian year is usually displayed using the
":latn" numbering system, regardless of what
numbering systems may be used for other parts
of the formatted date.
If the calendar’s year is linked to the solar
year (perhaps using leap months), then for that
calendar the ‘r’ year will always be at a fixed
offset from the ‘u’ year.
For the Gregorian calendar, the ‘r’ year
is the same as the ‘u’ year. For ‘r’, all field
lengths specify a minimum number of digits; there
is no special interpretation for “rr”.
"""
@spec related_year(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def related_year(related_year, n \\ @default_format, options \\ [])
def related_year(related_year, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
related_year(related_year, @default_format, locale, backend, Map.new(options))
end
def related_year(related_year, n, options) do
{locale, backend} = extract_locale!(options)
related_year(related_year, n, locale, backend, Map.new(options))
end
@spec related_year(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def related_year(date, n, locale, backend, options \\ %{})
def related_year(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> related_year(n, locale, backend, options)
end
def related_year(%{year: year, calendar: Gregorian}, n, _locale, _backend, options)
when n in 1..5 do
maybe_wrap(year, :related_year, options)
end
def related_year(date, n, _locale, _backend, options) when n in 1..5 do
date
|> Date.convert!(Gregorian)
|> Map.get(:year)
|> maybe_wrap(:related_year, options)
end
def related_year(date, _n, _locale, _backend, _options) do
error_return(date, "r", [:year, :calendar])
end
@doc """
Returns the `quarter` (format symbol `Q`) of a date
for given locale.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the quarter
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. There are no options
used in `quarter/5`
## Format Symbol
The representation of the quarter is made in accordance
with the following table:
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :-------------- |
| Q | 2 | Single digit |
| QQ | "02" | Two digits |
| QQQ | "Q2" | Abbreviated |
| QQQQ | "2nd quarter" | Wide |
| QQQQQ | "2" | Narrow |
## Examples
iex> Cldr.DateTime.Formatter.quarter ~D[2017-04-01], 1
2
iex> Cldr.DateTime.Formatter.quarter ~D[2017-04-01], 2
"02"
iex> Cldr.DateTime.Formatter.quarter ~D[2017-04-01], 3
"Q2"
iex> Cldr.DateTime.Formatter.quarter ~D[2017-04-01], 4
"2nd quarter"
iex> Cldr.DateTime.Formatter.quarter ~D[2017-04-01], 5
"2"
"""
@spec quarter(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def quarter(quarter, n \\ @default_format, options \\ [])
def quarter(quarter, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
quarter(quarter, @default_format, locale, backend, Map.new(options))
end
def quarter(quarter, n, options) do
{locale, backend} = extract_locale!(options)
quarter(quarter, n, locale, backend, Map.new(options))
end
@spec quarter(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def quarter(date, n, locale, backend, options \\ %{})
def quarter(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> quarter(n, locale, backend, options)
end
def quarter(date, 1, _locale, _backend, options) do
date
|> Cldr.Calendar.quarter_of_year()
|> maybe_wrap(:quarter, options)
end
def quarter(date, 2, _locale, _backend, options) do
date
|> Cldr.Calendar.quarter_of_year()
|> pad(2)
|> maybe_wrap(:quarter, options)
end
def quarter(date, 3, locale, backend, options) do
date
|> Cldr.Calendar.localize(:quarter, :format, :abbreviated, backend, locale)
|> maybe_wrap(:quarter, options)
end
def quarter(date, 4, locale, backend, options) do
date
|> Cldr.Calendar.localize(:quarter, :format, :wide, backend, locale)
|> maybe_wrap(:quarter, options)
end
def quarter(date, 5, locale, backend, options) do
date
|> Cldr.Calendar.localize(:quarter, :format, :narrow, backend, locale)
|> maybe_wrap(:quarter, options)
end
def quarter(date, _n, _locale, _backend, _options) do
error_return(date, "Q", [:month])
end
@doc """
Returns the standalone `quarter` (format symbol `a`) of a date
for given locale.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the quarter
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
* `options` is a `Keyword` list of options. There are no options
used in `standalone_quarter/5`
## Format Symbol
The representation of the quarter is made in accordance
with the following table:
| Symbol | Example | Cldr Format |
| :-------- | :-------------- | :-------------- |
| q | 2 | Single digit |
| qq | "02" | Two digits |
| qqq | "Q2" | Abbreviated |
| qqqq | "2nd quarter" | Wide |
| qqqqq | "2" | Narrow |
## Examples
iex> Cldr.DateTime.Formatter.standalone_quarter ~D[2019-06-08], 1
2
iex> Cldr.DateTime.Formatter.standalone_quarter ~D[2019-06-08], 2
"02"
iex> Cldr.DateTime.Formatter.standalone_quarter ~D[2019-06-08], 3
"Q2"
iex> Cldr.DateTime.Formatter.standalone_quarter ~D[2019-06-08], 4
"2nd quarter"
iex> Cldr.DateTime.Formatter.standalone_quarter ~D[2019-06-08], 5
"2"
"""
@spec standalone_quarter(Calendar.date(), integer, Keyword.t()) ::
String.t() | {:error, String.t()}
def standalone_quarter(standalone_quarter, n \\ @default_format, options \\ [])
def standalone_quarter(standalone_quarter, options, []) when is_list(options) do
{locale, backend} = extract_locale!(options)
standalone_quarter(standalone_quarter, @default_format, locale, backend, Map.new(options))
end
def standalone_quarter(standalone_quarter, n, options) do
{locale, backend} = extract_locale!(options)
standalone_quarter(standalone_quarter, n, locale, backend, Map.new(options))
end
@spec standalone_quarter(Calendar.date(), integer, Locale.locale_reference(), Cldr.backend(), map()) ::
String.t() | {:error, String.t()}
def standalone_quarter(date, n, locale, backend, options \\ %{})
def standalone_quarter(%{calendar: Calendar.ISO} = date, n, locale, backend, options) do
%{date | calendar: Cldr.Calendar.Gregorian}
|> standalone_quarter(n, locale, backend, options)
end
def standalone_quarter(date, 1, _locale, _backend, options) do
date
|> Cldr.Calendar.quarter_of_year()
|> maybe_wrap(:standalone_quarter, options)
end
def standalone_quarter(date, 2, _locale, _backend, options) do
date
|> Cldr.Calendar.quarter_of_year()
|> pad(2)
|> maybe_wrap(:standalone_quarter, options)
end
def standalone_quarter(date, 3, locale, backend, options) do
date
|> Cldr.Calendar.localize(:quarter, :stand_alone, :abbreviated, backend, locale)
|> maybe_wrap(:standalone_quarter, options)
end
def standalone_quarter(date, 4, locale, backend, options) do
date
|> Cldr.Calendar.localize(:quarter, :stand_alone, :wide, backend, locale)
|> maybe_wrap(:standalone_quarter, options)
end
def standalone_quarter(date, 5, locale, backend, options) do
date
|> Cldr.Calendar.localize(:quarter, :stand_alone, :narrow, backend, locale)
|> maybe_wrap(:standalone_quarter, options)
end
def standalone_quarter(date, _n, _locale, _backend, _options) do
error_return(date, "q", [:month])
end
@doc """
Returns the `month` (format symbol `M`) of a date
for given locale.
## Arguments
* `date` is a `Date` struct or any map that contains at least the
keys `:month` and `:calendar`
* `n` in an integer between 1 and 5 that determines the format of
the month
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`