Skip to content

Commit abd91a6

Browse files
committed
Move gps2decimal years
1 parent f329a40 commit abd91a6

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

include/swiftnav/gnss_time.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ void gps2date_params(const gps_time_t *gps_time,
336336
s32 *min,
337337
double *sec,
338338
const utc_params_t *p);
339+
/**
340+
* @brief Converts a GPS time to a decimal year.
341+
*
342+
* @param gps_time The GPS epoch to convert.
343+
* @return The epoch in decimal years representation.
344+
*/
345+
double gps2decimal_years(const gps_time_t &gps_time);
339346

340347
/* GPS-UTC time offset at given GPS time */
341348
double get_gps_utc_offset(const gps_time_t *t, const utc_params_t *p);

src/gnss_time.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ time_t gps2time(const gps_time_t *t_gps) {
316316
return t;
317317
}
318318

319+
double gps2decimal_years(const gps_time_t &gps_time) {
320+
utc_tm utc;
321+
make_utc_tm(&gps_time, &utc);
322+
double days_in_year = YEAR_DAYS;
323+
324+
if (is_leap_year(utc.year)) {
325+
days_in_year = LEAP_YEAR_DAYS;
326+
}
327+
328+
return static_cast<double>(utc.year) +
329+
static_cast<double>(utc.year_day) / days_in_year;
330+
}
331+
319332
/** Convert a Unix `time_t` to a `gps_time_t` GPS time structure.
320333
* Both input and output are assumed to be in GPS Time scale so no leap second
321334
* is applied
@@ -977,6 +990,20 @@ void gps2date_params(const gps_time_t *gps_time,
977990
utc2date(&utc_time, year, month, day, hour, min, sec);
978991
}
979992

993+
double gps_time_to_decimal_years(const gps_time_t &gps_time) {
994+
utc_tm utc;
995+
make_utc_tm(&gps_time, &utc);
996+
double days_in_year = YEAR_DAYS;
997+
998+
if (is_leap_year(utc.year)) {
999+
days_in_year = LEAP_YEAR_DAYS;
1000+
}
1001+
1002+
return static_cast<double>(utc.year) +
1003+
static_cast<double>(utc.year_day) / days_in_year;
1004+
}
1005+
1006+
9801007
/** Return the number of days in given month */
9811008
u8 days_in_month(u16 year, u8 month) {
9821009
static u8 days_in_month_lookup[13] = {

0 commit comments

Comments
 (0)