| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package time |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/deps" |
| 21 |
"github.com/gohugoio/hugo/langs" |
| 22 |
"github.com/gohugoio/hugo/tpl/internal" |
| 23 |
) |
| 24 |
|
| 25 |
const name = "time" |
| 26 |
|
| 27 |
func init() { |
| 28 |
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { |
| 29 |
if d.Conf.Language() == nil { |
| 30 |
panic("Language must be set") |
| 31 |
} |
| 32 |
lang := d.Conf.Language().(*langs.Language) |
| 33 |
ctx := New(langs.GetTimeFormatter(lang), langs.GetLocation(lang), d) |
| 34 |
|
| 35 |
ns := &internal.TemplateFuncsNamespace{ |
| 36 |
Name: name, |
| 37 |
Context: func(cctx context.Context, args ...any) (any, error) { |
| 38 |
// Handle overlapping "time" namespace and func. |
| 39 |
// |
| 40 |
// If no args are passed to `time`, assume namespace usage and |
| 41 |
// return namespace context. |
| 42 |
// |
| 43 |
// If args are passed, call AsTime(). |
| 44 |
|
| 45 |
switch len(args) { |
| 46 |
case 0: |
| 47 |
return ctx, nil |
| 48 |
case 1: |
| 49 |
return ctx.AsTime(args[0]) |
| 50 |
case 2: |
| 51 |
return ctx.AsTime(args[0], args[1]) |
| 52 |
|
| 53 |
// 3 or more arguments. Currently not supported. |
| 54 |
default: |
| 55 |
return nil, errors.New("invalid arguments supplied to `time`") |
| 56 |
} |
| 57 |
}, |
| 58 |
} |
| 59 |
|
| 60 |
ns.AddMethodMapping(ctx.AsTime, |
| 61 |
nil, |
| 62 |
[][2]string{ |
| 63 |
{`{{ (time "2015-01-21").Year }}`, `2015`}, |
| 64 |
}, |
| 65 |
) |
| 66 |
|
| 67 |
ns.AddMethodMapping(ctx.Duration, |
| 68 |
[]string{"duration"}, |
| 69 |
[][2]string{ |
| 70 |
{`{{ mul 60 60 | duration "second" }}`, `1h0m0s`}, |
| 71 |
}, |
| 72 |
) |
| 73 |
|
| 74 |
ns.AddMethodMapping(ctx.Format, |
| 75 |
[]string{"dateFormat"}, |
| 76 |
[][2]string{ |
| 77 |
{`dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}`, `dateFormat: Wednesday, Jan 21, 2015`}, |
| 78 |
}, |
| 79 |
) |
| 80 |
|
| 81 |
ns.AddMethodMapping(ctx.In, |
| 82 |
nil, |
| 83 |
[][2]string{}, |
| 84 |
) |
| 85 |
|
| 86 |
ns.AddMethodMapping(ctx.Now, |
| 87 |
[]string{"now"}, |
| 88 |
[][2]string{}, |
| 89 |
) |
| 90 |
|
| 91 |
ns.AddMethodMapping(ctx.ParseDuration, |
| 92 |
nil, |
| 93 |
[][2]string{ |
| 94 |
{`{{ "1h12m10s" | time.ParseDuration }}`, `1h12m10s`}, |
| 95 |
}, |
| 96 |
) |
| 97 |
|
| 98 |
return ns |
| 99 |
} |
| 100 |
|
| 101 |
internal.AddTemplateFuncsNamespace(f) |
| 102 |
} |