diff --git a/replacer.go b/replacer.go index 2fa8ef137..050c85466 100644 --- a/replacer.go +++ b/replacer.go @@ -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": diff --git a/replacer_test.go b/replacer_test.go index 4f20bede3..4b3b3217d 100644 --- a/replacer_test.go +++ b/replacer_test.go @@ -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