Add HabitTemplate::next_instance_date_after()

Rewrite HabitTemplate::next_instance_date() to use the new function
This commit is contained in:
Matthias Beyer 2017-12-19 19:09:15 +01:00
parent 6f244a52ff
commit ddf703eaa0

View file

@ -63,6 +63,9 @@ pub trait HabitTemplate : Sized {
/// Get instances for this template /// Get instances for this template
fn linked_instances(&self) -> Result<HabitInstanceStoreIdIterator>; fn linked_instances(&self) -> Result<HabitInstanceStoreIdIterator>;
/// Get the date of the next date when the habit should be done
fn next_instance_date_after(&self, base: &NaiveDateTime) -> Result<NaiveDate>;
/// Get the date of the next date when the habit should be done /// Get the date of the next date when the habit should be done
fn next_instance_date(&self) -> Result<NaiveDate>; fn next_instance_date(&self) -> Result<NaiveDate>;
@ -113,8 +116,7 @@ impl HabitTemplate for Entry {
Ok(HabitInstanceStoreIdIterator::new(sidi)) Ok(HabitInstanceStoreIdIterator::new(sidi))
} }
/// Get the date of the next date when the habit should be done fn next_instance_date_after(&self, base: &NaiveDateTime) -> Result<NaiveDate> {
fn next_instance_date(&self) -> Result<NaiveDate> {
use kairos::timetype::TimeType; use kairos::timetype::TimeType;
use kairos::parser::parse; use kairos::parser::parse;
use kairos::parser::Parsed; use kairos::parser::Parsed;
@ -129,22 +131,21 @@ impl HabitTemplate for Entry {
} }
}; };
let today = TimeType::today(); debug!("Base is {:?}", base);
let today = today.get_moment().unwrap(); // we know this is safe.
debug!("Today is {:?}", today);
let basedate = date_from_s(self.habit_basedate()?)?; let basedate = date_from_s(self.habit_basedate()?)?;
debug!("Basedate is {:?}", today); debug!("Basedate is {:?}", basedate);
let increment = date_from_s(self.habit_recur_spec()?)?; let increment = date_from_s(self.habit_recur_spec()?)?;
debug!("Increment is {:?}", today); debug!("Increment is {:?}", increment);
for element in basedate.every(increment)? { for element in basedate.every(increment)? {
debug!("Calculating: {:?}", element); debug!("Calculating: {:?}", element);
let element = element?.calculate()?; let element = element?.calculate()?;
debug!(" = {:?}", element);
if let Some(ndt) = element.get_moment() { if let Some(ndt) = element.get_moment() {
if ndt > today { if ndt >= base {
debug!("-> {:?} > {:?}", ndt, today); debug!("-> {:?} >= {:?}", ndt, base);
return Ok(ndt.date()) return Ok(ndt.date())
} }
} else { } else {
@ -155,6 +156,17 @@ impl HabitTemplate for Entry {
unreachable!() // until we have habit-end-date support unreachable!() // until we have habit-end-date support
} }
/// Get the date of the next date when the habit should be done
fn next_instance_date(&self) -> Result<NaiveDate> {
use kairos::timetype::TimeType;
let today = TimeType::today();
let today = today.get_moment().unwrap(); // we know this is safe.
debug!("Today is {:?}", today);
self.next_instance_date_after(&today.date().and_hms(0, 0, 0))
}
/// Check whether the instance is a habit by checking its headers for the habit data /// Check whether the instance is a habit by checking its headers for the habit data
fn is_habit_template(&self) -> Result<bool> { fn is_habit_template(&self) -> Result<bool> {
[ [