Print dates in a nicer format

This commit is contained in:
Joscha 2021-12-06 13:54:58 +00:00
parent 531a140c45
commit 3e70b2e2c9
5 changed files with 67 additions and 5 deletions

View file

@ -1,4 +1,6 @@
use chrono::NaiveDate;
use std::fmt;
use chrono::{Duration, NaiveDate};
use crate::files::commands::DoneDate;
use crate::files::primitives::Time;
@ -16,6 +18,25 @@ pub struct Dates {
times: Option<Times>,
}
impl fmt::Display for Dates {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (start, end) = self.start_end();
match self.start_end_time() {
Some((start_time, end_time)) if start == end && start_time == end_time => {
write!(f, "{} {}", start, start_time)
}
Some((start_time, end_time)) if start == end => {
write!(f, "{} {} -- {}", start, start_time, end_time)
}
Some((start_time, end_time)) => {
write!(f, "{} {} -- {} {}", start, start_time, end, end_time)
}
None if start == end => write!(f, "{}", start),
None => write!(f, "{} -- {}", start, end),
}
}
}
impl Dates {
pub fn new(root: NaiveDate, other: NaiveDate) -> Self {
Self {
@ -92,6 +113,14 @@ impl Dates {
pub fn end_time(&self) -> Option<Time> {
self.start_end_time().map(|times| times.1)
}
pub fn move_by(&self, delta: Duration) -> Self {
Self {
root: self.root + delta,
other: self.other + delta,
times: self.times,
}
}
}
impl From<DoneDate> for Dates {

View file

@ -4,9 +4,10 @@ use chrono::NaiveDate;
use crate::files::primitives::{Span, Time};
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// A delta step resulted in an invalid date.
#[error("delta step resulted in invalid date")]
DeltaInvalidStep {
span: Span,
start: NaiveDate,
@ -15,6 +16,7 @@ pub enum Error {
prev_time: Option<Time>,
},
/// A time-based delta step was applied to a date without time.
#[error("time-based delta step applied to date without time")]
DeltaNoTime {
span: Span,
start: NaiveDate,
@ -23,6 +25,7 @@ pub enum Error {
/// A `DATE`'s repeat delta did not move the date forwards in time. Instead,
/// it either remained at the current date (`to == from`) or moved backwards
/// in time (`to < from`).
#[error("repeat delta did not move forwards")]
RepeatDidNotMoveForwards {
span: Span,
from: NaiveDate,
@ -30,12 +33,16 @@ pub enum Error {
},
/// A `MOVE a TO b` statement was executed, but there was no entry at the
/// date `a`.
#[error("tried to move nonexisting entry")]
MoveWithoutSource { span: Span },
/// A division by zero has occurred.
#[error("tried to divide by zero")]
DivByZero { span: Span, date: NaiveDate },
/// A modulo operation by zero has occurred.
#[error("tried to modulo by zero")]
ModByZero { span: Span, date: NaiveDate },
/// Easter calculation failed.
#[error("easter calculation failed")]
Easter {
span: Span,
date: NaiveDate,