caretta-sync/core/src/utils/mod.rs

47 lines
1.3 KiB
Rust
Raw Normal View History

2025-08-12 07:29:23 +09:00
use prost_types::Timestamp;
2025-08-14 06:38:15 +09:00
use chrono::{DateTime, TimeZone, Timelike, Utc};
pub mod async_convert;
pub mod emptiable;
2025-07-04 08:05:43 +09:00
pub mod mergeable;
pub mod runnable;
2025-08-12 07:29:23 +09:00
2025-08-14 06:38:15 +09:00
/// ## Examples
/// ```
/// use chrono::Utc;
/// use std::time::SystemTime;
/// use prost_types::Timestamp;
/// use caretta_sync_core::utils::utc_to_timestamp;
2025-08-14 06:38:15 +09:00
///
/// let now_utc = Utc::now();
/// let now_timestamp = utc_to_timestamp(&now_utc);
/// assert_eq!(SystemTime::try_from(now_utc).unwrap(), SystemTime::try_from(now_timestamp).unwrap());
/// ```
pub fn utc_to_timestamp(utc: &DateTime<Utc>) -> Timestamp {
2025-08-12 07:29:23 +09:00
Timestamp{
seconds: utc.timestamp(),
nanos: i32::try_from(utc.nanosecond()).unwrap(),
}
2025-08-14 06:38:15 +09:00
}
/// ## Examples
/// ```
/// use std::time::SystemTime;
/// use prost_types::Timestamp;
/// use caretta_sync_core::utils::timestamp_to_utc;
2025-08-14 06:38:15 +09:00
///
/// let now_timestamp = Timestamp::from(SystemTime::now());
/// let now_utc = timestamp_to_utc(&now_timestamp);
/// assert_eq!(SystemTime::try_from(now_utc).unwrap(), SystemTime::try_from(now_timestamp).unwrap());
/// ```
pub fn timestamp_to_utc(t: &Timestamp) -> DateTime<Utc> {
Utc.timestamp_opt(t.seconds, u32::try_from(t.nanos).unwrap()).unwrap()
}
2025-08-16 11:47:04 +09:00
pub fn get_binary_name() -> Option<String> {
std::env::current_exe()
.ok()?
.file_name()?
.to_str()?
.to_owned()
.into()
}