hswaw/checkinator: convert timestamp to browsers timezone
Change-Id: Ib7439269bf13de530a5f170bf231f89d815b0f3e
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1246
Reviewed-by: q3k <q3k@hackerspace.pl>
diff --git a/hswaw/checkinator/at/templates/main.html b/hswaw/checkinator/at/templates/main.html
index 6012b21..36ab1cd 100644
--- a/hswaw/checkinator/at/templates/main.html
+++ b/hswaw/checkinator/at/templates/main.html
@@ -9,7 +9,7 @@
{% for user, timestamp in users %}
<li>
<a href="{{ user | wikiurl }}">
- {{ user }} ({{ timestamp|strfts() }})
+ {{ user }} (<span class="timestamp" data-datetime="{{ timestamp|utcisoformat }}">{{ timestamp|strfts() }} UTC</span>)
</a>
</li>
{% endfor %}
@@ -31,4 +31,19 @@
{% endtrans %}
<hr>
<a href="claim">Claim this device!</a>
+ <script>
+ function formatDate(d) {
+ function pad(n) {return n<10 ? '0'+n : n};
+ return d.getFullYear()+'-'
+ + pad(d.getMonth()+1)+'-'
+ + pad(d.getDate())+' '
+ + pad(d.getHours())+':'
+ + pad(d.getMinutes());
+ }
+
+ for (const tstamp of document.getElementsByClassName('timestamp')) {
+ const dt = new Date(tstamp.getAttribute("data-datetime"));
+ tstamp.innerHTML = formatDate(dt);
+ }
+ </script>
{% endblock %}
diff --git a/hswaw/checkinator/at/tracker.py b/hswaw/checkinator/at/tracker.py
index 18a139e..4ca71fb 100644
--- a/hswaw/checkinator/at/tracker.py
+++ b/hswaw/checkinator/at/tracker.py
@@ -7,7 +7,7 @@
import subprocess
import logging
from concurrent import futures
-from datetime import datetime
+from datetime import datetime, timezone
from .tracker_pb2 import DhcpClient, DhcpClients, HwAddrResponse
from .tracker_pb2_grpc import DhcpTrackerServicer, add_DhcpTrackerServicer_to_server
@@ -22,7 +22,8 @@
def lease_to_client(lease: DhcpLease) -> DhcpClient:
return DhcpClient(
hw_address = bytes.fromhex(lease.hwaddr.replace(':', '')),
- last_seen = datetime.utcfromtimestamp(lease.atime).isoformat(),
+ last_seen = datetime.utcfromtimestamp(lease.atime).replace(
+ tzinfo=timezone.utc).isoformat(),
client_hostname = lease.name,
ip_address = lease.ip
)
diff --git a/hswaw/checkinator/at/web.py b/hswaw/checkinator/at/web.py
index 9eb5e6a..f85aa25 100644
--- a/hswaw/checkinator/at/web.py
+++ b/hswaw/checkinator/at/web.py
@@ -1,7 +1,7 @@
import json
import sqlite3
from pathlib import Path
-from datetime import datetime
+from datetime import datetime, timezone
from typing import NamedTuple, Iterable, Iterator, List
from functools import wraps
from flask import Flask, render_template, abort, g, \
@@ -112,8 +112,13 @@
@app.template_filter('strfts')
- def strfts(ts, format='%d/%m/%Y %H:%M'):
- return datetime.fromtimestamp(ts).strftime(format)
+ def strfts(ts, format='%Y-%m-%d %H:%M'):
+ return datetime.utcfromtimestamp(ts).strftime(format)
+
+ @app.template_filter('utcisoformat')
+ def utcisoformat(ts):
+ return datetime.utcfromtimestamp(ts).replace(
+ tzinfo=timezone.utc).isoformat()
@app.template_filter('wikiurl')