jk-gan / aion

A friendly Rust utility crate for duration and datetime
MIT License
39 stars 0 forks source link

Add before & after methods, clean up interface and tests #2

Closed 0xSiO closed 4 years ago

0xSiO commented 4 years ago

The days_later and days_ago tests were failing since there is a small execution delay between the evaluation of each side of the equality assertion. We could just calculate the difference between the two values and then assert that it is small enough to consider the two sides equal, but a better solution is to implement a before and an after method to modify a given DateTime. Then the ago and from_now methods can be automatically implemented using before and after.

This also gives us a more expressive way to write more complex DateTimes, like 10.weeks().before(20.minutes().ago()).

Other changes:

jk-gan commented 4 years ago

Hi @lucis-fluxum , this is indeed a good catch and make the crate more flexible. Am wondering what is the better example for this(want to put on README to show use cases), currently in my thought is

let christmas = Utc.ymd(2020, 12, 25).and_hms(0, 0, 0);
let two_weeks_before_christmas = 2.weeks().before(christmas);

What do you think?

0xSiO commented 4 years ago

Hey @jk-gan, thanks for your feedback. I updated the main example and the code on the README to reflect the new API and avoid any redundant examples. To cut down on noise, I didn't include calls to println in the README. Let me know if everything looks okay.

jk-gan commented 4 years ago

Looks good to me. I will merge it. Thanks for the idea.