[perf-web] Switch to using explicit timezones



commit fc7bfcae30f6c99022e6c9accd326d8f178e98ad
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Thu Jul 24 14:47:18 2014 +0200

    Switch to using explicit timezones
    
    The Django USE_TZ=False behavior was introducing the local timezone offset into
    times in non-obvious places. Changing the local timezone to UTC would probably
    fix this, but to keep things most straightforward, switch to USE_TZ=True and
    make all datetime objects have an explicit UTC timezone.

 metrics/models.py |    3 ++-
 metrics/views.py  |   14 ++++++++------
 perf/settings.py  |    6 ++++--
 3 files changed, 14 insertions(+), 9 deletions(-)
---
diff --git a/metrics/models.py b/metrics/models.py
index ad28c19..e1a7f4b 100644
--- a/metrics/models.py
+++ b/metrics/models.py
@@ -1,5 +1,6 @@
 from datetime import datetime, timedelta
 from django.db import models
+from django.utils import timezone
 import sys
 
 class Metric(models.Model):
@@ -37,7 +38,7 @@ class Value(models.Model):
 
 def resummarize():
     # We give machines a 6 hours grace period to update results
-    now = datetime.utcnow()
+    now = timezone.now()
     cutoff = now - timedelta(hours=6)
 
     SummaryHour6.save_summaries(cutoff)
diff --git a/metrics/views.py b/metrics/views.py
index dbcc856..4935313 100644
--- a/metrics/views.py
+++ b/metrics/views.py
@@ -8,6 +8,7 @@ from django.conf import settings
 from django.db.models import Min, Max
 from django.template import Context, loader
 from django.http import HttpResponse, HttpResponseNotFound, HttpResponseBadRequest
+from django.utils import timezone
 from django.views.decorators.http import require_POST
 from django.views.decorators.csrf import csrf_exempt
 
@@ -15,7 +16,7 @@ import config
 from models import *
 from signed_request import check_signature, BadSignature
 
-_EPOCH = datetime(1970, 1, 1)
+_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
 
 # timedelta.total_seconds added in 2.7
 if hasattr(timedelta, 'total_seconds'):
@@ -31,7 +32,7 @@ def home(request):
 
     time_range = Report.objects.aggregate(min=Min('pull_time'), max=Max('pull_time'));
     if time_range['min'] is None:
-        time_range['min'] = time_range['max'] = datetime.now()
+        time_range['min'] = time_range['max'] = timezone.now()
 
     c = Context({
         'page_name': 'home',
@@ -55,7 +56,7 @@ def metric(request, metric_name):
     time_range = Report.objects \
                    .aggregate(min=Min('pull_time'), max=Max('pull_time'));
     if time_range['min'] is None:
-        time_range['min'] = time_range['max'] = datetime.now()
+        time_range['min'] = time_range['max'] = timezone.now()
 
     c = Context({
         'page_name': 'metric',
@@ -78,7 +79,7 @@ def target(request, machine_name, partition_name, tree_name, testset_name):
                    .filter(target__name=target.name) \
                    .aggregate(min=Min('pull_time'), max=Max('pull_time'));
     if time_range['min'] is None:
-        time_range['min'] = time_range['max'] = datetime.now()
+        time_range['min'] = time_range['max'] = timezone.now()
 
     c = Context({
         'page_name': 'target',
@@ -125,7 +126,7 @@ def values(request):
         m = re.match(r'(\d\d\d\d)-(\d\d)-(\d\d)$', start_str)
         if m is not None:
             try:
-                start = datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)))
+                start = datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)), tzinfo=timezone.utc)
             except ValueError:
                 pass
         if start is None:
@@ -137,7 +138,7 @@ def values(request):
         m = re.match(r'(\d\d\d\d)-(\d\d)-(\d\d)$', end_str)
         if m is not None:
             try:
-                end = datetime(int(m.group(1)), int(m.group(2)), int(m.group(3))) + timedelta(hours=24)
+                end = datetime(int(m.group(1)), int(m.group(2)), int(m.group(3)), tzinfo=timezone.utc) + 
timedelta(hours=24)
             except ValueError:
                 pass
         if end is None:
@@ -330,6 +331,7 @@ def process_report(data, machine_name):
     pull_time_str = child_string(data, 'pullTime')
     try:
         pull_time = datetime.strptime(pull_time_str, '%Y-%m-%d %H:%M:%S')
+        pull_time.replace(tzinfo=timezone.utc)
     except ValueError:
         raise ValidationError("Can't parse property 'pullTime'")
 
diff --git a/perf/settings.py b/perf/settings.py
index 95bd0a4..1aeb93f 100644
--- a/perf/settings.py
+++ b/perf/settings.py
@@ -25,7 +25,9 @@ ALLOWED_HOSTS = []
 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
 # although not all choices may be available on all operating systems.
 # In a Windows environment this must be set to your system time zone.
-TIME_ZONE = 'America/Chicago'
+# The main use of this is that when USE_TZ is false this is used to
+# interpret naive times as "local times".
+TIME_ZONE = 'UTC'
 
 # Language code for this installation. All choices can be found here:
 # http://www.i18nguy.com/unicode/language-identifiers.html
@@ -42,7 +44,7 @@ USE_I18N = True
 USE_L10N = True
 
 # If you set this to False, Django will not use timezone-aware datetimes.
-USE_TZ = False
+USE_TZ = True
 
 # Absolute filesystem path to the directory that will hold user-uploaded files.
 # Example: "/home/media/media.lawrence.com/media/"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]