/* This file is part of DarkFi (https://dark.fi)
*
* Copyright (C) 2020-2022 Dyne.org foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use std::{
env,
fs::{self, File},
io::Write,
process::Command,
};
use chrono::{Datelike, Local, NaiveDate};
use log::error;
use darkfi::{util::time::Timestamp, Result};
/// Parse due date (e.g. "1503" for 15 March) as i64 timestamp.
pub fn due_as_timestamp(due: &str) -> Option {
if due.len() != 4 || due.parse::().is_err() {
error!("Due date must be digits of length 4 (e.g. \"1503\" for 15 March)");
return None
}
let (day, month) = (due[..2].parse::().unwrap(), due[2..].parse::().unwrap());
if day > 31 || month > 12 {
error!("Invalid or out-of-range date");
return None
}
let mut year = Local::today().year();
// Ensure the due date is in future
if month < Local::today().month() {
year += 1;
}
if month == Local::today().month() && day < Local::today().day() {
year += 1;
}
let dt = NaiveDate::from_ymd(year, month, day).and_hms(12, 0, 0);
Some(dt.timestamp())
}
/// Start up the preferred editor to edit a task's description.
pub fn desc_in_editor() -> Result