-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
1236 lines (1183 loc) · 35.3 KB
/
time.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 ari
import (
"fmt"
"time"
"codeberg.org/anaseto/goal"
)
// TODO All functions under Time type https://pkg.go.dev/time@go1.22.5#Time
type Time struct {
Time *time.Time
}
// Append implements goal.BV.
func (time *Time) Append(_ *goal.Context, dst []byte, _ bool) []byte {
return append(dst, fmt.Sprintf("<%v %#v>", time.Type(), time.Time)...)
}
// LessT implements goal.BV.
func (time *Time) LessT(y goal.BV) bool {
switch yv := y.(type) {
case *Time:
return time.Time.Before(*yv.Time)
default:
return time.Type() < y.Type()
}
}
// Matches implements goal.BV.
func (time *Time) Matches(y goal.BV) bool {
switch yv := y.(type) {
case *Time:
return time.Time.Equal(*yv.Time)
default:
return false
}
}
// Type implements goal.BV.
func (time *Time) Type() string {
return "ari.Time"
}
type Location struct {
Location *time.Location
}
// Append implements goal.BV.
func (location *Location) Append(_ *goal.Context, dst []byte, _ bool) []byte {
return append(dst, fmt.Sprintf("<%v %#v>", location.Type(), location.Location)...)
}
// LessT implements goal.BV.
func (location *Location) LessT(y goal.BV) bool {
switch yv := y.(type) {
case *Location:
return location.Location.String() < yv.Location.String()
default:
return location.Type() < y.Type()
}
}
// Matches implements goal.BV.
func (location *Location) Matches(_ goal.BV) bool {
// You can have two location objects with the same name but different offsets and the offset is not exposed publicly,
// so we don't support equality directly.
return false
}
// Type implements goal.BV.
func (location *Location) Type() string {
return "ari.Location"
}
// Implements time.date function.
//
//nolint:cyclop,funlen,gocognit,gocyclo // the different arities are flat and similar, keeping them close is better
func vfTimeDate(_ *goal.Context, args []goal.V) goal.V {
// year int, month Month, day, hour, min, sec, nsec int, loc *Location
switch len(args) {
case monadic:
yearV := args[0]
if yearV.IsI() {
year := int(yearV.I())
month := time.January
day := 1
hour := 0
minute := 0
sec := 0
nsec := 0
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
}
return panicType("time.date year", "year", yearV)
case dyadic:
yearV := args[1]
monthV := args[0]
if !yearV.IsI() {
return panicType("time.date year month", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month", "month", monthV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := 1
hour := 0
minute := 0
sec := 0
nsec := 0
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
case triadic:
yearV := args[2]
monthV := args[1]
dayV := args[0]
if !yearV.IsI() {
return panicType("time.date year month day", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month day", "month", monthV)
}
if !dayV.IsI() {
return panicType("time.date year month day", "day", dayV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := int(dayV.I())
hour := 0
minute := 0
sec := 0
nsec := 0
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
case tesseradic:
yearV := args[3]
monthV := args[2]
dayV := args[1]
hourV := args[0]
if !yearV.IsI() {
return panicType("time.date year month day hour", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month day hour", "month", monthV)
}
if !dayV.IsI() {
return panicType("time.date year month day hour", "day", dayV)
}
if !hourV.IsI() {
return panicType("time.date year month day hour", "hour", hourV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := int(dayV.I())
hour := int(hourV.I())
minute := 0
sec := 0
nsec := 0
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
case pentadic:
yearV := args[4]
monthV := args[3]
dayV := args[2]
hourV := args[1]
minV := args[0]
if !yearV.IsI() {
return panicType("time.date year month day hour minute", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month day hour minute", "month", monthV)
}
if !dayV.IsI() {
return panicType("time.date year month day hour minute", "day", dayV)
}
if !hourV.IsI() {
return panicType("time.date year month day hour minute", "hour", hourV)
}
if !minV.IsI() {
return panicType("time.date year month day hour minute", "minute", minV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := int(dayV.I())
hour := int(hourV.I())
minute := int(minV.I())
sec := 0
nsec := 0
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
case hexadic:
yearV := args[5]
monthV := args[4]
dayV := args[3]
hourV := args[2]
minV := args[1]
secV := args[0]
if !yearV.IsI() {
return panicType("time.date year month day hour minute second", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month day hour minute second", "month", monthV)
}
if !dayV.IsI() {
return panicType("time.date year month day hour minute second", "day", dayV)
}
if !hourV.IsI() {
return panicType("time.date year month day hour minute second", "hour", hourV)
}
if !minV.IsI() {
return panicType("time.date year month day hour minute second", "minute", minV)
}
if !secV.IsI() {
return panicType("time.date year month day hour minute second", "second", secV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := int(dayV.I())
hour := int(hourV.I())
minute := int(minV.I())
sec := int(secV.I())
nsec := 0
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
case heptadic:
yearV := args[6]
monthV := args[5]
dayV := args[4]
hourV := args[3]
minV := args[2]
secV := args[1]
nsecV := args[0]
if !yearV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "month", monthV)
}
if !dayV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "day", dayV)
}
if !hourV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "hour", hourV)
}
if !minV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "minute", minV)
}
if !secV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "second", secV)
}
if !nsecV.IsI() {
return panicType("time.date year month day hour minute second nanosecond", "nanosecond", nsecV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := int(dayV.I())
hour := int(hourV.I())
minute := int(minV.I())
sec := int(secV.I())
nsec := int(nsecV.I())
loc := time.UTC
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
case octadic:
yearV := args[7]
monthV := args[6]
dayV := args[5]
hourV := args[4]
minV := args[3]
secV := args[2]
nsecV := args[1]
locV, ok := args[0].BV().(*Location)
if !ok {
return panicType("time.date year month day hour minute second nanosecond location", "location", args[0])
}
if !yearV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "year", yearV)
}
if !monthV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "month", monthV)
}
if !dayV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "day", dayV)
}
if !hourV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "hour", hourV)
}
if !minV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "minute", minV)
}
if !secV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "second", secV)
}
if !nsecV.IsI() {
return panicType("time.date year month day hour minute second nanosecond location", "nanosecond", nsecV)
}
year := int(yearV.I())
month := time.Month(monthV.I())
day := int(dayV.I())
hour := int(hourV.I())
minute := int(minV.I())
sec := int(secV.I())
nsec := int(nsecV.I())
loc := locV.Location
t := time.Date(year, month, day, hour, minute, sec, nsec, loc)
return goal.NewV(&Time{Time: &t})
default:
return goal.Panicf("time.date : too many arguments (%d), expects 1 to 8 arguments", len(args))
}
}
// Implements time.now function.
func vfTimeNow(_ *goal.Context, _ []goal.V) goal.V {
now := time.Now()
return goal.NewV(&Time{&now})
}
// Implements time.unix function.
func vfTimeUnix(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
tLocal, ok := x.BV().(*Time)
if !ok {
if x.IsI() {
t := time.Unix(x.I(), 0)
return goal.NewV(&Time{&t})
}
return panicType("time.unix time-or-i", "time-or-i", x)
}
ts := tLocal.Time.Unix()
return goal.NewI(ts)
case dyadic:
if x.IsI() {
y := args[0]
if y.IsI() {
sec := x.I()
nsec := y.I()
t := time.Unix(sec, nsec)
return goal.NewV(&Time{&t})
}
return panicType("isec time.unix insec", "insec", y)
}
return panicType("isec time.unix insec", "isec", x)
default:
return goal.Panicf("time.unix : too many arguments (%d), expects 1 or 2 arguments", len(args))
}
}
// Implements time.year function.
func vfTimeYear(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
tLocal, ok := x.BV().(*Time)
if !ok {
return panicType("time.year time", "time", x)
}
year := tLocal.Time.Year()
return goal.NewI(int64(year))
default:
return goal.Panicf("time.year : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.yearday function.
func vfTimeYearDay(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
tLocal, ok := x.BV().(*Time)
if !ok {
return panicType("time.yearday time", "time", x)
}
yearday := tLocal.Time.YearDay()
return goal.NewI(int64(yearday))
default:
return goal.Panicf("time.yearday : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.month function (January = 1).
func vfTimeMonth(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
tLocal, ok := x.BV().(*Time)
if !ok {
return panicType("time.month time", "time", x)
}
month := tLocal.Time.Month()
return goal.NewI(int64(month))
default:
return goal.Panicf("time.month : too many arguments (%d), expects 1 argument", len(args))
}
}
// CONSIDER: ISOWeek which is a tuple of year, week
// Implements time.day function, for day of month.
func vfTimeDay(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Day()))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Day())
} else {
return goal.Panicf("time.day time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.day time", "time", x)
}
default:
return goal.Panicf("time.day : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.weekday function, for day of week (Sunday = 0).
func vfTimeWeekDay(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Weekday()))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Weekday())
} else {
return goal.Panicf("time.weekday time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.weekday time", "time", x)
}
default:
return goal.Panicf("time.weekday : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.hour function.
func vfTimeHour(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Hour()))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Hour())
} else {
return goal.Panicf("time.hour time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.hour time", "time", x)
}
default:
return goal.Panicf("time.hour : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.minute function.
func vfTimeMinute(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Minute()))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Minute())
} else {
return goal.Panicf("time.minute time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.minute time", "time", x)
}
default:
return goal.Panicf("time.minute : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.second function.
func vfTimeSecond(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Second()))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Second())
} else {
return goal.Panicf("time.second time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.second time", "time", x)
}
default:
return goal.Panicf("time.second : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.millisecond function.
func vfTimeMillisecond(_ *goal.Context, args []goal.V) goal.V {
return vfTimeMilliMicro("time.millisecond", time.Millisecond, args)
}
// Implements time.microsecond function.
func vfTimeMicrosecond(_ *goal.Context, args []goal.V) goal.V {
return vfTimeMilliMicro("time.microsecond", time.Microsecond, args)
}
// Helper function for near-duplicate time.millisecond and time.microsecond implementations.
func vfTimeMilliMicro(goalName string, unit time.Duration, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Nanosecond() / int(unit)))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Nanosecond() / int(unit))
} else {
return goal.Panicf("%v time : expected array of times, "+
"but array has a %q value: %v", goalName, xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType(fmt.Sprintf("%v time", goalName), "time", x)
}
default:
return goal.Panicf("%v : too many arguments (%d), expects 1 argument", goalName, len(args))
}
}
// Implements time.nanosecond function.
func vfTimeNanosecond(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(int64(xv.Time.Nanosecond()))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = int64(t.Time.Nanosecond())
} else {
return goal.Panicf("time.nanosecond time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.nanosecond time", "time", x)
}
default:
return goal.Panicf("time.nanosecond : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.zonename function.
func vfTimeZoneName(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
name, _ := xv.Time.Zone()
return goal.NewS(name)
case *goal.AV:
r := make([]string, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
name, _ := t.Time.Zone()
r[i] = name
} else {
return goal.Panicf("time.zonename time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAS(r)
default:
return goal.Panicf("time.zonename time : expected time or array of times, "+
"but received a %q: %v", xv.Type(), xv)
}
default:
return goal.Panicf("time.zonename : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.zoneoffset function.
func vfTimeZoneOffset(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
_, offset := xv.Time.Zone()
return goal.NewI(int64(offset))
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
_, offset := t.Time.Zone()
r[i] = int64(offset)
} else {
return goal.Panicf("time.zoneoffset time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return goal.Panicf("time.zoneoffset time : expected time or array of times, "+
"but received a %q: %v", xv.Type(), xv)
}
default:
return goal.Panicf("time.zoneoffset : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.location function.
func vfTimeLocation(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
loc := xv.Time.Location()
return goal.NewV(&Location{Location: loc})
case *goal.AV:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
loc := t.Time.Location()
r[i] = goal.NewV(&Location{Location: loc})
} else {
return goal.Panicf("time.location time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAV(r)
default:
return panicType("time.location time", "time", x)
}
default:
return goal.Panicf("time.location : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.locationstring function.
func vfTimeLocationString(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Location:
return goal.NewS(xv.Location.String())
case *goal.AV:
r := make([]string, xv.Len())
for i, xi := range xv.Slice {
if l, ok := xi.BV().(*Location); ok {
r[i] = l.Location.String()
} else {
return goal.Panicf("time.locationstring time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAS(r)
default:
return goal.Panicf("time.locationstring time : expected time or array of times, "+
"but received a %q: %v", xv.Type(), xv)
}
default:
return goal.Panicf("time.locationstring : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.fixedZone dyad.
func vfTimeFixedZone(_ *goal.Context, args []goal.V) goal.V {
switch len(args) {
case dyadic:
x, ok := args[1].BV().(goal.S)
if !ok {
return panicType("time.fixedZone name offset-seconds-east-of-utc", "name", args[1])
}
y := args[0]
if !y.IsI() {
return panicType("time.fixedZone name offset-seconds-east-of-utc", "offset-seconds-east-of-utc", args[0])
}
loc := time.FixedZone(string(x), int(y.I()))
return goal.NewV(&Location{Location: loc})
default:
return goal.Panicf("time.fixedZone : wrong number of arguments (%d), expects 2 arguments", len(args))
}
}
// Implements time.loadlocation monad.
func vfTimeLoadLocation(_ *goal.Context, args []goal.V) goal.V {
switch len(args) {
case monadic:
name, ok := args[0].BV().(goal.S)
if !ok {
return panicType("time.loadlocation name", "name", args[0])
}
loc, err := time.LoadLocation(string(name))
// Can fail to load the system's IANA database
if err != nil {
return goal.Errorf("%v", err)
}
return goal.NewV(&Location{Location: loc})
default:
return goal.Panicf("time.loadlocation : wrong number of arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.unixmilli function.
//
// Given numbers, produces times; given times, produces numbers.
//
//nolint:dupl // yes, but not interested in further conditional logic here.
func vfTimeUnixMilli(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
if x.IsI() {
t := time.UnixMilli(x.I())
return goal.NewV(&Time{&t})
}
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(xv.Time.UnixMilli())
case *goal.AB:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
t := time.UnixMilli(int64(xi))
r[i] = goal.NewV(&Time{&t})
}
return goal.NewAV(r)
case *goal.AI:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
t := time.UnixMilli(xi)
r[i] = goal.NewV(&Time{&t})
}
return goal.NewAV(r)
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = t.Time.UnixMilli()
} else {
return goal.Panicf("time.unixmilli time-or-i : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.unixmilli time-or-i", "time", x)
}
default:
return goal.Panicf("time.unixmilli : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.unixmicro function.
//
// Given numbers, produces times; given times, produces numbers.
//
//nolint:dupl // yes, but not interested in further conditional logic here.
func vfTimeUnixMicro(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
if x.IsI() {
t := time.UnixMicro(x.I())
return goal.NewV(&Time{&t})
}
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(xv.Time.UnixMicro())
case *goal.AB:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
t := time.UnixMicro(int64(xi))
r[i] = goal.NewV(&Time{&t})
}
return goal.NewAV(r)
case *goal.AI:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
t := time.UnixMicro(xi)
r[i] = goal.NewV(&Time{&t})
}
return goal.NewAV(r)
case *goal.AV:
r := make([]int64, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
r[i] = t.Time.UnixMicro()
} else {
return goal.Panicf("time.unixmicro time-or-i : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAI(r)
default:
return panicType("time.unixmicro time-or-i", "time", x)
}
default:
return goal.Panicf("time.unixmicro : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.unixnano function.
//
// Providing a time returns a number; providing a number returns a time.
// Given how Go's time API is designed, the latter case is handled by dividing
// the given number by time.Second so that the time.Unix(sec int64, nsec int64)
// function can be used to construct the appropriate time struct.
func vfTimeUnixNano(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
if x.IsI() {
return goal.NewV(&Time{unixNano(x.I())})
}
switch xv := x.BV().(type) {
case *Time:
return goal.NewI(xv.Time.UnixNano())
case *goal.AB:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
r[i] = goal.NewV(&Time{unixNano(int64(xi))})
}
return goal.NewAV(r)
case *goal.AI:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
r[i] = goal.NewV(&Time{unixNano(xi)})
}
return goal.NewAV(r)
default:
return panicType("time.unixnano time-or-i", "time-or-i", x)
}
default:
return goal.Panicf("time.unixnano : too many arguments (%d), expects 1 argument", len(args))
}
}
// Given nanoseconds from the Unix epoch, return a time.Time struct for that instant in time.
func unixNano(nanos int64) *time.Time {
num, denom := nanos, int64(time.Second)
secs, nsecs := num/denom, num%denom
t := time.Unix(secs, nsecs)
return &t
}
// Implements time.utc function.
func vfTimeUTC(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
switch len(args) {
case monadic:
switch xv := x.BV().(type) {
case *Time:
t := xv.Time.UTC()
return goal.NewV(&Time{&t})
case *goal.AV:
r := make([]goal.V, xv.Len())
for i, xi := range xv.Slice {
if t, ok := xi.BV().(*Time); ok {
t := t.Time.UTC()
r[i] = goal.NewV(&Time{&t})
} else {
return goal.Panicf("time.utc time : expected array of times, "+
"but array has a %q value: %v", xi.Type(), xi)
}
}
return goal.NewAV(r)
default:
return panicType("time.utc time", "time", x)
}
default:
return goal.Panicf("time.utc : too many arguments (%d), expects 1 argument", len(args))
}
}
// Implements time.parse function.
func vfTimeParse(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
layoutS, ok := x.BV().(goal.S)
if !ok {
return panicType("slayout time.parse svalue", "sformat", x)
}
layout := string(layoutS)
switch len(args) {
case monadic:
return goal.Panicf("time.parse : too few arguments (%d), expects 2 arguments", len(args))
case dyadic:
y := args[0]
switch yv := y.BV().(type) {
case goal.S:
if !ok {
return panicType("slayout time.parse svalue", "svalue", y)
}
t, err := time.Parse(layout, string(yv))
if err != nil {
return goal.NewPanicError(err)
}
return goal.NewV(&Time{&t})
case *goal.AS:
r := make([]goal.V, yv.Len())
for i, yi := range yv.Slice {
t, err := time.Parse(layout, yi)
if err != nil {
return goal.NewPanicError(err)
}
r[i] = goal.NewV(&Time{&t})
}
return goal.NewAV(r)
default:
return goal.Panicf("slayout time.parse svalue : svalue must be a string or array of strings, "+
"but received a %q: %v", yv.Type(), yv)
}
default:
return goal.Panicf("time.parse : too many arguments (%d), expects 2 arguments", len(args))
}
}
// Implements time.format function.
func vfTimeFormat(_ *goal.Context, args []goal.V) goal.V {
x := args[len(args)-1]
formatS, ok := x.BV().(goal.S)
if !ok {
return panicType("sformat time.format time", "sformat", x)
}
format := string(formatS)
switch len(args) {
case dyadic:
y := args[0]
switch yv := y.BV().(type) {
case *Time:
s := yv.Time.Format(format)
return goal.NewS(s)
case *goal.AV:
r := make([]string, yv.Len())
for i, yi := range yv.Slice {
if t, ok := yi.BV().(*Time); ok {
r[i] = t.Time.Format(format)
} else {
return goal.Panicf("sformat time.format time : expected array of times, "+
"but array has a %q value: %v", yi.Type(), yi)
}
}
return goal.NewAS(r)