-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.rs
67 lines (56 loc) · 1.92 KB
/
datetime.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::time::{SystemTime, UNIX_EPOCH};
pub fn noop_datetime() -> String {
"".to_string()
}
/// Get the current time in the format yyyy-mm-dd hh:mm:ss
pub fn utc_current_time() -> String {
// Get the current system time
let now = SystemTime::now();
// Convert the adjusted time to a formatted string
match now.duration_since(UNIX_EPOCH) {
Ok(duration) => {
let secs = duration.as_secs();
let days = secs / (24 * 3600);
let mut years = 1970;
let mut days_left = days;
// Adjusting for leap years
loop {
let days_in_year = if is_leap_year(years) { 366 } else { 365 };
if days_left < days_in_year {
break;
}
days_left -= days_in_year;
years += 1;
}
let (month, day) = days_to_date(days_left, is_leap_year(years));
let hours = (secs / 3600) % 24;
let minutes = (secs / 60) % 60;
let seconds = secs % 60;
format!(
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
years,
month + 1,
day,
hours,
minutes,
seconds
)
}
Err(_) => "0".to_string(),
}
}
// Check if a year is a leap year
fn is_leap_year(year: u64) -> bool {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
// Calculate the month and day from the number of days since UNIX epoch
fn days_to_date(days: u64, leap: bool) -> (u64, u64) {
let days_in_month = [31, if leap { 29 } else { 28 }, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let mut days_left = days;
let mut month = 0;
while days_left >= days_in_month[month as usize] {
days_left -= days_in_month[month as usize];
month += 1;
}
(month, days_left + 1) // Adding 1 to day to make it 1-based
}