Skip to main content

boat_lib/
utils.rs

1use anyhow::{Context, Result};
2use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
3use rusqlite::Connection;
4use std::path::Path;
5
6/// Parses naive datetime string to local timezone.
7/// Handles DST gaps/folds by picking earliest valid.
8pub fn parse_local_dt(s: &str) -> Result<DateTime<Local>> {
9    let naive = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S%.f")
10        .with_context(|| format!("failed to parse '{s}' as naive datetime"))?;
11
12    Local
13        .from_local_datetime(&naive)
14        .earliest()
15        .context("invalid local time (e.g., DST gap)")
16}
17
18pub fn init_database(db_path: impl AsRef<Path>) -> Result<Connection> {
19    let conn = Connection::open(db_path)?;
20    let schema_sql = include_str!("schema.sql");
21    conn.execute_batch(schema_sql)?;
22    Ok(conn)
23}
24
25#[cfg(test)]
26pub fn init_test_logger() {
27    let _ = env_logger::builder()
28        .is_test(true)
29        .filter_level(log::LevelFilter::Trace)
30        .try_init();
31}