pub struct DateUtil;Expand description
日期工具,包含常用快捷生成,如获取当日时间字符串、格式化、时间字符串解析等
Implementations§
Source§impl DateUtil
impl DateUtil
pub fn current_date() -> NaiveDate
Sourcepub fn current_date_string() -> String
pub fn current_date_string() -> String
获取当前日期字符串,输出格式:%Y-%m-%d
pub fn current_datetime() -> NaiveDateTime
pub fn current_datetime_string() -> String
Sourcepub fn today_string() -> String
pub fn today_string() -> String
pub fn today_date() -> NaiveDate
pub fn parse_day_string(day_string: &str) -> Result<NaiveDate, ParseError>
Sourcepub fn day_offset(offset: i64) -> String
pub fn day_offset(offset: i64) -> String
Sourcepub fn yesterday_string() -> String
pub fn yesterday_string() -> String
pub fn yesterday_date() -> NaiveDate
Sourcepub fn tomorrow_string() -> String
pub fn tomorrow_string() -> String
pub fn tomorrow_date() -> NaiveDate
Sourcepub fn cmp_string(
time1: &str,
time2: &str,
fmt: &str,
) -> Result<Ordering, ParseError>
pub fn cmp_string( time1: &str, time2: &str, fmt: &str, ) -> Result<Ordering, ParseError>
比较两个时间字符串的大小
该函数将两个时间字符串按照指定的格式解析为DateTime对象,然后进行比较。
§参数
time1- 第一个时间字符串time2- 第二个时间字符串fmt- 时间格式字符串,用于解析time1和time2
§返回值
Ok(Ordering)- 比较结果,Ordering::Less表示time1小于time2, Ordering::Equal表示time1等于time2, Ordering::Greater表示time1大于time2Err(ParseError)- 时间解析失败时返回错误信息
§错误处理
如果任一时间字符串无法按照指定格式解析,函数将返回ParseError错误
Sourcepub fn current_time_string() -> String
pub fn current_time_string() -> String
获取当前时间字符串,输出格式:%Y-%m-%d %H:%M:%S
Sourcepub fn parse_datetime(time: &str) -> Result<NaiveDateTime, ParseError>
pub fn parse_datetime(time: &str) -> Result<NaiveDateTime, ParseError>
§参数
- time: 格式如:“2025-08-30 12:00:00”
pub fn parse_string(time: &str, fmt: &str) -> Result<NaiveDateTime, ParseError>
Sourcepub fn current_timestamp() -> u64
pub fn current_timestamp() -> u64
当前时间戳(秒),返回10位长度
Auto Trait Implementations§
impl Freeze for DateUtil
impl RefUnwindSafe for DateUtil
impl Send for DateUtil
impl Sync for DateUtil
impl Unpin for DateUtil
impl UnwindSafe for DateUtil
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more