1

I've recently stumbled upon this awesome pendulum "datetimes made easy" library.

It has a very nice feature of displaying "human like" diffs between datetimes:

In [1]: import pendulum

In [2]: now = pendulum.now()

In [3]: future = now.add(years=10)

In [4]: future.diff_for_humans()
Out[4]: '10 years from now'

But, is it possible to make it work for a more complicated difference - say "years" and "weeks"?

In [5]: future = now.add(years=10, weeks=5)

In [6]: future.diff_for_humans()
Out[6]: '10 years from now'

I would expect it to output 10 years and 5 weeks from now.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

1 Answers1

2

From the Pendulum module readme:

now = pendulum.now()
future = now.add(years=10, weeks=5)
delta = future - now
delta.in_words()
>>>'10 years 1 month 4 days'

https://github.com/sdispater/pendulum

BoboDarph
  • 2,751
  • 1
  • 10
  • 15
  • Good finding, I wonder why there is this difference between the `diff_for_humans()` and `in_words()`. Thanks! – alecxe Dec 15 '17 at 16:18
  • 1
    You could always look in the implementation for that. I linked the module sources. – BoboDarph Dec 15 '17 at 16:19
  • 1
    Okay, [read the source, Luke](https://blog.codinghorror.com/learn-to-read-the-source-luke/) :) – alecxe Dec 15 '17 at 16:22
  • It doesn't answer the question if the `'from now'` is desired. Just adding that manually isn't an option when localization is required. – Thijs van Dien Dec 15 '17 at 16:23
  • You could call pendulum.now(timezone) as your "now" value. Also in_words has a locale param. Worst comes to worst, just do string transformations if your locale is not supported. Or submit a merge request to the module maintainers with your locale :D – BoboDarph Dec 15 '17 at 16:27
  • 1
    I just opened an issue for this: https://github.com/sdispater/pendulum/issues/169. – Thijs van Dien Dec 15 '17 at 16:55