summaryrefslogtreecommitdiffhomepage
path: root/tpl/time/time.go
blob: f10f3a23a6f9aa4e67cd863328ce4ee7bef1bdc6 (plain)
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
// Copyright 2017 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package time provides template functions for measuring and displaying time.
package time

import (
	"fmt"
	"time"
	_time "time"

	"github.com/gohugoio/hugo/common/hreflect"

	"github.com/gohugoio/hugo/common/htime"

	"github.com/gohugoio/locales"

	"github.com/spf13/cast"
)

// New returns a new instance of the time-namespaced template functions.
func New(translator locales.Translator, location *time.Location) *Namespace {
	return &Namespace{
		timeFormatter: htime.NewTimeFormatter(translator),
		location:      location,
	}
}

// Namespace provides template functions for the "time" namespace.
type Namespace struct {
	timeFormatter htime.TimeFormatter
	location      *time.Location
}

// AsTime converts the textual representation of the datetime string into
// a time.Time interface.
func (ns *Namespace) AsTime(v interface{}, args ...interface{}) (interface{}, error) {
	if !hreflect.IsTruthful(v) {
		return time.Time{}, nil
	}
	loc := ns.location
	if len(args) > 0 {
		locStr, err := cast.ToStringE(args[0])
		if err != nil {
			return nil, err
		}
		loc, err = _time.LoadLocation(locStr)
		if err != nil {
			return nil, err
		}
	}

	return htime.ToTimeInDefaultLocationE(v, loc)

}

// Format converts the textual representation of the datetime string into
// the other form or returns it of the time.Time value. These are formatted
// with the layout string
func (ns *Namespace) Format(layout string, v interface{}) (string, error) {
	t, err := cast.ToTimeE(v)
	if err != nil {
		return "", err
	}

	return ns.timeFormatter.Format(t, layout), nil
}

// Now returns the current local time.
func (ns *Namespace) Now() _time.Time {
	return _time.Now()
}

// ParseDuration parses a duration string.
// A duration string is a possibly signed sequence of
// decimal numbers, each with optional fraction and a unit suffix,
// such as "300ms", "-1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// See https://golang.org/pkg/time/#ParseDuration
func (ns *Namespace) ParseDuration(in interface{}) (_time.Duration, error) {
	s, err := cast.ToStringE(in)
	if err != nil {
		return 0, err
	}

	return _time.ParseDuration(s)
}

var durationUnits = map[string]_time.Duration{
	"nanosecond":  _time.Nanosecond,
	"ns":          _time.Nanosecond,
	"microsecond": _time.Microsecond,
	"us":          _time.Microsecond,
	"µs":          _time.Microsecond,
	"millisecond": _time.Millisecond,
	"ms":          _time.Millisecond,
	"second":      _time.Second,
	"s":           _time.Second,
	"minute":      _time.Minute,
	"m":           _time.Minute,
	"hour":        _time.Hour,
	"h":           _time.Hour,
}

// Duration converts the given number to a time.Duration.
// Unit is one of nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m or hour/h.
func (ns *Namespace) Duration(unit interface{}, number interface{}) (_time.Duration, error) {
	unitStr, err := cast.ToStringE(unit)
	if err != nil {
		return 0, err
	}
	unitDuration, found := durationUnits[unitStr]
	if !found {
		return 0, fmt.Errorf("%q is not a valid duration unit", unit)
	}
	n, err := cast.ToInt64E(number)
	if err != nil {
		return 0, err
	}
	return _time.Duration(n) * unitDuration, nil
}