Skip to content

Commit 48ee0e7

Browse files
authored
Simplify (#1)
* 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
1 parent 7b47fa0 commit 48ee0e7

File tree

5 files changed

+69
-168
lines changed

5 files changed

+69
-168
lines changed

Diff for: Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Nathan Youngman
3+
Copyright (c) 2024 Nathan Youngman
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Diff for: src/lib.rs

-128
This file was deleted.

Diff for: src/main.rs

+62-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,69 @@
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::*;
44

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";
79

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)";
1112

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);
1317

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));
1520
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
1923
);
2024
}
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+
}

Diff for: tests/weeks.rs

-20
This file was deleted.

0 commit comments

Comments
 (0)