summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeemu Ikonen <tpikonen@mailbox.org>2021-12-27 00:37:53 +0200
committerTeemu Ikonen <tpikonen@mailbox.org>2023-06-17 17:59:33 +0300
commitcedeec4ec52881ecb76982905af36fac4f502148 (patch)
treee560da49e6c88647d1ab5896a9b0ca6c1ff3bdf0
parent1c16afc67cefe7dc448f8cedb2c4377d82ee70c8 (diff)
nmea-utils: Add function gclue_nmea_timestamp_to_timespan
-rw-r--r--src/gclue-nmea-utils.c36
-rw-r--r--src/gclue-nmea-utils.h1
2 files changed, 37 insertions, 0 deletions
diff --git a/src/gclue-nmea-utils.c b/src/gclue-nmea-utils.c
index b38196d..ed5aa18 100644
--- a/src/gclue-nmea-utils.c
+++ b/src/gclue-nmea-utils.c
@@ -36,3 +36,39 @@ gclue_nmea_type_is (const char *msg, const char *nmeatype)
g_str_has_prefix (msg+3, nmeatype);
}
+/**
+ * gclue_nmea_timestamp_to_timespan
+ * @timestamp: NMEA timestamp string
+ *
+ * Parse the NMEA timestamp string, which is a field in NMEA sentences
+ * like GGA and RMC.
+ *
+ * Returns: a GTimeSpan (gint64) value of microseconds since midnight,
+ * or -1, if reading fails.
+ */
+GTimeSpan
+gclue_nmea_timestamp_to_timespan (const gchar *timestamp)
+{
+ gint its, hours, minutes;
+ gdouble ts, seconds_f;
+ gchar *endptr;
+
+ if (!timestamp || !*timestamp)
+ return -1;
+
+ ts = g_ascii_strtod (timestamp, &endptr);
+ if (endptr != timestamp + g_utf8_strlen(timestamp, 12) ||
+ ts < 0.0 ||
+ ts >= 235960.0)
+ return -1;
+
+ its = (gint) ts; /* Truncate towards zero */
+ hours = its / 10000;
+ minutes = (its - 10000 * hours) / 100;
+ seconds_f = ts - 10000 * hours - 100 * minutes; /* Seconds plus fraction */
+
+ return (GTimeSpan) G_USEC_PER_SEC * (3600 * hours +
+ 60 * minutes +
+ seconds_f);
+}
+
diff --git a/src/gclue-nmea-utils.h b/src/gclue-nmea-utils.h
index 618dda7..fd51454 100644
--- a/src/gclue-nmea-utils.h
+++ b/src/gclue-nmea-utils.h
@@ -24,6 +24,7 @@
G_BEGIN_DECLS
gboolean gclue_nmea_type_is (const char *msg, const char *nmeatype);
+GTimeSpan gclue_nmea_timestamp_to_timespan (const gchar *timestamp);
G_END_DECLS