dateMenu: Update timezone offsets when timezone changes

Adds a signal handler to update the timezone offsets whenever the
user changes the system timezone.

https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/2209


(cherry picked from commit 64a3ecf9b1)
This commit is contained in:
Bryan Dunsmore 2020-05-14 22:04:12 +00:00 committed by Florian Müllner
parent 40481314d2
commit e84d10bf87

View File

@ -94,6 +94,7 @@ class WorldClocksSection extends St.Button {
}); });
this._clock = new GnomeDesktop.WallClock(); this._clock = new GnomeDesktop.WallClock();
this._clockNotifyId = 0; this._clockNotifyId = 0;
this._tzNotifyId = 0;
this._locations = []; this._locations = [];
@ -166,8 +167,6 @@ class WorldClocksSection extends St.Button {
layout.attach(header, 0, 0, 2, 1); layout.attach(header, 0, 0, 2, 1);
this.label_actor = header; this.label_actor = header;
let localOffset = GLib.DateTime.new_now_local().get_utc_offset();
for (let i = 0; i < this._locations.length; i++) { for (let i = 0; i < this._locations.length; i++) {
let l = this._locations[i].location; let l = this._locations[i].location;
@ -180,21 +179,8 @@ class WorldClocksSection extends St.Button {
let time = new St.Label({ style_class: 'world-clocks-time' }); let time = new St.Label({ style_class: 'world-clocks-time' });
const utcOffset = this._getTimeAtLocation(l).get_utc_offset();
const offsetCurrentTz = utcOffset - localOffset;
const offsetHours = Math.abs(offsetCurrentTz) / GLib.TIME_SPAN_HOUR;
const offsetMinutes =
(Math.abs(offsetCurrentTz) % GLib.TIME_SPAN_HOUR) /
GLib.TIME_SPAN_MINUTE;
const prefix = offsetCurrentTz >= 0 ? '+' : '-';
const text = offsetMinutes === 0
? '%s%d'.format(prefix, offsetHours)
: '%s%d\u2236%d'.format(prefix, offsetHours, offsetMinutes);
const tz = new St.Label({ const tz = new St.Label({
style_class: 'world-clocks-timezone', style_class: 'world-clocks-timezone',
text,
x_align: Clutter.ActorAlign.END, x_align: Clutter.ActorAlign.END,
y_align: Clutter.ActorAlign.CENTER, y_align: Clutter.ActorAlign.CENTER,
}); });
@ -212,32 +198,65 @@ class WorldClocksSection extends St.Button {
layout.attach(tz, 2, i + 1, 1, 1); layout.attach(tz, 2, i + 1, 1, 1);
} }
this._locations[i].actor = time; this._locations[i].timeLabel = time;
this._locations[i].tzLabel = tz;
} }
if (this._grid.get_n_children() > 1) { if (this._grid.get_n_children() > 1) {
if (!this._clockNotifyId) { if (!this._clockNotifyId) {
this._clockNotifyId = this._clockNotifyId =
this._clock.connect('notify::clock', this._updateLabels.bind(this)); this._clock.connect('notify::clock', this._updateTimeLabels.bind(this));
} }
this._updateLabels(); if (!this._tzNotifyId) {
this._tzNotifyId =
this._clock.connect('notify::timezone', this._updateTimezoneLabels.bind(this));
}
this._updateTimeLabels();
this._updateTimezoneLabels();
} else { } else {
if (this._clockNotifyId) if (this._clockNotifyId)
this._clock.disconnect(this._clockNotifyId); this._clock.disconnect(this._clockNotifyId);
this._clockNotifyId = 0; this._clockNotifyId = 0;
if (this._tzNotifyId)
this._clock.disconnect(this._tzNotifyId);
this._tzNotifyId = 0;
} }
} }
_getTimezoneOffsetAtLocation(location) {
const localOffset = GLib.DateTime.new_now_local().get_utc_offset();
const utcOffset = this._getTimeAtLocation(location).get_utc_offset();
const offsetCurrentTz = utcOffset - localOffset;
const offsetHours = Math.abs(offsetCurrentTz) / GLib.TIME_SPAN_HOUR;
const offsetMinutes =
(Math.abs(offsetCurrentTz) % GLib.TIME_SPAN_HOUR) /
GLib.TIME_SPAN_MINUTE;
const prefix = offsetCurrentTz >= 0 ? '+' : '-';
const text = offsetMinutes === 0
? '%s%d'.format(prefix, offsetHours)
: '%s%d\u2236%d'.format(prefix, offsetHours, offsetMinutes);
return text;
}
_getTimeAtLocation(location) { _getTimeAtLocation(location) {
let tz = GLib.TimeZone.new(location.get_timezone().get_tzid()); let tz = GLib.TimeZone.new(location.get_timezone().get_tzid());
return GLib.DateTime.new_now(tz); return GLib.DateTime.new_now(tz);
} }
_updateLabels() { _updateTimeLabels() {
for (let i = 0; i < this._locations.length; i++) { for (let i = 0; i < this._locations.length; i++) {
let l = this._locations[i]; let l = this._locations[i];
let now = this._getTimeAtLocation(l.location); let now = this._getTimeAtLocation(l.location);
l.actor.text = Util.formatTime(now, { timeOnly: true }); l.timeLabel.text = Util.formatTime(now, { timeOnly: true });
}
}
_updateTimezoneLabels() {
for (let i = 0; i < this._locations.length; i++) {
let l = this._locations[i];
l.tzLabel.text = this._getTimezoneOffsetAtLocation(l.location);
} }
} }