[gnome-weather/ewlsh/march-fixes-prerelease: 2/2] Ensure "Now" is the closest time column and is on the same day.
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-weather/ewlsh/march-fixes-prerelease: 2/2] Ensure "Now" is the closest time column and is on the same day.
- Date: Sat, 5 Mar 2022 07:02:11 +0000 (UTC)
commit 56c7e75754e04646912361fbe9b0f7df9b084268
Author: Evan Welsh <contact evanwelsh com>
Date: Fri Mar 4 22:53:58 2022 -0800
Ensure "Now" is the closest time column and is on the same day.
Fixes #217
src/app/hourlyForecast.js | 25 ++++++++++---------------
src/misc/util.js | 5 +++++
2 files changed, 15 insertions(+), 15 deletions(-)
---
diff --git a/src/app/hourlyForecast.js b/src/app/hourlyForecast.js
index a28123b..ce288a9 100644
--- a/src/app/hourlyForecast.js
+++ b/src/app/hourlyForecast.js
@@ -27,7 +27,8 @@ import Graphene from 'gi://Graphene';
import * as Util from '../misc/util.js';
// In microseconds
-const TWENTY_FOUR_HOURS = 24 * 3600 * 1000 * 1000;
+const ONE_HOUR = 60 * 60 * 1000 * 1000;
+const TWENTY_FOUR_HOURS = 24 * ONE_HOUR;
export class HourlyForecastBox extends Gtk.Box {
constructor() {
@@ -47,23 +48,19 @@ export class HourlyForecastBox extends Gtk.Box {
// Ensure that infos are sufficiently spaced, and
// remove infos for the wrong day
- _preprocess(now, infos) {
- let ret = [];
-
- for (let i = 0; i < infos.length; i++) {
- let info = infos[i];
-
+ _preprocess(now, forecastInfo, infos) {
+ const ret = [forecastInfo, ...infos].filter(info => {
let [, date] = info.get_value_update();
let datetime = GLib.DateTime.new_from_unix_utc(date).to_timezone(now.get_timezone());
if (datetime.difference(now) <= 0)
- continue;
+ return false;
if (datetime.difference(now) >= TWENTY_FOUR_HOURS)
- break;
+ return false;
- ret.push(info);
- }
+ return true;
+ });
return ret;
}
@@ -76,9 +73,7 @@ export class HourlyForecastBox extends Gtk.Box {
let tz = nearestCity.get_timezone();
let now = GLib.DateTime.new_now(tz);
- let hourlyInfo = this._preprocess(now, forecasts);
-
- hourlyInfo.unshift(info);
+ let hourlyInfo = this._preprocess(now, info, forecasts);
if (hourlyInfo.length > 0) {
for (let i = 0; i < hourlyInfo.length; i++) {
@@ -106,7 +101,7 @@ export class HourlyForecastBox extends Gtk.Box {
let [, date] = info.get_value_update();
let datetime = GLib.DateTime.new_from_unix_utc(date).to_timezone(tz);
- if (now.get_hour() == datetime.get_hour()) {
+ if (Util.isSameDay(now, datetime) && now.difference(datetime) < ONE_HOUR) {
timeLabel = _('Now');
} else {
let timeSetting = this._settings.get_string('clock-format');
diff --git a/src/misc/util.js b/src/misc/util.js
index 28207f0..5ff9e31 100644
--- a/src/misc/util.js
+++ b/src/misc/util.js
@@ -50,6 +50,10 @@ function arrayEqual(one, two) {
return one.every((a, i) => a === two[i]);
}
+function isSameDay(day, datetime) {
+ return arrayEqual(day.get_ymd(), datetime.get_ymd());
+}
+
function getSettings(schemaId) {
const schemaSource = Gio.SettingsSchemaSource.get_default();
const schemaObj = schemaSource.lookup(schemaId, true);
@@ -198,6 +202,7 @@ export {
getTempString,
getNight,
normalizeCasefoldAndUnaccent,
+ isSameDay,
arrayEqual,
getSettings,
getMorning,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]