kutil_http/headers/
date.rs

1use super::into::*;
2
3use {
4    http::*,
5    httpdate::*,
6    kutil_std::error::*,
7    std::{fs::*, io, path::*, time::*},
8};
9
10/// Current time as [HttpDate].
11pub fn now() -> HttpDate {
12    HttpDate::from(SystemTime::now())
13}
14
15/// File modification timestamp as HttpDate.
16pub fn file_modified<PathT>(path: PathT) -> io::Result<HttpDate>
17where
18    PathT: AsRef<Path>,
19{
20    let path = path.as_ref();
21    metadata(path)?.modified().map(|system_time| system_time.into()).with_path(path)
22}
23
24/// Whether we have been modified since a reference date.
25///
26/// If there is not enough information we will assume that we have been modified and return true.
27pub fn modified_since(modified_date: Option<HttpDate>, reference_date: Option<HttpDate>) -> bool {
28    if let Some(last_modified) = modified_date {
29        if let Some(reference_date) = reference_date {
30            if last_modified <= reference_date {
31                return false;
32            }
33        }
34    }
35
36    true
37}
38
39impl IntoHeaderValue for HttpDate {
40    fn into_header_value(self) -> HeaderValue {
41        HeaderValue::from_str(&fmt_http_date(self.into()))
42            .expect("fmt_http_date should always create a valid HeaderValue string")
43    }
44}