Skip to content

Commit

Permalink
More error tracing fixes in timescale.c
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Sep 17, 2024
1 parent 19892d0 commit f1d6441
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/timescale.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ double novas_get_time(const novas_timespec *time, enum novas_timescale timescale
* @param[out] ijd [day] The integer part of the Julian date in the requested timescale. It may
* be NULL if not required.
* @return [day] The fractional part of the Julian date in the requested timescale or
* NAN is the time argument is NULL.
* NAN is the time argument is NULL (ijd will be set to -1 also).
*
* @sa novas_set_split_time()
* @sa novas_get_time()
Expand All @@ -265,6 +265,8 @@ double novas_get_split_time(const novas_timespec *time, enum novas_timescale tim
static const char *fn = "novas_get_split_time";
double f;

if(ijd) *ijd = -1;

if(!time) {
novas_error(-1, EINVAL, fn, "NULL input time specification");
return NAN;
Expand Down Expand Up @@ -443,7 +445,7 @@ int novas_set_unix_time(time_t unix_time, long nanos, int leap, double dut1, nov
*
* @param time Pointer to the astronomical time specification data structure.
* @param[out] nanos [ns] UTC sub-second component. It may be NULL if not required.
* @return [s] The integer UNIX time
* @return [s] The integer UNIX time, or -1 if the input time is NULL.
*
* @sa novas_set_unix_time()
* @sa novas_get_time()
Expand All @@ -452,13 +454,17 @@ int novas_set_unix_time(time_t unix_time, long nanos, int leap, double dut1, nov
* @author Attila Kovacs
*/
time_t novas_get_unix_time(const novas_timespec *time, long *nanos) {
static const char *fn = "novas_get_unix_time";

long ijd, isod;
double sod;
time_t seconds;

sod = novas_get_split_time(time, NOVAS_UTC, &ijd) * DAY;
if(isnan(sod))
return novas_trace_nan("novas_get_unix_time");
if(isnan(sod)) {
if(nanos) *nanos = novas_trace_nan(fn);
return novas_trace(fn, -1, 0);
}

isod = (long) floor(sod);
seconds = UNIX_J2000 + (ijd - IJD_J2000) * IDAY + isod;
Expand Down
11 changes: 11 additions & 0 deletions test/src/test-errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,8 @@ static int test_time() {
if(check_nan("time:get:scale:-1", novas_get_time(&time, -1))) n++;
if(check_nan("time:get:scale:hi", novas_get_time(&time, NOVAS_TIMESCALES))) n++;

if(check("time:get_unix_time:time", -1, novas_offset_time(NULL, 0.1, &time))) n++;

if(check("time:offset:time", -1, novas_offset_time(NULL, 0.1, &time))) n++;
if(check("time:offset:out", -1, novas_offset_time(&time, 0.1, NULL))) n++;
if(check("time:offset:both", -1, novas_offset_time(NULL, 0.1, NULL))) n++;
Expand All @@ -1033,6 +1035,15 @@ static int test_time() {
if(check_nan("time:diff:t2", novas_diff_time(&time, NULL))) n++;
if(check_nan("time:diff:both", novas_diff_time(NULL, NULL))) n++;

if(check_nan("time:diff_tcg:t1", novas_diff_tcg(NULL, &time))) n++;
if(check_nan("time:diff_tcg:t2", novas_diff_tcg(&time, NULL))) n++;
if(check_nan("time:diff_tcg:both", novas_diff_tcg(NULL, NULL))) n++;

if(check_nan("time:diff_tcb:t1", novas_diff_tcb(NULL, &time))) n++;
if(check_nan("time:diff_tcb:t2", novas_diff_tcb(&time, NULL))) n++;
if(check_nan("time:diff_tcb:both", novas_diff_tcb(NULL, NULL))) n++;


return n;
}

Expand Down

0 comments on commit f1d6441

Please sign in to comment.