fix(stats): time now_local not working

This commit is contained in:
Conrad Ludgate 2024-01-01 16:42:36 +00:00 committed by GitHub
parent c7db7838e3
commit e2a4e9cf13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View File

@ -219,7 +219,13 @@ pub struct Settings {
// This is automatically loaded when settings is created. Do not set in
// config! Keep secrets and settings apart.
#[serde(skip)]
pub session_token: String,
// This is determined at startup and cached.
// This is due to non-threadsafe get-env limitations.
#[serde(skip)]
pub local_tz: Option<time::UtcOffset>,
}
impl Settings {
@ -488,6 +494,8 @@ impl Settings {
settings.session_token = String::from("not logged in");
}
settings.local_tz = time::UtcOffset::current_local_offset().ok();
Ok(settings)
}

View File

@ -83,31 +83,31 @@ impl Cmd {
self.period.join(" ")
};
let now = OffsetDateTime::now_utc();
let now = settings.local_tz.map_or(now, |local| now.to_offset(local));
let last_night = now.replace_time(Time::MIDNIGHT);
let history = if words.as_str() == "all" {
db.list(FilterMode::Global, &context, None, false, false)
.await?
} else if words.trim() == "today" {
let start = OffsetDateTime::now_local()?.replace_time(Time::MIDNIGHT);
let start = last_night;
let end = start + Duration::days(1);
db.range(start, end).await?
} else if words.trim() == "month" {
let end = OffsetDateTime::now_local()?.replace_time(Time::MIDNIGHT);
let end = last_night;
let start = end - Duration::days(31);
db.range(start, end).await?
} else if words.trim() == "week" {
let end = OffsetDateTime::now_local()?.replace_time(Time::MIDNIGHT);
let end = last_night;
let start = end - Duration::days(7);
db.range(start, end).await?
} else if words.trim() == "year" {
let end = OffsetDateTime::now_local()?.replace_time(Time::MIDNIGHT);
let end = last_night;
let start = end - Duration::days(365);
db.range(start, end).await?
} else {
let start = parse_date_string(
&words,
OffsetDateTime::now_local()?,
settings.dialect.into(),
)?;
let start = parse_date_string(&words, now, settings.dialect.into())?;
let end = start + Duration::days(1);
db.range(start, end).await?
};