Fix a few small issues with v2

This commit is contained in:
Campbell Alden 2024-11-07 21:54:04 +09:00
parent a11d5c0f5d
commit 27b9345377
4 changed files with 11 additions and 8 deletions

2
Cargo.lock generated
View file

@ -384,7 +384,7 @@ dependencies = [
[[package]] [[package]]
name = "time-track" name = "time-track"
version = "2.0.0" version = "2.0.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "time-track" name = "time-track"
version = "2.0.0" version = "2.0.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -10,15 +10,17 @@ use args::Args;
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Args::parse(); let args = Args::parse();
println!("{:?}", args);
let stdin = io::stdin(); let stdin = io::stdin();
println!("Input times one per line. Send an EOF character to finish inputting...");
let mut lines: Vec<String> = vec![]; let mut lines: Vec<String> = vec![];
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
lines.push(line.expect("Issues when reading from stdin")); lines.push(line.expect("Issues when reading from stdin"));
} }
// This is immeidately going to be turned back into a DateTime, which the doc says is fine
#[allow(deprecated)]
let midnight = Local::now().date().and_hms_opt(0, 0, 0).ok_or(anyhow!("Expected midnight to exist"))?; let midnight = Local::now().date().and_hms_opt(0, 0, 0).ok_or(anyhow!("Expected midnight to exist"))?;
let times = time::from_stream(&midnight.date(), lines.iter())?; let times = time::from_stream(&midnight, lines.iter())?;
let mut total_minutes: i64 = 0; let mut total_minutes: i64 = 0;
let mut first: Option<DateTime<Local>> = None; let mut first: Option<DateTime<Local>> = None;

View file

@ -1,9 +1,9 @@
use chrono::{Duration, Local, DateTime, Date}; use chrono::{Duration, Local, DateTime};
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use std::iter::Iterator; use std::iter::Iterator;
use regex::Regex; use regex::Regex;
fn parse_datetime(reference_date: &Date<Local>, s: &str) -> Result<DateTime<Local>> { fn parse_datetime(reference_date: &DateTime<Local>, s: &str) -> Result<DateTime<Local>> {
let re = Regex::new(r"^\s*([12]?\d):([012345]\d)\s*$")?; let re = Regex::new(r"^\s*([12]?\d):([012345]\d)\s*$")?;
let (_, [hrs, mins]) = re.captures(s).ok_or(anyhow!(format!("Failed to parse \"{s}\" as a time")))?.extract(); let (_, [hrs, mins]) = re.captures(s).ok_or(anyhow!(format!("Failed to parse \"{s}\" as a time")))?.extract();
let mins: u32 = mins.parse()?; let mins: u32 = mins.parse()?;
@ -15,10 +15,11 @@ fn parse_datetime(reference_date: &Date<Local>, s: &str) -> Result<DateTime<Loca
date = *reference_date + Duration::days(num_days.into()); date = *reference_date + Duration::days(num_days.into());
hrs = hrs % 24; hrs = hrs % 24;
} }
Ok(date.and_hms_opt(hrs, mins, 0).ok_or(anyhow!(format!("{hrs}:{mins} not a real time")))?) #[allow(deprecated)]
Ok(date.date().and_hms_opt(hrs, mins, 0).ok_or(anyhow!(format!("{}:{:0>2} not a real time", hrs, mins)))?)
} }
pub fn from_stream<'a>(reference_date: &Date<Local>, stream: impl Iterator<Item = &'a String>) -> Result<Vec<DateTime<Local>>> { pub fn from_stream<'a>(reference_date: &DateTime<Local>, stream: impl Iterator<Item = &'a String>) -> Result<Vec<DateTime<Local>>> {
let mut durations: Vec<DateTime<Local>> = vec![]; let mut durations: Vec<DateTime<Local>> = vec![];
for val in stream { for val in stream {
durations.push(parse_datetime(reference_date, val)?); durations.push(parse_datetime(reference_date, val)?);