1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::time::Duration;
pub use carla_sys::carla::client::Timestamp;
/// Extension trait for [`Timestamp`] providing convenient duration conversions.
///
/// This trait provides methods to convert the various timestamp fields to Rust's
/// [`std::time::Duration`] type for easier time manipulation and comparison.
#[cfg_attr(
carla_version_0916,
doc = " See [carla.Timestamp](https://carla.readthedocs.io/en/0.9.16/python_api/#carla.Timestamp) in the Python API."
)]
#[cfg_attr(
carla_version_0915,
doc = " See [carla.Timestamp](https://carla.readthedocs.io/en/0.9.15/python_api/#carla.Timestamp) in the Python API."
)]
#[cfg_attr(
carla_version_0914,
doc = " See [carla.Timestamp](https://carla.readthedocs.io/en/0.9.14/python_api/#carla.Timestamp) in the Python API."
)]
///
/// # Examples
///
/// ```no_run
/// use carla::{client::Client, prelude::*};
///
/// let client = Client::default();
/// let world = client.world();
/// let snapshot = world.wait_for_tick();
/// let timestamp = snapshot.timestamp();
///
/// // Get elapsed time as Duration
/// let elapsed = timestamp.elapsed_duration();
/// println!("Simulation time: {:?}", elapsed);
///
/// // Get delta time for this frame
/// let delta = timestamp.delta_duration();
/// println!("Frame delta: {:?}", delta);
/// ```
pub trait TimestampExt {
/// Converts `elapsed_seconds` to a [`Duration`].
///
/// Returns the total elapsed simulation time since the beginning of the episode.
fn elapsed_duration(&self) -> Duration;
/// Converts `delta_seconds` to a [`Duration`].
///
/// Returns the time elapsed since the previous tick. This is useful for
/// frame-rate independent calculations.
fn delta_duration(&self) -> Duration;
/// Converts `platform_timestamp` to a [`Duration`].
///
/// Returns the platform (OS) time when the frame was computed. This is
/// useful for synchronizing with real-world time.
fn platform_duration(&self) -> Duration;
}
impl TimestampExt for Timestamp {
fn elapsed_duration(&self) -> Duration {
Duration::from_secs_f64(self.elapsed_seconds)
}
fn delta_duration(&self) -> Duration {
Duration::from_secs_f64(self.delta_seconds)
}
fn platform_duration(&self) -> Duration {
Duration::from_secs_f64(self.platform_timestamp)
}
}