diff --git a/include/libswiftnav/time.h b/include/libswiftnav/time.h index 4fb45fd8..87641735 100644 --- a/include/libswiftnav/time.h +++ b/include/libswiftnav/time.h @@ -77,6 +77,7 @@ #define GPS_EPOCH 315964800 #define WN_UNKNOWN -1 +#define TOW_UNKNOWN -1 /** Structure representing a GPS time. */ typedef struct __attribute__((packed)) { @@ -84,6 +85,8 @@ typedef struct __attribute__((packed)) { s16 wn; /**< GPS week number. */ } gps_time_t; +#define GPS_TIME_UNKNOWN ((gps_time_t){TOW_UNKNOWN, WN_UNKNOWN}) + void normalize_gps_time(gps_time_t *t_gps); time_t gps2time(const gps_time_t *t); diff --git a/src/time.c b/src/time.c index 00181b71..77cbe741 100644 --- a/src/time.c +++ b/src/time.c @@ -144,11 +144,14 @@ gps_time_t glo_time2gps_time(u16 nt, u8 n4, s8 h, s8 m, s8 s) u8 j = 0; u16 day_of_year = 0; u32 glo_year = 0; - gps_time_t gps_t = {0, 0}; + gps_time_t gps_t = GPS_TIME_UNKNOWN; - assert(n4 >= N4_MIN && n4 <= N4_MAX); + if (n4 < N4_MIN || n4 > N4_MAX) + return GPS_TIME_UNKNOWN; - if (nt >= GLO_NT_0_FLOOR && nt <= GLO_NT_0_CEILING) { + if (nt < GLO_NT_0_FLOOR) + return GPS_TIME_UNKNOWN; + else if (nt <= GLO_NT_0_CEILING) { j = 1; day_of_year = nt; } @@ -165,7 +168,7 @@ gps_time_t glo_time2gps_time(u16 nt, u8 n4, s8 h, s8 m, s8 s) day_of_year = nt - LEAP_YEAR_DAYS - YEAR_DAYS * 2; } else - assert(0 && "invalid nt"); + return GPS_TIME_UNKNOWN; glo_year = 1996 + 4 * (n4 - 1) + (j - 1); diff --git a/tests/check_time.c b/tests/check_time.c index c9a93cfb..d50a4653 100644 --- a/tests/check_time.c +++ b/tests/check_time.c @@ -201,6 +201,14 @@ START_TEST(test_glo_time2gps_time) /* GLO time 31st Dec 2011 12:12:12 */ {.nt = 1461, .n4 = 4, .h = 12, .m = 12, .s = 12, .ret = {.wn = 1668, .tow = 551549}}, + {.nt = 0, .n4 = 4, .h = 12, .m = 12, .s = 12, + .ret = GPS_TIME_UNKNOWN}, + {.nt = 1462, .n4 = 4, .h = 12, .m = 12, .s = 12, + .ret = GPS_TIME_UNKNOWN}, + {.nt = 1461, .n4 = 0, .h = 12, .m = 12, .s = 12, + .ret = GPS_TIME_UNKNOWN}, + {.nt = 1461, .n4 = 32, .h = 12, .m = 12, .s = 12, + .ret = GPS_TIME_UNKNOWN}, }; for (size_t i = 0; i < sizeof(testcases) / sizeof(struct glo_time2gps_time_testcase);