@register.filter('timezone')
def do_timezone(value, arg):
 if (not isinstance(value, datetime)):
  return ''
 try:
  if timezone.is_naive(value):
   default_timezone = timezone.get_default_timezone()
   value = timezone.make_aware(value, default_timezone)
 except Exception:
  return ''
 if isinstance(arg, tzinfo):
  tz = arg
 elif isinstance(arg, str):
  try:
   tz = pytz.timezone(arg)
  except pytz.UnknownTimeZoneError:
   return ''
 else:
  return ''
 result = timezone.localtime(value, tz)
 result = datetimeobject(result.year, result.month, result.day, result.hour, result.minute, result.second, result.microsecond, result.tzinfo)
 result.convert_to_local_time = False
 return result

@ register . filter ( 'timezone' ) def do_timezone ( value , arg ) : if ( not isinstance ( value , datetime ) ) : return '' try : if timezone . is_naive ( value ) : default_timezone = timezone . get_default_timezone ( ) value = timezone . make_aware ( value , default_timezone ) except Exception : return '' if isinstance ( arg , tzinfo ) : tz = arg elif isinstance ( arg , str ) : try : tz = pytz . timezone ( arg ) except pytz . UnknownTimeZoneError : return '' else : return '' result = timezone . localtime ( value , tz ) result = datetimeobject ( result . year , result . month , result . day , result . hour , result . minute , result . second , result . microsecond , result . tzinfo ) result . convert_to_local_time = False return result
converts a datetime to local time in a given time zone .