tiempo-rs/src/old.rs

93 lines
3.2 KiB
Rust
Raw Normal View History

use std::io::Write;
use chrono::{DateTime, Utc, Local, LocalResult, TimeZone};
2021-08-25 14:30:27 -05:00
use ansi_term::{Color::Yellow, Style};
use crate::error::{Error::*, Result};
use crate::models::Entry;
use crate::database::{Database, DBVersion};
2021-08-25 14:30:27 -05:00
use crate::env::Env;
2021-07-17 22:04:54 -05:00
/// Treat t as if it wasnt actually in Utc but in the local timezone and return
/// the actual Utc time.
///
/// Used to convert times from the old database version.
fn local_to_utc(t: DateTime<Utc>) -> Result<DateTime<Utc>> {
let local_time = match Local.from_local_datetime(&t.naive_utc()) {
LocalResult::None => return Err(NoneLocalTime(t.naive_utc().to_string())),
LocalResult::Single(t) => t,
LocalResult::Ambiguous(t1, t2) => return Err(AmbiguousLocalTime {
orig: t.naive_utc().to_string(),
t1: t1.naive_local(),
t2: t2.naive_local(),
}),
};
Ok(Utc.from_utc_datetime(&local_time.naive_utc()))
}
2021-07-17 22:04:54 -05:00
/// takes an otherwise perfectly good timestamp in Utc and turns it into the
/// local timezone, but using the same DateTime<Utc> type.
///
/// Used to insert times into the old database format.
fn utc_to_local(t: DateTime<Utc>) -> DateTime<Utc> {
Utc.from_utc_datetime(&t.with_timezone(&Local).naive_local())
2021-07-17 22:04:54 -05:00
}
/// Maps an entire vector of entries from the old database format to the new,
/// converting their timestamps from the local timezone to Utc.
fn local_to_utc_vec(entries: Vec<Entry>) -> Result<Vec<Entry>> {
entries
.into_iter()
.map(|e| {
Ok(Entry {
start: local_to_utc(e.start)?,
2021-08-02 19:10:34 -05:00
end: e.end.map(local_to_utc).transpose()?,
..e
})
})
.collect()
}
2021-07-17 22:04:54 -05:00
/// the logic used by many subcommands that transform entries from the old
/// format to the new. Used in conjunction with warn_if_needed.
pub fn entries_or_warning<D: Database>(entries: Vec<Entry>, db: &D) -> Result<(Vec<Entry>, bool)> {
if let DBVersion::Timetrap = db.version()? {
// this indicates that times in the database are specified in the
// local time and need to be converted to utc before displaying
Ok((local_to_utc_vec(entries)?, true))
} else {
Ok((entries, false))
}
}
/// Wrapper around utc_to_local that also returns a flag in case a warning is
/// needed
2021-07-20 10:08:04 -05:00
pub fn time_or_warning<D: Database>(time: DateTime<Utc>, db: &D) -> Result<(DateTime<Utc>, bool)> {
2021-07-17 22:04:54 -05:00
if let DBVersion::Timetrap = db.version()? {
Ok((utc_to_local(time), true))
} else {
Ok((time, false))
}
}
/// emits the appropiate warning if the old database format was detected.
2021-08-25 14:30:27 -05:00
pub fn warn_if_needed<E: Write>(err: &mut E, needs_warning: bool, env: &Env) -> Result<()> {
if needs_warning && !env.supress_warming {
2021-08-02 19:10:34 -05:00
writeln!(
err,
"{} You are using the old timetrap format, it is advised that \
2022-08-26 11:33:56 -05:00
you update your database using t migrate. To supress this warning \
set TIEMPO_SUPRESS_TIMETRAP_WARNING=1",
2021-08-25 14:30:27 -05:00
if env.stderr_is_tty {
Yellow.bold().paint("[WARNING]")
} else {
Style::new().paint("[WARNING]")
},
2021-08-02 19:10:34 -05:00
).map_err(IOError)?;
}
Ok(())
}