core: add RFC 3339 time placeholder (#8140)

(cherry picked from commit b9f7a1c248)
This commit is contained in:
Zen Dodd 2026-10-05 07:22:04 +10:00 • committed by Francis Lavoie
parent d598c71d65
commit 8b456a0cbf
No known key found for this signature in database
2 changed files with 36 additions and 0 deletions

View file

@ -406,6 +406,8 @@ func (f globalDefaultReplacementProvider) replace(key string) (any, bool) {
// to generate the correct format.
// https://github.com/caddyserver/caddy/issues/5773
return nowFunc().UTC().Format(http.TimeFormat), true
case "time.now.rfc3339":
return nowFunc().Format(time.RFC3339), true
case "time.now.common_log":
return nowFunc().Format("02/Jan/2006:15:04:05 -0700"), true
case "time.now.year":

View file

@ -21,6 +21,7 @@ import (
"runtime"
"sync"
"testing"
"time"
)
func TestReplacer(t *testing.T) {
@ -484,6 +485,39 @@ func TestReplacerNewWithoutFile(t *testing.T) {
}
}
func TestReplacerTimeNowRFC3339(t *testing.T) {
originalNowFunc := nowFunc
t.Cleanup(func() { nowFunc = originalNowFunc })
for _, tc := range []struct {
name string
now time.Time
want string
}{
{
name: "UTC",
now: time.Date(2026, time.October, 3, 4, 30, 0, 123456789, time.UTC),
want: "2026-10-03T04:30:00Z",
},
{
name: "numeric offset",
now: time.Date(2026, time.October, 3, 10, 15, 0, 123456789, time.FixedZone("NPT", 5*60*60+45*60)),
want: "2026-10-03T10:15:00+05:45",
},
} {
t.Run(tc.name, func(t *testing.T) {
nowFunc = func() time.Time { return tc.now }
got, ok := NewReplacer().GetString("time.now.rfc3339")
if !ok {
t.Fatal("time.now.rfc3339 placeholder was not found")
}
if got != tc.want {
t.Fatalf("time.now.rfc3339 = %q; want %q", got, tc.want)
}
})
}
}
func BenchmarkReplacer(b *testing.B) {
type testCase struct {
name, input, empty string