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