REGEXVAULTv2.0
Localization/Time Formats
Verified Safe

Time with Fractional Seconds and Timezone Regex for Python

/^(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 Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

Python
# Time with Fractional Seconds and Timezone
# ReDoS-safe | RegexVault — Localization > Time Formats

import re

time_with_fractional_seconds_and_timezone_pattern = re.compile(r'^(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]))?$')

def validate_time_with_fractional_seconds_and_timezone(value: str) -> bool:
    return bool(time_with_fractional_seconds_and_timezone_pattern.fullmatch(value))

# Example
print(validate_time_with_fractional_seconds_and_timezone("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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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