How to display custom formatted clock in top bar on multiple displays in Ubuntu 18.04?












1















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.










share|improve this question

























  • I now believe the solution could be in editing the dateMenu.js file in libgnome-shell.so since the Clock Override extension seem to be overriding some of dateMenu's children during runtime. I will try to look into this more, but help would be appreciated since my gnome-shell and JavaScript skills are somewhat subpar.

    – Skillzore
    Nov 27 '18 at 9:09
















1















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.










share|improve this question

























  • I now believe the solution could be in editing the dateMenu.js file in libgnome-shell.so since the Clock Override extension seem to be overriding some of dateMenu's children during runtime. I will try to look into this more, but help would be appreciated since my gnome-shell and JavaScript skills are somewhat subpar.

    – Skillzore
    Nov 27 '18 at 9:09














1












1








1


1






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.










share|improve this question
















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.







multiple-monitors gnome-shell time clock top-bar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 '18 at 7:05







Skillzore

















asked Nov 26 '18 at 15:14









SkillzoreSkillzore

134




134













  • I now believe the solution could be in editing the dateMenu.js file in libgnome-shell.so since the Clock Override extension seem to be overriding some of dateMenu's children during runtime. I will try to look into this more, but help would be appreciated since my gnome-shell and JavaScript skills are somewhat subpar.

    – Skillzore
    Nov 27 '18 at 9:09



















  • I now believe the solution could be in editing the dateMenu.js file in libgnome-shell.so since the Clock Override extension seem to be overriding some of dateMenu's children during runtime. I will try to look into this more, but help would be appreciated since my gnome-shell and JavaScript skills are somewhat subpar.

    – Skillzore
    Nov 27 '18 at 9:09

















I now believe the solution could be in editing the dateMenu.js file in libgnome-shell.so since the Clock Override extension seem to be overriding some of dateMenu's children during runtime. I will try to look into this more, but help would be appreciated since my gnome-shell and JavaScript skills are somewhat subpar.

– Skillzore
Nov 27 '18 at 9:09





I now believe the solution could be in editing the dateMenu.js file in libgnome-shell.so since the Clock Override extension seem to be overriding some of dateMenu's children during runtime. I will try to look into this more, but help would be appreciated since my gnome-shell and JavaScript skills are somewhat subpar.

– Skillzore
Nov 27 '18 at 9:09










1 Answer
1






active

oldest

votes


















0














I also wanted to display a custom-formatted string for the clock in the Gnome top bar without the use of extensions. 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 the the 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 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 modified 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 %-d, %l:%M %p"


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 (%).




share










New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096203%2fhow-to-display-custom-formatted-clock-in-top-bar-on-multiple-displays-in-ubuntu%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I also wanted to display a custom-formatted string for the clock in the Gnome top bar without the use of extensions. 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 the the 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 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 modified 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 %-d, %l:%M %p"


    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 (%).




    share










    New contributor




    Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      I also wanted to display a custom-formatted string for the clock in the Gnome top bar without the use of extensions. 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 the the 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 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 modified 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 %-d, %l:%M %p"


      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 (%).




      share










      New contributor




      Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        I also wanted to display a custom-formatted string for the clock in the Gnome top bar without the use of extensions. 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 the the 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 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 modified 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 %-d, %l:%M %p"


        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 (%).




        share










        New contributor




        Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        I also wanted to display a custom-formatted string for the clock in the Gnome top bar without the use of extensions. 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 the the 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 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 modified 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 %-d, %l:%M %p"


        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 (%).





        share










        New contributor




        Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share


        share








        edited 1 min ago





















        New contributor




        Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 6 mins ago









        SteveSteve

        213




        213




        New contributor




        Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096203%2fhow-to-display-custom-formatted-clock-in-top-bar-on-multiple-displays-in-ubuntu%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            GameSpot

            日野市

            Tu-95轟炸機