我一直在try ,但这很难,因为类型并不完全匹配.
我目前的try 是:
use ::time::{PrimitiveDateTime, OffsetDateTime};
use std::time::{UNIX_EPOCH, Duration};
use chrono::{DateTime, Utc, NaiveDateTime};
fn my_func() {
let timestamp:u64 = 1696443084 // {integer from http request, representing timestamp in ms since epoch}
let d:SystemTime = UNIX_EPOCH + Duration::from_secs(timestamp/1000);
let datetime:DateTime<Utc> = DateTime::<Utc>::from(d); // ideally want to replace this line with one where I get a DateTime (and not DateTime<Utc>) from the integer.
let created_at:PrimitiveDateTime = PrimitiveDateTime::new(datetime.date_naive(), datetime.time());
}
我的错误消息(编译)如下:
106 | ... = PrimitiveDateTime::new(datetime.date_naive(), datetime.time(...
| ^^^^^^^^^^^^^^^^^^^^^^ --------------------- --------------- expected `Time`, found `NaiveTime`
| |
| expected `Date`, found `NaiveDate`
|
note: associated function defined here
--> /.../time-0.3.23/src/primitive_date_time.rs:92:18
|
92 | pub const fn new(date: ...
| ^^^
我需要使用PrimitiveDateTime,因为这是时间戳在psql DB中的存储方式.