0
use chrono::format::{parse, Item, Parsed};

static SUPPORTED_DATETIME_FORMATS: &[&'static str] = &["%s",];

static ref PARSED_DATETIME_FORMATS : Vec<Vec<Item<'static>>> = SUPPORTED_DATETIME_FORMATS
    .iter()
    .map(|format| StrftimeItems::new(format).collect())
    .collect();
    
fn parse(s: &str) -> Option<i64> {
    for format in PARSED_DATETIME_FORMATS.iter() {
        let mut parsed = Parsed::new();
        let dt = parse(&mut parsed, &s, format.iter().cloned()).and_then(|_| {
            DateTimeParser::apply_default_values(&mut parsed);
            parsed.to_datetime()
        });
        if dt.is_ok() {
            return dt.ok().and_then(|dt| DateTimeParser::extract_ticks(&dt));
        }
    }
    return None;
}

Unix timestamp should be supported according to the docs: https://docs.rs/chrono/0.3.1/chrono/format/strftime/index.html

However: let result = parse("994518299"); // result = None

Anyone knows why?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Renya Karasuma
  • 1,044
  • 4
  • 11
  • 18
  • You are linking the docs of an outdated (actually, even yanked!) version of the ``chrono`` crate. With the most recent release v0.4.13 the timestamp parses just fine. So I guess updating the deps of your project might fix the issue for you. – phg Jul 20 '20 at 08:20
  • @phg I'm using the 0.4.13 release, still not working. – Renya Karasuma Jul 21 '20 at 00:22

0 Answers0