about summary refs log tree commit diff
path: root/public/clock.js
diff options
context:
space:
mode:
authorSurinna Curtis <ekiru.0@gmail.com>2017-11-16 01:21:16 -0600
committerSurinna Curtis <ekiru.0@gmail.com>2017-11-16 01:21:16 -0600
commit35fbdc36f92b610e8a73e2acb220e87cf5fc83b0 (patch)
treefcd9bdb5ba49ab7a6a79590c74db858ae77b4239 /public/clock.js
parent30237259367a0ef2b20908518b86bbeb358999b5 (diff)
parent88627fd7aa2493a6890d60a5965459e4c7fe6fe9 (diff)
Merge tootsuite/master at 30237259367a0ef2b20908518b86bbeb358999b5
Diffstat (limited to 'public/clock.js')
-rw-r--r--public/clock.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/public/clock.js b/public/clock.js
new file mode 100644
index 000000000..ffb9beae8
--- /dev/null
+++ b/public/clock.js
@@ -0,0 +1,54 @@
+document.addEventListener("DOMContentLoaded", function(event) { 
+  updateClock();
+  setInterval(updateClock, 1000);
+});
+
+function getNextOpen(now) {
+    var days = [[0, 14], [4, 18], [8, 22], [12], [2, 16], [6, 20], [10]]
+    var nowday = now.getUTCDay();
+    var nour = now.getUTCHours();
+
+    var open_hour = -1;
+    var d = 0;
+
+    while (open_hour == -1) {
+        var times = days[(nowday + d) % 7];
+        for (var i = 0; i < times.length; ++i) {
+            var time = times[i];
+            if (time == nour) {
+                return "refresh";
+            } else if (time > nour || d > 0) {
+                open_hour = time;
+                break;
+            }
+        }
+        if (open_hour == -1) {
+            d += 1;
+            nour = -1;
+        }
+    }
+
+    var open = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + d));
+    var ts = open.setUTCHours(open_hour);
+    return open;
+}
+
+function updateClock() {
+    var clock = document.querySelector(".closed-registrations-message .clock");
+    var now = new Date();
+    var open = getNextOpen(now);
+
+    if (open == "refresh") {
+        location.reload();
+        return;
+    }
+
+    var until = open - now;
+    var ms = until % 1000;
+    var s =  Math.floor((until / 1000)) % 60;
+    var m =  Math.floor((until / 1000 / 60)) % 60;
+    var h =  Math.floor((until / 1000 / 60 / 60));
+    if (m < 10) m = "0" + m;
+    if (s < 10) s = "0" + s;
+    clock.innerHTML = h + ":" + m + ":" + s;
+}