d_engine_client/
utils.rs

1use std::time::Duration;
2use std::time::SystemTime;
3use std::time::UNIX_EPOCH;
4
5/// Returns seconds since epoch as u32
6/// # Warning
7/// This will overflow on 2038-01-19 (Year 2038 problem)
8#[inline]
9pub fn get_now_as_u32() -> u32 {
10    get_duration_since_epoch().as_secs() as u32
11}
12
13#[inline]
14pub fn get_duration_since_epoch() -> Duration {
15    SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards")
16}
17
18/// Normalizes an address string by ensuring it has a proper scheme prefix.
19///
20/// This function:
21/// - Detects and preserves existing "http://" or "https://" schemes
22/// - Defaults to "http://" when no scheme is present
23/// - Prevents scheme duplication
24///
25/// # Examples
26/// - "127.0.0.1:9000" -> "http://127.0.0.1:9000"
27/// - "http://127.0.0.1:9000" -> "http://127.0.0.1:9000"
28/// - "https://127.0.0.1:9000" -> "https://127.0.0.1:9000"
29/// - "node1:9000" -> "http://node1:9000"
30pub(crate) fn address_str(addr: &str) -> String {
31    // Detect if the address already has a scheme
32    if addr.starts_with("https://") {
33        // Preserve HTTPS scheme
34        addr.to_string()
35    } else if addr.starts_with("http://") {
36        // Preserve HTTP scheme
37        addr.to_string()
38    } else {
39        // No scheme present, default to HTTP
40        format!("http://{addr}",)
41    }
42}