UTC-based astimezone filter

 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

  1. astimezone template tag by whardier 2 years, 2 months ago
  2. Querying datetime aware objects in your local timezone by jayliew 11 months, 3 weeks ago
  3. UTC DateTime field by ludo 5 years, 8 months ago
  4. filter dates to user profile's timezone by Scanner 6 years, 1 month ago
  5. Template tags for localizing UTC times with pytz by wolever 1 year, 9 months ago

Comments

(Forgotten your password?)