diff --git a/internal/templates/funcmap.go b/internal/templates/funcmap.go index eb3c2e54..bb570c01 100644 --- a/internal/templates/funcmap.go +++ b/internal/templates/funcmap.go @@ -16,28 +16,28 @@ import ( // // The "toTime" function receives a time or a Unix epoch and returns a time.Time // in UTC. The "formatTime" function uses "toTime" and formats the resulting -// time using RFC3339. The functions "parseTime" and "mustParseTime" parses a -// string and returns the time.Time it represents. The "toLayout" function +// time using RFC3339. The functions "parseTime" and "mustParseTime" parse a +// string and return the time.Time it represents. The "toLayout" function // converts strings like "time.RFC3339" or "time.UnixDate" to the actual layout -// represented by the Go constant with the same name. The "fail" function sets +// represented by the Go constant with the same nameß. The "fail" function sets // the provided message, so that template errors are reported directly to the // template without having the wrapper that text/template adds. // // {{ toTime }} -// => time.Now().UTC() +// => time.Now().UTC() // {{ .Token.nbf | toTime }} -// => time.Unix(.Token.nbf, 0).UTC() +// => time.Unix(.Token.nbf, 0).UTC() // {{ .Token.nbf | formatTime }} -// => time.Unix(.Token.nbf, 0).UTC().Format(time.RFC3339) +// => time.Unix(.Token.nbf, 0).UTC().Format(time.RFC3339) // {{ "2024-07-02T23:16:02Z" | parseTime }} -// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z") +// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z") // {{ parseTime "time.RFC339" "2024-07-02T23:16:02Z" }} -// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z") +// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z") // {{ parseTime "time.UnixDate" "Tue Jul 2 16:20:48 PDT 2024" "America/Los_Angeles" }} -// => loc, _ := time.LoadLocation("America/Los_Angeles") -// time.ParseInLocation(time.UnixDate, "Tue Jul 2 16:20:48 PDT 2024", loc) +// => loc, _ := time.LoadLocation("America/Los_Angeles") +// time.ParseInLocation(time.UnixDate, "Tue Jul 2 16:20:48 PDT 2024", loc) // {{ toLayout "time.RFC3339" }} -// => time.RFC3339 +// => time.RFC3339 // // sprig "env" and "expandenv" functions are removed to avoid the leak of // information. diff --git a/internal/templates/funcmap_test.go b/internal/templates/funcmap_test.go index 5342e2cb..4309ae21 100644 --- a/internal/templates/funcmap_test.go +++ b/internal/templates/funcmap_test.go @@ -84,7 +84,12 @@ func TestGetFuncMap_toTime_formatTime(t *testing.T) { func TestGetFuncMap_parseTime_mustParseTime(t *testing.T) { now := time.Now().Truncate(time.Second) - loc, err := time.LoadLocation("America/Los_Angeles") + loc := time.Local + if zone, _ := now.Zone(); zone == "UTC" { + loc = time.UTC + } + + losAngeles, err := time.LoadLocation("America/Los_Angeles") require.NoError(t, err) type args struct { @@ -96,15 +101,15 @@ func TestGetFuncMap_parseTime_mustParseTime(t *testing.T) { want time.Time assertion assert.ErrorAssertionFunc }{ - {"now", args{[]string{now.Format(time.RFC3339)}}, now.Local(), assert.NoError}, + {"now", args{[]string{now.Format(time.RFC3339)}}, now.In(loc), assert.NoError}, {"with real layout", args{[]string{time.UnixDate, now.UTC().Format(time.UnixDate)}}, now.UTC(), assert.NoError}, - {"with name layout", args{[]string{"time.UnixDate", now.Format(time.UnixDate)}}, now.Local(), assert.NoError}, + {"with name layout", args{[]string{"time.UnixDate", now.Format(time.UnixDate)}}, now.In(loc), assert.NoError}, {"with locale UTC", args{[]string{"time.UnixDate", now.UTC().Format(time.UnixDate), "UTC"}}, now.UTC(), assert.NoError}, - {"with locale other", args{[]string{"time.UnixDate", now.In(loc).Format(time.UnixDate), "America/Los_Angeles"}}, now.In(loc), assert.NoError}, + {"with locale other", args{[]string{"time.UnixDate", now.In(losAngeles).Format(time.UnixDate), "America/Los_Angeles"}}, now.In(losAngeles), assert.NoError}, {"fail parse", args{[]string{now.Format(time.UnixDate)}}, time.Time{}, assert.Error}, {"fail parse with layout", args{[]string{"time.UnixDate", now.Format(time.RFC3339)}}, time.Time{}, assert.Error}, {"fail parse with locale", args{[]string{"time.UnixDate", now.Format(time.RFC3339), "america/Los_Angeles"}}, time.Time{}, assert.Error}, - {"fail load locale", args{[]string{"time.UnixDate", now.In(loc).Format(time.UnixDate), "America/The_Angels"}}, time.Time{}, assert.Error}, + {"fail load locale", args{[]string{"time.UnixDate", now.In(losAngeles).Format(time.UnixDate), "America/The_Angels"}}, time.Time{}, assert.Error}, {"fail arguments", args{[]string{"time.Layout", now.Format(time.Layout), "America/The_Angels", "extra"}}, time.Time{}, assert.Error}, } for _, tt := range tests { @@ -205,8 +210,8 @@ func TestTemplates(t *testing.T) { errorAssertion assert.ErrorAssertionFunc failAssertion assert.ValueAssertionFunc }{ - {"toTime", args{`{{ .nbf | toTime }}`}, now.String(), assert.NoError, assert.Empty}, - {"toTime toJson", args{`{{ .nbf | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty}, + {"toTime int64", args{`{{ .nbf | toTime }}`}, now.String(), assert.NoError, assert.Empty}, + {"toTime int64 toJson", args{`{{ .nbf | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty}, {"toTime float64 toJson", args{`{{ .float64 | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty}, {"formatTime", args{`{{ .nbf | formatTime }}`}, now.Format(time.RFC3339), assert.NoError, assert.Empty}, {"formatTime float64", args{`{{ .float64 | formatTime }}`}, now.Format(time.RFC3339), assert.NoError, assert.Empty},