openzeppelin_relayer/utils/
time.rs

1use chrono::Utc;
2
3/// Converts minutes to milliseconds
4pub const fn minutes_ms(minutes: i64) -> i64 {
5    minutes * 60 * 1000
6}
7
8/// Calculates a future timestamp by adding delay seconds to the current time
9///
10/// # Arguments
11/// * `delay_seconds` - Number of seconds to add to the current timestamp
12///
13/// # Returns
14/// Unix timestamp (seconds since epoch) representing the scheduled time
15pub fn calculate_scheduled_timestamp(delay_seconds: i64) -> i64 {
16    Utc::now().timestamp() + delay_seconds
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    #[test]
24    fn test_minutes_ms() {
25        assert_eq!(minutes_ms(1), 60_000);
26        assert_eq!(minutes_ms(5), 300_000);
27        assert_eq!(minutes_ms(10), 600_000);
28    }
29
30    #[test]
31    fn test_calculate_scheduled_timestamp() {
32        let before = Utc::now().timestamp();
33        let delay_seconds = 30;
34        let scheduled = calculate_scheduled_timestamp(delay_seconds);
35        let after = Utc::now().timestamp();
36
37        // The scheduled time should be approximately delay_seconds in the future
38        assert!(scheduled >= before + delay_seconds);
39        assert!(scheduled <= after + delay_seconds + 1); // Allow 1 second for test execution
40    }
41}