feat(time): 替换 chrono 为 time 库以优化时间处理

- 移除 chrono 依赖,引入 time 库并启用相关特性
- 重构时间工具模块,使用 OffsetDateTime 替代 DateTime
- 更新时间生成、转换和解析逻辑,提升代码一致性与可维护性
- 调整日期构造函数参数类型,增强类型安全
- 修正时间戳生成方式,确保东八区时间正确表示
This commit is contained in:
徐涛
2025-10-09 15:04:44 +08:00
parent cc05fc9a40
commit 21892e977f
3 changed files with 64 additions and 75 deletions

View File

@@ -1,22 +1,20 @@
use core::time;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, LazyLock, Mutex, OnceLock};
use chrono::NaiveDateTime;
use once_cell::sync::{Lazy, OnceCell};
use ::time::{macros::datetime, OffsetDateTime};
use thiserror::Error;
const HAIL_PERIOD_START: Lazy<i64> = Lazy::new(|| {
const HAIL_PERIOD_START: LazyLock<i64> = LazyLock::new(|| {
crate::time::date(2022, 2, 22)
.map(|d| d.and_hms_opt(22, 22, 22))
.flatten()
.map(|dt| crate::time::attach_asia_shanghai(dt))
.map(|dt| dt.timestamp())
.unwrap_or_else(|| NaiveDateTime::MIN.timestamp())
.map(|d| d.with_hms_nano(22, 22, 22, 222_222_222).unwrap())
.map(crate::time::attach_asia_shanghai)
.map(OffsetDateTime::unix_timestamp)
.unwrap_or_else(|| datetime!(1970-01-01 0:00 +8).unix_timestamp())
});
type TimestampValidator = fn(i64) -> bool;
type TimestampGenerator = fn() -> i64;
static INSTANCE: OnceCell<HailSerialCodeAlgorithm> = OnceCell::new();
static INSTANCE: OnceLock<HailSerialCodeAlgorithm> = OnceLock::new();
#[derive(Debug, Error)]
pub enum HailSerialCodeAlgorithmError {
@@ -65,7 +63,7 @@ impl HailSerialCodeAlgorithm {
/// 生成一个自计时起点以来的时间戳。
fn generate_timestamp(&self) -> i64 {
let current_time = crate::time::now_asia_shanghai().timestamp();
let current_time = crate::time::now_asia_shanghai().unix_timestamp();
current_time - *HAIL_PERIOD_START
}