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<()>
pub fn insert_end(&mut self, date: NaiveDate) -> Result<()>
Stamps the current time as the day’s end; calling again re-stamps.
Known gap: when no workday exists for the date, the UPDATE matches
nothing and this still returns Ok - kasl end then reports success
without having written anything (tracked for the doctor stage).
use chrono::Local;
let mut workdays = Workdays::new()?;
let today = Local::now().date_naive();
workdays.insert_start(today)?;
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 update_start(
&mut self,
date: NaiveDate,
new_start: NaiveDateTime,
) -> Result<()>
pub fn update_start( &mut self, date: NaiveDate, new_start: NaiveDateTime, ) -> Result<()>
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)?;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)?;