util: Use GLib.DateTime in formatTime()

The world clock uses GLib.DateTime instead of the built-in Date type
because of the much superior timezone support, and therefore cannot
use the new formatTime() helper. To make this possible, modify the
method to support a parameter of either type.

https://bugzilla.gnome.org/show_bug.cgi?id=745111
This commit is contained in:
Florian Müllner 2015-02-25 20:25:04 +01:00
parent 1c36ade125
commit 60f6715228

View File

@ -161,17 +161,26 @@ function _handleSpawnError(command, err) {
Main.notifyError(title, err.message); Main.notifyError(title, err.message);
} }
function formatTime(date, params) { function formatTime(time, params) {
let now = new Date(); let date;
// HACK: The built-in Date type sucks at timezones, which we need for the
// world clock; it's often more convenient though, so allow either
// Date or GLib.DateTime as parameter
if (time instanceof Date)
date = GLib.DateTime.new_from_unix_local(time.getTime() / 1000);
else
date = time;
let daysAgo = (now.getTime() - date.getTime()) / (24 * 60 * 60 * 1000); let now = GLib.DateTime.new_now_local();
let daysAgo = now.difference(date) / (24 * 60 * 60 * 1000 * 1000);
let format; let format;
if (_desktopSettings == null) if (_desktopSettings == null)
_desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' }); _desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
let clockFormat = _desktopSettings.get_string('clock-format'); let clockFormat = _desktopSettings.get_string('clock-format');
let hasAmPm = date.toLocaleFormat('%p') != ''; let hasAmPm = date.format('%p') != '';
params = Params.parse(params, { timeOnly: false }); params = Params.parse(params, { timeOnly: false });
@ -192,7 +201,7 @@ function formatTime(date, params) {
string in 24h format. i.e. "Monday, 14:30" */ string in 24h format. i.e. "Monday, 14:30" */
// xgettext:no-c-format // xgettext:no-c-format
format = N_("%A, %H\u2236%M"); format = N_("%A, %H\u2236%M");
else if (date.getYear() == now.getYear()) else if (date.get_year() == now.get_year())
/* Translators: this is the month name and day number /* Translators: this is the month name and day number
followed by a time string in 24h format. followed by a time string in 24h format.
i.e. "May 25, 14:30" */ i.e. "May 25, 14:30" */
@ -221,7 +230,7 @@ function formatTime(date, params) {
string in 12h format. i.e. "Monday, 2:30 pm" */ string in 12h format. i.e. "Monday, 2:30 pm" */
// xgettext:no-c-format // xgettext:no-c-format
format = N_("%A, %l\u2236%M %p"); format = N_("%A, %l\u2236%M %p");
else if (date.getYear() == now.getYear()) else if (date.get_year() == now.get_year())
/* Translators: this is the month name and day number /* Translators: this is the month name and day number
followed by a time string in 12h format. followed by a time string in 12h format.
i.e. "May 25, 2:30 pm" */ i.e. "May 25, 2:30 pm" */
@ -234,7 +243,7 @@ function formatTime(date, params) {
// xgettext:no-c-format // xgettext:no-c-format
format = N_("%B %d %Y, %l\u2236%M %p"); format = N_("%B %d %Y, %l\u2236%M %p");
} }
return date.toLocaleFormat(Shell.util_translate_time_string(format)); return date.format(Shell.util_translate_time_string(format));
} }
// lowerBound: // lowerBound: