{"record":{"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/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/time/time.go#L125-L151","documentation":"Thrown by time.Duration when the unit string is not in the allowed set: nanosecond/ns, microsecond/us/µs, millisecond/ms, second/s, minute/m, hour/h. The lookup map durationUnits rejects anything else, printing the offending unit via %q.","triggerScenarios":"Calling {{ time.Duration \"sec\" 5 }} or any unit not in the allowed set. The function casts unit to string then checks the map; a miss raises this error before the numeric conversion.","commonSituations":"Using pluralized units ('seconds', 'minutes'), abbreviations outside the set ('sec', 'min'), or passing a non-unit string by mistake. Also seen when the unit variable is nil/empty.","solutions":["Use one of the supported units exactly: ns, us, µs, ms, s, m, h, or the full names.","For plural intent, use the singular full name ('second', 'minute').","If you need a Go duration string like '5m30s', use time.ParseDuration via the Duration function's other overload instead."],"exampleFix":"// before:\n{{ time.Duration \"sec\" 5 }}\n// after:\n{{ time.Duration \"second\" 5 }}","handlingStrategy":"validation","validationCode":"{{ $validUnits := slice \"ns\" \"us\" \"µs\" \"ms\" \"s\" \"m\" \"h\" }}\n{{ if in $validUnits $unit }}{{ time.Duration $unit $n }}{{ else }}0{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use only the documented unit strings (ns/us/µs/ms/s/m/h or full names).","Avoid pluralized forms.","Whitelist-check dynamic unit values against the allowed set before calling."],"tags":["time","duration","validation","template"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}