Skip to content

Commit

Permalink
Refine timestamp printing
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanruth committed Apr 16, 2024
1 parent ff4824c commit 3986ad5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ What changed in mapiproxy, per version

- Add -o or --output= option to direct output to a file.

- Support proxying [Out-Of-Band (OOB)][OOB] signals.
- Print timestamp marker before the first message of each minute.

- Support proxying [Out-Of-Band (OOB)][OOB] signals (Linux only).

[OOB]: https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Out-of-band_data

Expand Down
41 changes: 27 additions & 14 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Renderer {
out: buffered,
current_style: Style::Normal,
at_start: Some(Style::Normal),
timing: TrackTime::default(),
timing: TrackTime::new(),
}
}

Expand All @@ -38,6 +38,7 @@ impl Renderer {
if let Some(announcement) = self.timing.announcement() {
let message = format!("TIME is {announcement}");
self.message_no_check_time(None, None, &message)?;
writeln!(self.out)?;
}
Ok(())
}
Expand Down Expand Up @@ -185,17 +186,25 @@ impl From<(Option<ConnectionId>, Option<Direction>)> for IdStream {
}
}

#[derive(Debug, Default)]
#[derive(Debug)]
struct TrackTime {
now: Option<Timestamp>,
last_activity: Option<Timestamp>,
last_announce: Option<Timestamp>,
next_announcement: Timestamp,
}

impl TrackTime {
const SEPARATOR_THRESHOLD: Duration = Duration::from_millis(500);
const ANNOUNCEMENT_THRESHOLD: Duration = Duration::from_secs(60);

fn new() -> Self {
TrackTime {
now: None,
last_activity: None,
next_announcement: Timestamp(Duration::ZERO),
}
}

fn set_time(&mut self, now: &Timestamp) {
self.now = Some(now.clone())
}
Expand All @@ -213,23 +222,27 @@ impl TrackTime {
self.elapsed_since(&prev) >= Self::SEPARATOR_THRESHOLD
}

fn must_announce(&self) -> bool {
let Some(prev) = &self.last_announce else {
return true;
};
self.elapsed_since(prev) >= Self::ANNOUNCEMENT_THRESHOLD
}

fn announcement(&mut self) -> Option<String> {
if !self.must_announce() {
if self.now() < &self.next_announcement {
return None;
}

// decide the next time
let units = self.now().0.as_secs_f64() / Self::ANNOUNCEMENT_THRESHOLD.as_secs_f64();
let mut ceil = units.ceil();
// we need strictly greater, not greater or equal
if ceil == units {
ceil += 1.0;
}
let ceil_seconds = ceil * Self::ANNOUNCEMENT_THRESHOLD.as_secs_f64();
self.next_announcement = Timestamp(Duration::from_secs_f64(ceil_seconds));

// format the timestamp
let now = self.now();
let epoch = chrono::DateTime::UNIX_EPOCH;
let utc_now = epoch + now.0;
let local: DateTime<Local> = DateTime::from(utc_now);
let formatted = local.to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
self.last_announce = Some(now.clone());
let local_now: DateTime<Local> = DateTime::from(utc_now);
let formatted = local_now.format("%Y-%m-%d %H:%M:%S%.3f").to_string();
Some(formatted)
}

Expand Down

0 comments on commit 3986ad5

Please sign in to comment.