How to display custom formatted clock in top bar on multiple displays in Ubuntu 18.04?
Solution 1:
BLUF: This is a permanent and clean way to alter formatting of the Gnome top bar clock to your liking without the use of extensions.
I also wanted to display a custom-formatted string for the clock in the Gnome top bar but with just a single display. However, this answer will extend to your problem as well and allow you to use just the multi-monitor top bar extension. My goal was to simply do this without any extension in my case so the custom time format would be seen consistently at the initial gdm login screen upon boot, while in active logged-in sessions, and on the session unlock screen. It took some digging but the key lies in how Gnome handles localization/internationalization and how the Gnome wall clock, as it's called, obtains the string to actually display in the top bar.
First, a bit of background on the use of .po and .mo files in Gnome localization/internationalization can be found here:
https://wiki.gnome.org/TranslationProject/LocalisationGuide
Basically, at runtime, Gnome wall clock takes the current time and applies localization/internationalization string formatting. It uses a string key to look up a formatting string to use for a given locale to determine what to actually display -- it does not rely on the locale file formatting strings. If you modify the value returned for a given key in the localization/internationalization file, the clock display will reflect the change. Localization/internationalization uses two files. The first is the human-readable .po file that translators populate for each locale and this is where the mapping from lookup keys to formatting strings can be found. The second is the .mo file that is generated from the .po file... more on this and how it relates in just a bit...
Below is a link to the source for the Gnome wall clock version used in Ubuntu 18.04:
https://github.com/GNOME/gnome-desktop/blob/gnome-3-28/libgnome-desktop/gnome-wall-clock.c
If you examine the function 'gnome_wall_clock_string_for_datetime' beginning at line 261, you'll see the localization/internationalization lookup keys in use. There are several. Ubuntu has various display settings for the wall clock depending on whether you want to display just the time, time with day, time with day and date, etc. Each of these corresponds to a given localization/internationalization lookup key that wall clock uses. What you see in quotations below is not the formatting string itself but rather the key to look up the actual formatting string. The _() that surrounds each key performs the lookup:
char *
gnome_wall_clock_string_for_datetime (GnomeWallClock *self,
GDateTime *now,
GDesktopClockFormat clock_format,
gboolean show_weekday,
gboolean show_full_date,
gboolean show_seconds)
{
const char *format_string;
if (clock_format == G_DESKTOP_CLOCK_FORMAT_24H) {
if (show_full_date) {
/* Translators: This is the time format with full date used
in 24-hour mode. */
format_string = show_seconds ? _("%a %b %e, %R:%S")
: _("%a %b %e, %R");
} else if (show_weekday) {
/* Translators: This is the time format with day used
in 24-hour mode. */
format_string = show_seconds ? _("%a %R:%S")
: _("%a %R");
} else {
/* Translators: This is the time format without date used
in 24-hour mode. */
format_string = show_seconds ? _("%R:%S") : _("%R");
}
} else {
if (show_full_date) {
/* Translators: This is a time format with full date used
for AM/PM. */
format_string = show_seconds ? _("%a %b %e, %l:%M:%S %p")
: _("%a %b %e, %l:%M %p");
} else if (show_weekday) {
/* Translators: This is a time format with day used
for AM/PM. */
format_string = show_seconds ? _("%a %l:%M:%S %p")
: _("%a %l:%M %p");
} else {
/* Translators: This is a time format without date used
for AM/PM. */
format_string = show_seconds ? _("%l:%M:%S %p")
: _("%l:%M %p");
}
}
return date_time_format (now, format_string);
}
For instance, I wanted a custom format string for displaying day, and date, and time in 12 hour format. That is in Ubuntu, I had set the clock to display the day, date, and time in 12 hour format which corresponds to the 'full date' in the above code. Examining the function, I was able to determine that this 'full date' lookup key occurred at line 288:
/* Translators: This is a time format with full date used
for AM/PM. */
format_string = show_seconds ? _("%a %b %e, %l:%M:%S %p")
: _("%a %b %e, %l:%M %p");
I wasn't interested in seconds so the key I needed to find in the localization/internationalization .po file was:
'%a %b %e, %l:%M %p'
The localization/internationalization .mo files are binary in format but easily generated from a human-readable plain-text mapping .po files that maps a given lookup key to formatting string. Gnome wall clock uses a file with the specific name of gnome-desktop-3.0.mo for localization/internationalization. It was not present on my system under the prescribed location for any locale:
/usr/share/locale/XX/LC_MESSAGES
So, I didn't need to worry about replacing it but rather creating it anew. Regardless, you need to first obtain the .po file for your locale:
https://github.com/GNOME/gnome-desktop/tree/gnome-3-28/po
Next, find the above mentioned specific key(or the key for which you want to change the formatting string based on your clock setting in Ubuntu) in the .po file. The string key is 'msgid' and the formatting string to be returned is 'msgstr'. For instance, the snippet from my .po file is(ignore their comment since the code lines don't actually match up):
#: ../libgnome-desktop/gnome-wall-clock.c:316
msgid "%a %b %e, %l:%M %p"
msgstr "%a %b %e, %l:%M %p"
After the modification:
#: ../libgnome-desktop/gnome-wall-clock.c:316
msgid "%a %b %e, %l:%M %p"
msgstr "%A %b %-d, %l:%M %p"
Prior to the change reflected in the above 'msgstr' it would have displayed:
Mon Feb 4, 12:22 PM
But after the above change the day is no longer abbreviated and the leading space in front of the date has been removed(see the end of this answer for the formatting specifiers):
Monday Feb 4, 12:22 PM
Next, take this .po file and run it through the following on the command line to obtain the messages.mo file where XX is your locale. The binary msgfmt is already installed on Ubuntu 18.04:
msgfmt -cv XX.po
Next, rename the resulting messages.mo file to gnome-desktop-3.0.mo and copy it to your specific locale LC_MESSAGE directory:
/usr/share/locale/XX/LC_MESSAGES/gnome-desktop-3.0.mo
Then, to effect the change, log out and log back in. If you find that the formatting did not change then double check that your current clock settings truly correspond to the key for which you changed the formatting string.
Finally, here are some formatting string specifiers to help you along. I borrowed these from another site so no guarantees that these all work in Gnome:
The % sign indicating a directive may be immediately followed by a padding modifier, e.g. %-d:
0 - zero-padding
_ - space-padding
- - disable padding
%a - abbreviated weekday name.*
%A - full weekday name.*
%b - abbreviated month name.*
%B - full month name.*
%d - zero-padded day of the month as a decimal number [01,31].
%e - space-padded day of the month as a decimal number [ 1,31]; equivalent to %_d.
%f - microseconds as a decimal number [000000, 999999].
%H - hour (24-hour clock) as a decimal number [00,23].
%I - hour (12-hour clock) as a decimal number [01,12].
%j - day of the year as a decimal number [001,366].
%m - month as a decimal number [01,12].
%M - minute as a decimal number [00,59].
%L - milliseconds as a decimal number [000, 999].
%p - either AM or PM.*
%Q - milliseconds since UNIX epoch.
%s - seconds since UNIX epoch.
%S - second as a decimal number [00,61].
%u - Monday-based (ISO 8601) weekday as a decimal number [1,7].
%U - Sunday-based week of the year as a decimal number [00,53].
%V - ISO 8601 week of the year as a decimal number [01, 53].
%w - Sunday-based weekday as a decimal number [0,6].
%W - Monday-based week of the year as a decimal number [00,53].
%x - the locale’s date, such as %-m/%-d/%Y.*
%X - the locale’s time, such as %-I:%M:%S %p.*
%y - year without century as a decimal number [00,99].
%Y - year with century as a decimal number.
%Z - time zone offset, such as -0700, -07:00, -07, or Z.
%% - a literal percent sign (%).