|
1 |
| -/// Calculate my age in weeks |
2 |
| -/// Inspired by Four Thousand Weeks by Oliver Burkeman. |
3 |
| -use weeks::{format_local, now, parse_date_time, Case, Person, Pronoun}; |
| 1 | +// Calculate my age in weeks |
| 2 | +// Inspired by Four Thousand Weeks by Oliver Burkeman. |
| 3 | +use chrono::prelude::*; |
4 | 4 |
|
5 |
| -fn main() { |
6 |
| - let now = now(); |
| 5 | +const NAME: &str = "Nathan"; |
| 6 | +const PRONOUN: &str = "He"; |
| 7 | +// NOTE: -08:00 is PST. Daylight Saving Time started in B.C. on Sunday, April 24, 1977. |
| 8 | +const BIRTHDATE: &str = "1977-04-05 11:58 -08:00"; |
7 | 9 |
|
8 |
| - // NOTE: -08 is PST. Daylight Saving Time started in B.C. on Sunday, April 24, 1977. |
9 |
| - let birthdate = parse_date_time("1977-04-05 11:58 -08").unwrap(); |
10 |
| - let person = Person::new("Nathan", Pronoun::HeHim, birthdate, "British Columbia"); |
| 10 | +const PARSE_FORMAT: &str = "%Y-%m-%d %H:%M %:z"; |
| 11 | +const TIME_FORMAT: &str = "%A, %B %-d, %Y at %-I:%M %p (%:z)"; |
11 | 12 |
|
12 |
| - println!("The current time is {}.\n", format_local(now, "Alberta")); |
| 13 | +fn main() { |
| 14 | + let now = now(); |
| 15 | + let birthdate = parse_date_time(BIRTHDATE); |
| 16 | + let (weeks, days, hours, minutes) = age(birthdate, now); |
13 | 17 |
|
14 |
| - println!("{} was born on {}.", person.name, person.birth()); |
| 18 | + println!("The current time is {}.\n", now.format(TIME_FORMAT)); |
| 19 | + println!("{} was born on {}.", NAME, birthdate.format(TIME_FORMAT)); |
15 | 20 | println!(
|
16 |
| - "{} has been alive for {}.", |
17 |
| - person.pronoun.subjective(Case::Capitalize), |
18 |
| - person.age(now) |
| 21 | + "{} has been alive for {} weeks, {} days, {} hours and {} minutes.", |
| 22 | + PRONOUN, weeks, days, hours, minutes |
19 | 23 | );
|
20 | 24 | }
|
| 25 | + |
| 26 | +fn now() -> DateTime<FixedOffset> { |
| 27 | + Local::now().fixed_offset() |
| 28 | +} |
| 29 | + |
| 30 | +fn parse_date_time(s: &str) -> DateTime<FixedOffset> { |
| 31 | + DateTime::parse_from_str(s, PARSE_FORMAT).unwrap() |
| 32 | +} |
| 33 | + |
| 34 | +fn age(birthdate: DateTime<FixedOffset>, now: DateTime<FixedOffset>) -> (i64, i64, i64, i64) { |
| 35 | + let local_birthdate = birthdate.with_timezone(&now.timezone()); |
| 36 | + let duration = now - local_birthdate; |
| 37 | + |
| 38 | + let weeks = duration.num_weeks(); |
| 39 | + let days = duration.num_days() - (duration.num_weeks() * 7); |
| 40 | + let hours = duration.num_hours() - (duration.num_days() * 24); |
| 41 | + let minutes = duration.num_minutes() - (duration.num_hours() * 60); |
| 42 | + |
| 43 | + (weeks, days, hours, minutes) |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use super::*; |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn test_parse_date_time() { |
| 52 | + let s = "2024-09-18 19:27 -06:00"; |
| 53 | + let dt = parse_date_time(s); |
| 54 | + assert_eq!(dt.format(PARSE_FORMAT).to_string(), s); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_age() { |
| 59 | + // -06:00 is MDT. |
| 60 | + let now = parse_date_time("2024-09-18 19:27 -06:00"); |
| 61 | + let birthdate = parse_date_time("1977-04-05 11:58 -08:00"); |
| 62 | + let (weeks, days, hours, minutes) = age(birthdate, now); |
| 63 | + |
| 64 | + assert_eq!(weeks, 2476); |
| 65 | + assert_eq!(days, 1); |
| 66 | + assert_eq!(hours, 5); |
| 67 | + assert_eq!(minutes, 29); |
| 68 | + } |
| 69 | +} |
0 commit comments