pub fn parse_iso8601_secs(ts: &str) -> Result<u64, String> {
if ts.len() < 19 {
return Err(format!(
"Timestamp too short ({} chars, need at least 19)",
ts.len()
));
}
let bytes = ts.as_bytes();
if bytes.len() >= 19 {
if bytes[4] != b'-' || bytes[7] != b'-' || bytes[13] != b':' || bytes[16] != b':' {
return Err("Invalid ISO 8601 separator characters".to_string());
}
if bytes[10] != b'T' && bytes[10] != b't' {
return Err("Invalid ISO 8601 separator at position 10 (expected 'T')".to_string());
}
}
let year: u64 = ts
.get(0..4)
.and_then(|s| s.parse().ok())
.ok_or_else(|| "Invalid year".to_string())?;
let month: u64 = ts
.get(5..7)
.and_then(|s| s.parse().ok())
.ok_or_else(|| "Invalid month".to_string())?;
let day: u64 = ts
.get(8..10)
.and_then(|s| s.parse().ok())
.ok_or_else(|| "Invalid day".to_string())?;
let hour: u64 = ts
.get(11..13)
.and_then(|s| s.parse().ok())
.ok_or_else(|| "Invalid hour".to_string())?;
let min: u64 = ts
.get(14..16)
.and_then(|s| s.parse().ok())
.ok_or_else(|| "Invalid minute".to_string())?;
let sec: u64 = ts
.get(17..19)
.and_then(|s| s.parse().ok())
.ok_or_else(|| "Invalid second".to_string())?;
const MAX_YEAR: u64 = 9999;
if year < 1970 {
return Err(format!("Year {year} is before Unix epoch (1970)"));
}
if year > MAX_YEAR {
return Err(format!("Year {year} exceeds maximum ({MAX_YEAR})"));
}
if month == 0 || month > 12 {
return Err(format!("Month {month} out of range 1..=12"));
}
let is_leap = (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400);
let max_day: u64 = match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if is_leap {
29
} else {
28
}
}
_ => return Err(format!("Month {month} out of range 1..=12")),
};
if day == 0 || day > max_day {
return Err(format!(
"Day {day} out of range for month {month} (max {max_day})"
));
}
if hour > 23 {
return Err(format!("Hour {hour} out of range 0..=23"));
}
if min > 59 {
return Err(format!("Minute {min} out of range 0..=59"));
}
if sec > 60 {
return Err(format!("Second {sec} out of range 0..=60"));
}
let month_offsets: [u64; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
let day_of_year = month_offsets[(month - 1) as usize] + (day - 1);
let leap_adjustment = if is_leap && month > 2 { 1 } else { 0 };
let leap_years_since_epoch = if year > 1970 {
let y = year - 1; let from_1970 = 1969u64; (y / 4 - from_1970 / 4) - (y / 100 - from_1970 / 100) + (y / 400 - from_1970 / 400)
} else {
0
};
let days_since_epoch =
(year - 1970) * 365 + leap_years_since_epoch + day_of_year + leap_adjustment;
Ok(days_since_epoch * 86400 + hour * 3600 + min * 60 + sec)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_iso8601_secs_valid_timestamp() {
let result = parse_iso8601_secs("2026-02-01T00:00:00Z");
assert!(result.is_ok());
assert!(result.unwrap() > 0);
}
#[test]
fn test_parse_iso8601_secs_valid_without_z() {
let result = parse_iso8601_secs("2026-02-01T12:30:45");
assert!(result.is_ok());
}
#[test]
fn test_parse_iso8601_secs_too_short() {
let result = parse_iso8601_secs("2026-02");
assert!(result.is_err());
assert!(result.unwrap_err().contains("too short"));
}
#[test]
fn test_parse_iso8601_secs_rejects_month_zero() {
let result = parse_iso8601_secs("2026-00-15T12:00:00Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Month"));
}
#[test]
fn test_parse_iso8601_secs_rejects_month_13() {
let result = parse_iso8601_secs("2026-13-01T00:00:00Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Month"));
}
#[test]
fn test_parse_iso8601_secs_rejects_year_before_epoch() {
let result = parse_iso8601_secs("1969-06-15T12:00:00Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("before Unix epoch"));
}
#[test]
fn test_parse_iso8601_secs_rejects_day_zero() {
let result = parse_iso8601_secs("2026-01-00T12:00:00Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Day"));
}
#[test]
fn test_parse_iso8601_secs_rejects_hour_25() {
let result = parse_iso8601_secs("2026-01-01T25:00:00Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Hour"));
}
#[test]
fn test_parse_iso8601_secs_allows_leap_second() {
let result = parse_iso8601_secs("2026-01-01T23:59:60Z");
assert!(result.is_ok());
}
#[test]
fn test_parse_iso8601_secs_rejects_second_61() {
let result = parse_iso8601_secs("2026-01-01T23:59:61Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Second"));
}
#[test]
fn test_parse_iso8601_secs_monotonic() {
let t1 = parse_iso8601_secs("2026-01-01T00:00:00Z").unwrap();
let t2 = parse_iso8601_secs("2026-01-02T00:00:00Z").unwrap();
let t3 = parse_iso8601_secs("2026-02-01T00:00:00Z").unwrap();
assert!(t1 < t2);
assert!(t2 < t3);
}
#[test]
fn test_parse_iso8601_secs_rejects_feb_31() {
let result = parse_iso8601_secs("2026-02-31T00:00:00Z");
assert!(result.is_err());
assert!(result.unwrap_err().contains("Day"), "Must reject Feb 31");
}
#[test]
fn test_parse_iso8601_secs_rejects_feb_29_non_leap() {
let result = parse_iso8601_secs("2026-02-29T00:00:00Z");
assert!(result.is_err(), "2026 is not a leap year — Feb 29 invalid");
}
#[test]
fn test_parse_iso8601_secs_accepts_feb_29_leap() {
let result = parse_iso8601_secs("2024-02-29T00:00:00Z");
assert!(result.is_ok(), "2024 is a leap year — Feb 29 valid");
}
#[test]
fn test_parse_iso8601_secs_rejects_day_31_in_30day_months() {
for month in &["04", "06", "09", "11"] {
let ts = format!("2026-{month}-31T00:00:00Z");
let result = parse_iso8601_secs(&ts);
assert!(result.is_err(), "Month {month} has only 30 days");
}
}
#[test]
fn test_parse_iso8601_secs_epoch_accuracy() {
let result = parse_iso8601_secs("2026-01-01T00:00:00Z").unwrap();
let actual_epoch = 1767225600u64;
let diff = result.abs_diff(actual_epoch);
assert!(
diff < 86400,
"Epoch calculation off by {diff} seconds (result={result}, expected={actual_epoch})"
);
}
#[test]
fn test_parse_iso8601_secs_monotonic_all_months() {
let mut prev = 0u64;
for month in 1..=12 {
let ts = format!("2026-{month:02}-01T00:00:00Z");
let val = parse_iso8601_secs(&ts).unwrap();
assert!(
val > prev,
"Month {month} must be greater than previous (got {val} <= {prev})"
);
prev = val;
}
}
#[test]
fn test_parse_iso8601_secs_rejects_year_overflow() {
assert!(parse_iso8601_secs("9999-01-01T00:00:00Z").is_ok());
}
#[test]
fn test_parse_iso8601_secs_year_9999_no_overflow() {
let result = parse_iso8601_secs("9999-12-31T23:59:59Z").unwrap();
assert!(
result > 200_000_000_000,
"Year 9999 timestamp should be >200B, got {result}"
);
}
#[test]
fn test_parse_iso8601_secs_century_leap_years() {
assert!(parse_iso8601_secs("2000-02-29T00:00:00Z").is_ok());
assert!(parse_iso8601_secs("2100-02-29T00:00:00Z").is_err());
}
}