-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanytime_test.go
1402 lines (1340 loc) · 38.5 KB
/
anytime_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
package anytime
import (
"fmt"
"log"
"math/rand"
"reflect"
"strings"
"testing"
"time"
gp "github.com/ijt/goparsify"
"github.com/tj/assert"
)
var now = time.Date(2022, 9, 29, 2, 48, 33, 123, time.UTC)
var today = truncateDay(now).Time
func dateAtTime(dateFrom time.Time, hour int, min int, sec int) time.Time {
t := dateFrom
return time.Date(t.Year(), t.Month(), t.Day(), hour, min, sec, 0, t.Location())
}
// Test parsing on cases that are expected to parse successfully.
func TestParse_goodTimes(t *testing.T) {
var cases = []struct {
Input string
WantTime time.Time
}{
// now
{`now`, now},
// minutes
{`a minute from now`, now.Add(time.Minute)},
{`a minute ago`, now.Add(-time.Minute)},
{`1 minute ago`, now.Add(-time.Minute)},
{`5 minutes ago`, now.Add(-5 * time.Minute)},
{`five minutes ago`, now.Add(-5 * time.Minute)},
{` 5 minutes ago `, now.Add(-5 * time.Minute)},
{`2 minutes from now`, now.Add(2 * time.Minute)},
{`two minutes from now`, now.Add(2 * time.Minute)},
// hours
{`an hour from now`, now.Add(time.Hour)},
{`an hour ago`, now.Add(-time.Hour)},
{`1 hour ago`, now.Add(-time.Hour)},
{`6 hours ago`, now.Add(-6 * time.Hour)},
{`1 hour from now`, now.Add(time.Hour)},
// times
{"noon", dateAtTime(today, 12, 0, 0)},
{"5:35:52pm", dateAtTime(today, 12+5, 35, 52)},
{`10am`, dateAtTime(today, 10, 0, 0)},
{`10 am`, dateAtTime(today, 10, 0, 0)},
{`5pm`, dateAtTime(today, 12+5, 0, 0)},
{`10:25am`, dateAtTime(today, 10, 25, 0)},
{`1:05pm`, dateAtTime(today, 12+1, 5, 0)},
{`10:25:10am`, dateAtTime(today, 10, 25, 10)},
{`1:05:10pm`, dateAtTime(today, 12+1, 5, 10)},
{`10:25`, dateAtTime(today, 10, 25, 0)},
{`10:25:30`, dateAtTime(today, 10, 25, 30)},
{`17:25:30`, dateAtTime(today, 17, 25, 30)},
// dates with times
{"On Friday at noon UTC", timeInLocation(dateAtTime(nextWeekdayFrom(now, time.Friday), 12, 0, 0), time.UTC)},
{"On Tuesday at 11am UTC", timeInLocation(dateAtTime(nextWeekdayFrom(now, time.Tuesday), 11, 0, 0), time.UTC)},
{"On 3 feb 2025 at 5:35:52pm", time.Date(2025, time.February, 3, 12+5, 35, 52, 0, now.Location())},
{"3 feb 2025 at 5:35:52pm", time.Date(2025, time.February, 3, 12+5, 35, 52, 0, now.Location())},
{`3 days ago at 11:25am`, dateAtTime(now.Add(-3*24*time.Hour), 11, 25, 0)},
{`3 days from now at 14:26`, dateAtTime(now.Add(3*24*time.Hour), 14, 26, 0)},
{`2 weeks ago at 8am`, dateAtTime(now.Add(-2*7*24*time.Hour), 8, 0, 0)},
{`Today at 10am`, dateAtTime(now, 10, 0, 0)},
{`10am today`, dateAtTime(now, 10, 0, 0)},
{`Yesterday 10am`, dateAtTime(now.AddDate(0, 0, -1), 10, 0, 0)},
{`10am yesterday`, dateAtTime(now.AddDate(0, 0, -1), 10, 0, 0)},
{`Yesterday at 10am`, dateAtTime(now.AddDate(0, 0, -1), 10, 0, 0)},
{`Yesterday at 10:15am`, dateAtTime(now.AddDate(0, 0, -1), 10, 15, 0)},
{`Tomorrow 10am`, dateAtTime(now.AddDate(0, 0, 1), 10, 0, 0)},
{`10am tomorrow`, dateAtTime(now.AddDate(0, 0, 1), 10, 0, 0)},
{`Tomorrow at 10am`, dateAtTime(now.AddDate(0, 0, 1), 10, 0, 0)},
{`Tomorrow at 10:15am`, dateAtTime(now.AddDate(0, 0, 1), 10, 15, 0)},
{`10:15am tomorrow`, dateAtTime(now.AddDate(0, 0, 1), 10, 15, 0)},
{"Next dec 22nd at 3pm", timeInLocation(nextMonthDayTime(now, time.December, 22, 12+3, 0, 0), now.Location())},
{"Next December 25th at 7:30am UTC-7", timeInLocation(nextMonthDayTime(now, time.December, 25, 7, 30, 0), fixedZone(-7))},
{`Next December 23rd AT 5:25 PM`, nextMonthDayTime(now, time.December, 23, 12+5, 25, 0)},
{`Last December 23rd AT 5:25 PM`, prevMonthDayTime(now, time.December, 23, 12+5, 25, 0)},
{`Last sunday at 5:30pm`, dateAtTime(prevWeekdayFrom(now, time.Sunday), 12+5, 30, 0)},
{`Next sunday at 22:45`, dateAtTime(nextWeekdayFrom(now, time.Sunday), 22, 45, 0)},
{`Next sunday at 22:45`, dateAtTime(nextWeekdayFrom(now, time.Sunday), 22, 45, 0)},
{`November 3rd, 1986 at 4:30pm`, time.Date(1986, 11, 3, 12+4, 30, 0, 0, now.Location())},
{"September 17, 2012 at 10:09am UTC", time.Date(2012, 9, 17, 10, 9, 0, 0, time.UTC)},
{"September 17, 2012 at 10:09am UTC-8", time.Date(2012, 9, 17, 10, 9, 0, 0, fixedZone(-8))},
{"September 17, 2012 at 10:09am UTC+8", time.Date(2012, 9, 17, 10, 9, 0, 0, fixedZone(8))},
{"September 17, 2012, 10:11:09", time.Date(2012, 9, 17, 10, 11, 9, 0, now.Location())},
{"September 17, 2012, 10:11", time.Date(2012, 9, 17, 10, 11, 0, 0, now.Location())},
{"September 17, 2012 10:11", time.Date(2012, 9, 17, 10, 11, 0, 0, now.Location())},
{"September 17 2012 10:11", time.Date(2012, 9, 17, 10, 11, 0, 0, now.Location())},
{"September 17 2012 at 10:11", time.Date(2012, 9, 17, 10, 11, 0, 0, now.Location())},
// formats from the Go time package:
// ANSIC
{"Mon Jan 2 15:04:05 2006", time.Date(2006, 1, 2, 15, 4, 5, 0, now.Location())},
// RubyDate
{"Mon Jan 02 15:04:05 -0700 2006", time.Date(2006, 1, 2, 15, 4, 5, 0, fixedZone(-7))},
// RFC1123Z
{"Mon, 02 Jan 2006 15:04:05 -0700", time.Date(2006, 1, 2, 15, 4, 5, 0, fixedZone(-7))},
{"Mon 02 Jan 2006 15:04:05 -0700", time.Date(2006, 1, 2, 15, 4, 5, 0, fixedZone(-7))},
// RFC3339
{"2006-01-02T15:04:05Z", time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)},
{"1990-12-31T15:59:59-08:00", time.Date(1990, 12, 31, 15, 59, 59, 0, time.FixedZone("", -8*60*60))},
// days
{`One day ago`, now.Add(-24 * time.Hour)},
{`1 day ago`, now.Add(-24 * time.Hour)},
{`3 days ago`, now.Add(-3 * 24 * time.Hour)},
{`Three days ago`, now.Add(-3 * 24 * time.Hour)},
{`1 day from now`, now.Add(24 * time.Hour)},
// weeks
{`1 week ago`, now.Add(-7 * 24 * time.Hour)},
{`2 weeks ago`, now.Add(-2 * 7 * 24 * time.Hour)},
{`A week from now`, now.Add(7 * 24 * time.Hour)},
{`A week from today`, now.Add(7 * 24 * time.Hour)},
// months
{`A month ago`, now.AddDate(0, -1, 0)},
{`1 month ago`, now.AddDate(0, -1, 0)},
{`2 months ago`, now.AddDate(0, -2, 0)},
{`12 months ago`, now.AddDate(0, -12, 0)},
{`A month from now`, now.AddDate(0, 1, 0)},
{`One month hence`, now.AddDate(0, 1, 0)},
{`1 month from now`, now.AddDate(0, 1, 0)},
{`2 months from now`, now.AddDate(0, 2, 0)},
{`Last January`, prevMonth(now, time.January).Time},
{`Last january`, prevMonth(now, time.January).Time},
{`Next january`, nextMonth(now, time.January).Time},
// years
{`One year ago`, now.AddDate(-1, 0, 0)},
{`One year from now`, now.AddDate(1, 0, 0)},
{`One year from today`, now.AddDate(1, 0, 0)},
{`Two years ago`, now.AddDate(-2, 0, 0)},
{`2 years ago`, now.AddDate(-2, 0, 0)},
{`This year`, truncateYear(now).Time},
{`1999AD`, time.Date(1999, 1, 1, 0, 0, 0, 0, now.Location())},
{`1999 AD`, time.Date(1999, 1, 1, 0, 0, 0, 0, now.Location())},
{`2008CE`, time.Date(2008, 1, 1, 0, 0, 0, 0, now.Location())},
{`2008 CE`, time.Date(2008, 1, 1, 0, 0, 0, 0, now.Location())},
}
for _, c := range cases {
t.Run(c.Input, func(t *testing.T) {
v, err := Parse(c.Input, now)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, c.WantTime, v)
// Run the parser at a lower level and make sure the token it
// returns matches the input string.
dp := Parser(now, DefaultToFuture)
node, _ := runParser(c.Input, dp)
want := strings.TrimSpace(c.Input)
if node.Token != want {
t.Errorf("parsed token = %q, want %q", node.Token, want)
}
})
}
}
func TestParse_goodDays(t *testing.T) {
var cases = []struct {
Input string
WantTime time.Time
}{
// years
{`Last year`, truncateYear(now.AddDate(-1, 0, 0)).Time},
{`Next year`, truncateYear(now.AddDate(1, 0, 0)).Time},
// today
{`Today`, now},
// yesterday
{`Yesterday`, now.AddDate(0, 0, -1)},
// tomorrow
{`Tomorrow`, now.AddDate(0, 0, 1)},
// weeks
{`Last week`, truncateWeek(now.AddDate(0, 0, -7)).Time},
{`Next week`, truncateWeek(now.AddDate(0, 0, 7)).Time},
// past weekdays
{`Last sunday`, prevWeekdayFrom(now, time.Sunday)},
{`Last monday`, prevWeekdayFrom(now, time.Monday)},
{`Last Monday`, prevWeekdayFrom(now, time.Monday)},
{`Last tuesday`, prevWeekdayFrom(now, time.Tuesday)},
{`Last wednesday`, prevWeekdayFrom(now, time.Wednesday)},
{`Last Thursday`, prevWeekdayFrom(now, time.Thursday)},
{`Last Friday`, prevWeekdayFrom(now, time.Friday)},
{`Last saturday`, prevWeekdayFrom(now, time.Saturday)},
// future weekdays
{`Next sunday`, nextWeekdayFrom(now, time.Sunday)},
{`Next monday`, nextWeekdayFrom(now, time.Monday)},
{`Next tuesday`, nextWeekdayFrom(now, time.Tuesday)},
{`Next wednesday`, nextWeekdayFrom(now, time.Wednesday)},
{`Next thursday`, nextWeekdayFrom(now, time.Thursday)},
{`Next friday`, nextWeekdayFrom(now, time.Friday)},
{`Next saturday`, nextWeekdayFrom(now, time.Saturday)},
// months
{`Last january`, prevMonth(now, time.January).Time},
{`Next january`, nextMonth(now, time.January).Time},
{`Last month`, truncateMonth(now.AddDate(0, -1, 0)).Time},
{`Next month`, truncateMonth(now.AddDate(0, 1, 0)).Time},
// absolute dates
{"January 2017", time.Date(2017, 1, 1, 0, 0, 0, 0, now.Location())},
{"January, 2017", time.Date(2017, 1, 1, 0, 0, 0, 0, now.Location())},
{"April 3 2017", time.Date(2017, 4, 3, 0, 0, 0, 0, now.Location())},
{"April 3, 2017", time.Date(2017, 4, 3, 0, 0, 0, 0, now.Location())},
{"Oct 7, 1970", time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location())},
{"Oct 7 1970", time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location())},
{"Oct. 7, 1970", time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location())},
{"September 17, 2012 UTC+7", time.Date(2012, 9, 17, 10, 9, 0, 0, fixedZone(7))},
{"September 17, 2012", time.Date(2012, 9, 17, 10, 9, 0, 0, now.Location())},
{"7 oct 1970", time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location())},
{"7 oct, 1970", time.Date(1970, 10, 7, 0, 0, 0, 0, now.Location())},
{"03 February 2013", time.Date(2013, 2, 3, 0, 0, 0, 0, now.Location())},
{"2 July 2013", time.Date(2013, 7, 2, 0, 0, 0, 0, now.Location())},
{"2022 Feb 1", time.Date(2022, 2, 1, 0, 0, 0, 0, now.Location())},
// yyyy/mm/dd, dd/mm/yyyy etc.
{"2014/3/31", time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location())},
{"2014/3/31 UTC", time.Date(2014, 3, 31, 0, 0, 0, 0, location("UTC"))},
{"2014/3/31 UTC+1", time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(1))},
{"2014/03/31", time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location())},
{"2014/03/31 UTC-1", time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(-1))},
{"2014-04-26", time.Date(2014, 4, 26, 0, 0, 0, 0, now.Location())},
{"2014-4-26", time.Date(2014, 4, 26, 0, 0, 0, 0, now.Location())},
{"2014-4-6", time.Date(2014, 4, 6, 0, 0, 0, 0, now.Location())},
{"31/3/2014 UTC-8", time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(-8))},
{"31-3-2014 UTC-8", time.Date(2014, 3, 31, 0, 0, 0, 0, fixedZone(-8))},
{"31/3/2014", time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location())},
{"31-3-2014", time.Date(2014, 3, 31, 0, 0, 0, 0, now.Location())},
// color month
// http://www.jdawiseman.com/papers/trivia/futures.html
{"White october", nextMonth(now, time.October).Time},
{"Red october", nextMonth(now, time.October).AddDate(1, 0, 0)},
{"Green october", nextMonth(now, time.October).AddDate(2, 0, 0)},
{"Blue october", nextMonth(now, time.October).AddDate(3, 0, 0)},
{"Gold october", nextMonth(now, time.October).AddDate(4, 0, 0)},
{"Purple october", nextMonth(now, time.October).AddDate(5, 0, 0)},
{"Orange october", nextMonth(now, time.October).AddDate(6, 0, 0)},
{"Pink october", nextMonth(now, time.October).AddDate(7, 0, 0)},
{"Silver october", nextMonth(now, time.October).AddDate(8, 0, 0)},
{"Copper october", nextMonth(now, time.October).AddDate(9, 0, 0)},
}
for _, c := range cases {
t.Run(c.Input, func(t *testing.T) {
v, err := Parse(c.Input, now)
if err != nil {
t.Fatal(err)
}
want := truncateDay(c.WantTime).Time
assert.Equal(t, want, v)
// Run the parser at a lower level and make sure the token it
// returns matches the input string.
dp := Parser(now, DefaultToFuture)
node, _ := runParser(c.Input, dp)
wantTok := strings.TrimSpace(c.Input)
if node.Token != wantTok {
t.Errorf("parsed token = %q, want %q", node.Token, wantTok)
}
})
}
}
// TestParse_futurePast tests dates and times that are ambiguously in the past
// or the future.
func TestParse_futurePast(t *testing.T) {
tests := []struct {
input string
wantFuture time.Time
wantPast time.Time
}{
{
"December 20",
nextMonthDayTime(now, time.December, 20, 0, 0, 0),
prevMonthDayTime(now, time.December, 20, 0, 0, 0),
},
{
"Thursday",
nextWeekdayFrom(now, time.Thursday),
prevWeekdayFrom(now, time.Thursday),
},
{
"On thursday",
nextWeekdayFrom(now, time.Thursday),
prevWeekdayFrom(now, time.Thursday),
},
{
"December 20 at 9pm",
nextMonthDayTime(now, time.December, 20, 21, 0, 0),
prevMonthDayTime(now, time.December, 20, 21, 0, 0),
},
{
"Thursday at 23:59",
setTime(nextWeekdayFrom(now, time.Thursday), 23, 59, 0, 0),
setTime(prevWeekdayFrom(now, time.Thursday), 23, 59, 0, 0),
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
// future
got, err := Parse(tt.input, now, DefaultToFuture)
if err != nil {
t.Fatal(err)
}
if got != tt.wantFuture {
t.Errorf("Parse(..., Future) = %v, want %v", got, tt.wantFuture)
}
// past
got, err = Parse(tt.input, now, DefaultToPast)
if err != nil {
t.Fatal(err)
}
if got != tt.wantPast {
t.Errorf("Parse(..., Past) = %v, want %v", got, tt.wantPast)
}
})
}
}
func TestParse_monthOnly(t *testing.T) {
tests := []struct {
input string
month time.Month
}{
{"January", time.January},
{"February", time.February},
{"March", time.March},
{"April", time.April},
{"May", time.May},
{"June", time.June},
{"July", time.July},
{"August", time.August},
{"september", time.September},
{"october", time.October},
{"november", time.November},
{"december", time.December},
{"December", time.December},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
// future
got, err := Parse(tt.input, now, DefaultToFuture)
if err != nil {
t.Errorf("Parse(..., DefaultToFuture) error = %v", err)
return
}
futureDate := nextMonth(now, tt.month).Time
if !reflect.DeepEqual(got, futureDate) {
t.Errorf("Parse(..., DefaultToFuture) got = %v, want %v", got, futureDate)
}
// past
got, err = Parse(tt.input, now, DefaultToPast)
if err != nil {
t.Errorf("Parse(..., DefaultToFuture) error = %v", err)
return
}
pastDate := prevMonth(now, tt.month).Time
if !reflect.DeepEqual(got, pastDate) {
t.Errorf("Parse(..., DefaultToFuture) got = %v, want %v", got, pastDate)
}
})
}
}
func TestParse_future(t *testing.T) {
tests := []struct {
input string
wantOutput time.Time
}{
{"january", nextMonth(now, time.January).Time},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got, err := Parse(tt.input, now, DefaultToFuture)
if err != nil {
t.Errorf("Parse() error = %v", err)
return
}
if !reflect.DeepEqual(got, tt.wantOutput) {
t.Errorf("Parse() got = %v, want %v", got, tt.wantOutput)
}
})
}
}
func location(locStr string) *time.Location {
l, err := time.LoadLocation(locStr)
if err != nil {
panic(fmt.Sprintf("loading location %q: %v", locStr, err))
}
return l
}
func timeInLocation(t time.Time, l *time.Location) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), l)
}
// Benchmark parsing.
func BenchmarkParse(b *testing.B) {
b.SetBytes(1)
for i := 0; i < b.N; i++ {
_, err := Parse(`december 23rd 2022 at 5:25pm`, time.Time{})
if err != nil {
log.Fatalf("error: %s", err)
}
}
}
// Test parsing with inputs that are expected to result in errors.
func TestParse_bad(t *testing.T) {
var badCases = []struct {
input string
}{
{``},
{`a`},
{`not a date or a time`},
{`right now`},
{` right now `},
{`Message me in 2 minutes`},
{`Message me in 2 minutes from now`},
{`Remind me in 1 hour`},
{`Remind me in 1 hour from now`},
{`Remind me in 1 hour and 3 minutes from now`},
{`Remind me in an hour`},
{`Remind me in an hour from now`},
{`Remind me one day from now`},
{`Remind me in a day`},
{`Remind me in one day`},
{`Remind me in one day from now`},
{`Message me in a week`},
{`Message me in one week`},
{`Message me in one week from now`},
{`Message me in two weeks from now`},
{`Message me two weeks from now`},
{`Message me in two weeks`},
{`Remind me in 12 months from now at 6am`},
{`Remind me in a month`},
{`Remind me in 2 months`},
{`Remind me in a month from now`},
{`Remind me in 2 months from now`},
{`Remind me in one year from now`},
{`Remind me in a year`},
{`Remind me in a year from now`},
{`Restart the server in 2 days from now`},
{`Remind me on the 5th of next month`},
{`Remind me on the 5th of next month at 7am`},
{`Remind me at 7am on the 5th of next month`},
{`Remind me in one month from now`},
{`Remind me in one month from now at 7am`},
{`Remind me on the December 25th at 7am`},
{`Remind me at 7am on December 25th`},
{`Remind me on the 25th of December at 7am`},
{`Check logs in the past 5 minutes`},
// "1 minute" is a duration, not a time.
{`1 minute`},
// "one minute" is also a duration.
{`one minute`},
// "1 hour" is also a duration.
{`1 hour`},
// "1 day" is also a duration.
{`1 day`},
// "1 week" is also a duration.
{`1 week`},
// "1 month" is also a duration.
{`1 month`},
// "next 2 months" is a date range, not a time or a date.
{`next 2 months`},
// These are currently considered bad input, although they may
{`10`},
{`17`},
// Bare years don't have enough context to be confidently parsed as dates.
{`1999`},
{`2008`},
// Goofy input:
{`10:am`},
}
for _, c := range badCases {
t.Run(c.input, func(t *testing.T) {
now := time.Time{}
v, err := Parse(c.input, now)
if err == nil {
t.Errorf("err is nil, result is %v", v)
}
})
}
}
func Test_truncateWeek(t *testing.T) {
type args struct {
t time.Time
}
tests := []struct {
name string
args args
want time.Time
}{
{
name: "Sunday",
args: args{
t: time.Date(2022, 10, 2, 23, 59, 59, 999999, time.UTC),
},
want: time.Date(2022, 10, 2, 0, 0, 0, 0, time.UTC),
},
{
name: "monday",
args: args{
t: time.Date(2022, 10, 3, 23, 59, 59, 999999, time.UTC),
},
want: time.Date(2022, 10, 2, 0, 0, 0, 0, time.UTC),
},
{
name: "saturday",
args: args{
t: time.Date(2022, 10, 15, 23, 59, 59, 999999, time.UTC),
},
want: time.Date(2022, 10, 9, 0, 0, 0, 0, time.UTC),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := truncateWeek(tt.args.t).Time; !reflect.DeepEqual(got, tt.want) {
t.Errorf("truncateWeek() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseRange(t *testing.T) {
tests := []struct {
input string
want Range
}{
// from A to B
{
"From 3 feb 2022 to 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
// A to B
{
"3 feb 2022 to 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
// A through B
{
"3 feb 2022 through 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
// from A until B
{
"from 3 feb 2022 until 6 oct 2022",
RangeFromTimes(
time.Date(2022, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location()),
),
},
{
"from tuesday at 5pm -12:00 until thursday 23:52 +14:00",
RangeFromTimes(
setLocation(setTime(nextWeekdayFrom(now, time.Tuesday), 12+5, 0, 0, 0), fixedZone(-12)),
setLocation(setTime(nextWeekdayFrom(now, time.Thursday), 23, 52, 0, 0), fixedZone(14)),
),
},
// yesterday
{
"Yesterday",
RangeFromTimes(
today.AddDate(0, 0, -1),
today.Add(-time.Second),
),
},
// today
{
"Today",
RangeFromTimes(
today,
today.AddDate(0, 0, 1).Add(-time.Second),
),
},
// tomorrow
{
"Tomorrow",
RangeFromTimes(
today.AddDate(0, 0, 1),
today.AddDate(0, 0, 2).Add(-time.Second),
),
},
{
"From today until next thursday",
RangeFromTimes(
today,
nextWeekdayFrom(today, time.Thursday),
),
},
{
"From tomorrow until next tuesday",
RangeFromTimes(
today.AddDate(0, 0, 1),
nextWeekdayFrom(today, time.Tuesday),
),
},
// last week
{
"Last week",
RangeFromTimes(
time.Date(2022, 9, 18, 0, 0, 0, 0, now.Location()),
time.Date(2022, 9, 25, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// this week
{
"This week",
RangeFromTimes(
time.Date(2022, 9, 25, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 2, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// next week
{
"next week",
RangeFromTimes(
time.Date(2022, 10, 2, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 9, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// last month
{
"Last month",
RangeFromTimes(
time.Date(2022, 8, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 9, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// this month
{
"This month",
RangeFromTimes(
time.Date(2022, 9, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 10, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// next month
{
"Next month",
RangeFromTimes(
time.Date(2022, 10, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 11, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// last year
{
"Last year",
RangeFromTimes(
time.Date(2021, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// this year
{
"This year",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2023, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// next year
{
"Next year",
RangeFromTimes(
time.Date(2023, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2024, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// absolute year
{
"2025Ad",
RangeFromTimes(
time.Date(2025, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2026, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// absolute month
{
"Feb 2025",
RangeFromTimes(
time.Date(2025, 2, 1, 0, 0, 0, 0, now.Location()),
time.Date(2025, 3, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// absolute day
{
"3 feb 2025",
RangeFromTimes(
time.Date(2025, 2, 3, 0, 0, 0, 0, now.Location()),
time.Date(2025, 2, 4, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// absolute hour
{
"3 feb 2025 at 5PM",
RangeFromTimes(
time.Date(2025, 2, 3, 12+5, 0, 0, 0, now.Location()),
time.Date(2025, 2, 3, 12+5+1, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// absolute minute
{
"3 feb 2025 at 5:35pm",
RangeFromTimes(
time.Date(2025, 2, 3, 12+5, 35, 0, 0, now.Location()),
time.Date(2025, 2, 3, 12+5, 36, 0, 0, now.Location()).Add(-time.Second),
),
},
// absolute second
{
"3 Feb 2025 at 5:35:52pm",
RangeFromTimes(
time.Date(2025, 2, 3, 12+5, 35, 52, 0, now.Location()),
time.Date(2025, 2, 3, 12+5, 35, 53, 0, now.Location()),
),
},
// 2022 jan 1 0:0:0
{
"2022 jan 1 0:0:0",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 1, 1, 0, 0, 1, 0, now.Location()),
),
},
// 2022 jan 1 0:0
{
"2022 jan 1 0:0",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 1, 1, 0, 1, 0, 0, now.Location()).Add(-time.Second),
),
},
// 2022 jan 1 12am
{
"2022 jan 1 12am",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 1, 1, 1, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// 2022 jan 1 0am
{
"2022 jan 1 0am",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 1, 1, 1, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// 2022 jan 1
{
"2022 jan 1",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 1, 2, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// 2022 jan
{
"2022 jan",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2022, 2, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// 2022
{
"2022ce",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2023, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
// 2022
{
"2022CE",
RangeFromTimes(
time.Date(2022, 1, 1, 0, 0, 0, 0, now.Location()),
time.Date(2023, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Second),
),
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got, err := ParseRange(tt.input, now, DefaultToFuture)
if err != nil {
t.Errorf("ParseRange() error = %v", err)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseRange() got =\n%v\nwant\n%v", got, tt.want)
}
// Run the parser at a lower level and make sure the token it
// returns matches the input string.
rp := RangeParser(now, DefaultToFuture)
node, _ := runParser(tt.input, rp)
want := strings.TrimSpace(tt.input)
if node.Token != want {
t.Errorf("parsed token = %q, want %q", node.Token, want)
}
})
}
}
func Test_nextWeek(t *testing.T) {
type args struct {
ref time.Time
}
tests := []struct {
name string
args args
want Range
}{
{
name: "2022-9-30",
args: args{
ref: now,
},
want: Range{
Time: time.Date(2022, 10, 2, 0, 0, 0, 0, now.Location()),
Duration: 7*24*time.Hour - time.Second,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := nextWeek(tt.args.ref); !reflect.DeepEqual(got, tt.want) {
t.Errorf("nextWeek() = \n%v\nwant\n%v", got, tt.want)
}
})
}
}
func TestRange_String(t *testing.T) {
type fields struct {
Time time.Time
Duration time.Duration
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "zeros",
fields: fields{
Time: time.Time{},
Duration: 0,
},
want: "{time: 0001-01-01 00:00:00 +0000 UTC, duration: 0s}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := Range{
Time: tt.fields.Time,
Duration: tt.fields.Duration,
}
if got := r.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
func Test_prevWeekdayFrom(t *testing.T) {
tests := []struct {
day time.Weekday
want time.Time
}{
{time.Thursday, time.Date(2022, 9, 22, 0, 0, 0, 0, now.Location())},
{time.Friday, time.Date(2022, 9, 23, 0, 0, 0, 0, now.Location())},
{time.Saturday, time.Date(2022, 9, 24, 0, 0, 0, 0, now.Location())},
{time.Sunday, time.Date(2022, 9, 25, 0, 0, 0, 0, now.Location())},
{time.Monday, time.Date(2022, 9, 26, 0, 0, 0, 0, now.Location())},
{time.Tuesday, time.Date(2022, 9, 27, 0, 0, 0, 0, now.Location())},
{time.Wednesday, time.Date(2022, 9, 28, 0, 0, 0, 0, now.Location())},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s", tt.day), func(t *testing.T) {
if got := prevWeekdayFrom(now, tt.day); !reflect.DeepEqual(got, tt.want) {
t.Errorf("prevWeekdayFrom() = %v, want %v", got, tt.want)
}
})
}
}
func Test_nextWeekdayFrom(t *testing.T) {
tests := []struct {
day time.Weekday
want time.Time
}{
{time.Friday, time.Date(2022, 9, 30, 0, 0, 0, 0, now.Location())},
{time.Saturday, time.Date(2022, 10, 1, 0, 0, 0, 0, now.Location())},
{time.Sunday, time.Date(2022, 10, 2, 0, 0, 0, 0, now.Location())},
{time.Monday, time.Date(2022, 10, 3, 0, 0, 0, 0, now.Location())},
{time.Tuesday, time.Date(2022, 10, 4, 0, 0, 0, 0, now.Location())},
{time.Wednesday, time.Date(2022, 10, 5, 0, 0, 0, 0, now.Location())},
{time.Thursday, time.Date(2022, 10, 6, 0, 0, 0, 0, now.Location())},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s", tt.day), func(t *testing.T) {
if got := nextWeekdayFrom(now, tt.day); !reflect.DeepEqual(got, tt.want) {
t.Errorf("prevWeekdayFrom() = %v, want %v", got, tt.want)
}
})
}
}
func runParser(input string, parser gp.Parser) (gp.Result, *gp.State) {
ps := gp.NewState(input)
result := gp.Result{}
parser(ps, &result)
return result, ps
}
func TestReplaceTimesByFunc(t *testing.T) {
type args struct {
s string
ref time.Time
f func(time.Time) string
options []func(o *opts)
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "empty",
args: args{
s: "",
ref: time.Time{},
f: nil,
options: nil,
},
want: "",
wantErr: false,
},
{
name: "issue 14 example without prefix",
args: args{
s: "on Tuesday at 11am UTC",
ref: time.Date(2022, time.Month(10), 24, 0, 0, 0, 0, time.UTC),
f: func(t time.Time) string {
return t.String()
},
options: nil,
},
want: "2022-10-25 11:00:00 +0000 UTC",
wantErr: false,
},
{
name: "issue 14 example",
args: args{