]> git.street.me.uk Git - andy/viking.git/commitdiff
Read/Write ISO date in GLib's way
authorGuilhem Bonnefille <guilhem.bonnefille@gmail.com>
Mon, 21 Apr 2008 21:04:03 +0000 (21:04 +0000)
committerGuilhem Bonnefille <guilhem.bonnefille@gmail.com>
Mon, 21 Apr 2008 21:04:03 +0000 (21:04 +0000)
Fix #1915121

ChangeLog
src/gpx.c

index 3208ed8dda7be395f03a7c8492671cc775c48c39..4d09e93a8abcf5f0a5e2dd46ab2f73e660e37267 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2008-04-21:
 Guilhem Bonnefille <guilhem.bonnefille@gmail.com>:
        * Bug #1915121: add an utility to reproduce (test/gpx2gpx)
+       * Fix #1915121: use GLib functions to handle ISO8601 dates
 
 2008-04-16:
 Tim Scofield <nospam546@comcast.net>:
index 66fa231b8afb6978bd88ffb9f3ea0d5ba8f53aeb..16ac2147ae8fe24b1b60c681a38345b62faa2cf4 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -33,8 +33,6 @@
 #include <glib.h>
 #include <math.h>
 
-#define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ"
-
 typedef enum {
         tt_unknown = 0,
 
@@ -249,7 +247,8 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
 
 static void gpx_end(VikTrwLayer *vtl, const char *el)
 {
-  static struct tm tm;
+  static GTimeVal tp_time;
+
   g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
 
   switch ( current_tag ) {
@@ -322,9 +321,8 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
        break;
 
      case tt_trk_trkseg_trkpt_time:
-
-       if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */
-         c_tp->timestamp = mktime(&tm);
+       if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
+         c_tp->timestamp = tp_time.tv_sec;
          c_tp->has_timestamp = TRUE;
        }
        g_string_erase ( c_cdata, 0, -1 );
@@ -640,7 +638,7 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
   FILE *f = context->file;
   static struct LatLon ll;
   gchar *s_lat,*s_lon, *s_alt;
-  static gchar time_buf[30];
+  gchar *time_iso8601;
   vik_coord_to_latlon ( &(tp->coord), &ll );
 
   if ( tp->newsegment )
@@ -665,19 +663,24 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
     fprintf ( f, "    <ele>%s</ele>\n", s_alt );
   g_free ( s_alt ); s_alt = NULL;
   
-  time_buf[0] = '\0';
+  time_iso8601 = NULL;
   if ( tp->has_timestamp ) {
-    time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0';
+    GTimeVal timestamp;
+    timestamp.tv_sec = tp->timestamp;
+  
+    time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
   }
   else if ( context->options != NULL && context->options->force_time )
   {
-    time_t rawtime;
-    time ( &rawtime );
+    GTimeVal current;
+    g_get_current_time ( &current );
   
-    time_buf [strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&rawtime)) ] ='\0';
+    time_iso8601 = g_time_val_to_iso8601 ( &current );
   }
-  if ( time_buf[0] != '\0' )
-    fprintf ( f, "    <time>%s</time>\n", time_buf );
+  if ( time_iso8601 != NULL )
+    fprintf ( f, "    <time>%s</time>\n", time_iso8601 );
+  g_free(time_iso8601);
+  time_iso8601 = NULL;
   
   if (tp->extended && (tp->fix_mode >= VIK_GPS_MODE_2D)) {
     if (!isnan(tp->course)) {