pub struct Workdays {
pub conn: Connection,
}Expand description
Workday table access.
Fields§
§conn: ConnectionImplementations§
Source§impl Workdays
impl Workdays
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Opens the database and ensures the workdays table exists.
use kasl::db::workdays::Workdays;
let mut workdays = Workdays::new()?;Sourcepub fn insert_start(&mut self, date: NaiveDate) -> Result<()>
pub fn insert_start(&mut self, date: NaiveDate) -> Result<()>
Starts a workday at the current time; a no-op if the date already has one, so repeated calls from the monitor are safe.
use chrono::Local;
let mut workdays = Workdays::new()?;
let today = Local::now().date_naive();
workdays.insert_start(today)?;Sourcepub fn insert_end(&mut self, date: NaiveDate) -> Result<bool>
pub fn insert_end(&mut self, date: NaiveDate) -> Result<bool>
Stamps the current time as the day’s end; calling again re-stamps.
Returns whether a day was there to close. The UPDATE matches no row
when the date has no workday, and for a long time that came back as
plain Ok - so kasl end announced a day it had not written. The
answer belongs in the return value rather than in the data: ending an
unstarted day must still create nothing.
use chrono::Local;
let mut workdays = Workdays::new()?;
let today = Local::now().date_naive();
workdays.insert_start(today)?;
assert!(workdays.insert_end(today)?);Sourcepub fn fetch(&mut self, date: NaiveDate) -> Result<Option<Workday>>
pub fn fetch(&mut self, date: NaiveDate) -> Result<Option<Workday>>
Fetches the workday for a date, or None if the date has none.
use chrono::Local;
let mut workdays = Workdays::new()?;
let today = Local::now().date_naive();
if let Some(workday) = workdays.fetch(today)? {
println!("Work started at: {}", workday.start);
if let Some(end_time) = workday.end {
println!("Work ended at: {}", end_time);
} else {
println!("Work session is still active");
}
} else {
println!("No work session recorded for today");
}Sourcepub fn fetch_month(&mut self, date: NaiveDate) -> Result<Vec<Workday>>
pub fn fetch_month(&mut self, date: NaiveDate) -> Result<Vec<Workday>>
Fetches every workday in the calendar month containing date.
use chrono::Local;
let mut workdays = Workdays::new()?;
let current_month = Local::now().date_naive();
let monthly_workdays = workdays.fetch_month(current_month)?;
println!("Found {} workdays this month", monthly_workdays.len());
for workday in monthly_workdays {
if let Some(end_time) = workday.end {
let duration = end_time - workday.start;
println!("Date: {}, Duration: {:?}", workday.date, duration);
}
}Sourcepub fn recorded_dates(
&mut self,
from: Option<NaiveDate>,
to: Option<NaiveDate>,
) -> Result<Vec<NaiveDate>>
pub fn recorded_dates( &mut self, from: Option<NaiveDate>, to: Option<NaiveDate>, ) -> Result<Vec<NaiveDate>>
Sets the day’s start to a specific timestamp; errors if the date has no workday.
use chrono::{Local, NaiveDateTime};
let mut workdays = Workdays::new()?;
let today = Local::now().date_naive();
let corrected_start = NaiveDateTime::parse_from_str(
&format!("{} 09:00:00", today.format("%Y-%m-%d")),
"%Y-%m-%d %H:%M:%S"
)?;
workdays.update_start(today, corrected_start)?;The dates that actually have a workday, oldest first.
from and to are inclusive, and either may be left open: with both
open this is the whole history the database holds.
One query rather than a fetch per calendar day, because the caller
that wants this is backfill, and walking a year of calendar to find
two hundred workdays asks the database three hundred and sixty five
questions to which it already knows the whole answer.
let mut workdays = Workdays::new()?;
// Everything ever recorded on this machine.
let all = workdays.recorded_dates(None, None)?;
println!("{} days recorded", all.len());pub fn update_start( &mut self, date: NaiveDate, new_start: NaiveDateTime, ) -> Result<()>
Sourcepub fn update_end(
&mut self,
date: NaiveDate,
new_end: Option<NaiveDateTime>,
) -> Result<()>
pub fn update_end( &mut self, date: NaiveDate, new_end: Option<NaiveDateTime>, ) -> Result<()>
Sets the day’s end to a specific timestamp, or reopens the day with
None; errors if the date has no workday.
use chrono::{Local, NaiveDateTime};
let mut workdays = Workdays::new()?;
let today = Local::now().date_naive();
let end_time = NaiveDateTime::parse_from_str(
&format!("{} 17:30:00", today.format("%Y-%m-%d")),
"%Y-%m-%d %H:%M:%S"
)?;
workdays.update_end(today, Some(end_time))?;
// Reopen the day
workdays.update_end(today, None)?;