Skip to content

Commit 7b415d6

Browse files
committed
Implement new decimal years functions
1 parent f329a40 commit 7b415d6

File tree

2 files changed

+68
-52
lines changed

2 files changed

+68
-52
lines changed

include/swiftnav/gnss_time.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,30 @@ void gps_time_match_weeks(gps_time_t *t, const gps_time_t *ref);
272272
u16 gps_adjust_week_cycle(u16 wn_raw, u16 wn_ref);
273273
u16 gps_adjust_week_cycle256(u16 wn_raw, u16 wn_ref);
274274

275+
/**
276+
* @brief Converts a decimal year to a MJD (modified Julian date).
277+
*
278+
* @param epoch_years The epoch in decimal years representation.
279+
* @return The epoch in MJD representation.
280+
*/
281+
double decimal_year_to_mjd(const double epoch_years);
282+
283+
/**
284+
* @brief Converts a GPS time to a decimal year.
285+
*
286+
* @param gps_time The GPS epoch to convert.
287+
* @return The epoch in decimal years representation.
288+
*/
289+
double gps_time_to_decimal_years(const gps_time_t *time);
290+
291+
/**
292+
* @brief Converts a epoch represented as a decimal year to a GPS time.
293+
*
294+
* @param years The epoch in decimal years representation.
295+
* @return The epoch in GPS time representation.
296+
*/
297+
gps_time_t decimal_years_to_gps_time(const double years);
298+
275299
static inline bool is_leap_year(s32 year) {
276300
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
277301
}

src/gnss_time.c

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,7 @@ gps_time_t time2gps_t(const time_t t_unix) {
332332
}
333333

