From aacd4dcef0b609bc5c0d4eecc16178266c0d67b6 Mon Sep 17 00:00:00 2001 From: lanjelot Date: Wed, 24 Mar 2021 20:37:21 +1100 Subject: [PATCH] Fix timeuntil js: Date() needs aware datetimes. Also keep localtime conversion server side --- ctfpad/models.py | 2 +- ctfpad/templates/ctfpad/ctfs/status.html | 4 ++-- ctfpad/templatetags/ctfpad_filters.py | 6 ++++-- static/js/utils.js | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/ctfpad/models.py b/ctfpad/models.py index 2b8ef98..7abf12f 100644 --- a/ctfpad/models.py +++ b/ctfpad/models.py @@ -387,7 +387,7 @@ def ctfs(self): @property def timezone_offset(self): if not self.timezone: - return 0 + return timedelta() return timedelta(hours = int(self.timezone.replace("UTC", ""), 10)) def to_local_date(self, utc_date): diff --git a/ctfpad/templates/ctfpad/ctfs/status.html b/ctfpad/templates/ctfpad/ctfs/status.html index 66fb396..a184ad1 100644 --- a/ctfpad/templates/ctfpad/ctfs/status.html +++ b/ctfpad/templates/ctfpad/ctfs/status.html @@ -1,4 +1,4 @@ -{% load humanize %} +{% load ctfpad_filters %}
@@ -24,7 +24,7 @@
{% else %} {% if ctf.is_running %}

{% elif ctf.is_finished %} diff --git a/ctfpad/templatetags/ctfpad_filters.py b/ctfpad/templatetags/ctfpad_filters.py index 862f93d..f496cfc 100644 --- a/ctfpad/templatetags/ctfpad_filters.py +++ b/ctfpad/templatetags/ctfpad_filters.py @@ -3,6 +3,7 @@ from django import template from django.contrib import messages from django.utils.safestring import mark_safe +from datetime import timezone import bleach @@ -10,8 +11,9 @@ register = template.Library() @register.filter -def as_local_datetime_for_member(utc_timezone, member): - return utc_timezone + member.timezone_offset +def as_local_datetime_for_member(naive_utc, member): + offset = timezone(member.timezone_offset) + return naive_utc.astimezone(offset) @register.filter diff --git a/static/js/utils.js b/static/js/utils.js index 1f9f09c..c98ada4 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -31,8 +31,8 @@ function timeuntil(datetime) { const end = new Date(datetime).getTime(); const now = new Date().getTime(); - const tz = new Date().getTimezoneOffset(); - const offset = end - now - tz*60*1000; + + const offset = end - now; if (offset < 0) return "Finished";