@@ -472,9 +472,27 @@ var Builtins = []*Function{
472
472
{
473
473
Name : "now" ,
474
474
Func : func (args ... any ) (any , error ) {
475
- return time .Now (), nil
475
+ if len (args ) == 0 {
476
+ return time .Now (), nil
477
+ }
478
+ if len (args ) == 1 {
479
+ if tz , ok := args [0 ].(* time.Location ); ok {
480
+ return time .Now ().In (tz ), nil
481
+ }
482
+ }
483
+ return nil , fmt .Errorf ("invalid number of arguments (expected 0, got %d)" , len (args ))
484
+ },
485
+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
486
+ if len (args ) == 0 {
487
+ return timeType , nil
488
+ }
489
+ if len (args ) == 1 {
490
+ if args [0 ].AssignableTo (locationType ) {
491
+ return timeType , nil
492
+ }
493
+ }
494
+ return anyType , fmt .Errorf ("invalid number of arguments (expected 0, got %d)" , len (args ))
476
495
},
477
- Types : types (new (func () time.Time )),
478
496
},
479
497
{
480
498
Name : "duration" ,
@@ -486,9 +504,17 @@ var Builtins = []*Function{
486
504
{
487
505
Name : "date" ,
488
506
Func : func (args ... any ) (any , error ) {
507
+ tz , ok := args [0 ].(* time.Location )
508
+ if ok {
509
+ args = args [1 :]
510
+ }
511
+
489
512
date := args [0 ].(string )
490
513
if len (args ) == 2 {
491
514
layout := args [1 ].(string )
515
+ if tz != nil {
516
+ return time .ParseInLocation (layout , date , tz )
517
+ }
492
518
return time .Parse (layout , date )
493
519
}
494
520
if len (args ) == 3 {
@@ -516,18 +542,32 @@ var Builtins = []*Function{
516
542
time .RFC1123 ,
517
543
}
518
544
for _ , layout := range layouts {
519
- t , err := time .Parse (layout , date )
520
- if err == nil {
521
- return t , nil
545
+ if tz == nil {
546
+ t , err := time .Parse (layout , date )
547
+ if err == nil {
548
+ return t , nil
549
+ }
550
+ } else {
551
+ t , err := time .ParseInLocation (layout , date , tz )
552
+ if err == nil {
553
+ return t , nil
554
+ }
522
555
}
523
556
}
524
557
return nil , fmt .Errorf ("invalid date %s" , date )
525
558
},
526
- Types : types (
527
- new (func (string ) time.Time ),
528
- new (func (string , string ) time.Time ),
529
- new (func (string , string , string ) time.Time ),
530
- ),
559
+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
560
+ if len (args ) < 1 {
561
+ return anyType , fmt .Errorf ("invalid number of arguments (expected at least 1, got %d)" , len (args ))
562
+ }
563
+ if args [0 ].AssignableTo (locationType ) {
564
+ args = args [1 :]
565
+ }
566
+ if len (args ) > 3 {
567
+ return anyType , fmt .Errorf ("invalid number of arguments (expected at most 3, got %d)" , len (args ))
568
+ }
569
+ return timeType , nil
570
+ },
531
571
},
532
572
{
533
573
Name : "timezone" ,
0 commit comments