A version of http://djangosnippets.org/snippets/2388/ which assumes UTC-based dates.
This is when you store dates in UTC (as you should), and want to display them in your site's local timezone, and you notice that Django's timesince/time template tags still do not support timezones.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from django import template
from django.conf import settings
import pytz
register = template.Library()
@register.filter
def astimezone(date, destination=None):
if not destination:
destination = settings.TIME_ZONE
if date.tzinfo:
return date.astimezone(pytz.timezone(destination))
else:
return pytz.timezone('UTC').localize(date).astimezone(pytz.timezone(destination))
|
More like this
- LazyPrimaryKeyRelatedField by LLyaudet 2 days, 12 hours ago
- CacheInDictManager by LLyaudet 2 days, 19 hours ago
- MYSQL Full Text Expression by Bidaya0 3 days, 13 hours ago
- Custom model manager chaining (Python 3 re-write) by Spotted1270 1 week, 2 days ago
- Django Standard API Response Middleware for DRF for modern frontend easy usage by Denactive 3 weeks, 4 days ago
Comments
Please login first before commenting.