-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaceholder_time.go
49 lines (40 loc) · 1.92 KB
/
placeholder_time.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
package extraplaceholders
import (
"fmt"
"time"
"github.com/caddyserver/caddy/v2"
)
// setTimePlaceholders sets placeholders for date, time, and custom format,
// using the provided time.Time. If isUTC is true, ".utc" is added in the placeholder path.
func (e ExtraPlaceholders) setTimePlaceholders(repl *caddy.Replacer, t time.Time, isUTC bool) {
// Determine the base path, with or without ".utc"
base := "extra.time.now"
if isUTC {
base += ".utc"
}
// Placeholder support
// Dynamically resolve the time format using the replacer
timeFormatCustom := repl.ReplaceAll(e.TimeFormatCustom, defaultTimeFormatCustom)
// Set date and time components with the specified base path
repl.Set(fmt.Sprintf("%s.month", base), int(t.Month()))
repl.Set(fmt.Sprintf("%s.month_padded", base), fmt.Sprintf("%02d", t.Month()))
repl.Set(fmt.Sprintf("%s.day", base), t.Day())
repl.Set(fmt.Sprintf("%s.day_padded", base), fmt.Sprintf("%02d", t.Day()))
repl.Set(fmt.Sprintf("%s.hour", base), t.Hour())
repl.Set(fmt.Sprintf("%s.hour_padded", base), fmt.Sprintf("%02d", t.Hour()))
repl.Set(fmt.Sprintf("%s.minute", base), t.Minute())
repl.Set(fmt.Sprintf("%s.minute_padded", base), fmt.Sprintf("%02d", t.Minute()))
repl.Set(fmt.Sprintf("%s.second", base), t.Second())
repl.Set(fmt.Sprintf("%s.second_padded", base), fmt.Sprintf("%02d", t.Second()))
// Set timezone offset and name
repl.Set(fmt.Sprintf("%s.timezone_offset", base), t.Format("-0700"))
repl.Set(fmt.Sprintf("%s.timezone_name", base), t.Format("MST"))
// Set the day of the week as an integer (Sunday = 0, Monday = 1, ..., Saturday = 6)
repl.Set(fmt.Sprintf("%s.weekday_int", base), int(t.Weekday()))
// Set ISO week and year components
isoYear, isoWeek := t.ISOWeek()
repl.Set(fmt.Sprintf("%s.iso_week", base), isoWeek)
repl.Set(fmt.Sprintf("%s.iso_year", base), isoYear)
// Set custom time format placeholder
repl.Set(fmt.Sprintf("%s.custom", base), t.Format(timeFormatCustom))
}