mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Adding {when_unix_ms} requests placeholder (unix timestamp with a milliseconds precision) (#2260)
This commit is contained in:
parent
73273c5bf8
commit
9edc16e4d6
2 changed files with 9 additions and 2 deletions
|
@ -372,6 +372,8 @@ func (r *replacer) getSubstitution(key string) string {
|
||||||
return now().UTC().Format(timeFormatISOUTC)
|
return now().UTC().Format(timeFormatISOUTC)
|
||||||
case "{when_unix}":
|
case "{when_unix}":
|
||||||
return strconv.FormatInt(now().Unix(), 10)
|
return strconv.FormatInt(now().Unix(), 10)
|
||||||
|
case "{when_unix_ms}":
|
||||||
|
return strconv.FormatInt(nanoToMilliseconds(now().UnixNano()), 10)
|
||||||
case "{file}":
|
case "{file}":
|
||||||
_, file := path.Split(r.request.URL.Path)
|
_, file := path.Split(r.request.URL.Path)
|
||||||
return file
|
return file
|
||||||
|
@ -521,9 +523,13 @@ func (r *replacer) getSubstitution(key string) string {
|
||||||
return r.emptyValue
|
return r.emptyValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nanoToMilliseconds(d int64) int64 {
|
||||||
|
return d / 1e6
|
||||||
|
}
|
||||||
|
|
||||||
// convertToMilliseconds returns the number of milliseconds in the given duration
|
// convertToMilliseconds returns the number of milliseconds in the given duration
|
||||||
func convertToMilliseconds(d time.Duration) int64 {
|
func convertToMilliseconds(d time.Duration) int64 {
|
||||||
return d.Nanoseconds() / 1e6
|
return nanoToMilliseconds(d.Nanoseconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets key to value in the r.customReplacements map.
|
// Set sets key to value in the r.customReplacements map.
|
||||||
|
|
|
@ -86,7 +86,7 @@ func TestReplace(t *testing.T) {
|
||||||
|
|
||||||
old := now
|
old := now
|
||||||
now = func() time.Time {
|
now = func() time.Time {
|
||||||
return time.Date(2006, 1, 2, 15, 4, 5, 02, time.FixedZone("hardcoded", -7))
|
return time.Date(2006, 1, 2, 15, 4, 5, 99999999, time.FixedZone("hardcoded", -7))
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
now = old
|
now = old
|
||||||
|
@ -102,6 +102,7 @@ func TestReplace(t *testing.T) {
|
||||||
{"{when}", "02/Jan/2006:15:04:05 +0000"},
|
{"{when}", "02/Jan/2006:15:04:05 +0000"},
|
||||||
{"{when_iso}", "2006-01-02T15:04:12Z"},
|
{"{when_iso}", "2006-01-02T15:04:12Z"},
|
||||||
{"{when_unix}", "1136214252"},
|
{"{when_unix}", "1136214252"},
|
||||||
|
{"{when_unix_ms}", "1136214252099"},
|
||||||
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
|
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
|
||||||
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
|
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
|
||||||
{"The Custom response header is {<Custom}.", "The Custom response header is CustomResponseHeader."},
|
{"The Custom response header is {<Custom}.", "The Custom response header is CustomResponseHeader."},
|
||||||
|
|
Loading…
Reference in a new issue