From 7e53ad9f78487c9df37a7457c01b40b07f9741ac Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 31 Jan 2018 22:22:54 +0100 Subject: [PATCH] Add datetime <-> string conversion utility --- lib/etc/libimagutil/src/date.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/etc/libimagutil/src/date.rs b/lib/etc/libimagutil/src/date.rs index dc9f9de0..984f028b 100644 --- a/lib/etc/libimagutil/src/date.rs +++ b/lib/etc/libimagutil/src/date.rs @@ -18,6 +18,7 @@ // use chrono::NaiveDate; +use chrono::NaiveDateTime; use chrono::format::ParseError; pub const NAIVE_DATE_STRING_FORMAT : &'static str = "%Y-%m-%d"; @@ -30,3 +31,13 @@ pub fn date_from_string(s: String) -> Result { NaiveDate::parse_from_str(&s, NAIVE_DATE_STRING_FORMAT) } +pub const NAIVE_DATETIME_STRING_FORMAT : &'static str = "%Y-%m-%d %H:%M:%S"; + +pub fn datetime_to_string(ndt: &NaiveDateTime) -> String { + ndt.format(NAIVE_DATETIME_STRING_FORMAT).to_string() +} + +pub fn datetime_from_string(s: &str) -> Result { + NaiveDateTime::parse_from_str(s, NAIVE_DATETIME_STRING_FORMAT) +} +