From f4c3e412a2103d50555cfbde62453eae1fffde10 Mon Sep 17 00:00:00 2001 From: Denis Treskunov Date: Sun, 9 Feb 2020 21:59:51 -0800 Subject: [PATCH] fix bugs in showMoonTimes in clock module 1. as reported on https://github.com/MichMich/MagicMirror/pull/1885, toLocaleString is not supported on old iPad Safari 2. SunCalc.getMoonTimes returns moonRise/moonSet times for the given date, so moonRise can be after moonSet. We want to display the most relevant times, which are today's moonRise and the next moonSet. --- modules/default/clock/clock.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/default/clock/clock.js b/modules/default/clock/clock.js index 96c31143..6e70f1e2 100644 --- a/modules/default/clock/clock.js +++ b/modules/default/clock/clock.js @@ -179,11 +179,19 @@ Module.register("clock",{ if (this.config.showMoonTimes) { const moonIllumination = SunCalc.getMoonIllumination(now.toDate()); const moonTimes = SunCalc.getMoonTimes(now, this.config.lat, this.config.lon); - const isVisible = now.isBetween(moonTimes.rise, moonTimes.set); - const illuminatedFractionString = moonIllumination.fraction.toLocaleString(undefined, {style: 'percent'}); + const moonRise = moonTimes.rise; + var moonSet; + if (moment(moonTimes.set).isAfter(moonTimes.rise)) { + moonSet = moonTimes.set; + } else { + const nextMoonTimes = SunCalc.getMoonTimes(now.clone().add(1, 'day'), this.config.lat, this.config.lon); + moonSet = nextMoonTimes.set; + } + const isVisible = now.isBetween(moonRise, moonSet) || moonTimes.alwaysUp === true; + const illuminatedFractionString = moonIllumination.fraction.toFixed(2) * 100 + '%'; moonWrapper.innerHTML = ' ' + illuminatedFractionString + '' + - ' ' + formatTime(this.config, moonTimes.rise) + ''+ - ' ' + formatTime(this.config, moonTimes.set) + ''; + ' ' + (moonRise ? formatTime(this.config, moonRise) : '...') + ''+ + ' ' + (moonSet ? formatTime(this.config, moonSet) : '...') + ''; } /****************************************************************