Description: Display time on the greeter in accordance with the system wide settings in Language Support.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=651506
Bug-Ubuntu: https://launchpad.net/bugs/777264
Forwarded: yes
Author: Gunnar Hjalmarsson <ubuntu@gunnar.cc>

diff -Nur -x '*.orig' -x '*~' gdm-3.0.4/gui/simple-greeter/gdm-clock-widget.c gdm-3.0.4.new/gui/simple-greeter/gdm-clock-widget.c
--- gdm-3.0.4/gui/simple-greeter/gdm-clock-widget.c	2011-05-31 17:46:56.000000000 +0200
+++ gdm-3.0.4.new/gui/simple-greeter/gdm-clock-widget.c	2011-11-19 21:57:42.501647152 +0100
@@ -30,6 +30,8 @@
 #include <errno.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#include <locale.h>
+#include <langinfo.h>
 
 #include <glib.h>
 #include <glib/gi18n.h>
@@ -46,8 +48,6 @@
         char      *time_format;
         char      *tooltip_format;
         guint      update_clock_id;
-        guint      should_show_seconds : 1;
-        guint      should_show_date : 1;
 };
 
 static void     gdm_clock_widget_class_init  (GdmClockWidgetClass *klass);
@@ -57,62 +57,52 @@
 
 G_DEFINE_TYPE (GdmClockWidget, gdm_clock_widget, GTK_TYPE_ALIGNMENT)
 
+gboolean
+is_24h (void)
+{
+        static const char *formats_24h[] = {"%H", "%R", "%T", "%OH", "%k", NULL};
+        const char        *t_fmt = nl_langinfo(T_FMT);
+        int                i;
+
+        for (i = 0; formats_24h[i]; ++i) {
+                if (strstr (t_fmt, formats_24h[i]) != NULL) {
+                        return TRUE;
+                }
+        }
+        return FALSE;
+}
+
 static void
-update_time_format (GdmClockWidget *clock)
+update_clock_format (GdmClockWidget *clock)
 {
-        char       *clock_format;
+        char       *time_format;
         char       *tooltip_format;
 
-        if (clock->priv->should_show_date && clock->priv->should_show_seconds) {
-                /* translators: This is the time format to use when both
-                 * the date and time with seconds are being shown together.
-                 */
-                clock_format = _("%a %b %e, %l:%M:%S %p");
-                tooltip_format = NULL;
-        } else if (clock->priv->should_show_date && !clock->priv->should_show_seconds) {
-                /* translators: This is the time format to use when both
-                 * the date and time without seconds are being shown together.
-                 */
-                clock_format = _("%a %b %e, %l:%M %p");
-
-                tooltip_format = NULL;
-        } else if (!clock->priv->should_show_date && clock->priv->should_show_seconds) {
-                /* translators: This is the time format to use when there is
-                 * no date, just weekday and time with seconds.
-                 */
-                clock_format = _("%a %l:%M:%S %p");
-
-                /* translators: This is the time format to use for the date
-                 */
-                tooltip_format = "%x";
+        setlocale (LC_TIME, "");
+        if (is_24h ()) {
+                time_format = "%H:%M";
         } else {
-                /* translators: This is the time format to use when there is
-                 * no date, just weekday and time without seconds.
-                 */
-                clock_format = _("%a %l:%M %p");
-
-                tooltip_format = "%x";
+                time_format = "%l:%M %p";
         }
+        tooltip_format = "%x";
 
         g_free (clock->priv->time_format);
-        clock->priv->time_format = g_locale_from_utf8 (clock_format, -1, NULL, NULL, NULL);
+        clock->priv->time_format = g_locale_from_utf8 (time_format, -1, NULL, NULL, NULL);
 
         g_free (clock->priv->tooltip_format);
-
-        if (tooltip_format != NULL) {
-                clock->priv->tooltip_format = g_locale_from_utf8 (tooltip_format, -1, NULL, NULL, NULL);
-        } else {
-                clock->priv->tooltip_format = NULL;
-        }
+        clock->priv->tooltip_format = g_locale_from_utf8 (tooltip_format, -1, NULL, NULL, NULL);
 }
 
 static void
 update_clock (GtkLabel   *label,
-              const char *clock_format,
+              const char *time_format,
               const char *tooltip_format)
 {
         time_t     t;
         struct tm *tm;
+        char       displayed_time[32];
+        char      *msg_locale;
+        char       weekday[32];
         char       buf[256];
         char      *utf8;
         char      *markup;
@@ -123,27 +113,34 @@
                 g_warning ("Unable to get broken down local time");
                 return;
         }
-        if (strftime (buf, sizeof (buf), clock_format, tm) == 0) {
-                g_warning ("Couldn't format time: %s", clock_format);
-                strcpy (buf, "???");
+        if (strftime (displayed_time, sizeof (displayed_time), time_format, tm) == 0) {
+                g_warning ("Couldn't format time: %s", time_format);
+                strcpy (displayed_time, "???");
         }
+
+        if ((msg_locale = getenv ("LC_MESSAGES")) == NULL)
+                msg_locale = getenv ("LANG");
+        setlocale (LC_TIME, msg_locale);
+        if (strftime (weekday, sizeof (weekday), "%a", tm) == 0) {
+                g_warning ("Couldn't format weekday: %%a");
+                strcpy (weekday, "???");
+        }
+        sprintf (buf, "%s %s", weekday, displayed_time);
+
         utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
         markup = g_strdup_printf ("<b><span foreground=\"white\">%s</span></b>", utf8);
         gtk_label_set_markup (label, markup);
         g_free (markup);
         g_free (utf8);
 
-        if (tooltip_format != NULL) {
-                if (strftime (buf, sizeof (buf), tooltip_format, tm) == 0) {
-                        g_warning ("Couldn't format tooltip date: %s", tooltip_format);
-                        strcpy (buf, "???");
-                }
-                utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
-                gtk_widget_set_tooltip_text (GTK_WIDGET (label), utf8);
-                g_free (utf8);
-        } else {
-                gtk_widget_set_has_tooltip (GTK_WIDGET (label), FALSE);
+        setlocale (LC_TIME, "");
+        if (strftime (buf, sizeof (buf), tooltip_format, tm) == 0) {
+                g_warning ("Couldn't format tooltip date: %s", tooltip_format);
+                strcpy (buf, "???");
         }
+        utf8 = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
+        gtk_widget_set_tooltip_text (GTK_WIDGET (label), utf8);
+        g_free (utf8);
 }
 
 static void
@@ -160,11 +157,7 @@
 
         g_get_current_time (&tv);
         timeouttime = (G_USEC_PER_SEC - tv.tv_usec) / 1000 + 1;
-
-        /* timeout of one minute if we don't care about the seconds */
-        if (! clock->priv->should_show_seconds) {
-                timeouttime += 1000 * (59 - now % 60);
-        }
+        timeouttime += 1000 * (59 - now % 60);
 
         clock->priv->update_clock_id = g_timeout_add (timeouttime,
                                                       (GSourceFunc)update_timeout_cb,
@@ -281,7 +274,7 @@
         gtk_widget_show (widget->priv->label);
         gtk_box_pack_start (GTK_BOX (box), widget->priv->label, FALSE, FALSE, 0);
 
-        update_time_format (widget);
+        update_clock_format (widget);
         update_timeout_cb (widget);
 }
 
