refactor: modularize utils from main.rs
Signed-off-by: Christina Sørensen <ces@fem.gg>
This commit is contained in:
parent
f8f4069a3e
commit
3cc47178eb
2 changed files with 211 additions and 212 deletions
214
src/main.rs
214
src/main.rs
|
@ -2,6 +2,8 @@
|
|||
|
||||
extern crate test;
|
||||
|
||||
mod utils;
|
||||
|
||||
use chrono::prelude::*;
|
||||
use std::{env, io};
|
||||
use utils::print_time_delta;
|
||||
|
@ -27,218 +29,6 @@ impl SharedState {
|
|||
}
|
||||
}
|
||||
|
||||
mod utils {
|
||||
use chrono::{prelude::*, TimeDelta};
|
||||
|
||||
use crate::SharedState;
|
||||
|
||||
#[inline]
|
||||
pub fn naive_time_to_local_datetime(ctx: &SharedState, t: NaiveTime) -> DateTime<Local> {
|
||||
NaiveDateTime::new(ctx.now.date_naive(), t)
|
||||
.and_local_timezone(ctx.tz)
|
||||
.single()
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn parse_timestamp(ctx: &SharedState, input: &String) -> DateTime<Local> {
|
||||
let tz = ctx.tz;
|
||||
|
||||
if let Some(t) = input.parse::<DateTime<Local>>().ok() {
|
||||
t
|
||||
} else if let Some(t) = NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M:%S").ok() {
|
||||
t.and_local_timezone(tz).single().unwrap().into()
|
||||
} else if let Some(t) = NaiveTime::parse_from_str(&[&input, ":00"].concat(), "%H:%M").ok() {
|
||||
naive_time_to_local_datetime(ctx, t)
|
||||
} else if let Some(t) = NaiveTime::parse_from_str(input, "%H:%M").ok() {
|
||||
naive_time_to_local_datetime(ctx, t)
|
||||
} else if let Some(t) = NaiveTime::parse_from_str(input, "%H:%M:%S").ok() {
|
||||
naive_time_to_local_datetime(ctx, t)
|
||||
} else {
|
||||
panic!("Couldn't parse timestamp.");
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn print_time_delta(delta: TimeDelta) {
|
||||
let days = ((delta.num_seconds() / 60) / 60) / 24;
|
||||
let hours = ((delta.num_seconds() / 60) / 60) % 24;
|
||||
let minutes = (delta.num_seconds() / 60) % 60;
|
||||
let seconds = delta.num_seconds() % 60;
|
||||
|
||||
println!(
|
||||
"{}d {}h {:0>2}m {:0>2}s",
|
||||
days, &hours, minutes as f32, seconds
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use test::{black_box, Bencher};
|
||||
|
||||
const DATE: &str = "1998-01-20T16:00:00+01:00";
|
||||
|
||||
#[inline]
|
||||
fn get_ctx() -> SharedState {
|
||||
let test_date = DATE.parse::<DateTime<Local>>().unwrap();
|
||||
dbg!(test_date);
|
||||
SharedState::new_from(test_date, *test_date.offset())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_h() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"10".to_string()),
|
||||
"1998-01-20T10:00:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_h_high() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24".to_string()),
|
||||
"1998-01-20T10:00:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_hm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:59".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hm_highh() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:59".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hm_highm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:60".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hm_highhm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:60".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_hms() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:59:59".to_string()),
|
||||
"1998-01-20T23:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_parse_timestamp_hms(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
for i in 1..1000 {
|
||||
black_box(parse_timestamp(&get_ctx(), &"23:59:59".to_string()));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highh() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:59:59".to_string()),
|
||||
"1998-01-20T24:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:60:59".to_string()),
|
||||
"1998-01-20T23:60:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highs() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:59:61".to_string()),
|
||||
"1998-01-20T23:59:61+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highhms() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:60:61".to_string()),
|
||||
"1998-01-20T24:60:61+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_default() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"1998-01-20T23:59:59+01:00".to_string()),
|
||||
"1998-01-20T23:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"1998-01-20T23:59:59Z".to_string()),
|
||||
"1998-01-21T00:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
// FIXME
|
||||
// assert_eq!(
|
||||
// parse_timestamp(&get_ctx(), &"1998-01-20T23:59:59Z+02:00".to_string()),
|
||||
// "1998-01-21T23:59:59+01:00"
|
||||
// .parse::<DateTime<Local>>()
|
||||
// .unwrap()
|
||||
// );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
|
|
209
src/utils.rs
Normal file
209
src/utils.rs
Normal file
|
@ -0,0 +1,209 @@
|
|||
use chrono::{prelude::*, TimeDelta};
|
||||
|
||||
use crate::SharedState;
|
||||
|
||||
#[inline]
|
||||
pub fn naive_time_to_local_datetime(ctx: &SharedState, t: NaiveTime) -> DateTime<Local> {
|
||||
NaiveDateTime::new(ctx.now.date_naive(), t)
|
||||
.and_local_timezone(ctx.tz)
|
||||
.single()
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn parse_timestamp(ctx: &SharedState, input: &String) -> DateTime<Local> {
|
||||
let tz = ctx.tz;
|
||||
|
||||
if let Some(t) = input.parse::<DateTime<Local>>().ok() {
|
||||
t
|
||||
} else if let Some(t) = NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M:%S").ok() {
|
||||
t.and_local_timezone(tz).single().unwrap().into()
|
||||
} else if let Some(t) = NaiveTime::parse_from_str(&[&input, ":00"].concat(), "%H:%M").ok() {
|
||||
naive_time_to_local_datetime(ctx, t)
|
||||
} else if let Some(t) = NaiveTime::parse_from_str(input, "%H:%M").ok() {
|
||||
naive_time_to_local_datetime(ctx, t)
|
||||
} else if let Some(t) = NaiveTime::parse_from_str(input, "%H:%M:%S").ok() {
|
||||
naive_time_to_local_datetime(ctx, t)
|
||||
} else {
|
||||
panic!("Couldn't parse timestamp.");
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn print_time_delta(delta: TimeDelta) {
|
||||
let days = ((delta.num_seconds() / 60) / 60) / 24;
|
||||
let hours = ((delta.num_seconds() / 60) / 60) % 24;
|
||||
let minutes = (delta.num_seconds() / 60) % 60;
|
||||
let seconds = delta.num_seconds() % 60;
|
||||
|
||||
println!(
|
||||
"{}d {}h {:0>2}m {:0>2}s",
|
||||
days, &hours, minutes as f32, seconds
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use test::{black_box, Bencher};
|
||||
|
||||
const DATE: &str = "1998-01-20T16:00:00+01:00";
|
||||
|
||||
#[inline]
|
||||
fn get_ctx() -> SharedState {
|
||||
let test_date = DATE.parse::<DateTime<Local>>().unwrap();
|
||||
dbg!(test_date);
|
||||
SharedState::new_from(test_date, *test_date.offset())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_h() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"10".to_string()),
|
||||
"1998-01-20T10:00:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_h_high() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24".to_string()),
|
||||
"1998-01-20T10:00:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_hm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:59".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hm_highh() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:59".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hm_highm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:60".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hm_highhm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:60".to_string()),
|
||||
"1998-01-20T23:59:00+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_hms() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:59:59".to_string()),
|
||||
"1998-01-20T23:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_parse_timestamp_hms(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
for i in 1..1000 {
|
||||
black_box(parse_timestamp(&get_ctx(), &"23:59:59".to_string()));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highh() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:59:59".to_string()),
|
||||
"1998-01-20T24:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highm() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:60:59".to_string()),
|
||||
"1998-01-20T23:60:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highs() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"23:59:61".to_string()),
|
||||
"1998-01-20T23:59:61+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Couldn't parse timestamp.")]
|
||||
fn parse_timestamp_hms_highhms() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"24:60:61".to_string()),
|
||||
"1998-01-20T24:60:61+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_timestamp_default() {
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"1998-01-20T23:59:59+01:00".to_string()),
|
||||
"1998-01-20T23:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
parse_timestamp(&get_ctx(), &"1998-01-20T23:59:59Z".to_string()),
|
||||
"1998-01-21T00:59:59+01:00"
|
||||
.parse::<DateTime<Local>>()
|
||||
.unwrap()
|
||||
);
|
||||
// FIXME
|
||||
// assert_eq!(
|
||||
// parse_timestamp(&get_ctx(), &"1998-01-20T23:59:59Z+02:00".to_string()),
|
||||
// "1998-01-21T23:59:59+01:00"
|
||||
// .parse::<DateTime<Local>>()
|
||||
// .unwrap()
|
||||
// );
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue