1use std::{
2 cell::Cell,
3 fmt::{self, Write},
4 rc::Rc,
5 time::{Duration, Instant, SystemTime},
6};
7
8use actix_rt::{task::JoinHandle, time::interval};
9
10pub(crate) const DATE_VALUE_LENGTH: usize = 29;
12
13#[derive(Clone, Copy)]
14pub(crate) struct Date {
15 pub(crate) bytes: [u8; DATE_VALUE_LENGTH],
16 pos: usize,
17}
18
19impl Date {
20 fn new() -> Date {
21 let mut date = Date {
22 bytes: [0; DATE_VALUE_LENGTH],
23 pos: 0,
24 };
25 date.update();
26 date
27 }
28
29 fn update(&mut self) {
30 self.pos = 0;
31 write!(self, "{}", httpdate::HttpDate::from(SystemTime::now())).unwrap();
32 }
33}
34
35impl fmt::Write for Date {
36 fn write_str(&mut self, s: &str) -> fmt::Result {
37 let len = s.len();
38 self.bytes[self.pos..self.pos + len].copy_from_slice(s.as_bytes());
39 self.pos += len;
40 Ok(())
41 }
42}
43
44pub(crate) struct DateService {
46 current: Rc<Cell<(Date, Instant)>>,
47 handle: JoinHandle<()>,
48}
49
50impl DateService {
51 pub(crate) fn new() -> Self {
52 let current = Rc::new(Cell::new((Date::new(), Instant::now())));
54 let current_clone = Rc::clone(¤t);
55 let handle = actix_rt::spawn(async move {
58 #[cfg(test)]
59 let _notify = crate::notify_on_drop::NotifyOnDrop::new();
60
61 let mut interval = interval(Duration::from_millis(500));
62 loop {
63 let now = interval.tick().await;
64 let date = Date::new();
65 current_clone.set((date, now.into_std()));
66 }
67 });
68
69 DateService { current, handle }
70 }
71
72 pub(crate) fn now(&self) -> Instant {
73 self.current.get().1
74 }
75
76 pub(crate) fn with_date<F: FnMut(&Date)>(&self, mut f: F) {
77 f(&self.current.get().0);
78 }
79}
80
81impl fmt::Debug for DateService {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 f.debug_struct("DateService").finish_non_exhaustive()
84 }
85}
86
87impl Drop for DateService {
88 fn drop(&mut self) {
89 self.handle.abort();
91 }
92}