[glib/wip/rancell/iso8601: 1/2] GTimeZone: Support the Unicode minus character



commit 2171cdc43158ee8e601cba400227b511660aed82
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Aug 25 11:55:10 2016 +1200

    GTimeZone: Support the Unicode minus character

 glib/gtimezone.c       |   39 ++++++++++++++++++++++++++++-----------
 glib/tests/gdatetime.c |    6 ++++++
 2 files changed, 34 insertions(+), 11 deletions(-)
---
diff --git a/glib/gtimezone.c b/glib/gtimezone.c
index 2e2369c..39a398d 100644
--- a/glib/gtimezone.c
+++ b/glib/gtimezone.c
@@ -14,7 +14,8 @@
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
- * Author: Ryan Lortie <desrt desrt ca>
+ * Authors: Ryan Lortie <desrt desrt ca>
+ *          Robert Ancell <robert ancell canonical com>
  */
 
 /* Prologue {{{1 */
@@ -337,6 +338,13 @@ parse_time (const gchar *time_,
   return *time_ == '\0';
 }
 
+/* Check if text has Unicode minus symbol prefix (−) */
+static gboolean
+has_minus_prefix (const gchar *text)
+{
+    return (guint8) text[0] == 0xe2 && (guint8) text[1] == 0x88 && (guint8) text[2] == 0x92;
+}
+
 static gboolean
 parse_constant_offset (const gchar *name,
                        gint32      *offset)
@@ -347,28 +355,37 @@ parse_constant_offset (const gchar *name,
       return TRUE;
     }
 
-  if (*name >= '0' && '9' >= *name)
+  if (name[0] >= '0' && '9' >= name[0])
     return parse_time (name, offset);
 
-  switch (*name++)
+  if (name[0] == 'Z')
     {
-    case 'Z':
       *offset = 0;
-      return !*name;
+      return name[1] == '\0';
+    }
 
-    case '+':
-      return parse_time (name, offset);
+  else if (name[0] == '+')
+    return parse_time (name + 1, offset);
 
-    case '-':
-      if (parse_time (name, offset))
+  else if (name[0] == '-')
+    {
+      if (parse_time (name + 1, offset))
         {
           *offset = -*offset;
           return TRUE;
         }
+    }
 
-    default:
-      return FALSE;
+  else if (has_minus_prefix (name))
+    {
+      if (parse_time (name + 3, offset))
+        {
+          *offset = -*offset;
+          return TRUE;
+        }
     }
+
+  return FALSE;
 }
 
 static void
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index f6c3cf0..e2039bf 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -222,6 +222,12 @@ test_GDateTime_equal (void)
   g_assert (g_date_time_equal (dt1, dt2));
   g_date_time_unref (dt1);
   g_date_time_unref (dt2);
+
+  /* Check Unicode minus character */
+  tz = g_time_zone_new ("−03:00");
+  dt1 = g_date_time_new (tz, 2010, 5, 24,  8, 0, 0);
+  g_time_zone_unref (tz);
+  g_assert_cmpint (g_date_time_get_utc_offset (dt1) / G_USEC_PER_SEC, ==, (-3 * 3600));
 }
 
 static void


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