第一版
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
use chrono::DateTime;
|
||||
|
||||
/// 将多种类型统一转为时间戳 (Unix epoch 秒)
|
||||
pub fn 转化为时间戳(ts: &str) -> Option<i64> {
|
||||
// 尝试解析为整数时间戳
|
||||
if let Ok(v) = ts.parse::<i64>() {
|
||||
return Some(v);
|
||||
}
|
||||
// 尝试解析为浮点时间戳
|
||||
if let Ok(v) = ts.parse::<f64>() {
|
||||
return Some(v as i64);
|
||||
}
|
||||
// 尝试 ISO 格式日期字符串
|
||||
if let Ok(dt) = DateTime::parse_from_rfc3339(ts) {
|
||||
return Some(dt.timestamp());
|
||||
}
|
||||
// 尝试 "YYYY-MM-DD HH:MM:SS" 格式
|
||||
if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(ts, "%Y-%m-%d %H:%M:%S") {
|
||||
return Some(dt.and_utc().timestamp());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// 将多种类型转为时间戳数字 (i64)
|
||||
pub fn 转化为时间戳_数字(ts: &str) -> Option<i64> {
|
||||
转化为时间戳(ts)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/// Format f64 with Python :g semantics — strip trailing zeros, no scientific notation for common values
|
||||
pub fn format_f64_g(value: f64) -> String {
|
||||
if value.is_nan() {
|
||||
return "nan".to_string();
|
||||
}
|
||||
if value.is_infinite() {
|
||||
return if value > 0.0 { "inf".to_string() } else { "-inf".to_string() };
|
||||
}
|
||||
|
||||
// Use high precision then trim trailing zeros
|
||||
let s = format!("{:.15}", value);
|
||||
let s = s.trim_end_matches('0');
|
||||
s.trim_end_matches('.').to_string()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod datetime;
|
||||
pub mod format;
|
||||
|
||||
pub use datetime::{转化为时间戳, 转化为时间戳_数字};
|
||||
pub use format::format_f64_g;
|
||||
Reference in New Issue
Block a user