[gnome-shell] Work around Spidermonkey problem with Unicode date formats



commit 7ad89dc46b5307ecb17c1eba133b410eb010aee1
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Sat Mar 12 19:41:23 2011 -0500

    Work around Spidermonkey problem with Unicode date formats
    
    Monkey-patch Date.prototype.toLocaleFormat() with a version that uses
    g_date_time_format() since the Spidermonkey built-in can't handle
    format strings with Unicode characters.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=643350

 js/ui/environment.js |    5 +++++
 src/shell-util.c     |   39 +++++++++++++++++++++++++++++++++++++++
 src/shell-util.h     |    3 +++
 3 files changed, 47 insertions(+), 0 deletions(-)
---
diff --git a/js/ui/environment.js b/js/ui/environment.js
index e832262..9061a77 100644
--- a/js/ui/environment.js
+++ b/js/ui/environment.js
@@ -64,6 +64,11 @@ function init() {
     Tweener.init();
     String.prototype.format = Format.format;
 
+    // Work around https://bugzilla.mozilla.org/show_bug.cgi?id=508783
+    Date.prototype.toLocaleFormat = function(format) {
+        return Shell.util_format_date(format, this.getTime());
+    };
+
     // Set the default direction for St widgets (this needs to be done before any use of St)
     if (Gettext_gtk30.gettext('default:LTR') == 'default:RTL') {
         St.Widget.set_default_direction(St.TextDirection.RTL);
diff --git a/src/shell-util.c b/src/shell-util.c
index 86cd3a4..d5aec13 100644
--- a/src/shell-util.c
+++ b/src/shell-util.c
@@ -439,3 +439,42 @@ shell_util_set_hidden_from_pick (ClutterActor *actor,
       g_object_set_data (G_OBJECT (actor), "shell-stop-pick", NULL);
     }
 }
+
+/**
+ * shell_util_format_date:
+ * @format: a strftime-style string format, as parsed by
+ *   g_date_time_format()
+ * @time_ms: milliseconds since 1970-01-01 00:00:00 UTC; the
+ *   value returned by Date.getTime()
+ *
+ * Formats a date for the current locale. This should be
+ * used instead of the Spidermonkey Date.toLocaleFormat()
+ * extension because Date.toLocaleFormat() is buggy for
+ * Unicode format strings:
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=508783
+ *
+ * Return value: the formatted date. If the date is
+ *  outside of the range of a GDateTime (which contains
+ *  any plausible dates we actually care about), will
+ *  return an empty string.
+ */
+char *
+shell_util_format_date (const char *format,
+                        gint64      time_ms)
+{
+  GDateTime *datetime;
+  GTimeVal tv;
+  char *result;
+
+  tv.tv_sec = time_ms / 1000;
+  tv.tv_usec = (time_ms % 1000) * 1000;
+
+  datetime = g_date_time_new_from_timeval_local (&tv);
+  if (!datetime) /* time_ms is out of range of GDateTime */
+    return g_strdup ("");
+
+  result = g_date_time_format (datetime, format);
+
+  g_date_time_unref (datetime);
+  return result;
+}
diff --git a/src/shell-util.h b/src/shell-util.h
index e655905..e4914c3 100644
--- a/src/shell-util.h
+++ b/src/shell-util.h
@@ -13,6 +13,9 @@ GIcon *shell_util_get_icon_for_uri (const char *text_uri);
 GIcon *shell_util_icon_from_string (const char *string, GError **error);
 void   shell_util_set_hidden_from_pick (ClutterActor *actor, gboolean hidden);
 
+char *shell_util_format_date (const char *format,
+                              gint64      time_ms);
+
 G_END_DECLS
 
 #endif /* __SHELL_UTIL_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]