headers/common/
if_unmodified_since.rs
1use std::time::SystemTime;
2use util::HttpDate;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34pub struct IfUnmodifiedSince(HttpDate);
35
36derive_header! {
37 IfUnmodifiedSince(_),
38 name: IF_UNMODIFIED_SINCE
39}
40
41impl IfUnmodifiedSince {
42 pub fn precondition_passes(&self, last_modified: SystemTime) -> bool {
44 self.0 >= last_modified.into()
45 }
46}
47
48impl From<SystemTime> for IfUnmodifiedSince {
49 fn from(time: SystemTime) -> IfUnmodifiedSince {
50 IfUnmodifiedSince(time.into())
51 }
52}
53
54impl From<IfUnmodifiedSince> for SystemTime {
55 fn from(date: IfUnmodifiedSince) -> SystemTime {
56 date.0.into()
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63 use std::time::Duration;
64
65 #[test]
66 fn precondition_passes() {
67 let newer = SystemTime::now();
68 let exact = newer - Duration::from_secs(2);
69 let older = newer - Duration::from_secs(4);
70
71 let if_unmod = IfUnmodifiedSince::from(exact);
72 assert!(!if_unmod.precondition_passes(newer));
73 assert!(if_unmod.precondition_passes(exact));
74 assert!(if_unmod.precondition_passes(older));
75 }
76}