-
Notifications
You must be signed in to change notification settings - Fork 1
/
lunes_test.go
1003 lines (937 loc) · 29.5 KB
/
lunes_test.go
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package lunes
import (
"errors"
"fmt"
"log"
"strings"
"testing"
"time"
)
type ParseTestLocale struct {
lang string
value string
expectedErr error
}
type ParseTest struct {
name string
format string
stdValue string
hasTZ bool // has time zone
hasWD bool // has a weekday
yearSign int // -1 indicates the year is not present in the layout
locales *[]ParseTestLocale
}
//nolint:varcheck // order matters
const (
sun = iota
mon
tue
wed
thu
fri
sat
)
//nolint:varcheck // order matters
const (
jan = iota
feb
mar
apr
may
jun
jul
aug
sep
oct
nov
dec
)
const (
am = iota
pm = iota
)
type replacement struct {
field int
value int
}
var emptyReplacements []replacement
func allLocalesTests(valuePattern string, replacements []replacement) *[]ParseTestLocale {
return genAllLocalesTests(valuePattern, replacements, func(v string) string { return v })
}
func genAllLocalesTests(valuePattern string, replacements []replacement, formatter func(v string) string) *[]ParseTestLocale {
var tests []ParseTestLocale
for lang := range tables {
test := ParseTestLocale{lang: lang}
locale := genericLocale{lang: lang, table: tables[lang]}
args := make([]any, 0, len(replacements)/2)
var unsupportedElem string
for _, repl := range replacements {
fieldVal := getLocaleFieldValue(&locale, repl.field)
if len(fieldVal) == 0 {
// fallback to english so it replaces the placeholders with any value
fieldVal = tables["en"][repl.field]
if unsupportedElem == "" {
switch repl.field {
case shortDayNamesField, longDayNamesField:
unsupportedElem = fieldVal[mon]
case shortMonthNamesField, longMonthNamesField:
unsupportedElem = fieldVal[jan]
case dayPeriodsField:
unsupportedElem = "PM"
}
}
}
var fv string
if formatter != nil {
fv = formatter(fieldVal[repl.value])
} else {
fv = fieldVal[repl.value]
}
args = append(args, fv)
}
if unsupportedElem != "" {
test.expectedErr = newUnsupportedLayoutElemError(unsupportedElem, &locale)
}
test.value = fmt.Sprintf(valuePattern, args...)
tests = append(tests, test)
}
return &tests
}
func getLocaleFieldValue(locale Locale, field int) []string {
switch field {
case shortDayNamesField:
return locale.ShortDayNames()
case longDayNamesField:
return locale.LongDayNames()
case shortMonthNamesField:
return locale.ShortMonthNames()
case longMonthNamesField:
return locale.LongMonthNames()
case dayPeriodsField:
return locale.DayPeriods()
default:
return nil
}
}
// Most of these tests were taken from the time.format_test.go file.
// Even if it's not translating any placeholder, it should keep working and don't break the underline
// time.Parse functionality.
var parseTests = []ParseTest{
{
name: "ANSIC",
format: time.ANSIC,
stdValue: "Thu May 1 21:00:57 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 1 21:00:57 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, may}})},
{
name: "UnixDate",
format: time.UnixDate,
stdValue: "Fri Sep 4 21:00:57 PST 2010",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57 PST 2010", []replacement{{shortDayNamesField, fri}, {shortMonthNamesField, sep}}),
},
{
name: "RubyDate",
format: time.RubyDate,
stdValue: "Tue Dec 05 21:00:57 -0800 2010",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 05 21:00:57 -0800 2010", []replacement{{shortDayNamesField, tue}, {shortMonthNamesField, dec}}),
},
{
name: "RFC850",
format: time.RFC850,
stdValue: "Tuesday, 04-Dec-10 21:00:57 PST",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04-%s-10 21:00:57 PST", []replacement{{longDayNamesField, tue}, {shortMonthNamesField, dec}}),
},
{
name: "RFC1123",
format: time.RFC1123,
stdValue: "Thu, 04 Feb 2010 21:00:57 PST",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04 %s 2010 21:00:57 PST", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC1123",
format: time.RFC1123,
stdValue: "Thu, 04 Feb 2010 22:00:57 PDT",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04 %s 2010 22:00:57 PDT", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC1123Z",
format: time.RFC1123Z,
stdValue: "Thu, 04 Feb 2010 21:00:57 -0800",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04 %s 2010 21:00:57 -0800", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC3339",
format: time.RFC3339,
stdValue: "2010-02-04T21:00:57-08:00",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04T21:00:57-08:00", emptyReplacements),
},
{
name: "custom: \"2006-01-02 15:04:05-07\"",
format: "2006-01-02 15:04:05-07",
stdValue: "2010-02-04 21:00:57-08",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57-08", emptyReplacements),
},
// Optional fractional seconds
{
name: "ANSIC",
format: time.ANSIC,
stdValue: "Thu Feb 4 21:00:57.0 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.0 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "UnixDate",
format: time.UnixDate,
stdValue: "Thu Feb 4 21:00:57.01 PST 2010",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.01 PST 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RubyDate",
format: time.RubyDate,
stdValue: "Thu Feb 04 21:00:57.012 -0800 2010",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 04 21:00:57.012 -0800 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC850",
format: time.RFC850,
stdValue: "Thursday, 04-Feb-10 21:00:57.0123 PST",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04-%s-10 21:00:57.0123 PST", []replacement{{longDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC1123",
format: time.RFC1123,
stdValue: "Thu, 04 Feb 2010 21:00:57.01234 PST",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04 %s 2010 21:00:57.01234 PST", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC1123Z",
format: time.RFC1123Z,
stdValue: "Thu, 04 Feb 2010 21:00:57.01234 -0800",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s, 04 %s 2010 21:00:57.01234 -0800", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "RFC3339",
format: time.RFC3339,
stdValue: "2010-02-04T21:00:57.012345678-08:00",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04T21:00:57.012345678-08:00", emptyReplacements),
},
{
name: "custom: \"2006-01-02 15:04:05\"",
format: "2006-01-02 15:04:05",
stdValue: "2010-02-04 21:00:57.0",
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.0", emptyReplacements),
},
// Amount of white space should not matter.
{
name: "ANSIC",
format: time.ANSIC,
stdValue: "Thu Feb 4 21:00:57 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "ANSIC",
format: time.ANSIC,
stdValue: "Thu Feb 4 21:00:57 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
// Case should not matter
{
name: "ANSIC",
format: time.ANSIC,
stdValue: "THU FEB 4 21:00:57 2010",
hasWD: true,
yearSign: 1,
locales: genAllLocalesTests("%s %s 4 21:00:57 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}, strings.ToUpper),
},
{
name: "ANSIC",
format: time.ANSIC,
stdValue: "thu feb 4 21:00:57 2010",
hasWD: true,
yearSign: 1,
locales: genAllLocalesTests("%s %s 4 21:00:57 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}, strings.ToLower),
},
// Fractional seconds
{
name: "millisecond:: dot separator",
format: "Mon Jan _2 15:04:05.000 2006",
stdValue: "Thu Feb 4 21:00:57.012 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.012 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "microsecond:: dot separator",
format: "Mon Jan _2 15:04:05.000000 2006",
stdValue: "Thu Feb 4 21:00:57.012345 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.012345 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "nanosecond:: dot separator",
format: "Mon Jan _2 15:04:05.000000000 2006",
stdValue: "Thu Feb 4 21:00:57.012345678 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.012345678 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "millisecond:: comma separator",
format: "Mon Jan _2 15:04:05,000 2006",
stdValue: "Thu Feb 4 21:00:57.012 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.012 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "microsecond:: comma separator",
format: "Mon Jan _2 15:04:05,000000 2006",
stdValue: "Thu Feb 4 21:00:57.012345 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.012345 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
{
name: "nanosecond:: comma separator",
format: "Mon Jan _2 15:04:05,000000000 2006",
stdValue: "Thu Feb 4 21:00:57.012345678 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 4 21:00:57.012345678 2010", []replacement{{shortDayNamesField, thu}, {shortMonthNamesField, feb}}),
},
// Leading zeros in other places should not be taken as fractional seconds.
{
name: "zero1",
format: "2006.01.02.15.04.05.0",
stdValue: "2010.02.04.21.00.57.0",
yearSign: 1,
locales: allLocalesTests("2010.02.04.21.00.57.0", emptyReplacements),
},
{
name: "zero2",
format: "2006.01.02.15.04.05.00",
stdValue: "2010.02.04.21.00.57.01",
yearSign: 1,
locales: allLocalesTests("2010.02.04.21.00.57.01", emptyReplacements),
},
// Month and day names only match when not followed by a lower-case letter.
{
name: "Janet",
format: "Hi Janet, the Month is January: Jan _2 15:04:05 2006",
stdValue: "Hi Janet, the Month is February: Feb 4 21:00:57 2010",
hasWD: true,
yearSign: 1,
locales: allLocalesTests("Hi Janet, the Month is %s: %s 4 21:00:57 2010", []replacement{{longMonthNamesField, feb}, {shortMonthNamesField, feb}}),
},
// GMT with offset.
{
name: "GMT-8",
format: time.UnixDate,
stdValue: "Fri Feb 5 05:00:57 GMT-8 2010",
hasTZ: true,
hasWD: true,
yearSign: 1,
locales: allLocalesTests("%s %s 5 05:00:57 GMT-8 2010", []replacement{{shortDayNamesField, fri}, {shortMonthNamesField, feb}}),
},
// Accept any number of fractional second digits (including none)
{
format: "2006-01-02 15:04:05.9999 -0700 MST",
stdValue: "2010-02-04 21:00:57 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05.999999999 -0700 MST",
stdValue: "2010-02-04 21:00:57 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05.9999 -0700 MST",
stdValue: "2010-02-04 21:00:57.0123 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.0123 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05.999999999 -0700 MST",
stdValue: "2010-02-04 21:00:57.0123 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.0123 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05.9999 -0700 MST",
stdValue: "2010-02-04 21:00:57.012345678 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.012345678 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05.999999999 -0700 MST",
stdValue: "2010-02-04 21:00:57.012345678 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.012345678 -0800 PST", emptyReplacements),
},
// Comma "," separator.
{
format: "2006-01-02 15:04:05,9999 -0700 MST",
stdValue: "2010-02-04 21:00:57 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05,999999999 -0700 MST",
stdValue: "2010-02-04 21:00:57 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05,9999 -0700 MST",
stdValue: "2010-02-04 21:00:57.0123 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.0123 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05,999999999 -0700 MST",
stdValue: "2010-02-04 21:00:57.0123 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.0123 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05,9999 -0700 MST",
stdValue: "2010-02-04 21:00:57.012345678 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.012345678 -0800 PST", emptyReplacements),
},
{
format: "2006-01-02 15:04:05,999999999 -0700 MST",
stdValue: "2010-02-04 21:00:57.012345678 -0800 PST",
hasTZ: true,
yearSign: 1,
locales: allLocalesTests("2010-02-04 21:00:57.012345678 -0800 PST", emptyReplacements),
},
{
format: time.StampNano,
stdValue: "Feb 4 21:00:57.012345678",
yearSign: -1,
locales: allLocalesTests("%s 4 21:00:57.012345678", []replacement{{shortMonthNamesField, feb}}),
},
{
format: "Jan _2 15:04:05.999",
stdValue: "Feb 4 21:00:57.012300000",
yearSign: -1,
locales: allLocalesTests("%s 4 21:00:57.012300000", []replacement{{shortMonthNamesField, feb}}),
},
{
format: "Jan _2 15:04:05.999",
stdValue: "Feb 4 21:00:57.012345678",
yearSign: -1,
locales: allLocalesTests("%s 4 21:00:57.012345678", []replacement{{shortMonthNamesField, feb}}),
},
{
format: "Jan _2 15:04:05.999999999",
stdValue: "Feb 4 21:00:57.0123",
yearSign: -1,
locales: allLocalesTests("%s 4 21:00:57.0123", []replacement{{shortMonthNamesField, feb}}),
},
{
format: "Jan _2 15:04:05.999999999",
stdValue: "Feb 4 21:00:57.012345678",
yearSign: -1,
locales: allLocalesTests("%s 4 21:00:57.012345678", []replacement{{shortMonthNamesField, feb}}),
},
// Day of the year.
{
format: "2006-01-02 002 15:04:05",
stdValue: "2010-02-04 035 21:00:57",
yearSign: 1,
locales: allLocalesTests("2010-02-04 035 21:00:57", emptyReplacements),
},
{
format: "2006-01 002 15:04:05",
stdValue: "2010-02 035 21:00:57",
yearSign: 1,
locales: allLocalesTests("2010-02 035 21:00:57", emptyReplacements),
},
{
format: "2006-002 15:04:05",
stdValue: "2010-035 21:00:57",
yearSign: 1,
locales: allLocalesTests("2010-035 21:00:57", emptyReplacements),
},
{
format: "200600201 15:04:05",
stdValue: "201003502 21:00:57",
yearSign: 1,
locales: allLocalesTests("201003502 21:00:57", emptyReplacements),
},
{
format: "200600204 15:04:05",
stdValue: "201003504 21:00:57",
yearSign: 1,
locales: allLocalesTests("201003504 21:00:57", emptyReplacements),
},
// Seconds time zone
{
format: "2006-01-02T15:04:05-070000",
stdValue: "1871-01-01T05:33:02-003408",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02-003408", emptyReplacements),
},
{
format: "2006-01-02T15:04:05-07:00:00",
stdValue: "1871-01-01T05:33:02-00:34:08",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02-00:34:08", emptyReplacements),
},
{
format: "2006-01-02T15:04:05-070000",
stdValue: "1871-01-01T05:33:02+003408",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02+003408", emptyReplacements),
},
{
format: "2006-01-02T15:04:05-07:00:00",
stdValue: "1871-01-01T05:33:02+00:34:08",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02+00:34:08", emptyReplacements),
},
{
format: "2006-01-02T15:04:05Z070000",
stdValue: "1871-01-01T05:33:02-003408",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02-003408", emptyReplacements),
},
{
format: "2006-01-02T15:04:05Z07:00:00",
stdValue: "1871-01-01T05:33:02+00:34:08",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02+00:34:08", emptyReplacements),
},
{
format: "2006-01-02T15:04:05-07",
stdValue: "1871-01-01T05:33:02+01",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02+01", emptyReplacements),
},
{
format: "2006-01-02T15:04:05-07",
stdValue: "1871-01-01T05:33:02-02",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02-02", emptyReplacements),
},
{
format: "2006-01-02T15:04:05Z07",
stdValue: "1871-01-01T05:33:02-02",
yearSign: 1,
hasTZ: true,
locales: allLocalesTests("1871-01-01T05:33:02-02", emptyReplacements),
},
// Underscore 2xxx
{
format: "15:04_20060102",
stdValue: "14:38_20150618",
yearSign: -1,
locales: allLocalesTests("14:38_20150618", emptyReplacements),
},
// Extra layouts
{
format: "Monday Jan 2 2006",
stdValue: "Wednesday Oct 9 2024",
yearSign: 1,
hasWD: true,
locales: allLocalesTests("%s %s 9 2024", []replacement{{longDayNamesField, wed}, {shortMonthNamesField, oct}}),
},
{
format: "Monday 02 January 2006 15:04:05",
stdValue: "Wednesday 09 October 2024 21:20:49",
yearSign: 1,
hasWD: true,
locales: allLocalesTests("%s 09 %s 2024 21:20:49", []replacement{{longDayNamesField, wed}, {longMonthNamesField, oct}}),
},
{
format: "Mon January __2 2006 03:04:05PM",
stdValue: "Thu February 54 2006 09:15:05AM",
yearSign: 1,
hasWD: true,
locales: allLocalesTests("%s %s 54 2006 09:15:05%s", []replacement{{shortDayNamesField, thu}, {longMonthNamesField, feb}, {dayPeriodsField, am}}),
},
{
format: "Mon January 2 2006 03:04:05PM",
stdValue: "Thu February 4 2006 09:15:05PM",
yearSign: 1,
hasWD: true,
locales: allLocalesTests("%s %s 4 2006 09:15:05%s", []replacement{{shortDayNamesField, thu}, {longMonthNamesField, feb}, {dayPeriodsField, pm}}),
},
}
var defaultLocation *time.Location
func init() {
var err error
defaultLocation, err = time.LoadLocation("America/Los_Angeles")
if err != nil {
log.Fatal(err)
}
}
func TestParseInLocation(t *testing.T) {
testParseFunc(t, parseTests, func(format, stdValue string) (time.Time, error) {
return time.ParseInLocation(format, stdValue, defaultLocation)
}, func(format string, value string, locale string) (time.Time, error) {
return ParseInLocation(format, value, locale, defaultLocation)
})
}
func TestParse(t *testing.T) {
testParseFunc(t, parseTests, time.Parse, Parse)
}
type stdParseFunction func(format, value string) (time.Time, error)
type parseFunction func(format string, value string, locale string) (time.Time, error)
func testParseFunc(t *testing.T, tests []ParseTest, stdFn stdParseFunction, parseFn parseFunction) {
var err error
var result, expected time.Time
for _, test := range tests {
var name string
if test.name != "" {
name = test.name
} else {
name = test.format
}
t.Run(name, func(t *testing.T) {
expected, err = stdFn(test.format, test.stdValue)
if err != nil {
t.Errorf("%s error: failed to parse stdValue %s: %v", test.name, test.stdValue, err)
}
result, err = parseFn(test.format, test.stdValue, LocaleEn)
if err != nil {
t.Errorf("%s error: %v", test.name, err)
} else {
checkParsedTime(result, expected, test, t)
}
if test.locales != nil {
for _, lt := range *test.locales {
t.Run(lt.lang, func(t *testing.T) {
result, err = parseFn(test.format, lt.value, lt.lang)
if err != nil {
if lt.expectedErr == nil {
t.Errorf("%s error parsing '%v': %v", test.name, lt.value, err)
} else if err.Error() != lt.expectedErr.Error() {
t.Errorf("%s expected error: '%v', got: '%v'", test.name, lt.expectedErr, err)
}
return
} else if lt.expectedErr != nil {
t.Errorf("%s expected error: '%v', got: nil", test.name, lt.expectedErr)
return
}
checkParsedTime(result, expected, test, t)
})
}
}
})
}
}
func checkParsedTime(value time.Time, expected time.Time, test ParseTest, t *testing.T) {
if test.yearSign >= 0 && test.yearSign*value.Year() != expected.Year() {
t.Errorf("%s: bad year: %d not %d", test.name, value.Year(), expected.Year())
}
if value.Month() != expected.Month() {
t.Errorf("%s: bad month: %s not %s", test.name, value.Month(), expected.Month())
}
if value.Day() != expected.Day() {
t.Errorf("%s: bad day: %d not %d", test.name, value.Day(), expected.Day())
}
if value.Hour() != expected.Hour() {
t.Errorf("%s: bad hour: %d not %d", test.name, value.Hour(), expected.Hour())
}
if value.Minute() != expected.Minute() {
t.Errorf("%s: bad minute: %d not %d", test.name, value.Minute(), expected.Minute())
}
if value.Second() != expected.Second() {
t.Errorf("%s: bad second: %d not %d", test.name, value.Second(), expected.Second())
}
if value.Nanosecond() != expected.Nanosecond() {
t.Errorf("%s: bad nanosecond: %d not %d", test.name, value.Nanosecond(), expected.Nanosecond())
}
zoneName, zoneOffset := value.Zone()
expZoneName, expZoneOffset := expected.Zone()
if test.hasTZ && zoneOffset != expZoneOffset {
t.Errorf("%s: bad tz offset: %s %d not %d", test.name, zoneName, zoneOffset, expZoneOffset)
}
if test.hasTZ && zoneName != expZoneName {
t.Errorf("%s: bad tz name: %s not %s", test.name, zoneName, expZoneName)
}
if test.hasWD && value.Weekday() != expected.Weekday() {
t.Errorf("%s: bad weekday: %s not %s", test.name, value.Weekday(), expected.Weekday())
}
}
func TestLayoutMismatch(t *testing.T) {
format := "Mon January 03:04:05PM"
value := "February Mon 03:04:05PM"
expected := newLayoutMismatchError("Mon", value)
t.Run("Parse", func(t *testing.T) {
_, err := Parse(format, value, LocaleEn)
if err == nil || !errors.Is(err, expected) {
t.Errorf("ErrLayoutMismatch expected, got %v", err)
}
})
t.Run("ParseInLocation", func(t *testing.T) {
_, err := ParseInLocation(format, value, LocaleEn, defaultLocation)
if err == nil || !errors.Is(err, expected) {
t.Errorf("ErrLayoutMismatch expected, got %v", err)
}
})
}
func TestParsingWithUnsupportedLocale(t *testing.T) {
lang := "ann"
format := "Mon January 03:04:05PM"
value := "Feb Monday 03:04:05PM"
t.Run("Parse", func(t *testing.T) {
_, err := Parse(format, value, lang)
var e *ErrUnsupportedLocale
if !errors.As(err, &e) {
t.Errorf("expected error: '%v', got: '%v'", &ErrUnsupportedLocale{lang}, err)
}
})
t.Run("ParseInLocation", func(t *testing.T) {
_, err := ParseInLocation(format, value, lang, defaultLocation)
var e *ErrUnsupportedLocale
if !errors.As(err, &e) {
t.Errorf("expected error: '%v', got: '%v'", &ErrUnsupportedLocale{lang}, err)
}
})
}
func TestDayPeriodsLayoutCase(t *testing.T) {
tests := []struct {
name string
format string
value string
lang string
}{
{
name: "AllLowerPm",
format: "Monday January 03:04:05pm",
value: "lunes enero 03:04:05a.m.",
lang: LocaleEs,
},
{
name: "AllUpperPm",
format: "Monday January 03:04:05PM",
value: "Monday January 03:04:05AM",
lang: LocaleEnUS,
},
{
name: "UpperPmLowerValue",
format: "Monday January 03:04:05PM",
value: "Monday January 03:04:05am",
lang: LocaleEnUS,
},
{
name: "LowerPmUpperValue",
format: "Monday January 03:04:05pm",
value: "Monday January 03:04:05AM",
lang: LocaleEnUS,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("ParseWith%s", test.name), func(t *testing.T) {
_, err := Parse(test.format, test.value, test.lang)
if err != nil {
t.Errorf("no error expected, got: '%v'", err)
}
})
t.Run(fmt.Sprintf("ParseInLocationWith%s", test.name), func(t *testing.T) {
_, err := ParseInLocation(test.format, test.value, test.lang, defaultLocation)
if err != nil {
t.Errorf("no error expected, got: '%v'", err)
}
})
}
}
func TestAllLocalesReplacements(t *testing.T) {
var shortLayoutTests []ParseTest
var longLayoutTests []ParseTest
for month := 0; month < 12; month++ {
day := month % 7
period := month % 2
shortStdValue := fmt.Sprintf("%s %s 03:04:05%s", shortDayNamesStd[day], shortMonthNamesStd[month], dayPeriodsStdUpper[period])
shortLayoutTests = append(shortLayoutTests, ParseTest{
name: shortStdValue,
format: "Mon Jan 03:04:05PM",
stdValue: shortStdValue,
hasWD: true,
yearSign: -1,
locales: allLocalesTests("%s %s 03:04:05%s", []replacement{{shortDayNamesField, day}, {shortMonthNamesField, month}, {dayPeriodsField, period}}),
})
longStdValue := fmt.Sprintf("%s %s 03:04:05%s", longDayNamesStd[day], longMonthNamesStd[month], dayPeriodsStdUpper[period])
longLayoutTests = append(longLayoutTests, ParseTest{
format: "Monday January 03:04:05PM",
stdValue: longStdValue,
hasWD: true,
yearSign: -1,
locales: allLocalesTests("%s %s 03:04:05%s", []replacement{{longDayNamesField, day}, {longMonthNamesField, month}, {dayPeriodsField, period}}),
})
}
t.Run("ParseShortNames", func(t *testing.T) {
testParseFunc(t, shortLayoutTests, time.Parse, Parse)
})
t.Run("ParseLongNames", func(t *testing.T) {
testParseFunc(t, shortLayoutTests, time.Parse, Parse)
})
t.Run("ParseInLocationShortNames", func(t *testing.T) {
testParseFunc(t, longLayoutTests, func(format, value string) (time.Time, error) {
return time.ParseInLocation(format, value, defaultLocation)
}, func(format string, value string, locale string) (time.Time, error) {
return ParseInLocation(format, value, locale, defaultLocation)
})
})
t.Run("ParseInLocationLongNames", func(t *testing.T) {
testParseFunc(t, longLayoutTests, func(format, value string) (time.Time, error) {
return time.ParseInLocation(format, value, defaultLocation)
}, func(format string, value string, locale string) (time.Time, error) {
return ParseInLocation(format, value, locale, defaultLocation)
})
})
}
func TestUnsupportedLayoutElements(t *testing.T) {
locale := genericLocale{
lang: LocaleEn,
table: [5][]string{
{}, {}, {}, {}, {},
},
}
doTest := func(t *testing.T, layout, elem string) {
expectedErr := newUnsupportedLayoutElemError(elem, &locale)
_, err := ParseWithLocale(layout, layout, &locale)
if err == nil {
t.Errorf("expected error: '%v', got: nil", expectedErr)
return
}
if !errors.Is(err, expectedErr) {
t.Errorf("expected error: '%v', got: '%v'", expectedErr, err)
}
}
t.Run("ShortWeekDayName", func(t *testing.T) {
doTest(t, "Mon February 2006", "Mon")
})
t.Run("LongWeekDayName", func(t *testing.T) {
doTest(t, "Monday February 2006", "Monday")
})
t.Run("ShortMonthName", func(t *testing.T) {
doTest(t, "Jan 2006", "Jan")
})
t.Run("LongMonthName", func(t *testing.T) {
doTest(t, "January 2006", "January")
})
t.Run("DayPeriod", func(t *testing.T) {
doTest(t, "03:04:05PM February 2006 ", "PM")
})
}
func TestTranslate(t *testing.T) {
translate, err := Translate("Monday _2 2006 27", "viernes 15 2006 27", LocaleEsES)
if err != nil {
return
}
if translate != "Friday 15 2006 27" {
t.Errorf("expected value \"Friday 15 2006 27\", got: \"%s\"", translate)
}
}
func TestTranslateSpacedValue(t *testing.T) {
locale, err := NewDefaultLocale(LocaleEsES)
if err != nil {
return
}
translate, err := TranslateWithLocale("Monday _2 2006 27", " viernes 15 2006 27 ", locale)
if err != nil {
return
}
if translate != " Friday 15 2006 27 " {
t.Errorf("expected spced value ' Friday 15 2006 27 ', got: '%s'", translate)
}
}
func TestTranslateWithUnsupportedLocale(t *testing.T) {
lang := "ann"
format := "Mon January 03:04:05PM"
value := "Feb Monday 03:04:05PM"
_, err := Translate(format, value, lang)
var e *ErrUnsupportedLocale
if !errors.As(err, &e) {