github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/tpl/time/time.go (about) 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 "fmt" 18 _time "time" 19 20 "github.com/spf13/cast" 21 ) 22 23 // New returns a new instance of the time-namespaced template functions. 24 func New() *Namespace { 25 return &Namespace{} 26 } 27 28 // Namespace provides template functions for the "time" namespace. 29 type Namespace struct{} 30 31 // AsTime converts the textual representation of the datetime string into 32 // a time.Time interface. 33 func (ns *Namespace) AsTime(v interface{}) (interface{}, error) { 34 t, err := cast.ToTimeE(v) 35 if err != nil { 36 return nil, err 37 } 38 39 return t, nil 40 } 41 42 // Format converts the textual representation of the datetime string into 43 // the other form or returns it of the time.Time value. These are formatted 44 // with the layout string 45 func (ns *Namespace) Format(layout string, v interface{}) (string, error) { 46 t, err := cast.ToTimeE(v) 47 if err != nil { 48 return "", err 49 } 50 51 return t.Format(layout), nil 52 } 53 54 // Now returns the current local time. 55 func (ns *Namespace) Now() _time.Time { 56 return _time.Now() 57 } 58 59 // ParseDuration parses a duration string. 60 // A duration string is a possibly signed sequence of 61 // decimal numbers, each with optional fraction and a unit suffix, 62 // such as "300ms", "-1.5h" or "2h45m". 63 // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". 64 // See https://golang.org/pkg/time/#ParseDuration 65 func (ns *Namespace) ParseDuration(in interface{}) (_time.Duration, error) { 66 s, err := cast.ToStringE(in) 67 if err != nil { 68 return 0, err 69 } 70 71 return _time.ParseDuration(s) 72 } 73 74 var durationUnits = map[string]_time.Duration{ 75 "nanosecond": _time.Nanosecond, 76 "ns": _time.Nanosecond, 77 "microsecond": _time.Microsecond, 78 "us": _time.Microsecond, 79 "µs": _time.Microsecond, 80 "millisecond": _time.Millisecond, 81 "ms": _time.Millisecond, 82 "second": _time.Second, 83 "s": _time.Second, 84 "minute": _time.Minute, 85 "m": _time.Minute, 86 "hour": _time.Hour, 87 "h": _time.Hour, 88 } 89 90 // Duration converts the given number to a time.Duration. 91 // Unit is one of nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m or hour/h. 92 func (ns *Namespace) Duration(unit interface{}, number interface{}) (_time.Duration, error) { 93 unitStr, err := cast.ToStringE(unit) 94 if err != nil { 95 return 0, err 96 } 97 unitDuration, found := durationUnits[unitStr] 98 if !found { 99 return 0, fmt.Errorf("%q is not a valid duration unit", unit) 100 } 101 n, err := cast.ToInt64E(number) 102 if err != nil { 103 return 0, err 104 } 105 return _time.Duration(n) * unitDuration, nil 106 }