增加日期的格式化定义
This commit is contained in:
parent
de82c83a2a
commit
da22405c3e
|
@ -4,7 +4,7 @@ pub mod member;
|
|||
|
||||
const DEFAULT_PAGE_SIZE: u32 = 30;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct Paginate<T: Serialize> {
|
||||
pub total: u32,
|
||||
pub total_page: u32,
|
||||
|
|
|
@ -5,4 +5,6 @@ pub mod handler;
|
|||
pub mod model;
|
||||
pub mod view;
|
||||
pub mod form;
|
||||
pub mod util;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, crate::err::Error>;
|
|
@ -1,5 +1,3 @@
|
|||
#![allow(unused, dead_code)]
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::Router;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::util::default_datetime_format;
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -34,6 +35,7 @@ impl Display for MemberTypes {
|
|||
pub struct Member {
|
||||
pub id: u32,
|
||||
pub name: String,
|
||||
#[serde(with = "default_datetime_format")]
|
||||
pub dateline: chrono::DateTime<chrono::Local>,
|
||||
pub balance: u32,
|
||||
pub types: MemberTypes,
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
|
||||
|
||||
pub mod default_datetime_format {
|
||||
use chrono::{DateTime, Local, NaiveDateTime};
|
||||
use serde::{self, Deserialize, Serializer, Deserializer};
|
||||
|
||||
const FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
|
||||
|
||||
pub fn serialize<S>(
|
||||
date: &DateTime<Local>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let s = format!("{}", date.format(FORMAT));
|
||||
serializer.serialize_str(&s)
|
||||
}
|
||||
pub fn deserialize<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<DateTime<Local>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let datetime_str = String::deserialize(deserializer)?;
|
||||
let dt = NaiveDateTime::parse_from_str(&datetime_str, FORMAT).map_err(serde::de::Error::custom)?;
|
||||
Ok(dt.and_local_timezone(Local).unwrap())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue