Skip to content

Commit

Permalink
feat!: port chrono to jiff
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Aug 7, 2024
1 parent 896d8bd commit 567a0b0
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 152 deletions.
249 changes: 143 additions & 106 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"

# config
jiff = "0.1.4"
num-traits = "0.2.19"
ron = "0.8"
serde = { version = "1", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Config(
moments: [
// My birthday
(
start_date: "1980-Oct-15",
// Currently, only `start_date` and `duration` are available
start_date: "1980-10-30",
// Currently, only `start_date` and `duration` are available
format: "👶 {{ 'Aragorn II Elessar age (Me)' | cyan | bold }}\nBorn at: {{ start_date | red }}\nAge: {{ duration }}\n",
),
],
Expand Down
2 changes: 1 addition & 1 deletion examples/achievements.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Config(
moments: [
(
start_date: "1980-Oct-15",
start_date: "1980-10-15",
format: "{{ 'My Achievements' | yellow | bold }}\n\n{{ 'Aragorn II Elessar' | cyan | bold }}\n\nBirthdate: {{ start_date | blue }}\n\nAge: {{ duration }}\n\n- Successfully completed another year of life\n- Embarked on a fulfilling career at {{ 'Rohan Equestrian Supplies' | underline }}\n- Celebrated the joyous occasion of marriage with Arwen Undómiel\n- Faced and overcame numerous challenges\n- Gained wisdom through life's experiences\n\nI am grateful for the opportunities and support that have shaped my journey. Looking forward to embracing new adventures!\n",
),
],
Expand Down
10 changes: 5 additions & 5 deletions examples/formal.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ Config(
moments: [
// My birthday
(
start_date: "1980-Oct-15",
start_date: "1980-10-15",
format: "👶 {{ 'Aragorn II Elessar age (Me)' | cyan | bold }}\nBorn at: {{ start_date | red }}\nAge: {{ duration }}\n\n",
),
// Arwen Undómiel's birthday
(
start_date: "1992-Mar-27",
start_date: "1992-03-27",
format: "👶 {{ 'Arwen Undómiel age' | cyan | bold }}\nBorn at: {{ start_date | red }}\nAge: {{ duration }}\n\n",
),
// ldarion Telcontar's birthday
(
start_date: "2018-Jun-10",
start_date: "2018-07-10",
format: "👶 {{ 'ldarion Telcontar age' | cyan | bold }}\nBorn at: {{ start_date | red }}\nAge: {{ duration }}\n\n",
),
// Marriage
(
start_date: "2015-Apr-08",
start_date: "2015-04-08",
format: "💍 {{ 'Marriage' | cyan | bold }}\nYou got married on {{ start_date | red }}\nIt has been {{ duration }}\n\n",
),
// Work
(
start_date: "2015-Apr-08",
start_date: "2015-04-08",
format: "🛠 {{ 'Work' | cyan | bold }}\nYou began your career at {{ 'Rohan Equestrian Supplies' | underline }} on {{ start_date | red }}\nIt has been {{ duration }}\n",
),
],
Expand Down
6 changes: 3 additions & 3 deletions examples/journal.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Config(
moments: [
(
start_date: "2023-Jun-01",
start_date: "2023-07-01",
format: "{{ 'Journal Entry' | green | bold }}\n\nDate: {{ start_date | red }}\n\nDear Journal,\n\nToday marks another milestone in my journey. It has been {{ duration }} since I last wrote.\n\nI wanted to capture the memories of my recent experiences. On this day, I celebrated my birthday and took a moment to reflect on the passing years.",
),
(
start_date: "2021-May-01",
start_date: "2021-03-01",
format: "In the realm of work, I have been dedicated to my career at {{ 'Rohan Equestrian Supplies' | underline }} staring from {{ start_date }}. The challenges and accomplishments have shaped me into a stronger individual."
),
(
start_date: "2010-Feb-20",
start_date: "2010-02-20",
format: "Furthermore, love has been a guiding force in my life. On {{ start_date | red }}, I exchanged vows with my beloved, Arwen Undómiel. Together, we continue to build a life filled with love, support, and shared dreams.\n\nAs I write these words, I am filled with gratitude for the adventures and lessons life has bestowed upon me. Each step has led me closer to self-discovery and personal growth.\n\nUntil the next entry,\n{{ 'Aragorn II Elessar' | cyan | bold }}\n",
),
],
Expand Down
6 changes: 3 additions & 3 deletions examples/letter.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Config(
moments: [
(
start_date: "2021-Jun-23",
start_date: "2021-07-23",
format: "Dear {{ 'Friend' | cyan | bold }},\n\nI hope this letter finds you well. It has been {{ duration }} since we last met.\n\nI wanted to share some news with you. On {{ start_date | red }}, I celebrated my birthday, and it reminded me of our shared memories.\n"
),
(
start_date: "2022-Jun-23",
start_date: "2022-07-23",
format: "In recent years, I have been working diligently at {{ 'Rohan Equestrian Supplies' | underline }} since {{ start_date }}. It has been an exciting journey, and I have learned so much.\n"
),
(
start_date: "2023-Jan-23",
start_date: "2023-01-23",
format: "On {{ start_date | red }}, I embarked on a new chapter in my life by marrying my beloved, Arwen Undómiel. Our love continues to grow stronger every day.\n\nI cherish our friendship and the adventures we have had together. Let's plan a reunion soon and create more memories.\n\nWishing you all the best,\n{{ 'Aragorn II Elessar' | cyan | bold }}\n",
),
],
Expand Down
6 changes: 3 additions & 3 deletions examples/story.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ Config(
moments: [
// birthday
(
start_date: "1980-Oct-15",
start_date: "1980-10-15",
format: "{{ 'My Story' | brightMagenta | bold }}\n\nOnce upon a time. In the kingdom of {{ 'Gondor' | underline }}. {{ 'Aragorn II Elessar' | cyan | bold }} the brave warrior, was born on {{ start_date | red }}.\n",
),
// work
(
start_date: "2015-Apr-08",
start_date: "2015-04-08",
format: "Years passed, and on {{ start_date | green }} our hero began his career at {{ 'Rohan Equestrian Supplies' | underline }}.\n"
),
// marriage
(
start_date: "2018-Jan-01",
start_date: "2018-01-01",
format: "Love was in the air, and on {{ start_date | blue }} Aragorn II Elessar and Arwen Undómiel tied the knot.\nTheir love story continues. Onward to new adventures!\n",
),
],
Expand Down
10 changes: 2 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,8 @@ impl std::convert::From<std::env::VarError> for Error {
}
}

impl std::convert::From<chrono::ParseError> for Error {
fn from(err: chrono::ParseError) -> Self {
Error::Internal(err.to_string())
}
}

impl std::convert::From<chrono::OutOfRangeError> for Error {
fn from(err: chrono::OutOfRangeError) -> Self {
impl std::convert::From<jiff::Error> for Error {
fn from(err: jiff::Error) -> Self {
Error::Internal(err.to_string())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {

fn test_case(format: &str) -> Moment {
Moment {
start_date: "1987-Dec-19".to_string(),
start_date: "1987-12-19".to_string(),
format: format.to_string(),
}
}
Expand Down
34 changes: 14 additions & 20 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{Local, NaiveDate};
use jiff::civil;

pub struct Time {
date: NaiveDate,
date: civil::Date,
}

impl Time {
Expand All @@ -17,32 +17,26 @@ impl Time {

/// Calculate elapsed time
pub fn duration(&self) -> Result<humantime::FormattedDuration, crate::Error> {
let today = today()?;
let duration = today.signed_duration_since(self.date);
let today = jiff::Zoned::now().date();
let duration = today.since(self.date)?;
Self::format_duration(duration)
}

/// Parse string time into `NaiveDate` struct
fn parse(date: &str) -> Result<NaiveDate, crate::Error> {
let format = "%Y-%b-%d";
Ok(NaiveDate::parse_from_str(date, format)?)
/// Parse string time into `plain Date`
fn parse(date: &str) -> Result<civil::Date, crate::Error> {
let date: civil::Date = date.parse()?;
Ok(date)
}

/// Turn duration into human readable format
fn format_duration(
duration: chrono::Duration,
) -> Result<humantime::FormattedDuration, crate::Error> {
Ok(humantime::format_duration(duration.to_std()?))
fn format_duration(duration: jiff::Span) -> Result<humantime::FormattedDuration, crate::Error> {
let duration = humantime::parse_duration(&format!("{}hours", duration.get_hours()))?;
let duration = humantime::format_duration(duration);
Ok(duration)
}

/// Formats the given date into a custom string format.
fn format_date(date: NaiveDate) -> Result<String, crate::Error> {
let format = "%a, %d %b %Y";
Ok(date.format(format).to_string())
fn format_date(date: civil::Date) -> Result<String, crate::Error> {
Ok(date.strftime("%a, %d %b %Y").to_string())
}
}

/// Today's date
fn today() -> Result<NaiveDate, crate::Error> {
Ok(Local::now().date_naive())
}

0 comments on commit 567a0b0

Please sign in to comment.