Skip to content

Commit

Permalink
Per #2412, switch from using sec_of_year_diff() to day_of_year_diff().
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHalleyGotway committed Jan 20, 2023
1 parent dd91e35 commit cbfffba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
35 changes: 21 additions & 14 deletions src/basic/vx_cal/doyhms_to_unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,18 @@ return ( s );

////////////////////////////////////////////////////////////////////////

int unix_to_sec_of_year(unixtime u) {

int unix_to_day_of_year(unixtime u) {

int mon, day, yr, hr, min, sec;

unix_to_mdyhms(u, mon, day, yr, hr, min, sec);

return ( (int) mdyhms_to_unix(mon, day, 1970, hr, min, sec) );
int sec_of_year = mdyhms_to_unix(mon, day, 1970, 0, 0, 0);

int sec_per_day = 60 * 60 * 24;

return ( sec_of_year / sec_per_day );

}

Expand Down Expand Up @@ -139,33 +144,35 @@ int s1 = unix_to_sec_of_day(ut1);

int s2 = unix_to_sec_of_day(ut2);

int ds = s2 - s1;
int dt = s2 - s1;

if ( ds < -1 * sec_per_day/2 ) ds += sec_per_day;
else if ( ds > sec_per_day/2 ) ds -= sec_per_day;
if ( dt < -1 * sec_per_day/2 ) dt += sec_per_day;
else if ( dt > sec_per_day/2 ) dt -= sec_per_day;

return ( ds );
return ( dt );

}


////////////////////////////////////////////////////////////////////////


int sec_of_year_diff(unixtime ut1, unixtime ut2) {
int day_of_year_diff(unixtime ut1, unixtime ut2) {


int sec_per_year = 60 * 60 * 24 * 365;
int sec_per_day = 60 * 60 * 24;
int day_per_year = 365;

int s1 = unix_to_sec_of_year(ut1);
int d1 = unix_to_day_of_year(ut1);

int s2 = unix_to_sec_of_year(ut2);
int d2 = unix_to_day_of_year(ut2);

int ds = s2 - s1;
int dt = d2 - d1;

if ( ds < -1 * sec_per_year/2 ) ds += sec_per_year;
else if ( ds > sec_per_year/2 ) ds -= sec_per_year;
if ( dt < -1 * day_per_year/2.0 ) dt += day_per_year;
else if ( dt > day_per_year/2.0 ) dt -= day_per_year;

return ( ds );
return ( dt );

}

Expand Down
4 changes: 2 additions & 2 deletions src/basic/vx_cal/vx_cal.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extern int hms_to_sec (int hour, int min, int sec);

extern int unix_to_sec_of_day (unixtime u);

extern int unix_to_sec_of_year (unixtime u);
extern int unix_to_day_of_year (unixtime u);

extern long unix_to_long_yyyymmddhh (unixtime u);

Expand Down Expand Up @@ -132,7 +132,7 @@ extern ConcatString sec_to_timestring(int);

extern int sec_of_day_diff (unixtime ut1, unixtime ut2);

extern int sec_of_year_diff (unixtime ut1, unixtime ut2);
extern int day_of_year_diff (unixtime ut1, unixtime ut2);


////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit cbfffba

Please sign in to comment.