When I left Rails, this is one area I really missed.
Time.use_zone("Singapore") { (Time.zone.now - 3.days).beginning_of_week }
This readable line in datetime utils or even pendulum, is such a pain.
It’s now stable (since January actually) and has gain traction since I first introduced it, so I thought I’d share it here again so that it can be of use to more people.
For those wondering, Pendulum is a library for Python to ease datetimes, timedeltas and timezones manipulation.
Each Pendulum classes are subclasses of the standard classes so you can use them as drop-in replacements in your code (some exceptions exist, see https://pendulum.eustace.io/docs/#limitations)
Link to the official website: https://pendulum.eustace.io
Link to the official documentation: https://pendulum.eustace.io/docs/
Link to the github project: https://github.com/sdispater/pendulum
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
A lot of effort has been put into getting DST right so I hope it will be what you are looking for.
Disclaimer: I am the author of Pendulum :-)
It is heavily inspired by [Carbon](http://carbon.nesbot.com) for PHP.
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 time delta class and, finally, the Period class is a datetime-aware timedelta.
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’
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-arrowLink to the official documentation: https://pendulum.eustace.io/docs/
Link to the github project: https://github.com/sdispater/pendulum
It is heavily inspired by Carbon (http://carbon.nesbot.com) for PHP.
Basically, the Pendulum class is a replacement for the native datetime one with some (I hope) useful and intuitive methods and the PendulumInterval class is intended to be a better timedelta class.
An important note about the Pendulum instances is that, unlike the native datetime ones, they are mutable via the appropriate methods.
To those wondering: yes I know Arrow (http://arrow.readthedocs.io) 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.
It’s still fresh so any feedback is appreciated :-).
Link to the official documentation: http://pendulum.eustace.io
Link to the github project: https://github.com/sdispater/pendulum