bookdata/parsing/
dates.rs

1use chrono::prelude::*;
2use chrono::ParseResult;
3use log::*;
4
5/// The date format used in the GoodReads data.
6///
7/// This almost RFC2822, but hte year is in the wrong place.
8const GR_DATE_FMT: &'static str = "%A %B %d %H:%M:%S %z %Y";
9
10/// Parse a GoodReads date.
11pub fn parse_gr_date(s: &str) -> ParseResult<NaiveDateTime> {
12    let date = DateTime::parse_from_str(s, GR_DATE_FMT)?.naive_utc();
13    Ok(date)
14}
15
16/// Check a date and convert to a timestamp.
17pub fn check_ts(context: &'static str, cutoff: i32) -> impl Fn(NaiveDateTime) -> f32 {
18    move |date| {
19        if date.year() < cutoff {
20            warn!("{} is ancient: {}", context, date);
21        }
22        date.timestamp() as f32
23    }
24}