Added timezone support
Build and Publish / Build and Publish Docker Image (push) Successful in 1m9s

This commit is contained in:
Ultradesu
2026-05-11 13:46:40 +01:00
parent 357a2ed423
commit 87fb8c744d
2 changed files with 32 additions and 1 deletions
Generated
+1 -1
View File
@@ -3255,7 +3255,7 @@ dependencies = [
[[package]] [[package]]
name = "web-petting" name = "web-petting"
version = "0.1.3" version = "0.1.4"
dependencies = [ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
+31
View File
@@ -0,0 +1,31 @@
use chrono::TimeZone;
use chrono_tz::Tz;
use cot::db::{Database, Model, query};
use crate::models::Setting;
const DEFAULT_TZ: &str = "UTC";
/// Load timezone from the database settings.
pub async fn load_tz(db: &Database) -> Tz {
let key = "timezone".to_string();
let tz_str = query!(Setting, $key == key)
.get(db)
.await
.ok()
.flatten()
.map(|s| s.value)
.unwrap_or_else(|| DEFAULT_TZ.to_string());
tz_str.parse::<Tz>().unwrap_or(Tz::UTC)
}
/// Current date+time in the configured timezone, returned as NaiveDateTime.
#[allow(dead_code)]
pub fn now_in_tz(tz: Tz) -> chrono::NaiveDateTime {
chrono::Utc::now().with_timezone(&tz).naive_local()
}
/// Today's date in the configured timezone.
pub fn today_in_tz(tz: Tz) -> chrono::NaiveDate {
chrono::Utc::now().with_timezone(&tz).date_naive()
}