Closed TCeason closed 2 hours ago
Now I can use this to replace:
use jiff::civil::Date;
use jiff::Timestamp;
use jiff::Unit;
use jiff::Zoned;
use num_traits::AsPrimitive;
pub const MICROS_PER_SEC: i64 = 1_000_000;
pub trait DateConverter {
fn to_timestamp_tz(&self, tz: &str) -> Zoned;
}
impl<T> DateConverter for T
where
T: AsPrimitive<i64>,
{
fn to_timestamp_tz(&self, tz: &str) -> Zoned {
// Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow.
let micros = self.as_();
let (mut secs, mut nanos) = (micros / MICROS_PER_SEC, (micros % MICROS_PER_SEC) * 1_000);
if nanos < 0 {
secs -= 1;
nanos += 1_000_000_000;
}
let ts = Timestamp::new(secs, nanos as i32).unwrap();
let a = ts.intz(tz).unwrap();
a
}
}
fn main() {
let i: i64 = 1630320462000000;
let a = i
.to_timestamp_tz("Asia/Shanghai")
.date()
.since((Unit::Day, Date::new(1970, 1, 1).unwrap()))
.unwrap()
.get_days();
println!("{}", a);
}
That looks right to me.
In the future, when filing issues, please focus more on the higher level problem you're trying to solve. :-)
https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html#method.from_num_days_from_ce
It return NaiveDate and I can convert it as i32.
But in jiff
pub(crate) fn to_unix_epoch_days(self) -> UnixEpochDays {
is public in crate.Can we support it?