2017-03-13 22:55:02 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-11-30 03:32:53 +00:00
|
|
|
// Package time provides template functions for measuring and displaying time.
|
2017-03-13 22:55:02 +00:00
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2017-09-08 15:58:39 +00:00
|
|
|
"fmt"
|
2021-07-27 11:45:05 +00:00
|
|
|
"time"
|
2017-03-13 22:55:02 +00:00
|
|
|
_time "time"
|
|
|
|
|
2021-07-26 16:28:57 +00:00
|
|
|
"github.com/gohugoio/hugo/common/htime"
|
|
|
|
|
2017-03-13 22:55:02 +00:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new instance of the time-namespaced template functions.
|
2022-04-05 07:57:58 +00:00
|
|
|
func New(timeFormatter htime.TimeFormatter, location *time.Location) *Namespace {
|
2021-07-26 16:28:57 +00:00
|
|
|
return &Namespace{
|
2022-04-05 07:57:58 +00:00
|
|
|
timeFormatter: timeFormatter,
|
2021-07-27 11:45:05 +00:00
|
|
|
location: location,
|
2021-07-26 16:28:57 +00:00
|
|
|
}
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace provides template functions for the "time" namespace.
|
2021-07-26 16:28:57 +00:00
|
|
|
type Namespace struct {
|
|
|
|
timeFormatter htime.TimeFormatter
|
2021-07-27 11:45:05 +00:00
|
|
|
location *time.Location
|
2021-07-26 16:28:57 +00:00
|
|
|
}
|
2017-03-13 22:55:02 +00:00
|
|
|
|
|
|
|
// AsTime converts the textual representation of the datetime string into
|
|
|
|
// a time.Time interface.
|
2022-03-17 21:03:27 +00:00
|
|
|
func (ns *Namespace) AsTime(v any, args ...any) (any, error) {
|
2021-07-27 11:45:05 +00:00
|
|
|
loc := ns.location
|
|
|
|
if len(args) > 0 {
|
|
|
|
locStr, err := cast.ToStringE(args[0])
|
2020-10-21 01:07:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-27 11:45:05 +00:00
|
|
|
loc, err = _time.LoadLocation(locStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-19 22:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:02:42 +00:00
|
|
|
return htime.ToTimeInDefaultLocationE(v, loc)
|
2021-07-27 11:45:05 +00:00
|
|
|
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 16:12:08 +00:00
|
|
|
// Format converts the textual representation of the datetime string in v into
|
|
|
|
// time.Time if needed and formats it with the given layout.
|
2022-03-17 21:03:27 +00:00
|
|
|
func (ns *Namespace) Format(layout string, v any) (string, error) {
|
2021-10-30 14:06:00 +00:00
|
|
|
t, err := htime.ToTimeInDefaultLocationE(v, ns.location)
|
2017-03-13 22:55:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:28:57 +00:00
|
|
|
return ns.timeFormatter.Format(t, layout), nil
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 17:57:04 +00:00
|
|
|
// Now returns the current local time or `clock` time
|
2017-03-13 22:55:02 +00:00
|
|
|
func (ns *Namespace) Now() _time.Time {
|
2022-04-26 17:57:04 +00:00
|
|
|
return htime.Now()
|
2017-03-13 22:55:02 +00:00
|
|
|
}
|
2017-09-08 15:58:39 +00:00
|
|
|
|
2022-04-30 16:12:08 +00:00
|
|
|
// ParseDuration parses the duration string s.
|
2017-09-08 15:58:39 +00:00
|
|
|
// 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
|
2022-04-30 16:12:08 +00:00
|
|
|
func (ns *Namespace) ParseDuration(s any) (_time.Duration, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
2017-09-08 15:58:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-04-30 16:12:08 +00:00
|
|
|
return _time.ParseDuration(ss)
|
2017-09-08 15:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-03-17 21:03:27 +00:00
|
|
|
func (ns *Namespace) Duration(unit any, number any) (_time.Duration, error) {
|
2017-09-08 15:58:39 +00:00
|
|
|
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
|
|
|
|
}
|