10

I have a pretty specific problem, that I have had no luck trying to find a working solution to. I have a laptop running Ubuntu 18.04 docked using 2 external monitors resulting in 3 screens. I want to have my top bar on all 3 screens to easily look up on the current screen to see the time. I enable this using the Multi Monitors Add-On Gnome extension. I am running Ubuntu in English, but set to Swedish formats. This makes for a very ugly and incorrect date format in the top bar.

Image of top bar clock

This is pretty much English format with Swedish names for days and months. On a side note, this localization "bug" has been fixed and is ready for an upcoming gnome release.

My main issue is that I want to change the format of the date displayed to something like:

Image of desired format

I can easily get this format using any of the many clock formatting Gnome extensions (Clock Override, Datetime Format). However, none of these extensions work with my Multi Monitors Add-On, leaving me with a nicely formatted clock on the main monitor, but ugly formatted clocks on the other two screens. I have tried getting the developers of these addons to collaborate, but apparently dependencies between gnome extensions is "hell".

I have also tried editing the locale files directly, both the en_US and sv_SE. Specifically the LC_TIME d_t_fmt format for date and time strings, but this seems to have no impact on the clock format.

In a last attempt I also unpacked the libgnome-shell.so file located in /usr/lib/gnome-shell/ to try and edit the JavaScript files used to display the clock, using this answer. I had a look in dateMenu.js and calendar.js and changed most of the format strings that I could find to my desired format %Y-%m-%d. I then ran GNOME_SHELL_JS=$HOME/gnome-shell-js gnome-shell --replace, but again it seemed to have no effect. To be fair, I am in deep waters here and could definitely have failed in my attempts. As such, the solution could still be in this method.

Have anyone had any experience with a situation like this? Changing the format of date and time displayed on multiple monitors using Ubuntu 18.04? Feel free to try what I have tried already as I can certainly have missed something that could have made it work.

1
13

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 (%).
12
  • 1
    Oh, my, God! Thank you! My diggin' stalled weeks ago and I haven't really bothered with it since. Never in a million years thought that I would get an elaborate and all around great answer like this. Thanks a lot man!
    – Skillzore
    Feb 18 '19 at 14:01
  • 1
    @Steve, you are a genius Sir... thank you SO much, this has been bugging me for months. The Gnome folks should have wrapped this into core tweaks a LONG time ago... this is a very creative work-around... props indeed! Apr 6 '19 at 13:10
  • 1
    @Steve, you say "Gnome wall clock uses a file with the specific name of gnome-desktop-3.0.mo", can you point me to where this is referenced or inherited in the source code please. May 2 '19 at 6:49
  • 1
    I didn't need to dig that deep. In the internationalization guide it provides guidance on what to try...
    – Steve
    May 3 '19 at 14:20
  • 1
    "...This should be placed in the /usr/share/locale/XX/LC_MESSAGES/ directory (where XX is the code for your locale) and given the name of the program. Examples might be metacity.mo or nautilus.mo. Sometimes the program name requires a version number on it. Unfortunately, there is no easy way to work out what the name of the .mo should be for a given package. You will need to look at already-existing filenames in a sister directory to see what they use and copy those. Have a look around the different locales in /usr/share/locale/ and you should find good examples to copy."
    – Steve
    May 3 '19 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.