-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
69 additions
and
168 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.