Skip to content

Commit

Permalink
feat: Input date as DD MM YYYY numbers to avoid cultural format diffe…
Browse files Browse the repository at this point in the history
…rences
  • Loading branch information
ducdetronquito committed Jan 14, 2024
1 parent 7654ff5 commit 4858b2e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ of your OS, but you can also override this behavior by defining a custom path vi

```shell
# Add someone's birthday
$ birthday add "Ben Dover" 03/05/1990
$ birthday add "Hugh Jarse" 10/12/1995
$ birthday add "Anita Bath" 22/09/1987
$ birthday add "Ben Dover" 03 05 1990
$ birthday add "Hugh Jarse" 10 12 1995
$ birthday add "Anita Bath" 22 09 1987

# Show all birthdays
$ birthday all
Expand Down
3 changes: 1 addition & 2 deletions src/birthday_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use rusqlite::Connection;

use crate::Birthday;

pub fn add(name: String, date: String) -> Result<()> {
pub fn add(name: String, date: NaiveDate) -> Result<()> {
let db = get_db()?;
let date = NaiveDate::parse_from_str(&date, "%Y-%m-%d")?;
let timestamp = to_timestamp(date);
db.execute(
"INSERT INTO birthdays(name, date_timestamp) VALUES(?1, ?2)",
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use anyhow::{bail, Result};
pub use birthday::Birthday;
use chrono::{Datelike, NaiveDate};

pub fn add(name: String, date: String) -> Result<()> {
birthday_store::add(name, date)
pub fn add(name: String, day: u32, month: u32, year: i32) -> Result<()> {
let birthdate = NaiveDate::from_ymd_opt(year, month, day).expect("Invalid date");
birthday_store::add(name, birthdate)
}

pub fn get_all() -> Result<Vec<Birthday>> {
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ struct Cli {
#[derive(Subcommand, Debug)]
enum Command {
#[command(about = "Add a person's birthday")]
Add { name: String, date: String },
Add {
name: String,
day: u32,
month: u32,
#[arg(allow_negative_numbers = true)]
year: i32,
},
#[command(about = "Show all birthdays")]
All {},
#[command(about = "Show the next birthday")]
Expand Down Expand Up @@ -42,7 +48,12 @@ fn main() -> Result<()> {
let cli = Cli::parse();
let today = Utc::now().date_naive();
match cli.command {
Command::Add { name, date } => birthday::add(name, date),
Command::Add {
name,
day,
month,
year,
} => birthday::add(name, day, month, year),
Command::All {} => {
let birthdays = birthday::get_all()?;
output::output(birthdays, today);
Expand Down

0 comments on commit 4858b2e

Please sign in to comment.