Check if a sample of i64 values look like epoch timestamps. Heuristic: at least 80% of values fall within a reasonable epoch range (2000-01-01 to 2100-01-01 in seconds, milliseconds, microseconds, or nanoseconds).
(values: &[i64])
| 109 | /// Heuristic: at least 80% of values fall within a reasonable epoch range |
| 110 | /// (2000-01-01 to 2100-01-01 in seconds, milliseconds, microseconds, or nanoseconds). |
| 111 | fn looks_epoch_like(values: &[i64]) -> bool { |
| 112 | if values.is_empty() { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | // Epoch ranges for different resolutions. |
| 117 | let ranges: &[(i64, i64)] = &[ |
| 118 | (946_684_800, 4_102_444_800), // seconds |
| 119 | (946_684_800_000, 4_102_444_800_000), // milliseconds |
| 120 | (946_684_800_000_000, 4_102_444_800_000_000), // microseconds |
| 121 | (946_684_800_000_000_000, 4_102_444_800_000_000_000), // nanoseconds |
| 122 | ]; |
| 123 | |
| 124 | let threshold = (values.len() * 4) / 5; // 80% |
| 125 | for &(min, max) in ranges { |
| 126 | let matches = values.iter().filter(|&&v| v >= min && v <= max).count(); |
| 127 | if matches >= threshold { |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | false |
| 132 | } |
| 133 | |
| 134 | #[cfg(test)] |
| 135 | mod tests { |
no test coverage detected