fix: Chrono deprecation warnings

This commit is contained in:
Arne Beer 2022-11-21 15:51:13 +01:00
parent 816886d7d5
commit 7df4a38d65
No known key found for this signature in database
GPG key ID: CC9408F679023B65
4 changed files with 36 additions and 14 deletions

View file

@ -1,9 +1,27 @@
use std::collections::BTreeMap;
use chrono::Local;
use chrono::{DateTime, Local, LocalResult};
use pueue_lib::{settings::Settings, task::Task};
/// Try to get the start of the current date to the best of our abilities.
/// Throw an error, if we can't.
pub fn start_of_today() -> DateTime<Local> {
let result = Local::now()
.date_naive()
.and_hms_opt(0, 0, 0)
.expect("Failed to find start of today.")
.and_local_timezone(Local);
// Try to get the start of the current date.
// If there's no unambiguous result for today's midnight, we pick the first value as a backup.
match result {
LocalResult::None => panic!("Failed to find start of today."),
LocalResult::Single(today) => today,
LocalResult::Ambiguous(today, _) => today,
}
}
/// Sort given tasks by their groups.
/// This is needed to print a table for each group.
pub fn sort_tasks_by_group(tasks: Vec<Task>) -> BTreeMap<String, Vec<Task>> {
@ -36,7 +54,7 @@ pub fn formatted_start_end(task: &Task, settings: &Settings) -> (String, String)
// If the task started today, just show the time.
// Otherwise show the full date and time.
let started_today = start >= Local::today().and_hms(0, 0, 0);
let started_today = start >= start_of_today();
let formatted_start = if started_today {
start
.format(&settings.client.status_time_format)
@ -55,7 +73,7 @@ pub fn formatted_start_end(task: &Task, settings: &Settings) -> (String, String)
// If the task ended today we only show the time.
// In all other circumstances, we show the full date.
let finished_today = end >= Local::today().and_hms(0, 0, 0);
let finished_today = end >= start_of_today();
let formatted_end = if finished_today {
end.format(&settings.client.status_time_format).to_string()
} else {

View file

@ -1,11 +1,11 @@
use chrono::{Duration, Local};
use chrono::Duration;
use comfy_table::presets::UTF8_HORIZONTAL_ONLY;
use comfy_table::*;
use pueue_lib::settings::Settings;
use pueue_lib::task::{Task, TaskResult, TaskStatus};
use super::helper::formatted_start_end;
use super::helper::{formatted_start_end, start_of_today};
use super::OutputStyle;
use crate::query::Rule;
@ -218,8 +218,7 @@ impl<'a> TableBuilder<'a> {
} = task.status
{
// Only show the date if the task is not supposed to be enqueued today.
let enqueue_today =
enqueue_at <= Local::today().and_hms(0, 0, 0) + Duration::days(1);
let enqueue_today = enqueue_at <= start_of_today() + Duration::days(1);
let formatted_enqueue_at = if enqueue_today {
enqueue_at.format(&self.settings.client.status_time_format)
} else {

View file

@ -83,8 +83,9 @@ pub fn datetime(section: Pair<'_, Rule>, query_result: &mut QueryResult) -> Resu
Rule::time => {
let time = NaiveTime::parse_from_str(operand.as_str(), "%X")
.context("Expected hh:mm:ss time format")?;
let date = Local::today();
DateOrDateTime::DateTime(date.and_time(time).unwrap())
let today = Local::now().date_naive();
let datetime = today.and_time(time).and_local_timezone(Local).unwrap();
DateOrDateTime::DateTime(datetime)
}
Rule::datetime => {
let datetime = NaiveDateTime::parse_from_str(operand.as_str(), "%F %X")
@ -145,7 +146,10 @@ pub fn datetime(section: Pair<'_, Rule>, query_result: &mut QueryResult) -> Resu
DateOrDateTime::Date(date) => {
// Get the start of the given day.
// Use the most inclusive datetime in case of ambiguity
let start_of_day = date.and_hms(0, 0, 0).and_local_timezone(Local);
let start_of_day = date
.and_hms_opt(0, 0, 0)
.expect("Couldn't determine start of day for given date.")
.and_local_timezone(Local);
let start_of_day = match start_of_day.latest() {
None => return false,
Some(datetime) => datetime,
@ -154,7 +158,8 @@ pub fn datetime(section: Pair<'_, Rule>, query_result: &mut QueryResult) -> Resu
// Get the end of the given day.
// Use the most inclusive datetime in case of ambiguity
let end_of_day = (date + Duration::days(1))
.and_hms(0, 0, 0)
.and_hms_opt(0, 0, 0)
.expect("Couldn't determine start of day for given date.")
.and_local_timezone(Local);
let end_of_day = match end_of_day.latest() {
None => return false,

View file

@ -76,7 +76,7 @@ pub async fn get_task_context(settings: &Settings) -> Result<HashMap<String, Str
if let Some(start) = task.start {
// Use datetime format for datetimes that aren't today.
let format = if start.date() == Local::today() {
let format = if start.date_naive() == Local::now().date_naive() {
&settings.client.status_time_format
} else {
&settings.client.status_datetime_format
@ -88,7 +88,7 @@ pub async fn get_task_context(settings: &Settings) -> Result<HashMap<String, Str
}
if let Some(end) = task.end {
// Use datetime format for datetimes that aren't today.
let format = if end.date() == Local::today() {
let format = if end.date_naive() == Local::now().date_naive() {
&settings.client.status_time_format
} else {
&settings.client.status_datetime_format
@ -107,7 +107,7 @@ pub async fn get_task_context(settings: &Settings) -> Result<HashMap<String, Str
} = task.status
{
// Use datetime format for datetimes that aren't today.
let format = if enqueue_at.date() == Local::today() {
let format = if enqueue_at.date_naive() == Local::now().date_naive() {
&settings.client.status_time_format
} else {
&settings.client.status_datetime_format