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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Copyright (c) 2025, Astute Systems PTY LTD
//
// This file is part of the VivoeX SDK project developed by Astute Systems.
//
// See the commercial LICENSE file in the project root for full license details.
//
//! Time utility and helper functions
use chrono::prelude::*;
use chrono::{DateTime, Utc};
pub struct Time {}
impl Time {
/// Get the current time in seconds since the Unix epoch
/// # Returns
/// * `i64` - Current time in seconds since the Unix epoch
/// # Example
/// ```
/// use cot::time::Time;
/// let current_time = Time::now();
/// ```
///
pub fn now() -> i64 {
let current_utc: DateTime<Utc> = Utc::now();
current_utc.timestamp() as i64
}
pub fn now_millis() -> i64 {
let current_utc: DateTime<Utc> = Utc::now();
current_utc.timestamp_millis()
}
/// Get the current time in milliseconds since the Unix epoch
/// # Returns
/// * `i64` - Current time in milliseconds since the Unix epoch
/// # Example
/// ```
/// use cot::time::Time;
/// let current_time = Time::now_millis();
/// ```
///
pub fn stale_time() -> i64 {
let stale: i64 = Time::now() + 120;
stale
}
pub fn stale_time_millis() -> i64 {
// Add 120 seconds to the current time
let stale: i64 = Time::now_millis() + 120000;
stale
}
/// Convert time in seconds since the Unix epoch to a string
/// # Arguments
/// * `time` - Time in seconds since the Unix epoch
/// # Returns
/// * `String` - Time in string format
/// # Example
/// ```
/// use cot::time::Time;
/// let time = 1611770000;
/// let time_string = Time::time_to_string(time);
/// ```
///
pub fn time_to_string(time: i64) -> String {
let current_utc: DateTime<Utc> = DateTime::from_timestamp(time, 0).unwrap();
let formatted = current_utc.format("%Y-%m-%dT%H:%M:%S%.6fZ");
formatted.to_string()
}
/// Get the current time in string format
/// # Returns
/// * `String` - Current time in string format
/// # Example
/// ```
/// use cot::time::Time;
/// let current_time = Time::now_string();
/// ```
///
pub fn now_string() -> String {
let current_utc: DateTime<Local> = Local::now();
// Local as 2025-01-27T06:18:18Z
let formatted = current_utc.format("%Y-%m-%dT%H:%M:%S%.6fZ");
formatted.to_string()
}
}