quad_timestamp/
lib.rs

1 #[no_mangle]
2extern "C" fn quad_timestamp_crate_version() -> u32 {
3    let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u32>().unwrap();
4    let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u32>().unwrap();
5    let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u32>().unwrap();
6    (major << 24) + (minor << 16) + patch
7}
8
9#[cfg(target_arch = "wasm32")]
10mod internal{
11    extern "C" {
12        pub fn timestamp_utc() -> u32;
13        pub fn timestamp_utc_ms() -> u32;
14    }
15}
16
17/// Returns an `Option<i64>` with the current UTC timestamp in seconds.
18/// 
19/// # Example
20/// 
21/// ```
22/// use quad_timestamp::timestamp_utc;
23/// 
24/// let timestamp = timestamp_utc();
25/// ```
26/// 
27/// # Returns
28/// 
29/// `Option<i64>` - The current UTC timestamp in seconds. None if the timestamp could not be retrieved.
30///  
31pub fn timestamp_utc() -> Option<i64>{ 
32    
33    #[cfg(target_arch = "wasm32")]
34    {
35        let mut timestamp = None; 
36        unsafe {
37            timestamp = Some(internal::timestamp_utc() as i64);
38        }
39        return timestamp;
40    }
41
42    #[cfg(not(target_arch = "wasm32"))]
43    {
44        return Some(chrono::Utc::now().timestamp());
45    }
46}
47
48/// Returns an `Option<i64>` with the current UTC timestamp in milliseconds.
49/// 
50/// # Example
51/// 
52/// ```
53/// use quad_timestamp::timestamp_utc_ms;
54/// 
55/// let timestamp = timestamp_utc_ms();
56/// ```
57/// 
58/// # Returns
59/// 
60/// `Option<i64>` - The current UTC timestamp in milliseconds. None if the timestamp could not be retrieved.
61/// 
62pub fn timestamp_utc_ms() -> Option<i64>{ 
63    #[cfg(target_arch = "wasm32")]
64    {
65        let mut timestamp = None; 
66        unsafe {
67            timestamp = Some(internal::timestamp_utc_ms() as i64);
68        }
69        return timestamp;
70    }
71
72    #[cfg(not(target_arch = "wasm32"))]
73    {
74        return Some(chrono::Utc::now().timestamp_millis());
75    }
76}