Skip to content

Commit 3ce3d6b

Browse files
committed
feat: detect TZ from trial_run.md metadata file
- trial_run.md contains explicit timezone: **Started:** ...T...-07:00 - Much more reliable than scanning CSV file timestamps - Falls back to baseline scanning if trial_run.md not found
1 parent 420dea0 commit 3ce3d6b

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

scripts/verify_parity.rs

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,54 +270,89 @@ fn parse_tz_offset(args: &[String]) -> Option<i32> {
270270
None // Auto-detect from baseline
271271
}
272272

273-
/// Auto-detect timezone offset by extracting an hour from the baseline.
274-
/// We look at the hour values and try both -7 and -8, picking the one
275-
/// that's more likely based on typical Pacific time patterns.
276-
/// If baseline hour is 00-07, it was likely recorded in Pacific time.
273+
/// Auto-detect timezone offset from trial_run.md metadata file.
274+
/// Falls back to baseline file scanning if trial_run.md not found.
275+
/// trial_run.md contains: **Started:** 2026-03-11T22:18:32.7876612-07:00
277276
fn detect_tz_from_baseline(baseline_path: &Path) -> i32 {
277+
// First, try to read trial_run.md in the same directory
278+
let drive_dir = baseline_path.parent().unwrap_or(baseline_path);
279+
let trial_run_path = drive_dir.join("trial_run.md");
280+
281+
if let Some(offset) = detect_tz_from_trial_run(&trial_run_path) {
282+
return offset;
283+
}
284+
285+
// Fallback: scan baseline for most recent date
286+
detect_tz_from_baseline_fallback(baseline_path)
287+
}
288+
289+
/// Parse trial_run.md for the Started timestamp with explicit timezone.
290+
/// Format: **Started:** 2026-03-11T22:18:32.7876612-07:00
291+
fn detect_tz_from_trial_run(path: &Path) -> Option<i32> {
292+
let content = std::fs::read_to_string(path).ok()?;
293+
294+
// Look for pattern: **Started:** YYYY-MM-DDTHH:MM:SS...+/-HH:00
295+
for line in content.lines() {
296+
if line.contains("**Started:**") {
297+
// Find the timezone offset at the end: -07:00 or -08:00
298+
if let Some(tz_pos) = line.rfind('-') {
299+
let tz_part = &line[tz_pos..];
300+
if tz_part.starts_with("-07:00") {
301+
println!("Auto-detected from trial_run.md: -7 (PDT)");
302+
println!(" {}", line.trim());
303+
return Some(-7);
304+
} else if tz_part.starts_with("-08:00") {
305+
println!("Auto-detected from trial_run.md: -8 (PST)");
306+
println!(" {}", line.trim());
307+
return Some(-8);
308+
}
309+
}
310+
// Also check for positive offsets (unlikely for Pacific but be safe)
311+
if let Some(tz_pos) = line.rfind('+') {
312+
let tz_part = &line[tz_pos..];
313+
if let Ok(hours) = tz_part[1..3].parse::<i32>() {
314+
println!("Auto-detected from trial_run.md: +{}", hours);
315+
println!(" {}", line.trim());
316+
return Some(hours);
317+
}
318+
}
319+
}
320+
}
321+
None
322+
}
323+
324+
/// Fallback: scan baseline CSV for most recent datetime.
325+
fn detect_tz_from_baseline_fallback(baseline_path: &Path) -> i32 {
278326
let file = match std::fs::File::open(baseline_path) {
279327
Ok(f) => f,
280328
Err(_) => return -7,
281329
};
282330
let reader = std::io::BufReader::new(file);
283331

284-
// Find the most recent date and extract hour info
285332
let mut most_recent_date: Option<(i32, u32, u32)> = None;
286-
let mut sample_hour: Option<u32> = None;
287333

288334
for line in std::io::BufRead::lines(reader).take(20).flatten() {
289-
for (year, month, day, hour) in extract_all_datetimes_from_line(&line) {
335+
for (year, month, day, _hour) in extract_all_datetimes_from_line(&line) {
290336
if let Some((cy, cm, cd)) = most_recent_date {
291337
if year > cy
292338
|| (year == cy && month > cm)
293339
|| (year == cy && month == cm && day > cd)
294340
{
295341
most_recent_date = Some((year, month, day));
296-
sample_hour = Some(hour);
297342
}
298343
} else {
299344
most_recent_date = Some((year, month, day));
300-
sample_hour = Some(hour);
301345
}
302346
}
303347
}
304348

305349
if let Some((year, month, day)) = most_recent_date {
306-
// Use calendar-based DST rules for Pacific time
307350
let offset = pacific_tz_offset(year, month, day);
308351
let tz_name = if offset == -7 { "PDT" } else { "PST" };
309-
310-
if let Some(hour) = sample_hour {
311-
println!(
312-
"Auto-detected from capture {}-{:02}-{:02} {:02}:xx → {} ({})",
313-
year, month, day, hour, offset, tz_name
314-
);
315-
} else {
316-
println!(
317-
"Auto-detected from capture date {}-{:02}-{:02}: {} ({})",
318-
year, month, day, offset, tz_name
319-
);
320-
}
352+
println!(
353+
"Auto-detected from baseline date {}-{:02}-{:02}: {} ({}) [fallback]",
354+
year, month, day, offset, tz_name
355+
);
321356
return offset;
322357
}
323358

0 commit comments

Comments
 (0)