MCPcopy
hub / github.com/caddyserver/caddy / funcHumanize

Method funcHumanize

modules/caddyhttp/templates/tplcontext.go:475–502  ·  view source on GitHub ↗

funcHumanize transforms size and time inputs to a human readable format. Size inputs are expected to be integers, and are formatted as a byte size, such as "83 MB". Time inputs are parsed using the given layout (default layout is RFC1123Z) and are formatted as a relative time, such as "2 weeks ago

(formatType, data string)

Source from the content-addressed store, hash-verified

473// and are formatted as a relative time, such as "2 weeks ago".
474// See https://pkg.go.dev/time#pkg-constants for time layout docs.
475func (c TemplateContext) funcHumanize(formatType, data string) (string, error) {
476 // The format type can optionally be followed
477 // by a colon to provide arguments for the format
478 parts := strings.Split(formatType, ":")
479
480 switch parts[0] {
481 case "size":
482 dataint, dataerr := strconv.ParseUint(data, 10, 64)
483 if dataerr != nil {
484 return "", fmt.Errorf("humanize: size cannot be parsed: %s", dataerr.Error())
485 }
486 return humanize.Bytes(dataint), nil
487
488 case "time":
489 timelayout := time.RFC1123Z
490 if len(parts) > 1 {
491 timelayout = parts[1]
492 }
493
494 dataint, dataerr := time.Parse(timelayout, data)
495 if dataerr != nil {
496 return "", fmt.Errorf("humanize: time cannot be parsed: %s", dataerr.Error())
497 }
498 return humanize.Time(dataint), nil
499 }
500
501 return "", fmt.Errorf("no know function was given")
502}
503
504// funcMaybe invokes the plugged-in function named functionName if it is plugged in
505// (is a module in the 'http.handlers.templates.functions' namespace). If it is not

Callers 1

TestHumanizeFunction · 0.80

Calls 1

ErrorMethod · 0.45

Tested by 1

TestHumanizeFunction · 0.64