tina-core 0.0.2

Tina platform
Documentation
//! 请求扩展
use crate::tina::data::date_time::LocalDateTime;
use crate::tina::data::AppResult;
use crate::tina::server::application::{AppConfig, Application};
use futures_util::lock::Mutex;

/// 请求时间
pub struct RequestTime(Application, Mutex<Option<LocalDateTime>>);

impl RequestTime {
    /// 构建
    pub fn new(application: Application) -> Self {
        Self(application, Mutex::new(None))
    }
    /// 获取时间
    pub async fn get_date_time(&self) -> AppResult<LocalDateTime> {
        let mut lock = self.1.lock().await;
        match lock.as_ref() {
            None => {
                let service = self.0.get_date_time_service()?;
                let date_time = service.get_current_date_time().await?;
                (*lock) = Some(date_time);
                Ok(date_time)
            }
            Some(date_time) => Ok(*date_time),
        }
    }
}

impl Default for RequestTime {
    fn default() -> Self {
        Self(Application::from(AppConfig::new()), Mutex::new(Some(LocalDateTime::now())))
    }
}