334334
/** Checks if GPS time t is within begin and end */
335-
bool gpstime_in_range(const gps_time_t *bgn,
336-
const gps_time_t *end,
335+
bool gpstime_in_range(const gps_time_t *bgn, const gps_time_t *end,
337336
const gps_time_t *t) {
338337
assert(bgn);
339338
assert((int)bgn->tow != TOW_UNKNOWN);
@@ -392,8 +391,7 @@ double gpsdifftime(const gps_time_t *end, const gps_time_t *beginning) {
392391
* \return True, if the time difference could be computed, false otherwise, e.g.
393392
* for end or start times with unknown week numbers.
394393
*/
395-
bool gpsdifftime_week_second(const gps_time_t *end,
396-
const gps_time_t *beginning,
394+
bool gpsdifftime_week_second(const gps_time_t *end, const gps_time_t *beginning,
397395
gps_time_duration_t *dt) {
398396
dt->seconds = 0;
399397
dt->weeks = 0;
@@ -443,8 +441,8 @@ bool gps_time_match_weeks_safe(gps_time_t *t, const gps_time_t *ref) {
443441
}
444442

445443
if (!gps_time_valid(t)) {
446-
log_info(
447-
"t=(%lf,%d) ref=(%lf,%d)", t->tow, (int)t->wn, ref->tow, (int)ref->wn);
444+
log_info("t=(%lf,%d) ref=(%lf,%d)", t->tow, (int)t->wn, ref->tow,
445+
(int)ref->wn);
448446
return false;
449447
}
450448

@@ -507,6 +505,30 @@ u16 gps_adjust_week_cycle256(u16 wn_raw, u16 wn_ref) {
507505
return wn_raw + 256 * ((wn_ref + 255 - wn_raw) / 256);
508506
}
509507

508+
double decimal_year_to_mjd(const double epoch_years) {
509+
const double integer_year = floor(epoch_years);
510+
const double fractional_year = epoch_years - integer_year;
511+
const double mjd_start_of_year =
512+
date2mjd((int32_t)integer_year, 1, 1, 0, 0, 0);
513+
const double mjd_start_of_following_year =
514+
date2mjd((int32_t)integer_year + 1, 1, 1, 0, 0, 0);
515+
const double mjd_per_year = mjd_start_of_following_year - mjd_start_of_year;
516+
const double epoch_mjd = mjd_start_of_year + (fractional_year * mjd_per_year);
517+
return epoch_mjd;
518+
}
519+
520+
double gps_time_to_decimal_years(const gps_time_t *time) {
521+
utc_tm utc;
522+
make_utc_tm(time, &utc);
523+
double days_in_year = YEAR_DAYS;
524+
525+
if (is_leap_year(utc.year)) {
526+
days_in_year = LEAP_YEAR_DAYS;
527+
}
528+
529+
return (double)utc.year + (double)utc.year_day / days_in_year;
530+
}
531+
510532
/** Transformation of GLONASS-M current data information into gps_time_t.
511533
*
512534
* Reference: GLONASS ICD Edition 5.1 2008
@@ -739,8 +761,7 @@ bool decode_utc_parameters(const u32 words[8], utc_params_t *u) {
739761
* \retval true UTC parameters have been decoded.
740762
* \retval false Decoding error.
741763
*/
742-
bool decode_utc_parameters_with_wn_ref(const u32 words[8],
743-
utc_params_t *u,
764+
bool decode_utc_parameters_with_wn_ref(const u32 words[8], utc_params_t *u,
744765
u16 wn_ref) {
745766
bool retval = false;
746767

@@ -823,12 +844,7 @@ double date2mjd(s32 year, s32 month, s32 day, s32 hour, s32 min, double sec) {
823844
*/
824845
/* NOTE: This function will be inaccurate by up to a second on the day of a leap
825846
* second. */
826-
void mjd2date(double mjd,
827-
s32 *year,
828-
s32 *month,
829-
s32 *day,
830-
s32 *hour,
831-
s32 *min,
847+
void mjd2date(double mjd, s32 *year, s32 *month, s32 *day, s32 *hour, s32 *min,
832848
double *sec) {
833849
s32 J, C, Y, M;
834850

@@ -868,12 +884,8 @@ utc_tm mjd2utc(double mjd) {
868884
* second. */
869885
double utc2mjd(const utc_tm *utc_time) {
870886
double secs = (double)utc_time->second_int + utc_time->second_frac;
871-
return date2mjd(utc_time->year,
872-
utc_time->month,
873-
utc_time->month_day,
874-
utc_time->hour,
875-
utc_time->minute,
876-
secs);
887+
return date2mjd(utc_time->year, utc_time->month, utc_time->month_day,
888+
utc_time->hour, utc_time->minute, secs);
877889
}
878890

879891
/* NOTE: This function will be inaccurate by up to a second on the week of a
@@ -883,13 +895,8 @@ utc_tm date2utc(s32 year, s32 month, s32 day, s32 hour, s32 min, double sec) {
883895
return mjd2utc(mjd);
884896
}
885897

886-
void utc2date(const utc_tm *utc_time,
887-
s32 *year,
888-
s32 *month,
889-
s32 *day,
890-
s32 *hour,
891-
s32 *min,
892-
double *sec) {
898+
void utc2date(const utc_tm *utc_time, s32 *year, s32 *month, s32 *day,
899+
s32 *hour, s32 *min, double *sec) {
893900
*year = utc_time->year;
894901
*month = utc_time->month;
895902
*day = utc_time->month_day;
@@ -939,38 +946,23 @@ double gps2mjd_params(const gps_time_t *gps_time, const utc_params_t *p) {
939946

940947
/* NOTE: This function will be inaccurate by up to a second on the week of a
941948
* leap second. */
942-
gps_time_t date2gps(
943-
s32 year, s32 month, s32 day, s32 hour, s32 min, double sec) {
949+
gps_time_t date2gps(s32 year, s32 month, s32 day, s32 hour, s32 min,
950+
double sec) {
944951
return date2gps_params(year, month, day, hour, min, sec, NULL);
945952
}
946953

947-
gps_time_t date2gps_params(s32 year,
948-
s32 month,
949-
s32 day,
950-
s32 hour,
951-
s32 min,
952-
double sec,
953-
const utc_params_t *p) {
954+
gps_time_t date2gps_params(s32 year, s32 month, s32 day, s32 hour, s32 min,
955+
double sec, const utc_params_t *p) {
954956
return mjd2gps_params(date2mjd(year, month, day, hour, min, sec), p);
955957
}
956958

957-
void gps2date(const gps_time_t *gps_time,
958-
s32 *year,
959-
s32 *month,
960-
s32 *day,
961-
s32 *hour,
962-
s32 *min,
963-
double *sec) {
959+
void gps2date(const gps_time_t *gps_time, s32 *year, s32 *month, s32 *day,
960+
s32 *hour, s32 *min, double *sec) {
964961
gps2date_params(gps_time, year, month, day, hour, min, sec, NULL);
965962
}
966963

967-
void gps2date_params(const gps_time_t *gps_time,
968-
s32 *year,
969-
s32 *month,
970-
s32 *day,
971-
s32 *hour,
972-
s32 *min,
973-
double *sec,
964+
void gps2date_params(const gps_time_t *gps_time, s32 *year, s32 *month,
965+
s32 *day, s32 *hour, s32 *min, double *sec,
974966
const utc_params_t *p) {
975967
utc_tm utc_time;
976968
gps2utc(gps_time, &utc_time, p);
@@ -979,8 +971,8 @@ void gps2date_params(const gps_time_t *gps_time,
979971

980972
/** Return the number of days in given month */
981973
u8 days_in_month(u16 year, u8 month) {
982-
static u8 days_in_month_lookup[13] = {
983-
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
974+
static u8 days_in_month_lookup[13] = {0, 31, 28, 31, 30, 31, 30,
975+
31, 31, 30, 31, 30, 31};
984976
if (month == 2 && is_leap_year(year)) {
985977
return 29;
986978
}

0 commit comments

Comments
 (0)