[Pelican] Add Build Timestamp to Pelican Site Automatically


When we generate the website using Python Pelican static site generator, we may want to add "Last Updated" time to our website at the footer or somewhere else automatically. This post will show you how to do it.

In pelicanconf.py, add following code:

from datetime import datetime
import pytz

# modify TIMEZONE to your timezone
TIMEZONE = 'Asia/Taipei'
BUILD_TIME = datetime.now(pytz.timezone(TIMEZONE))

In the template html, you can use the BUILD_TIME as follows:

Last Updated: {{ BUILD_TIME | strftime("%b %d, %Y, %H:%M:%S") }} ({{ TIMEZONE }})

If you do not know about Python strftime, check this post.

Tested on: Ubuntu Linux 20.04, Python 3.8.5.


References:

[1]
[2]How to keep your copyright year up-to-date • bernhard.scheirle.de
[3]python - Print the last modification of a jinja2 template in pelican - Stack Overflow
[4]How do I get a value of datetime.today() in Python that is "timezone aware"? - Stack Overflow