mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
@@ -15,6 +15,7 @@ package time
|
||||
|
||||
import (
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
"github.com/gohugoio/hugo/tpl/internal"
|
||||
)
|
||||
|
||||
@@ -22,7 +23,10 @@ const name = "time"
|
||||
|
||||
func init() {
|
||||
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
|
||||
ctx := New()
|
||||
if d.Language == nil {
|
||||
panic("Language must be set")
|
||||
}
|
||||
ctx := New(langs.GetTranslator(d.Language))
|
||||
|
||||
ns := &internal.TemplateFuncsNamespace{
|
||||
Name: name,
|
||||
|
@@ -16,6 +16,9 @@ package time
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gohugoio/hugo/config"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
|
||||
"github.com/gohugoio/hugo/htesting/hqt"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
@@ -29,7 +32,9 @@ func TestInit(t *testing.T) {
|
||||
var ns *internal.TemplateFuncsNamespace
|
||||
|
||||
for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
|
||||
ns = nsf(&deps.Deps{})
|
||||
ns = nsf(&deps.Deps{
|
||||
Language: langs.NewDefaultLanguage(config.New()),
|
||||
})
|
||||
if ns.Name == name {
|
||||
found = true
|
||||
break
|
||||
|
@@ -18,6 +18,10 @@ import (
|
||||
"fmt"
|
||||
_time "time"
|
||||
|
||||
"github.com/gohugoio/hugo/common/htime"
|
||||
|
||||
"github.com/go-playground/locales"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
@@ -49,12 +53,16 @@ var timeFormats = []string{
|
||||
}
|
||||
|
||||
// New returns a new instance of the time-namespaced template functions.
|
||||
func New() *Namespace {
|
||||
return &Namespace{}
|
||||
func New(translator locales.Translator) *Namespace {
|
||||
return &Namespace{
|
||||
timeFormatter: htime.NewTimeFormatter(translator),
|
||||
}
|
||||
}
|
||||
|
||||
// Namespace provides template functions for the "time" namespace.
|
||||
type Namespace struct{}
|
||||
type Namespace struct {
|
||||
timeFormatter htime.TimeFormatter
|
||||
}
|
||||
|
||||
// AsTime converts the textual representation of the datetime string into
|
||||
// a time.Time interface.
|
||||
@@ -105,7 +113,7 @@ func (ns *Namespace) Format(layout string, v interface{}) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return t.Format(layout), nil
|
||||
return ns.timeFormatter.Format(t, layout), nil
|
||||
}
|
||||
|
||||
// Now returns the current local time.
|
||||
|
@@ -16,12 +16,14 @@ package time
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
translators "github.com/bep/gotranslators"
|
||||
)
|
||||
|
||||
func TestTimeLocation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New()
|
||||
ns := New(translators.Get("en"))
|
||||
|
||||
for i, test := range []struct {
|
||||
value string
|
||||
@@ -59,7 +61,7 @@ func TestTimeLocation(t *testing.T) {
|
||||
func TestFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New()
|
||||
ns := New(translators.Get("en"))
|
||||
|
||||
for i, test := range []struct {
|
||||
layout string
|
||||
@@ -76,6 +78,8 @@ func TestFormat(t *testing.T) {
|
||||
{time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"},
|
||||
{time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"},
|
||||
{time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"},
|
||||
// Custom layouts, as introduced in Hugo 0.87.
|
||||
{":date_medium", "2015-01-21", "Jan 21, 2015"},
|
||||
} {
|
||||
result, err := ns.Format(test.layout, test.value)
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
@@ -97,7 +101,7 @@ func TestFormat(t *testing.T) {
|
||||
func TestDuration(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ns := New()
|
||||
ns := New(translators.Get("en"))
|
||||
|
||||
for i, test := range []struct {
|
||||
unit interface{}
|
||||
|
Reference in New Issue
Block a user