Hacker News new | ask | show | jobs
Pendulum (Python datetimes made easy) reaches its first stable version (pendulum.eustace.io)
4 points by sdispater 3439 days ago
1 comments

For those of you who don’t know, Pendulum is a library for Python to ease datetimes, timedeltas and timezones manipulation. It has come a long way since the early releases and has now reached its first stable version.

It provides objects and classes that directly inherits from the standard library ones so you can use them transparently (exceptions exist, see https://github.com/sdispater/pendulum#limitations)

Basically, the Pendulum class is a replacement for the native datetime one with some useful and intuitive methods, the Interval class is intended to be a better timedelta class and the Period class is a datetime-aware timedelta. It also provides Date and Time classes (introduced in version 0.7.0).

Timezones are also easier to deal with: Pendulum will automatically normalize your datetime to handle DST transitions for you.

    import pendulum
    
    pendulum.create(2013, 3, 31, 2, 30, 0, 0, 'Europe/Paris’)
    # 2:30 for the 31th of March 2013 does not exist
    # so pendulum will return the actual time which is 3:30+02:00
    '2013-03-31T03:30:00+02:00’
    
    dt = pendulum.create(2013, 3, 31, 1, 59, 59, 999999, 'Europe/Paris’)
    '2013-03-31T01:59:59.999999+01:00’
    dt = dt.add(microseconds=1)
    '2013-03-31T03:00:00+02:00’
    dt.subtract(microseconds=1)
    '2013-03-31T01:59:59.999998+01:00’
Note that on creation, the normalization behavior is configurable (see https://pendulum.eustace.io/docs/#timezones for more information).

To those wondering: yes I know Arrow (http://crsmithdev.com/arrow/) exists but its flaws and strange API (you can throw almost anything at get() and it will do its best to determine what you wanted, for instance) motivated me to start this project. You can check why I think Arrow is flawed here: https://pendulum.eustace.io/faq/#why-not-arrow (or https://github.com/sdispater/pendulum#why-not-arrow)

Link to the official documentation: https://pendulum.eustace.io/docs/

Link to the github project: https://github.com/sdispater/pendulum

Thanks for this. Pendulum is, in my opinion, the most palatable datetime lib for Python. Things I'm looking for are:

- a domain model that doesn't conflate unrelated concepts: a human-perceived 'calendar date' is not the same as a timestamp. The two are not inherently comparable, and should be modelled as distinct classes [1].

- an API design that tries to adhere to the language's typical conventions, and cater to the mental model and needs of the language's users.

Unfortunately, Python has had a warped understanding of datetime from the very beginning, so these two are always at odds. But Pendulum does a better job than most, by normalizing stuff like .tomorrow() to midnight in the given timezone, by having sane defaults that tend to avoid common pitfalls, and by resisting Arrow's fast and loose way of dealing with datetime.

Delorean, a worthy alternative, makes slightly different design decisions, (e.g. about extending the datetime object, and how to treat naive datetimes) and I think a detailed comparison would benefit the community.

[1] https://news.ycombinator.com/item?id=12364805

Thanks for the kind words :-) I really appreciate it and I'm glad you find it useful.