fxprof_processed_profile/
timestamp.rs

1use serde::ser::{Serialize, Serializer};
2
3/// The type used for sample and marker timestamps.
4///
5/// Timestamps in the profile are stored in reference to the profile's [`ReferenceTimestamp`](crate::ReferenceTimestamp).
6#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
7pub struct Timestamp {
8    nanos: u64,
9}
10
11impl Timestamp {
12    pub fn from_nanos_since_reference(nanos: u64) -> Self {
13        Self { nanos }
14    }
15
16    pub fn from_millis_since_reference(millis: f64) -> Self {
17        Self {
18            nanos: (millis * 1_000_000.0) as u64,
19        }
20    }
21}
22
23impl Serialize for Timestamp {
24    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
25        // In the profile JSON, timestamps are currently expressed as float milliseconds
26        // since profile.meta.startTime.
27        serializer.serialize_f64((self.nanos as f64) / 1_000_000.0)
28    }
29}