Fix a few small issues with v2
This commit is contained in:
parent
a11d5c0f5d
commit
27b9345377
4 changed files with 11 additions and 8 deletions
|
|
@ -10,15 +10,17 @@ use args::Args;
|
|||
|
||||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
println!("{:?}", args);
|
||||
let stdin = io::stdin();
|
||||
println!("Input times one per line. Send an EOF character to finish inputting...");
|
||||
let mut lines: Vec<String> = vec![];
|
||||
for line in stdin.lock().lines() {
|
||||
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 times = time::from_stream(&midnight.date(), lines.iter())?;
|
||||
let times = time::from_stream(&midnight, lines.iter())?;
|
||||
|
||||
let mut total_minutes: i64 = 0;
|
||||
let mut first: Option<DateTime<Local>> = None;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use chrono::{Duration, Local, DateTime, Date};
|
||||
use chrono::{Duration, Local, DateTime};
|
||||
use anyhow::{Result, anyhow};
|
||||
use std::iter::Iterator;
|
||||
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 (_, [hrs, mins]) = re.captures(s).ok_or(anyhow!(format!("Failed to parse \"{s}\" as a time")))?.extract();
|
||||
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());
|
||||
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![];
|
||||
for val in stream {
|
||||
durations.push(parse_datetime(reference_date, val)?);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue