浏览代码

timezone.to_local: Only convert naive datetimes to UTC. Closes #855

Ask Solem 12 年之前
父节点
当前提交
10ca9582fe
共有 1 个文件被更改,包括 9 次插入5 次删除
  1. 9 5
      celery/utils/timeutils.py

+ 9 - 5
celery/utils/timeutils.py

@@ -48,8 +48,9 @@ class _Zone(object):
         return self.get_timezone(tzinfo)
 
     def to_local(self, dt, local=None, orig=None):
-        return set_tz(dt, orig or self.utc).astimezone(
-                    self.tz_or_local(local))
+        if is_naive(dt):
+            dt = set_tz(dt, orig or self.utc)
+        return dt.astimezone(self.tz_or_local(local))
 
     def get_timezone(self, zone):
         if isinstance(zone, basestring):
@@ -202,10 +203,13 @@ def is_naive(dt):
 
 def set_tz(dt, tz):
     """Sets the timezone for a datetime object."""
-    if hasattr(tz, 'localize'):
+    try:
+        localize = tz.localize
+    except AttributeError:
+        return dt.replace(tzinfo=tz)
+    else:
         # works on pytz timezones
-        return tz.localize(dt, is_dst=None)
-    return dt.replace(tzinfo=tz)
+        return localize(dt, is_dst=None)
 
 
 def to_utc(dt):