0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Implement {when_iso_local} placeholder (#2363)

Implement `{when_iso_local}` placeholder

This implements the `{when_iso_local}` placeholder. This is like the
`{when_iso}` placeholder but the output is in the current timezone
rather than UTC.

Resolves #2362
This commit is contained in:
Kurtis Rader 2018-12-18 14:42:05 -08:00 committed by Toby Allen
parent 1570bc5d03
commit 0684cf8611
2 changed files with 8 additions and 0 deletions

View file

@ -368,6 +368,8 @@ func (r *replacer) getSubstitution(key string) string {
return url.QueryEscape(r.request.URL.RequestURI())
case "{when}":
return now().Format(timeFormat)
case "{when_iso_local}":
return now().Format(timeFormatISO)
case "{when_iso}":
return now().UTC().Format(timeFormatISOUTC)
case "{when_unix}":
@ -539,6 +541,7 @@ func (r *replacer) Set(key, value string) {
const (
timeFormat = "02/Jan/2006:15:04:05 -0700"
timeFormatISO = "2006-01-02T15:04:05" // ISO 8601 with timezone to be assumed as local
timeFormatISOUTC = "2006-01-02T15:04:05Z" // ISO 8601 with timezone to be assumed as UTC
headerContentType = "Content-Type"
contentTypeJSON = "application/json"

View file

@ -86,6 +86,7 @@ func TestReplace(t *testing.T) {
old := now
now = func() time.Time {
// Note that the `-7` is seconds, not hours.
return time.Date(2006, 1, 2, 15, 4, 5, 99999999, time.FixedZone("hardcoded", -7))
}
defer func() {
@ -101,6 +102,7 @@ func TestReplace(t *testing.T) {
{"The response status is {status}.", "The response status is 200."},
{"{when}", "02/Jan/2006:15:04:05 +0000"},
{"{when_iso}", "2006-01-02T15:04:12Z"},
{"{when_iso_local}", "2006-01-02T15:04:05"},
{"{when_unix}", "1136214252"},
{"{when_unix_ms}", "1136214252099"},
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
@ -276,6 +278,7 @@ func BenchmarkReplace(b *testing.B) {
recordRequest.Header().Set("Custom", "CustomResponseHeader")
now = func() time.Time {
// Note that the `-7` is seconds, not hours.
return time.Date(2006, 1, 2, 15, 4, 5, 02, time.FixedZone("hardcoded", -7))
}
@ -308,6 +311,7 @@ func BenchmarkReplaceEscaped(b *testing.B) {
recordRequest.Header().Set("Custom", "CustomResponseHeader")
now = func() time.Time {
// Note that the `-7` is seconds, not hours.
return time.Date(2006, 1, 2, 15, 4, 5, 02, time.FixedZone("hardcoded", -7))
}
@ -334,6 +338,7 @@ func TestResponseRecorderNil(t *testing.T) {
old := now
now = func() time.Time {
// Note that the `-7` is seconds, not hours.
return time.Date(2006, 1, 2, 15, 4, 5, 02, time.FixedZone("hardcoded", -7))
}
defer func() {