MCPcopy Create free account
hub / github.com/github/gh-aw / parseRelativeTimeSpec

Function parseRelativeTimeSpec

pkg/workflow/time_delta.go:422–471  ·  view source on GitHub ↗

parseRelativeTimeSpec parses a relative time specification string. Supports: h (hours), d (days), w (weeks), m (months ~30 days), y (years ~365 days) Examples: "2h" = 2 hours, "7d" = 168 hours, "2w" = 336 hours, "1m" = 720 hours, "1y" = 8760 hours Returns 0 if the format is invalid or if the duratio

(spec string)

Source from the content-addressed store, hash-verified

420// Examples: "2h" = 2 hours, "7d" = 168 hours, "2w" = 336 hours, "1m" = 720 hours, "1y" = 8760 hours
421// Returns 0 if the format is invalid or if the duration is less than 2 hours
422func parseRelativeTimeSpec(spec string) int {
423 timeDeltaLog.Printf("DEBUG: parseRelativeTimeSpec called with spec: %s", spec)
424 if spec == "" {
425 return 0
426 }
427
428 // Get the last character (unit)
429 unit := spec[len(spec)-1:]
430 // Get the number part
431 numStr := spec[:len(spec)-1]
432
433 // Parse the number
434 var num int
435 _, err := fmt.Sscanf(numStr, "%d", &num)
436 if err != nil || num <= 0 {
437 timeDeltaLog.Printf("Invalid expires time spec number: %s", spec)
438 return 0
439 }
440
441 // Convert to hours based on unit
442 switch unit {
443 case "h", "H":
444 // Reject durations less than 2 hours
445 if num < 2 {
446 timeDeltaLog.Printf("Invalid expires duration: %d hours is less than the minimum 2 hours", num)
447 return 0
448 }
449 timeDeltaLog.Printf("Parsed %d hours from spec: %s", num, spec)
450 return num
451 case "d", "D":
452 hours := num * 24
453 timeDeltaLog.Printf("Converted %d days to %d hours", num, hours)
454 return hours
455 case "w", "W":
456 hours := num * 7 * 24
457 timeDeltaLog.Printf("Converted %d weeks to %d hours", num, hours)
458 return hours
459 case "m", "M":
460 hours := num * 30 * 24 // months to hours (approximate)
461 timeDeltaLog.Printf("Converted %d months to %d hours", num, hours)
462 return hours
463 case "y", "Y":
464 hours := num * 365 * 24 // years to hours (approximate)
465 timeDeltaLog.Printf("Converted %d years to %d hours", num, hours)
466 return hours
467 default:
468 timeDeltaLog.Printf("Invalid expires time spec unit: %s", spec)
469 return 0
470 }
471}
472
473// Time delta validation limits
474//

Callers 2

parseExpiresFromConfigFunction · 0.85

Calls 1

PrintfMethod · 0.45

Tested by 1