NormalizeUTCOffset validates and normalizes a numeric UTC offset.
(raw string)
| 17 | |
| 18 | // NormalizeUTCOffset validates and normalizes a numeric UTC offset. |
| 19 | func NormalizeUTCOffset(raw string) (string, error) { |
| 20 | trimmed := strings.TrimSpace(raw) |
| 21 | matches := utcOffsetPattern.FindStringSubmatch(trimmed) |
| 22 | if matches == nil { |
| 23 | utcOffsetLog.Printf("UTC offset %q does not match expected +HH:MM/-HH:MM format", trimmed) |
| 24 | return "", errors.New("must be a numeric UTC offset like +00:00 or -08:00") |
| 25 | } |
| 26 | |
| 27 | hours, err := strconv.Atoi(matches[2]) |
| 28 | if err != nil { |
| 29 | return "", errors.New("must be a numeric UTC offset like +00:00 or -08:00") |
| 30 | } |
| 31 | minutes, err := strconv.Atoi(matches[3]) |
| 32 | if err != nil { |
| 33 | return "", errors.New("must be a numeric UTC offset like +00:00 or -08:00") |
| 34 | } |
| 35 | if hours > 14 || minutes > 59 || (hours == 14 && minutes != 0) { |
| 36 | utcOffsetLog.Printf("UTC offset %q out of range (hours=%d, minutes=%d)", trimmed, hours, minutes) |
| 37 | return "", errors.New("must be a numeric UTC offset like +00:00 or -08:00") |
| 38 | } |
| 39 | |
| 40 | normalized := fmt.Sprintf("%s%02d:%02d", matches[1], hours, minutes) |
| 41 | utcOffsetLog.Printf("Normalized UTC offset %q to %q", trimmed, normalized) |
| 42 | return normalized, nil |
| 43 | } |
| 44 | |
| 45 | // ParseUTCOffsetLocation converts a numeric UTC offset to a fixed time.Location. |
| 46 | func ParseUTCOffsetLocation(raw string) (*time.Location, error) { |
no test coverage detected