ugps: fix 64-bit time_t
[project/ugps.git] / nmea.c
diff --git a/nmea.c b/nmea.c
index 4a49528130b70dea4ddea13eb41845f5ac42f022..1204c20f61eb444bf0b58b2f95c14d807c527307 100644 (file)
--- a/nmea.c
+++ b/nmea.c
@@ -18,6 +18,7 @@
 
 #define _DEFAULT_SOURCE
 #define _XOPEN_SOURCE
+#define _BSD_SOURCE
 #include <time.h>
 
 #include <sys/types.h>
@@ -89,6 +90,10 @@ nmea_rmc_cb(void)
                &tm.tm_mday, &tm.tm_mon, &tm.tm_year) != 3) {
                ERROR("failed to parse date '%s'\n", nmea_params[9].str);
        }
+       else if (tm.tm_year == 0) {
+               DEBUG(4, "waiting for valid date\n");
+               return;
+       }
        else {
                tm.tm_year += 100; /* year starts with 1900 */
                tm.tm_mon -= 1; /* month starts with 0 */
@@ -97,12 +102,14 @@ nmea_rmc_cb(void)
                DEBUG(3, "date: %s UTC\n", tmp);
 
                if (adjust_clock) {
-                       struct timeval tv = { timegm(&tm), 0 };
+                       time_t sec = timegm(&tm);
                        struct timeval cur;
 
                        gettimeofday(&cur, NULL);
 
-                       if (abs(cur.tv_sec - tv.tv_sec) > MAX_TIME_OFFSET) {
+                       if ((sec < 0) || (llabs(cur.tv_sec - sec) > MAX_TIME_OFFSET)) {
+                               struct timeval tv = { 0 };
+                               tv.tv_sec = sec;
                                if (++nmea_bad_time > MAX_BAD_TIME) {
                                        LOG("system time differs from GPS time by more than %d seconds. Using %s UTC as the new time\n", MAX_TIME_OFFSET, tmp);
                                        /* only set datetime if specified by command line argument! */
@@ -115,7 +122,7 @@ nmea_rmc_cb(void)
        }
 
        if (strlen(nmea_params[3].str) < 9 || strlen(nmea_params[5].str) < 10) {
-               ERROR("lat/lng have invalid string length %d<9, %d<10\n",
+               ERROR("lat/lng have invalid string length %zu<9, %zu<10\n",
                       strlen(nmea_params[3].str), strlen(nmea_params[5].str));
        } else {
                float minutes;
@@ -230,7 +237,8 @@ static void
 nmea_process(char *a)
 {
        char *csum;
-       int cnt, i;
+       int cnt;
+       unsigned int i;
 
        if (strncmp(a, "$GP", 3))
                return;