use super::Apply;
use std::future::Future;
use std::time::{Duration, Instant, SystemTime};
use chrono::{DateTime, Local, Utc};
use futures::FutureExt;
const LAST_MODIFIED_TIME_FORMAT: &str = "%a, %d %b %Y %T GMT";
pub fn to_rfc3339(time: SystemTime) -> String {
let time: DateTime<Local> = time.into();
time.to_rfc3339()
}
pub fn rfc3339_to_last_modified(s: &str) -> Result<String, chrono::ParseError> {
let time: DateTime<Utc> = DateTime::parse_from_rfc3339(s)?.into();
time.format(LAST_MODIFIED_TIME_FORMAT).to_string().apply(Ok)
}
pub fn map_opt_rfc3339_to_last_modified(
s: Option<&str>,
) -> Result<Option<String>, chrono::ParseError> {
s.map(rfc3339_to_last_modified).transpose()
}
pub fn count_duration<F>(f: F) -> impl Future<Output = (F::Output, Duration)> + Send
where
F: Future + Send,
{
let t0 = Instant::now();
f.map(move |ans| {
let dur = t0.elapsed();
(ans, dur)
})
}