Ticket #3378: astimezone.2.py

File astimezone.2.py, 834 bytes (added by arvin, 18 years ago)

filter to convert between timezones

Line 
1"""
2Timezone converter for Django. This filter converts a datetime to the
3specified timezone. If the specified timezone is unknown the filter
4fails silently and returns the original datetime.
5
6Requires the Python-timezone library (pytz) from http://pytz.sourceforge.net/.
7"""
8
9from datetime import datetime
10from os import environ
11from pytz import timezone, utc
12from django import template
13
14register = template.Library ()
15
16@register.filter
17def astimezone (value, arg):
18
19 try:
20 src_tz = timezone (environ['TZ'])
21 dst_tz = timezone (arg)
22 except KeyError:
23 return value
24
25 print environ['TZ'], value, arg
26
27 tmp = datetime (value.year, value.month, value.day,
28 value.hour, value.minute, value.second,
29 value.microsecond, src_tz)
30
31 return tmp.astimezone (dst_tz)
32
Back to Top