From 05d1e97c32ea82ab89f2229f46e2008c8d24c228 Mon Sep 17 00:00:00 2001 From: Monica Cellio Date: Tue, 1 Jun 2021 22:52:56 -0400 Subject: [PATCH 1/4] fixes issue where between UTC midnight and local midnight, dates were wrong --- public/assets/community/judaism.js | 77 +++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/public/assets/community/judaism.js b/public/assets/community/judaism.js index b1a6d248c..b5de0d340 100644 --- a/public/assets/community/judaism.js +++ b/public/assets/community/judaism.js @@ -528,6 +528,81 @@ $(() => { * Calendar script, added 2021-04-01 by @luap42 */ + // Use *local* time for ISO string (Date.toISOString() uses UTC). + function toIsoString(date) { + var tzo = -date.getTimezoneOffset(), + dif = tzo >= 0 ? '+' : '-', + pad = function(num) { + var norm = Math.floor(Math.abs(num)); + return (norm < 10 ? '0' : '') + norm; + }; + + return date.getFullYear() + + '-' + pad(date.getMonth() + 1) + + '-' + pad(date.getDate()) + + 'T' + pad(date.getHours()) + + ':' + pad(date.getMinutes()) + + ':' + pad(date.getSeconds()) + + dif + pad(tzo / 60) + + ':' + pad(tzo % 60); +} + + +window.addEventListener("load", async () => { + const container = document.createElement('div'); + container.innerHTML = "
Today is:
loading date...
"; + container.classList.add('widget', 'has-margin-4'); + + const disclaimerNotice = document.querySelector('.widget.is-yellow:first-child'); + disclaimerNotice.parentNode.insertBefore(container, disclaimerNotice.nextSibling); + + let todayDate = new Date(); + + // Start new day at 8pm local time previous day (if it's 8 or later today is tomorrow). + // Day is zero-indexed, hence > 19 (not > 20). + if (todayDate.getHours() > 19) { + todayDate.setDate(todayDate.getDate() + 1); + } + + // Use actual year and month (rather than now) in URL to dodge caching. + const result = await fetch('https://www.hebcal.com/hebcal?v=1&cfg=json&year=' + todayDate.getFullYear() + '&month=' + (todayDate.getMonth()+1) + '&d=on&o=on'); + const response = await result.json() + + const parsedData = response.items.reduce((rv, x) => { + (rv[x.date] = rv[x.date] || []).push(x); + return rv; + }, {}); + + // Do not use Date.toISOString(), which uses UTC not local time. + let now = toIsoString(todayDate).substr(0, 10); + + const fields = parsedData[now]; + container.querySelector('._cal_val').innerHTML = ""; + + fields.forEach(field => { + const fieldContainer = document.createElement('div'); + fieldContainer.classList.add('has-font-size-larger', 'h-fw-bold', 'h-m-t-2'); + container.querySelector('._cal_val').appendChild(fieldContainer); + + fieldContainer.innerText = field.title; + }); + + const DAY_LIST = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; + + const todayDay = DAY_LIST[(todayDate.getDay() + 6) % 7]; + const yesterdayDay = DAY_LIST[(todayDate.getDay() + 5) % 7]; + + + const fieldContainer = document.createElement('div'); + fieldContainer.classList.add('h-m-t-2', 'has-font-size-caption'); + container.querySelector('._cal_val').appendChild(fieldContainer); + + fieldContainer.innerText = yesterdayDay + ' night (' + todayDay + ')'; +}); + + + +///////// window.addEventListener("load", async () => { const container = document.createElement('div'); container.innerHTML = "
Today is:
loading date...
"; @@ -578,4 +653,4 @@ window.addEventListener("load", async () => { container.querySelector('._cal_val').appendChild(fieldContainer); fieldContainer.innerText = yesterdayDay + ' night (' + todayDay + ')'; -}); +}); \ No newline at end of file From 220aac818a9ddbb74ace045601764bcdf242528c Mon Sep 17 00:00:00 2001 From: Monica Cellio Date: Tue, 1 Jun 2021 22:54:22 -0400 Subject: [PATCH 2/4] uh, it helps to delete the code I want to replace... --- public/assets/community/judaism.js | 53 ------------------------------ 1 file changed, 53 deletions(-) diff --git a/public/assets/community/judaism.js b/public/assets/community/judaism.js index b5de0d340..05c8fed76 100644 --- a/public/assets/community/judaism.js +++ b/public/assets/community/judaism.js @@ -601,56 +601,3 @@ window.addEventListener("load", async () => { }); - -///////// -window.addEventListener("load", async () => { - const container = document.createElement('div'); - container.innerHTML = "
Today is:
loading date...
"; - container.classList.add('widget', 'has-margin-4'); - - const disclaimerNotice = document.querySelector('.widget.is-yellow:first-child'); - disclaimerNotice.parentNode.insertBefore(container, disclaimerNotice.nextSibling); - - const result = await fetch('https://www.hebcal.com/hebcal?v=1&cfg=json&year=now&month=now&d=on&o=on'); - const response = await result.json() - - const parsedData = response.items.reduce((rv, x) => { - (rv[x.date] = rv[x.date] || []).push(x); - return rv; - }, {}); - - let now = new Date(); - if (now.getHours() > 20) { - now.setDate(now.getDate() + 1); - } - - now = now.toISOString().substr(0, 10); - - const fields = parsedData[now]; - container.querySelector('._cal_val').innerHTML = ""; - - fields.forEach(field => { - const fieldContainer = document.createElement('div'); - fieldContainer.classList.add('has-font-size-larger', 'h-fw-bold', 'h-m-t-2'); - container.querySelector('._cal_val').appendChild(fieldContainer); - - fieldContainer.innerText = field.title; - }); - - const DAY_LIST = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; - - let today = new Date(); - if (today.getHours() > 20) { - today.setDate(today.getDate() + 1); - } - - const todayDay = DAY_LIST[(today.getDay() + 6) % 7]; - today.setDate(today.getDate() - 1); - const yesterdayDay = DAY_LIST[(today.getDay() + 6) % 7]; - - const fieldContainer = document.createElement('div'); - fieldContainer.classList.add('h-m-t-2', 'has-font-size-caption'); - container.querySelector('._cal_val').appendChild(fieldContainer); - - fieldContainer.innerText = yesterdayDay + ' night (' + todayDay + ')'; -}); \ No newline at end of file From 6310c6061c4ddae196b7576ed4d5a345e6c38c67 Mon Sep 17 00:00:00 2001 From: Monica Cellio Date: Tue, 1 Jun 2021 23:48:27 -0400 Subject: [PATCH 3/4] addressed review comments --- public/assets/community/judaism.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/public/assets/community/judaism.js b/public/assets/community/judaism.js index 05c8fed76..cac143373 100644 --- a/public/assets/community/judaism.js +++ b/public/assets/community/judaism.js @@ -528,8 +528,9 @@ $(() => { * Calendar script, added 2021-04-01 by @luap42 */ - // Use *local* time for ISO string (Date.toISOString() uses UTC). - function toIsoString(date) { + // Use *local* time for ISO string (Date.toISOString() uses UTC). + // We only need the date here, so punting on time. + function toIsoDate(date) { var tzo = -date.getTimezoneOffset(), dif = tzo >= 0 ? '+' : '-', pad = function(num) { @@ -539,12 +540,7 @@ $(() => { return date.getFullYear() + '-' + pad(date.getMonth() + 1) + - '-' + pad(date.getDate()) + - 'T' + pad(date.getHours()) + - ':' + pad(date.getMinutes()) + - ':' + pad(date.getSeconds()) + - dif + pad(tzo / 60) + - ':' + pad(tzo % 60); + '-' + pad(date.getDate()); } @@ -559,7 +555,7 @@ window.addEventListener("load", async () => { let todayDate = new Date(); // Start new day at 8pm local time previous day (if it's 8 or later today is tomorrow). - // Day is zero-indexed, hence > 19 (not > 20). + // Hour is zero-indexed, hence > 19 (not > 20). if (todayDate.getHours() > 19) { todayDate.setDate(todayDate.getDate() + 1); } @@ -574,7 +570,7 @@ window.addEventListener("load", async () => { }, {}); // Do not use Date.toISOString(), which uses UTC not local time. - let now = toIsoString(todayDate).substr(0, 10); + let now = toIsoDate(todayDate); const fields = parsedData[now]; container.querySelector('._cal_val').innerHTML = ""; From 3bd1b9c7897310d2544fea8e8ad72c90922c2de9 Mon Sep 17 00:00:00 2001 From: Monica Cellio Date: Wed, 2 Jun 2021 10:58:21 -0400 Subject: [PATCH 4/4] removed dead code --- public/assets/community/judaism.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/public/assets/community/judaism.js b/public/assets/community/judaism.js index cac143373..c6e963890 100644 --- a/public/assets/community/judaism.js +++ b/public/assets/community/judaism.js @@ -531,9 +531,7 @@ $(() => { // Use *local* time for ISO string (Date.toISOString() uses UTC). // We only need the date here, so punting on time. function toIsoDate(date) { - var tzo = -date.getTimezoneOffset(), - dif = tzo >= 0 ? '+' : '-', - pad = function(num) { + var pad = function(num) { var norm = Math.floor(Math.abs(num)); return (norm < 10 ? '0' : '') + norm; };