Procházet zdrojové kódy

Don't localize twice

Ask Solem před 12 roky
rodič
revize
dde9b5a5b7
2 změnil soubory, kde provedl 24 přidání a 15 odebrání
  1. 13 12
      celery/schedules.py
  2. 11 3
      celery/utils/timeutils.py

+ 13 - 12
celery/schedules.py

@@ -25,7 +25,7 @@ from .utils.timeutils import (
 from .datastructures import AttributeDict
 
 
-def weak_bool(s):
+def _weak_bool(s):
     return 0 if s == '0' else s
 
 
@@ -116,10 +116,10 @@ class schedule(object):
     def utc_enabled(self):
         return self.app.conf.CELERY_ENABLE_UTC
 
-    @cached_property
-    def to_local(self):
-        return (timezone.to_local if self.utc_enabled
-                                  else timezone.to_local_fallback)
+    def to_local(self, dt):
+        if not self.utc_enabled:
+            return timezone.to_local_fallback(dt, self.tz)
+        return dt
 
 
 class crontab_parser(object):
@@ -423,11 +423,11 @@ class crontab(schedule):
 
     def __repr__(self):
         return ('<crontab: %s %s %s %s %s (m/h/d/dM/MY)>' %
-                        (weak_bool(self._orig_minute) or '*',
-                         weak_bool(self._orig_hour) or '*',
-                         weak_bool(self._orig_day_of_week) or '*',
-                         weak_bool(self._orig_day_of_month) or '*',
-                         weak_bool(self._orig_month_of_year) or '*'))
+                        (_weak_bool(self._orig_minute) or '*',
+                         _weak_bool(self._orig_hour) or '*',
+                         _weak_bool(self._orig_day_of_week) or '*',
+                         _weak_bool(self._orig_day_of_month) or '*',
+                         _weak_bool(self._orig_month_of_year) or '*'))
 
     def __reduce__(self):
         return (self.__class__, (self._orig_minute,
@@ -488,8 +488,9 @@ class crontab(schedule):
                     delta = self._delta_to_next(last_run_at,
                                                 next_hour, next_minute)
 
-        return remaining(self.to_local(last_run_at, tz),
-                         delta, self.to_local(self.now(), tz))
+        now = self.maybe_make_aware(self.now())
+        return remaining(self.to_local(last_run_at), delta,
+                         self.to_local(now))
 
     def is_due(self, last_run_at):
         """Returns tuple of two items `(is_due, next_time_to_run)`,

+ 11 - 3
celery/utils/timeutils.py

@@ -8,6 +8,7 @@
 """
 from __future__ import absolute_import
 
+import os
 import time as _time
 
 from kombu.utils import cached_property
@@ -26,6 +27,9 @@ except ImportError:     # pragma: no cover
     pytz = None         # noqa
 
 
+C_REMDEBUG = os.environ.get('C_REMDEBUG', False)
+
+
 DAYNAMES = 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'
 WEEKDAYS = dict((name, dow) for name, dow in zip(DAYNAMES, range(7)))
 
@@ -128,7 +132,7 @@ class _Zone(object):
 
     @cached_property
     def local(self):
-        return tz.tzlocal()
+        return _get_local_timezone()
 
     @cached_property
     def utc(self):
@@ -188,7 +192,7 @@ def delta_resolution(dt, delta):
     return dt
 
 
-def remaining(start, ends_in, now=None, relative=False):
+def remaining(start, ends_in, now=None, relative=False, debug=False):
     """Calculate the remaining time for a start date and a timedelta.
 
     e.g. "how many seconds left for 30 seconds after start?"
@@ -206,7 +210,11 @@ def remaining(start, ends_in, now=None, relative=False):
     end_date = start + ends_in
     if relative:
         end_date = delta_resolution(end_date, ends_in)
-    return end_date - now
+    ret = end_date - now
+    if C_REMDEBUG:
+        print('rem: NOW:%s START:%s END_DATE:%s REM:%s' % (
+            now, start, end_date, ret))
+    return ret
 
 
 def rate(rate):