Fix: Let the track command parse the start/end time more intelligently

It understands "now" from this point and can parse dates or date-times
(whereas dates get normalized to date + hour 0, minute 0, second 0).
This commit is contained in:
Matthias Beyer 2017-09-14 20:47:49 +02:00
parent 53c7d602eb
commit e049559bed
2 changed files with 30 additions and 13 deletions

View file

@ -17,7 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::str::FromStr; use std::process::exit;
use clap::ArgMatches; use clap::ArgMatches;
use chrono::naive::NaiveDateTime; use chrono::naive::NaiveDateTime;
@ -28,6 +28,9 @@ use libimagtimetrack::tag::TimeTrackingTag;
use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetrackingstore::TimeTrackStore;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
const DATE_TIME_PARSE_FMT : &'static str = "%Y-%m-%dT%H:%M:%S";
const DATE_PARSE_FMT : &'static str = "%Y-%m-%d";
pub fn track(rt: &Runtime) -> i32 { pub fn track(rt: &Runtime) -> i32 {
let (_, cmd) = rt.cli().subcommand(); let (_, cmd) = rt.cli().subcommand();
let cmd = cmd.unwrap(); // checked in main() let cmd = cmd.unwrap(); // checked in main()
@ -35,18 +38,30 @@ pub fn track(rt: &Runtime) -> i32 {
// Gets the appropriate time from the commandline or None on error (errors already logged, so // Gets the appropriate time from the commandline or None on error (errors already logged, so
// callee can directly return in case of error // callee can directly return in case of error
fn get_time(cmd: &ArgMatches, clap_name: &str, errname: &str) -> Option<NaiveDateTime> { fn get_time(cmd: &ArgMatches, clap_name: &str, errname: &str) -> Option<NaiveDateTime> {
let val = cmd match cmd.value_of(clap_name) {
.value_of(clap_name) Some("now") => Some(::chrono::offset::Local::now().naive_local()),
.map(::chrono::naive::NaiveDateTime::from_str) Some(els) => {
.unwrap(); // clap has our back match ::chrono::naive::NaiveDateTime::parse_from_str(els, DATE_TIME_PARSE_FMT) {
match val {
Ok(ndt) => Some(ndt), Ok(ndt) => Some(ndt),
Err(e) => { Err(e_ndt) => {
trace_error(&e); match ::chrono::naive::NaiveDate::parse_from_str(els, DATE_PARSE_FMT) {
error!("Cannot continue, not having {} time", errname); Ok(ndt) => Some(ndt.and_hms(0, 0, 0)),
None Err(e_nd) => {
}, error!("Cannot parse date {}:", errname);
trace_error(&e_nd);
error!("Cannot parse date-time {}:", errname);
trace_error(&e_ndt);
exit(1)
}
}
}
}
}
None => {
error!("Not specified in commandline: {}", clap_name);
exit(1)
}
} }
} }
@ -55,7 +70,7 @@ pub fn track(rt: &Runtime) -> i32 {
None => return 1, None => return 1,
}; };
let stop = match get_time(&cmd, "stop-time", "stop") { let stop = match get_time(&cmd, "end-time", "stop") {
Some(t) => t, Some(t) => t,
None => return 1, None => return 1,
}; };

View file

@ -53,6 +53,8 @@ This section contains the changelog from the last release to the next release.
* We compile with rustc 1.17, 1.18, .., nightly * We compile with rustc 1.17, 1.18, .., nightly
* The `imag-store` binary now uses positional arguments in its CLI * The `imag-store` binary now uses positional arguments in its CLI
* The "toml-query" dependency was updated to 0.3.1 * The "toml-query" dependency was updated to 0.3.1
* `imag-timetrack track` is now able to parse "now", date-only start/stop
dates and date-time start/stop times.
* Stats * Stats
## 0.3.0 ## 0.3.0