It’s been a long time since the last update, I am so sorry for that. Today is a special day - February 29th, which is only appears in leap years. On this day, everything seems to have a sense of ritual, that’s why I decide to update my blog(smile).
Recently, I noticed a nice Python library called Penduium, whose syntax is simpler than datetime. It’s time to forget ‘datetime.datetime.fromtimestamp()‘ or something.
Here’s a simple introduction.

1. Install Penduium

pip install pendulum

2. Basic features

2.1 Create and parse time

import pendulum

# 获取当前时间,并设置时区为巴黎 
now_in_paris = pendulum.now('Europe/Paris')
print(now_in_paris)

2.2 Timezones

# 时区转换
>>> in_paris = pendulum.datetime(2016, 8, 7, 22, 24, 30, tz='Europe/Paris')
'2016-08-07T22:24:30+02:00'
>>> in_paris.in_timezone('America/New_York')
'2016-08-07T16:24:30-04:00'
>>> in_paris.in_tz('Asia/Tokyo')
'2016-08-08T05:24:30+09:00'

2.3 Time calculation

# 计算明天和上周的时间 
tomorrow = pendulum.now().add(days=1)
last_week = pendulum.now().subtract(weeks=1)

2.4 Duration calcualtion

# 计算时间差并输出具体小时数 
delta = past - last_week
print(delta.hours)  # 输出:23

# 使用自然语言表示时间差 
print(delta.in_words(locale='en'))  # 输出:'6 days 23 hours 58 minutes'

3. Advanced features

3.1 Fluent helpers

Pendulum provides helpers that return a new instance with some attributes modified compared to the original instance.

>>> import pendulum

>>> dt = pendulum.now()

>>> dt.set(year=1975, month=5, day=21).to_datetime_string()
'1975-05-21 13:45:18'

>>> dt.set(hour=22, minute=32, second=5).to_datetime_string()
'2016-11-16 22:32:05'

You can also use the on() and at() methods to change the date and the time respectively.

>>> dt.on(1975, 5, 21).at(22, 32, 5).to_datetime_string()
'1975-05-21 22:32:05'

>>> dt.at(10).to_datetime_string()
'2016-11-16 10:00:00'

>>> dt.at(10, 30).to_datetime_string()
'2016-11-16 10:30:00'

You can also modify the timezone.

>>> dt.set(tz='Europe/London')

3.2 String formatting

Easy and pretty grammar.

>>> import pendulum

>>> dt = pendulum.datetime(1975, 12, 25, 14, 15, 16)
>>> print(dt)
'1975-12-25T14:15:16+00:00'

>>> dt.to_date_string()
'1975-12-25'

>>> dt.to_formatted_date_string()
'Dec 25, 1975'

>>> dt.to_time_string()
'14:15:16'

>>> dt.to_datetime_string()
'1975-12-25 14:15:16'

>>> dt.to_day_datetime_string()
'Thu, Dec 25, 1975 2:15 PM'

# You can also use the format() method
>>> dt.format('dddd Do [of] MMMM YYYY HH:mm:ss A')
'Thursday 25th of December 1975 02:15:16 PM'

# Of course, the strftime method is still available
>>> dt.strftime('%A %-d%t of %B %Y %I:%M:%S %p')
'Thursday 25th of December 1975 02:15:16 PM'

Surprisingly, Penduium can also handle the ‘daylight saving time’ problem.
For more festures and methods, please refer to https://pendulum.eustace.io/docs/
Github: https://github.com/sdispater/pendulum