slack_morphism/models/common/
datetime.rs1#[cfg(not(feature = "obsolete-chrono"))]
14pub type SlackUtcDateTime = jiff::Timestamp;
15
16#[cfg(feature = "obsolete-chrono")]
21pub type SlackUtcDateTime = chrono::DateTime<chrono::Utc>;
22
23#[cfg(not(feature = "obsolete-chrono"))]
28pub type SlackCivilDate = jiff::civil::Date;
29
30#[cfg(feature = "obsolete-chrono")]
35pub type SlackCivilDate = chrono::NaiveDate;
36
37pub(crate) mod unix_seconds {
41 #[cfg(not(feature = "obsolete-chrono"))]
42 pub use jiff::fmt::serde::timestamp::second::required::{deserialize, serialize};
43
44 #[cfg(feature = "obsolete-chrono")]
45 pub use chrono::serde::ts_seconds::{deserialize, serialize};
46}
47
48pub(crate) fn now() -> SlackUtcDateTime {
50 #[cfg(not(feature = "obsolete-chrono"))]
51 {
52 jiff::Timestamp::now()
53 }
54 #[cfg(feature = "obsolete-chrono")]
55 {
56 chrono::Utc::now()
57 }
58}
59
60pub(crate) fn from_unix_seconds_micros(secs: i64, micros: u32) -> Option<SlackUtcDateTime> {
64 let nanos = i32::try_from(micros).ok()?.checked_mul(1_000)?;
65
66 #[cfg(not(feature = "obsolete-chrono"))]
67 {
68 jiff::Timestamp::new(secs, nanos).ok()
69 }
70 #[cfg(feature = "obsolete-chrono")]
71 {
72 use chrono::TimeZone;
73 match chrono::Utc.timestamp_opt(secs, u32::try_from(nanos).ok()?) {
74 chrono::LocalResult::None => None,
75 chrono::LocalResult::Single(result) => Some(result),
76 chrono::LocalResult::Ambiguous(first, _) => Some(first),
77 }
78 }
79}
80
81#[cfg_attr(feature = "obsolete-chrono", allow(dead_code))]
86pub(crate) fn unix_seconds(date_time: &SlackUtcDateTime) -> i64 {
87 #[cfg(not(feature = "obsolete-chrono"))]
88 {
89 date_time.as_second()
90 }
91 #[cfg(feature = "obsolete-chrono")]
92 {
93 date_time.timestamp()
94 }
95}
96
97#[cfg_attr(feature = "obsolete-chrono", allow(dead_code))]
100pub(crate) fn to_rfc2822(date_time: &SlackUtcDateTime) -> String {
101 #[cfg(not(feature = "obsolete-chrono"))]
102 {
103 jiff::fmt::rfc2822::to_string(&date_time.to_zoned(jiff::tz::TimeZone::UTC))
104 .unwrap_or_else(|_| date_time.to_string())
105 }
106 #[cfg(feature = "obsolete-chrono")]
107 {
108 date_time.to_rfc2822()
109 }
110}
111
112pub(crate) fn parse_civil_date(value: &str) -> Option<SlackCivilDate> {
114 #[cfg(not(feature = "obsolete-chrono"))]
115 {
116 jiff::fmt::strtime::parse("%Y-%m-%d", value)
117 .and_then(|parsed| parsed.to_date())
118 .ok()
119 }
120 #[cfg(feature = "obsolete-chrono")]
121 {
122 SlackCivilDate::parse_from_str(value, "%Y-%m-%d").ok()
123 }
124}
125
126#[cfg(feature = "signature-verifier")]
131pub(crate) fn current_unix_seconds() -> i64 {
132 std::time::SystemTime::now()
133 .duration_since(std::time::UNIX_EPOCH)
134 .map(|elapsed| elapsed.as_secs() as i64)
135 .unwrap_or_default()
136}