Skip to content

Commit

Permalink
Simplify (#1)
Browse files Browse the repository at this point in the history
* cargo update

* change integration test to unit test

* simplify: remove place

* remove Oxford comma

* simplify date formatting

* simplify

* simplify, remove lib

* simplify + test

* help fn for test

* just use a string

* unwrap

* test for parse

* 2024

* colon

* tests

* comment

* BIRTHDATE

* simplify test
  • Loading branch information
nathany authored Sep 19, 2024
1 parent 7b47fa0 commit 48ee0e7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 168 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Nathan Youngman
Copyright (c) 2024 Nathan Youngman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
128 changes: 0 additions & 128 deletions src/lib.rs

This file was deleted.

75 changes: 62 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,69 @@
/// Calculate my age in weeks
/// Inspired by Four Thousand Weeks by Oliver Burkeman.
use weeks::{format_local, now, parse_date_time, Case, Person, Pronoun};
// Calculate my age in weeks
// Inspired by Four Thousand Weeks by Oliver Burkeman.
use chrono::prelude::*;

fn main() {
let now = now();
const NAME: &str = "Nathan";
const PRONOUN: &str = "He";
// NOTE: -08:00 is PST. Daylight Saving Time started in B.C. on Sunday, April 24, 1977.
const BIRTHDATE: &str = "1977-04-05 11:58 -08:00";

// NOTE: -08 is PST. Daylight Saving Time started in B.C. on Sunday, April 24, 1977.
let birthdate = parse_date_time("1977-04-05 11:58 -08").unwrap();
let person = Person::new("Nathan", Pronoun::HeHim, birthdate, "British Columbia");
const PARSE_FORMAT: &str = "%Y-%m-%d %H:%M %:z";
const TIME_FORMAT: &str = "%A, %B %-d, %Y at %-I:%M %p (%:z)";

println!("The current time is {}.\n", format_local(now, "Alberta"));
fn main() {
let now = now();
let birthdate = parse_date_time(BIRTHDATE);
let (weeks, days, hours, minutes) = age(birthdate, now);

println!("{} was born on {}.", person.name, person.birth());
println!("The current time is {}.\n", now.format(TIME_FORMAT));
println!("{} was born on {}.", NAME, birthdate.format(TIME_FORMAT));
println!(
"{} has been alive for {}.",
person.pronoun.subjective(Case::Capitalize),
person.age(now)
"{} has been alive for {} weeks, {} days, {} hours and {} minutes.",
PRONOUN, weeks, days, hours, minutes
);
}

fn now() -> DateTime<FixedOffset> {
Local::now().fixed_offset()
}

fn parse_date_time(s: &str) -> DateTime<FixedOffset> {
DateTime::parse_from_str(s, PARSE_FORMAT).unwrap()
}

fn age(birthdate: DateTime<FixedOffset>, now: DateTime<FixedOffset>) -> (i64, i64, i64, i64) {
let local_birthdate = birthdate.with_timezone(&now.timezone());
let duration = now - local_birthdate;

let weeks = duration.num_weeks();
let days = duration.num_days() - (duration.num_weeks() * 7);
let hours = duration.num_hours() - (duration.num_days() * 24);
let minutes = duration.num_minutes() - (duration.num_hours() * 60);

(weeks, days, hours, minutes)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_date_time() {
let s = "2024-09-18 19:27 -06:00";
let dt = parse_date_time(s);
assert_eq!(dt.format(PARSE_FORMAT).to_string(), s);
}

#[test]
fn test_age() {
// -06:00 is MDT.
let now = parse_date_time("2024-09-18 19:27 -06:00");
let birthdate = parse_date_time("1977-04-05 11:58 -08:00");
let (weeks, days, hours, minutes) = age(birthdate, now);

assert_eq!(weeks, 2476);
assert_eq!(days, 1);
assert_eq!(hours, 5);
assert_eq!(minutes, 29);
}
}
20 changes: 0 additions & 20 deletions tests/weeks.rs

This file was deleted.

0 comments on commit 48ee0e7

Please sign in to comment.