From e0e7b55d99445a8b1abe57d3c88d7cac9b82790b Mon Sep 17 00:00:00 2001 From: Nick Stokoe Date: Sat, 31 Dec 2022 10:32:19 +0000 Subject: [PATCH] www/countdown.js - allow mocking the current time --- www/countdown.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/www/countdown.js b/www/countdown.js index 0ecf120..3d1c5e0 100644 --- a/www/countdown.js +++ b/www/countdown.js @@ -1,8 +1,8 @@ // This is deliberately browser-compatible JS, so we avoid fat arrows etc. var locale = "EN-GB"; -var now = new Date(); -var nextNewYearUTC = new Date(Date.UTC(now.getFullYear()+1, 0, 1)); +var now = getTimeNow(); +var nextNewYearUTC = new Date(Date.UTC(getTimeNow().getFullYear()+1, 0, 1)); var secsRemaining = 0; var nextZone; var counter = document.getElementById("counter"); @@ -12,6 +12,15 @@ var untilDate = document.getElementById("until-date"); var countDownSound = new Audio('get-ready.ogg'); +// This function returns the current time - or we can adjust it to a +// fake time, if we're testing! +function getTimeNow() { + var now = new Date(); + var offset = 0; // hours + + return new Date(now.valueOf() + offset*3600000); +} + // Convert the next new year time to 'date, hours:mins:secs' in the target timezone function zoneTime(zone, refTime) { return refTime.toLocaleString('EN-US', { @@ -291,7 +300,7 @@ function handleTick(now) { } function ticker() { - var now = new Date(); + var now = getTimeNow(); handleTick(now); var millis = now.getMilliseconds()