Skip to content

Conversation

@Rottschaefer
Copy link

I added a simple example for calculating an user's age using the crate in the arithmetic section. Tried to avoid complexity as much as I could. If you have a suggestion we definitely can improve on it :)

@@ -1 +1,106 @@
# Arithmetic

Let's suppose you are creating an finance app for people who are above 18 years old. Normally, instead of asking the age itself, you would ask for the birthdate to track the age evolution, rigth?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Age may not be the best example, as all years are not the same length. Perhaps something occurring every so many days of weeks?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. What do you think about calculating the next payment date of a monthly subscription?

We could add an example of adding 30 days in a date, considering different date lengths. We could also consider when the current month is December and the result would be a date in the next year to show how to handle these "corner" cases.

I'm thinking of using Duration to add the amount of days in a Date object with the .add_days()

What are your thoughts on that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe calculating the date of arrival of an online purchase 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrival for a purchase would work! That's something that wouldn't care about the lengths of months/years.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I'll come up with an example and we can work upon that :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just commited the change in the example :)


```rust

const SECONDS_PER_YEAR: i64 = 365*24*60*60;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once changed to days or weeks, constants like this can be avoided by utilizing the time::convert module. Alternatively, the duration itself can be divided by another duration, such as foo / Duration::WEEK.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the last commit, I used let time_to_deliver: Duration = 45*(Duration::DAY) . That's was what you meant?

Rottschaefer and others added 3 commits April 5, 2024 20:09
Co-authored-by: Jacob Pratt <[email protected]>
…arrival date of an online purchase to make it clear :)
…arrival date of an online purchase to make it clear :)
@MrBns
Copy link

MrBns commented Jun 19, 2025

after a year. why this pull request is not getting merged ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants