podman
152 строки · 3.0 Кб
1package sprig
2
3import (
4"strconv"
5"time"
6)
7
8// Given a format and a date, format the date string.
9//
10// Date can be a `time.Time` or an `int, int32, int64`.
11// In the later case, it is treated as seconds since UNIX
12// epoch.
13func date(fmt string, date interface{}) string {
14return dateInZone(fmt, date, "Local")
15}
16
17func htmlDate(date interface{}) string {
18return dateInZone("2006-01-02", date, "Local")
19}
20
21func htmlDateInZone(date interface{}, zone string) string {
22return dateInZone("2006-01-02", date, zone)
23}
24
25func dateInZone(fmt string, date interface{}, zone string) string {
26var t time.Time
27switch date := date.(type) {
28default:
29t = time.Now()
30case time.Time:
31t = date
32case *time.Time:
33t = *date
34case int64:
35t = time.Unix(date, 0)
36case int:
37t = time.Unix(int64(date), 0)
38case int32:
39t = time.Unix(int64(date), 0)
40}
41
42loc, err := time.LoadLocation(zone)
43if err != nil {
44loc, _ = time.LoadLocation("UTC")
45}
46
47return t.In(loc).Format(fmt)
48}
49
50func dateModify(fmt string, date time.Time) time.Time {
51d, err := time.ParseDuration(fmt)
52if err != nil {
53return date
54}
55return date.Add(d)
56}
57
58func mustDateModify(fmt string, date time.Time) (time.Time, error) {
59d, err := time.ParseDuration(fmt)
60if err != nil {
61return time.Time{}, err
62}
63return date.Add(d), nil
64}
65
66func dateAgo(date interface{}) string {
67var t time.Time
68
69switch date := date.(type) {
70default:
71t = time.Now()
72case time.Time:
73t = date
74case int64:
75t = time.Unix(date, 0)
76case int:
77t = time.Unix(int64(date), 0)
78}
79// Drop resolution to seconds
80duration := time.Since(t).Round(time.Second)
81return duration.String()
82}
83
84func duration(sec interface{}) string {
85var n int64
86switch value := sec.(type) {
87default:
88n = 0
89case string:
90n, _ = strconv.ParseInt(value, 10, 64)
91case int64:
92n = value
93}
94return (time.Duration(n) * time.Second).String()
95}
96
97func durationRound(duration interface{}) string {
98var d time.Duration
99switch duration := duration.(type) {
100default:
101d = 0
102case string:
103d, _ = time.ParseDuration(duration)
104case int64:
105d = time.Duration(duration)
106case time.Time:
107d = time.Since(duration)
108}
109
110u := uint64(d)
111neg := d < 0
112if neg {
113u = -u
114}
115
116var (
117year = uint64(time.Hour) * 24 * 365
118month = uint64(time.Hour) * 24 * 30
119day = uint64(time.Hour) * 24
120hour = uint64(time.Hour)
121minute = uint64(time.Minute)
122second = uint64(time.Second)
123)
124switch {
125case u > year:
126return strconv.FormatUint(u/year, 10) + "y"
127case u > month:
128return strconv.FormatUint(u/month, 10) + "mo"
129case u > day:
130return strconv.FormatUint(u/day, 10) + "d"
131case u > hour:
132return strconv.FormatUint(u/hour, 10) + "h"
133case u > minute:
134return strconv.FormatUint(u/minute, 10) + "m"
135case u > second:
136return strconv.FormatUint(u/second, 10) + "s"
137}
138return "0s"
139}
140
141func toDate(fmt, str string) time.Time {
142t, _ := time.ParseInLocation(fmt, str, time.Local)
143return t
144}
145
146func mustToDate(fmt, str string) (time.Time, error) {
147return time.ParseInLocation(fmt, str, time.Local)
148}
149
150func unixEpoch(date time.Time) string {
151return strconv.FormatInt(date.Unix(), 10)
152}
153