{"id":"d384f635c32c96c7","repo":"gohugoio/hugo","slug":"q-is-not-a-valid-duration-unit","errorCode":null,"errorMessage":"%q is not a valid duration unit","messagePattern":"%q is not a valid duration unit","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/time/time.go","lineNumber":143,"sourceCode":"\t\"ms\":          time.Millisecond,\n\t\"second\":      time.Second,\n\t\"s\":           time.Second,\n\t\"minute\":      time.Minute,\n\t\"m\":           time.Minute,\n\t\"hour\":        time.Hour,\n\t\"h\":           time.Hour,\n}\n\n// Duration converts the given number to a time.Duration.\n// Unit is one of nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m or hour/h.\nfunc (ns *Namespace) Duration(unit any, number any) (time.Duration, error) {\n\tunitStr, err := cast.ToStringE(unit)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\tunitDuration, found := durationUnits[unitStr]\n\tif !found {\n\t\treturn 0, fmt.Errorf(\"%q is not a valid duration unit\", unit)\n\t}\n\tn, err := cast.ToInt64E(number)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\treturn time.Duration(n) * unitDuration, nil\n}\n","sourceCodeStart":125,"sourceCodeEnd":151,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/time/time.go#L125-L151","documentation":"Hugo's `time.Duration` template function (alias `duration`) converts a number into a Go time.Duration using a unit name looked up in a fixed map (durationUnits in tpl/time/time.go). If the unit string passed as the first argument isn't one of the supported keys, the lookup fails and this error is returned. Only nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m, and hour/h are accepted.","triggerScenarios":"Calling `{{ duration \"seconds\" 60 }}` or `{{ time.Duration \"days\" 2 }}` in a template — any unit string not exactly matching a durationUnits key, including plural forms (\"minutes\", \"hours\"), capitalized forms (\"Second\"), or unsupported units (\"day\", \"week\", \"d\").","commonSituations":"Developers writing cache-busting or scheduling logic in templates who assume plural units work (`duration \"minutes\" 5`); trying to express days or weeks, which Go's time.Duration units don't include; passing the number and unit in swapped order so the number becomes the unit string.","solutions":["Use an exact supported unit: nanosecond/ns, microsecond/us, millisecond/ms, second/s, minute/m, hour/h — singular, lowercase.","For days or weeks, multiply hours: `{{ duration \"hour\" (mul 24 7) }}`.","Check argument order: unit comes first, number second (`duration \"second\" 60`)."],"exampleFix":"<!-- before -->\n{{ duration \"minutes\" 5 }}\n<!-- after -->\n{{ duration \"minute\" 5 }}","handlingStrategy":"validation","validationCode":"{{ $unit := \"days\" }}\n{{ $valid := slice \"nanosecond\" \"microsecond\" \"millisecond\" \"second\" \"minute\" \"hour\" }}\n{{ if in $valid $unit }}{{ time.Duration $unit 3 }}{{ end }}","typeGuard":null,"tryCatchPattern":"{{ with try (time.Duration $unit $n) }}{{ if .Err }}{{ warnf \"bad duration unit: %s\" .Err }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["Only pass one of the six supported units: nanosecond, microsecond, millisecond, second, minute, hour — there is no day/week/month unit","Whitelist-check dynamic unit strings (e.g. from front matter or data files) before calling time.Duration","Normalize unit strings with lower/trim before comparison"],"tags":["hugo","templates","time","duration","validation"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}