Переглянути джерело

minmea_gettimeofday -> minmea_gettime

Kosma Moczek 11 роки тому
батько
коміт
b777fbe0b9
3 змінених файлів з 12 додано та 15 видалено
  1. 3 3
      minmea.c
  2. 1 4
      minmea.h
  3. 8 8
      tests.c

+ 3 - 3
minmea.c

@@ -517,7 +517,7 @@ bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
     return true;
 }
 
-int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time_)
+int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_)
 {
     if (date->year == -1 || time_->hours == -1)
         return -1;
@@ -533,8 +533,8 @@ int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, cons
 
     time_t timestamp = timegm(&tm); /* See README.md if your system lacks timegm(). */
     if (timestamp != -1) {
-        tv->tv_sec = timestamp;
-        tv->tv_usec = time_->microseconds;
+        ts->tv_sec = timestamp;
+        ts->tv_nsec = time_->microseconds * 1000;
         return 0;
     } else {
         return -1;

+ 1 - 4
minmea.h

@@ -9,8 +9,6 @@
 #ifndef MINMEA_H
 #define MINMEA_H
 
-#define _BSD_SOURCE
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -21,7 +19,6 @@ extern "C" {
 #include <errno.h>
 #include <time.h>
 #include <math.h>
-#include <sys/time.h>
 
 #define MINMEA_MAX_LENGTH 80
 
@@ -182,7 +179,7 @@ bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
 /**
  * Convert GPS UTC date/time representation to a UNIX timestamp.
  */
-int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time_);
+int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
 
 /**
  * Rescale a fixed-point value to a different scale. Rounds towards zero.

+ 8 - 8
tests.c

@@ -811,21 +811,21 @@ START_TEST(test_minmea_usage1)
 }
 END_TEST
 
-START_TEST(test_minmea_gettimeofday)
+START_TEST(test_minmea_gettime)
 {
     struct minmea_date d = { 14, 2, 14 };
     struct minmea_time t = { 13, 0, 9, 123456 };
-    struct timeval tv;
-    ck_assert(minmea_gettimeofday(&tv, &d, &t) == 0);
-    ck_assert_int_eq(tv.tv_sec, 1392382809);
-    ck_assert_int_eq(tv.tv_usec, 123456);
+    struct timespec ts;
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(ts.tv_sec, 1392382809);
+    ck_assert_int_eq(ts.tv_nsec, 123456000);
 
     d.year = -1;
-    ck_assert(minmea_gettimeofday(&tv, &d, &t) != 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) != 0);
     d.year = 2014;
 
     t.hours = -1;
-    ck_assert(minmea_gettimeofday(&tv, &d, &t) != 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) != 0);
 }
 END_TEST
 
@@ -912,7 +912,7 @@ static Suite *minmea_suite(void)
     suite_add_tcase(s, tc_usage);
 
     TCase *tc_utils = tcase_create("minmea_utils");
-    tcase_add_test(tc_utils, test_minmea_gettimeofday);
+    tcase_add_test(tc_utils, test_minmea_gettime);
     tcase_add_test(tc_utils, test_minmea_rescale);
     tcase_add_test(tc_utils, test_minmea_float);
     tcase_add_test(tc_utils, test_minmea_coord);