MCPcopy Create free account
hub / github.com/rilldata/rill / parseDuration

Function parseDuration

runtime/parser/parse_partial_schedule.go:88–117  ·  view source on GitHub ↗

parseDuration parses a value into a time duration. If no unit is specified, it assumes the value is in seconds.

(v any)

Source from the content-addressed store, hash-verified

86// parseDuration parses a value into a time duration.
87// If no unit is specified, it assumes the value is in seconds.
88func parseDuration(v any) (time.Duration, error) {
89 switch v := v.(type) {
90 case int:
91 return time.Duration(v) * time.Second, nil
92 case string:
93 // Try parsing as an int first
94 res, err := strconv.Atoi(v)
95 if err == nil {
96 return time.Duration(res) * time.Second, nil
97 }
98 // Try parsing as a Go duration string
99 d, err := time.ParseDuration(v)
100 if err == nil {
101 return d, nil
102 }
103 // Try parsing as an ISO 8601 duration string
104 id, err := duration.ParseISO8601(v)
105 if err == nil {
106 d, ok := id.EstimateNative()
107 if !ok {
108 return 0, fmt.Errorf("time duration string %q can't be resolved to an absolute duration", v)
109 }
110 return d, nil
111 }
112 // Give up
113 return 0, fmt.Errorf("invalid time duration string %q", v)
114 default:
115 return 0, fmt.Errorf("invalid time duration value <%v>", v)
116 }
117}

Callers 4

parseReportMethod · 0.85
parseScheduleYAMLMethod · 0.85
parseAlertMethod · 0.85
parseModelMethod · 0.85

Calls 3

ParseISO8601Function · 0.92
EstimateNativeMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected