1use chrono::{DateTime, Duration, Local, NaiveDate, NaiveDateTime, ParseError, TimeZone, Utc};
2use std::cmp::Ordering;
3use std::time::{SystemTime, UNIX_EPOCH};
4
5pub struct DateUtil;
7
8impl DateUtil {
9 pub fn current_date() -> NaiveDate {
10 let now = Local::now();
12 now.date_naive()
13 }
14
15 pub fn current_date_string() -> String {
18 let now = Local::now();
19 now.format("%Y-%m-%d").to_string()
20 }
21
22 pub fn current_datetime() -> NaiveDateTime {
23 let now = Local::now();
25 now.naive_local()
26 }
27
28 pub fn current_datetime_string() -> String {
29 let now = Local::now();
31 now.format("%Y-%m-%d %H:%M:%S").to_string()
32 }
33
34 pub fn today_string() -> String {
41 let now = Local::now();
42 now.format("%Y-%m-%d").to_string()
43 }
44
45 pub fn today_date() -> NaiveDate {
46 let now = Local::now();
48 now.date_naive()
49 }
50
51 pub fn parse_day_string(day_string: &str) -> Result<NaiveDate, ParseError> {
52 NaiveDate::parse_from_str(day_string, "%Y-%m-%d")
53 }
54
55 pub fn day_offset(offset: i64) -> String {
60 let now: DateTime<Local> = Local::now();
61 let duration = Duration::days(offset);
62 let now = now + duration;
63 now.format("%Y-%m-%d").to_string()
64 }
65
66 pub fn yesterday_string() -> String {
81 let now = Local::now();
83 let yesterday = now - Duration::days(1);
85 yesterday.format("%Y-%m-%d").to_string()
87 }
88
89 pub fn yesterday_date() -> NaiveDate {
90 let now = Local::now();
92 let yesterday = now - Duration::days(1);
94 yesterday.date_naive()
96 }
97
98 pub fn tomorrow_string() -> String {
105 let now = Local::now();
107 let tomorrow = now + Duration::days(1);
109 tomorrow.format("%Y-%m-%d").to_string()
111 }
112
113 pub fn tomorrow_date() -> NaiveDate {
114 let now = Local::now();
116 let tomorrow = now + Duration::days(1);
118 tomorrow.date_naive()
120 }
121
122 pub fn cmp_string(time1: &str, time2: &str, fmt: &str) -> Result<Ordering, ParseError> {
140 let naive1 = NaiveDateTime::parse_from_str(time1, fmt)?;
142 let naive2 = NaiveDateTime::parse_from_str(time2, fmt)?;
143 let datetime1: DateTime<Utc> = Utc.from_utc_datetime(&naive1);
144 let datetime2: DateTime<Utc> = Utc.from_utc_datetime(&naive2);
145 let c = datetime1.cmp(&datetime2);
146 Ok(c)
147 }
148
149 pub fn current_time_string() -> String {
152 let now = Local::now();
153 now.format("%Y-%m-%d %H:%M:%S").to_string()
154 }
155
156 pub fn parse_datetime(time: &str) -> Result<NaiveDateTime, chrono::ParseError> {
159 let res = NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S")?;
160 Ok(res)
161 }
162
163 pub fn parse_string(time: &str, fmt: &str) -> Result<NaiveDateTime, chrono::ParseError> {
164 let res = NaiveDateTime::parse_from_str(time, fmt)?;
165 Ok(res)
166 }
167
168 pub fn current_timestamp() -> u64 {
170 let time = Local::now();
171 DateUtil::timestamp(time)
172 }
173
174 pub fn timestamp(time: DateTime<Local>) -> u64 {
176 let system_time: SystemTime = time.into();
177 let duration = system_time.duration_since(UNIX_EPOCH).unwrap();
178 let timestamp = duration.as_secs();
179 timestamp
180 }
181
182 pub fn timestamp_add(time: DateTime<Local>, seconds: i64) -> u64 {
184 let diff = Duration::seconds(seconds);
185 let time = time + diff;
186 DateUtil::timestamp(time)
187 }
188}
189
190#[cfg(test)]
191mod tests {
192
193 use super::*;
194
195 #[test]
196 fn test_current_time_string() {
197 let timestring = DateUtil::current_time_string();
198 println!("timestring = {}", timestring);
199 }
200
201 #[test]
202 fn test_current_timestamp() {
203 let ts = DateUtil::current_timestamp();
204 println!("ts = {}", ts);
205 }
206
207 #[test]
208 fn test_parse_datetime() {
209 let x = DateUtil::parse_datetime("2025-08-30 12:00:00");
210 println!("{:?}", x);
211 }
212
213 #[test]
214 fn test_cmp_string() {
215 let c = DateUtil::cmp_string(
216 "2025-08-30 13:00:00",
217 "2025-08-30 12:00:01",
218 "%Y-%m-%d %H:%M:%S",
219 );
220 println!("{:?}", c);
221 }
222}