| 1 | """
|
|---|
| 2 | Timezone converter for Django. This filter converts a datetime to the
|
|---|
| 3 | 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 os import environ
|
|---|
| 11 | from pytz import timezone, utc
|
|---|
| 12 | from django import template
|
|---|
| 13 |
|
|---|
| 14 | register = template.Library ()
|
|---|
| 15 |
|
|---|
| 16 | @register.filter
|
|---|
| 17 | def 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 |
|
|---|