Skip to content

Commit

Permalink
Merge pull request #44 from hugsy/fix-js
Browse files Browse the repository at this point in the history
Fix timeuntil js: Date() needs aware datetimes
  • Loading branch information
hugsy authored Mar 25, 2021
2 parents 10f8c46 + aacd4dc commit 7d54b62
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ctfpad/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions ctfpad/templates/ctfpad/ctfs/status.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load humanize %}
{% load ctfpad_filters %}

<br>

Expand All @@ -24,7 +24,7 @@ <h5 class="card-title">
{% else %}
{% if ctf.is_running %}
<script>
setInterval(() => { document.getElementById("countdown").innerHTML = `${timeuntil("{{ ctf.end_date|date:'%Y/%m/%d %H:%i' }}")} left`; }, 1000);
setInterval(() => { document.getElementById("countdown").innerHTML = `${timeuntil("{{ ctf.end_date|as_local_datetime_for_member:request.user.member|date:'c' }}")} left`; }, 1000);
</script>
<p id="countdown"></p>
{% elif ctf.is_finished %}
Expand Down
6 changes: 4 additions & 2 deletions ctfpad/templatetags/ctfpad_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
from django import template
from django.contrib import messages
from django.utils.safestring import mark_safe
from datetime import timezone

import bleach


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
Expand Down
4 changes: 2 additions & 2 deletions static/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit 7d54b62

Please # to comment.