REGEXVAULTv2.0
Localization/Time Formats
Verified Safe

Time with Fractional Seconds and Timezone Regex for Go

/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching time with fractional seconds and timezone, ported and verified for Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.

Go Implementation

Go
// Time with Fractional Seconds and Timezone
// ReDoS-safe | RegexVault — Localization > Time Formats

package validation

import "regexp"

var timeWithFractionalSecondsAndTimezoneRe = regexp.MustCompile(`^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$`)

func ValidateTimeWithFractionalSecondsAndTimezone(s string) bool {
    return timeWithFractionalSecondsAndTimezoneRe.MatchString(s)
}

// Example
// fmt.Println(ValidateTimeWithFractionalSecondsAndTimezone("12:30:45")) // true

Test Cases

Matches (Valid)
Rejects (Invalid)
12:30:4524:00:00Z
12:30:45Z12:60:00Z
12:30:45.12312:30:45+25:00
12:30:45.123456789Z12:30:45.1234567890
12:30:45+08:00
00:00:00-05:30

When to use this pattern

This pattern is drawn from the Localization > Time Formats category and carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

India Standard Time (+05:30), Newfoundland (-03:30), Iran (+03:30), and Australia Central (+09:30) use 30-minute offsets. Nepal (+05:45) uses a 45-minute offset. Pattern allows all combinations.

Technical Notes

Capture groups: 1=hour, 2=minute, 3=second, 4=fractional, 5=tz sign, 6=tz hour, 7=tz minute. Z = UTC. Fractional seconds up to 9 places (nanoseconds). Half-hour timezones (+05:30 India, +09:30 Australia) are supported.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern