-
Notifications
You must be signed in to change notification settings - Fork 9
/
tf-datetime.go
51 lines (44 loc) · 1.32 KB
/
tf-datetime.go
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
package easygen
import (
"fmt"
"strconv"
"time"
)
// now is function that represents the current time in UTC. This is here
// primarily for the tests to override times.
var now = func() time.Time { return time.Now() }
//==========================================================================
// template function for date & time
// https://godoc.org/time#pkg-constants
// Date returns the date or year
func Date(fmt string) string {
switch fmt {
case "Y4":
// returns the year string of length 4
return time.Now().Format("2006")
case "I":
// returns the output of `date -I`
return time.Now().Format("2006-01-02")
// for the output of `date -Iseconds`
// use `timestamp`
default:
return time.Now().Format("2006" + fmt + "01" + fmt + "02")
}
}
// https://github.com/hashicorp/consul-template/blob/de2ebf4/template_functions.go#L666-L682
// Timestamp returns the current UNIX timestamp in UTC. If an argument is
// specified, it will be used to format the timestamp.
func Timestamp(s ...string) (string, error) {
switch len(s) {
case 0:
return now().Format(time.RFC3339), nil
case 1:
if s[0] == "unix" {
return strconv.FormatInt(now().Unix(), 10), nil
}
return now().Format(s[0]), nil
default:
return "", fmt.Errorf("timestamp: wrong number of arguments, expected 0 or 1"+
", but got %d", len(s))
}
}