Closed AdairStone closed 3 weeks ago
let local_offset_time = OffsetTime::local_rfc_3339().expect("could not get local offset");
建议使用chrono实现
我本地的示例
pub fn current_date() -> DateTime { //default FixedOffset::east_opt(0).unwrap() let local_offset = Local::now().offset().fix().local_minus_utc(); let fixed_offset = FixedOffset::east_opt(local_offset) .unwrap_or(FixedOffset::east_opt(8*3600).unwrap()); let r = Utc::now().with_timezone(&fixed_offset); return r; } fn format_date_ymdhms(date: DateTime) -> String { let fmt = "%Y-%m-%d %H:%M:%S%.3f"; return date.format(fmt).to_string(); } struct LogTimer;
impl FormatTime for LogTimer { fn format_time(&self, w: &mut tracingsubscriber::fmt::format::Writer<'>) -> std::fmt::Result { write!( w, "{}", format_date_ymdhms(current_date()) ) } }
有报什么错吗 可以参考之前的这个Issue: https://github.com/zino-rs/zino/issues/98
建议使用chrono实现
我本地的示例
pub fn current_date() -> DateTime {
//default FixedOffset::east_opt(0).unwrap()
let local_offset = Local::now().offset().fix().local_minus_utc();
let fixed_offset = FixedOffset::east_opt(local_offset)
.unwrap_or(FixedOffset::east_opt(8*3600).unwrap());
let r = Utc::now().with_timezone(&fixed_offset);
return r;
}
fn format_date_ymdhms(date: DateTime) -> String {
let fmt = "%Y-%m-%d %H:%M:%S%.3f";
return date.format(fmt).to_string();
}
struct LogTimer;
impl FormatTime for LogTimer { fn format_time(&self, w: &mut tracingsubscriber::fmt::format::Writer<'>) -> std::fmt::Result { write!( w, "{}", format_date_ymdhms(current_date()) ) } }