Debian Codegraph @

util/sprig/date.go

raw
1 package sprig
2
3 import (
4 "math"
5 "strconv"
6 "time"
7 )
8
9 // date formats a date according to the provided format string.
10 //
11 // Parameters:
12 // - fmt: A Go time format string (e.g., "2006-01-02 15:04:05")
13 // - date: Can be a time.Time, *time.Time, or int/int32/int64 (seconds since UNIX epoch)
14 //
15 // If date is not one of the recognized types, the current time is used.
16 //
17 // Example usage in templates: {{ now | date "2006-01-02" }}
18 func date(fmt string, date any) string {
19 return dateInZone(fmt, date, "Local")
20 }
21
22 // htmlDate formats a date in HTML5 date format (YYYY-MM-DD).
23 //
24 // Parameters:
25 // - date: Can be a time.Time, *time.Time, or int/int32/int64 (seconds since UNIX epoch)
26 //
27 // If date is not one of the recognized types, the current time is used.
28 //
29 // Example usage in templates: {{ now | htmlDate }}
30 func htmlDate(date any) string {
31 return dateInZone("2006-01-02", date, "Local")
32 }
33
34 // htmlDateInZone formats a date in HTML5 date format (YYYY-MM-DD) in the specified timezone.
35 //
36 // Parameters:
37 // - date: Can be a time.Time, *time.Time, or int/int32/int64 (seconds since UNIX epoch)
38 // - zone: Timezone name (e.g., "UTC", "America/New_York")
39 //
40 // If date is not one of the recognized types, the current time is used.
41 // If the timezone is invalid, UTC is used.
42 //
43 // Example usage in templates: {{ now | htmlDateInZone "UTC" }}
44 func htmlDateInZone(date any, zone string) string {
45 return dateInZone("2006-01-02", date, zone)
46 }
47
48 // dateInZone formats a date according to the provided format string in the specified timezone.
49 //
50 // Parameters:
51 // - fmt: A Go time format string (e.g., "2006-01-02 15:04:05")
52 // - date: Can be a time.Time, *time.Time, or int/int32/int64 (seconds since UNIX epoch)
53 // - zone: Timezone name (e.g., "UTC", "America/New_York")
54 //
55 // If date is not one of the recognized types, the current time is used.
56 // If the timezone is invalid, UTC is used.
57 //
58 // Example usage in templates: {{ now | dateInZone "2006-01-02 15:04:05" "UTC" }}
59 func dateInZone(fmt string, date any, zone string) string {
60 var t time.Time
61 switch date := date.(type) {
62 default:
63 t = time.Now()
64 case time.Time:
65 t = date
66 case *time.Time:
67 t = *date
68 case int64:
69 t = time.Unix(date, 0)
70 case int:
71 t = time.Unix(int64(date), 0)
72 case int32:
73 t = time.Unix(int64(date), 0)
74 }
75 loc, err := time.LoadLocation(zone)
76 if err != nil {
77 loc, _ = time.LoadLocation("UTC")
78 }
79 return t.In(loc).Format(fmt)
80 }
81
82 // dateModify modifies a date by adding a duration and returns the resulting time.
83 //
84 // Parameters:
85 // - fmt: A duration string (e.g., "24h", "-12h30m", "1h15m30s")
86 // - date: The time.Time to modify
87 //
88 // If the duration string is invalid, the original date is returned.
89 //
90 // Example usage in templates: {{ now | dateModify "-24h" }}
91 func dateModify(fmt string, date time.Time) time.Time {
92 d, err := time.ParseDuration(fmt)
93 if err != nil {
94 return date
95 }
96 return date.Add(d)
97 }
98
99 // mustDateModify modifies a date by adding a duration and returns the resulting time or an error.
100 //
101 // Parameters:
102 // - fmt: A duration string (e.g., "24h", "-12h30m", "1h15m30s")
103 // - date: The time.Time to modify
104 //
105 // Unlike dateModify, this function returns an error if the duration string is invalid.
106 //
107 // Example usage in templates: {{ now | mustDateModify "24h" }}
108 func mustDateModify(fmt string, date time.Time) (time.Time, error) {
109 d, err := time.ParseDuration(fmt)
110 if err != nil {
111 return time.Time{}, err
112 }
113 return date.Add(d), nil
114 }
115
116 // dateAgo returns a string representing the time elapsed since the given date.
117 //
118 // Parameters:
119 // - date: Can be a time.Time, int, or int64 (seconds since UNIX epoch)
120 //
121 // If date is not one of the recognized types, the current time is used.
122 //
123 // Example usage in templates: {{ "2023-01-01" | toDate "2006-01-02" | dateAgo }}
124 func dateAgo(date any) string {
125 var t time.Time
126 switch date := date.(type) {
127 default:
128 t = time.Now()
129 case time.Time:
130 t = date
131 case int64:
132 t = time.Unix(date, 0)
133 case int:
134 t = time.Unix(int64(date), 0)
135 }
136 return time.Since(t).Round(time.Second).String()
137 }
138
139 // duration converts seconds to a duration string.
140 //
141 // Parameters:
142 // - sec: Can be a string (parsed as int64), or int64 representing seconds
143 //
144 // Example usage in templates: {{ 3600 | duration }} -> "1h0m0s"
145 func duration(sec any) string {
146 var n int64
147 switch value := sec.(type) {
148 default:
149 n = 0
150 case string:
151 n, _ = strconv.ParseInt(value, 10, 64)
152 case int64:
153 n = value
154 }
155 return (time.Duration(n) * time.Second).String()
156 }
157
158 // durationRound formats a duration in a human-readable rounded format.
159 //
160 // Parameters:
161 // - duration: Can be a string (parsed as duration), int64 (nanoseconds),
162 // or time.Time (time since that moment)
163 //
164 // Returns a string with the largest appropriate unit (y, mo, d, h, m, s).
165 //
166 // Example usage in templates: {{ 3600 | duration | durationRound }} -> "1h"
167 func durationRound(duration any) string {
168 var d time.Duration
169 switch duration := duration.(type) {
170 default:
171 d = 0
172 case string:
173 d, _ = time.ParseDuration(duration)
174 case int64:
175 d = time.Duration(duration)
176 case time.Time:
177 d = time.Since(duration)
178 }
179 u := uint64(math.Abs(float64(d)))
180 var (
181 year = uint64(time.Hour) * 24 * 365
182 month = uint64(time.Hour) * 24 * 30
183 day = uint64(time.Hour) * 24
184 hour = uint64(time.Hour)
185 minute = uint64(time.Minute)
186 second = uint64(time.Second)
187 )
188 switch {
189 case u > year:
190 return strconv.FormatUint(u/year, 10) + "y"
191 case u > month:
192 return strconv.FormatUint(u/month, 10) + "mo"
193 case u > day:
194 return strconv.FormatUint(u/day, 10) + "d"
195 case u > hour:
196 return strconv.FormatUint(u/hour, 10) + "h"
197 case u > minute:
198 return strconv.FormatUint(u/minute, 10) + "m"
199 case u > second:
200 return strconv.FormatUint(u/second, 10) + "s"
201 }
202 return "0s"
203 }
204
205 // toDate parses a string into a time.Time using the specified format.
206 //
207 // Parameters:
208 // - fmt: A Go time format string (e.g., "2006-01-02")
209 // - str: The date string to parse
210 //
211 // If parsing fails, returns a zero time.Time.
212 //
213 // Example usage in templates: {{ "2023-01-01" | toDate "2006-01-02" }}
214 func toDate(fmt, str string) time.Time {
215 t, _ := time.ParseInLocation(fmt, str, time.Local)
216 return t
217 }
218
219 // mustToDate parses a string into a time.Time using the specified format or returns an error.
220 //
221 // Parameters:
222 // - fmt: A Go time format string (e.g., "2006-01-02")
223 // - str: The date string to parse
224 //
225 // Unlike toDate, this function returns an error if parsing fails.
226 //
227 // Example usage in templates: {{ mustToDate "2006-01-02" "2023-01-01" }}
228 func mustToDate(fmt, str string) (time.Time, error) {
229 return time.ParseInLocation(fmt, str, time.Local)
230 }
231
232 // unixEpoch returns the Unix timestamp (seconds since January 1, 1970 UTC) for the given time.
233 //
234 // Parameters:
235 // - date: A time.Time value
236 //
237 // Example usage in templates: {{ now | unixEpoch }}
238 func unixEpoch(date time.Time) string {
239 return strconv.FormatInt(date.Unix(), 10)
240 }