Skip to content

Instantly share code, notes, and snippets.

@billygoat
Last active December 16, 2015 11:49
Show Gist options
  • Save billygoat/5430582 to your computer and use it in GitHub Desktop.
Save billygoat/5430582 to your computer and use it in GitHub Desktop.
TZAwareDate - Pelican plugin that adds two variables to Article objects date_tz: Datetime object that is timezone aware. If the post has a time zone value, it uses that. Otherwise, it applies the TIMEZONE configuration value date_utc: Datetime object that is timezone aware and adjusted to UTC.
"""
Time Zone Aware Date plugin for Pelican
================================
Adds a datetime object with the selected time zone
"""
from pelican import signals
from datetime import datetime
from dateutil import tz
def add_tz_aware_date(generator):
timeZone = generator.settings.get('TIMEZONE', 'UTC')
fromZone = tz.gettz(timeZone)
toZone = tz.gettz('UTC')
for article in generator.articles:
if article.date.tzinfo is None:
article.date_tz = article.date.replace(tzinfo=fromZone)
else:
article.date_tz = article.date
article.date_utc = article.date_tz.astimezone(toZone)
def register():
signals.article_generator_finalized.connect(add_tz_aware_date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment