From 640496deb262349101567de12d2476cbfe5065b3 Mon Sep 17 00:00:00 2001
From: Alban Gruin
Date: Sat, 14 Oct 2017 11:25:46 +0200
Subject: timezone.now() retourne l’heure UTC, ce qui empêche la mise à jour de
 la semaine prochaine le samedi à minuit. Remplacement de ces appels par une
 fonction qui retourne la bonne heure dans le bon fuseau horaire.

---
 management/commands/timetables.py | 10 +++++-----
 tests.py                          |  3 ++-
 utils.py                          | 10 +++++++---
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/management/commands/timetables.py b/management/commands/timetables.py
index a152f9e..35fb26e 100644
--- a/management/commands/timetables.py
+++ b/management/commands/timetables.py
@@ -21,7 +21,7 @@ from django.db.models import Min
 from django.utils import timezone
 
 from edt.models import Course, Timetable
-from edt.utils import get_week
+from edt.utils import get_week, tz_now
 from ._private import delete_courses_in_week, get_events, get_update_date, get_weeks, get_xml
 
 @transaction.atomic
@@ -32,7 +32,7 @@ def process_timetable_week(timetable, soup, weeks_in_soup, force, year=None, wee
     if force:
         today = begin
     else:
-        today = timezone.make_aware(datetime.datetime.now())
+        today = tz_now()
 
     # On récupère la mise à jour la plus ancienne dans les cours de l’emploi du temps
     # commençant à partir de maintenant
@@ -107,16 +107,16 @@ class Command(BaseCommand):
         if options["all"]:
             weeks = None
         elif options["week"] is None:
-            _, week, day = timezone.now().isocalendar()
+            _, week, day = tz_now().isocalendar()
             if day >= 6:
-                year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar()
+                year, week, _ = (tz_now() + datetime.timedelta(weeks=1)).isocalendar()
             weeks = [week]
         else:
             weeks = options["week"]
 
         if not options["all"]:
             if options["year"] is None and year is None:
-                year = timezone.now().year
+                year = tz_now().year
             elif year is None:
                 year = options["year"][0]
 
diff --git a/tests.py b/tests.py
index 1d23620..be7b884 100644
--- a/tests.py
+++ b/tests.py
@@ -17,11 +17,12 @@ from django.test import TestCase
 from django.utils import timezone
 
 from .models import Course, Group, Timetable, Year
+from .utils import tz_now
 
 
 class CourseTestCase(TestCase):
     def setUp(self):
-        dt = timezone.now()
+        dt = tz_now()
 
         self.year = Year(name="L2", slug="l2")
         self.year.save()
diff --git a/utils.py b/utils.py
index bd337e4..28a1ba2 100644
--- a/utils.py
+++ b/utils.py
@@ -19,12 +19,12 @@ import re
 from django.utils import timezone
 
 def get_current_week():
-    return timezone.now().isocalendar()[:2]
+    return tz_now().isocalendar()[:2]
 
 def get_current_or_next_week():
-    year, week, day = timezone.now().isocalendar()
+    year, week, day = tz_now().isocalendar()
     if day >= 6:
-        year, week, _ = (timezone.now() + datetime.timedelta(weeks=1)).isocalendar()
+        year, week, _ = (tz_now() + datetime.timedelta(weeks=1)).isocalendar()
 
     return year, week
 
@@ -74,3 +74,7 @@ def parse_group(name):
         return parts[0], parts[5], parts[6], None
     elif parts[7] is not None: # Groupe de TP
         return parts[0], parts[8], parts[9], parts[10]
+
+def tz_now():
+    """Retourne la date et l’heure avec le bon fuseau horaire"""
+    return timezone.make_aware(datetime.datetime.now())
-- 
cgit v1.2.1