#[cfg(not(feature = "obsolete-chrono"))]
pub type SlackUtcDateTime = jiff::Timestamp;
#[cfg(feature = "obsolete-chrono")]
pub type SlackUtcDateTime = chrono::DateTime<chrono::Utc>;
#[cfg(not(feature = "obsolete-chrono"))]
pub type SlackCivilDate = jiff::civil::Date;
#[cfg(feature = "obsolete-chrono")]
pub type SlackCivilDate = chrono::NaiveDate;
pub(crate) mod unix_seconds {
#[cfg(not(feature = "obsolete-chrono"))]
pub use jiff::fmt::serde::timestamp::second::required::{deserialize, serialize};
#[cfg(feature = "obsolete-chrono")]
pub use chrono::serde::ts_seconds::{deserialize, serialize};
}
pub(crate) fn now() -> SlackUtcDateTime {
#[cfg(not(feature = "obsolete-chrono"))]
{
jiff::Timestamp::now()
}
#[cfg(feature = "obsolete-chrono")]
{
chrono::Utc::now()
}
}
pub(crate) fn from_unix_seconds_micros(secs: i64, micros: u32) -> Option<SlackUtcDateTime> {
let nanos = i32::try_from(micros).ok()?.checked_mul(1_000)?;
#[cfg(not(feature = "obsolete-chrono"))]
{
jiff::Timestamp::new(secs, nanos).ok()
}
#[cfg(feature = "obsolete-chrono")]
{
use chrono::TimeZone;
match chrono::Utc.timestamp_opt(secs, u32::try_from(nanos).ok()?) {
chrono::LocalResult::None => None,
chrono::LocalResult::Single(result) => Some(result),
chrono::LocalResult::Ambiguous(first, _) => Some(first),
}
}
}
#[cfg_attr(feature = "obsolete-chrono", allow(dead_code))]
pub(crate) fn unix_seconds(date_time: &SlackUtcDateTime) -> i64 {
#[cfg(not(feature = "obsolete-chrono"))]
{
date_time.as_second()
}
#[cfg(feature = "obsolete-chrono")]
{
date_time.timestamp()
}
}
#[cfg_attr(feature = "obsolete-chrono", allow(dead_code))]
pub(crate) fn to_rfc2822(date_time: &SlackUtcDateTime) -> String {
#[cfg(not(feature = "obsolete-chrono"))]
{
jiff::fmt::rfc2822::to_string(&date_time.to_zoned(jiff::tz::TimeZone::UTC))
.unwrap_or_else(|_| date_time.to_string())
}
#[cfg(feature = "obsolete-chrono")]
{
date_time.to_rfc2822()
}
}
pub(crate) fn parse_civil_date(value: &str) -> Option<SlackCivilDate> {
#[cfg(not(feature = "obsolete-chrono"))]
{
jiff::fmt::strtime::parse("%Y-%m-%d", value)
.and_then(|parsed| parsed.to_date())
.ok()
}
#[cfg(feature = "obsolete-chrono")]
{
SlackCivilDate::parse_from_str(value, "%Y-%m-%d").ok()
}
}
#[cfg(feature = "signature-verifier")]
pub(crate) fn current_unix_seconds() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|elapsed| elapsed.as_secs() as i64)
.unwrap_or_default()
}