|
|
|
| Listing 6.5 | |
|
|
|
|
NOTES:
1. The routine returns an error indication, of a date consisting entirely of
zeroes, in the event that the value of 'dow_index' is invalid.
2. Note that this routine returns the date corresponding to the DOW closest
to the given date. To obtain the 'Nth' closest DOW, use this routine to
obtain the date for the FIRST closest, and then use the 'Nth_DOW' routines
to obtain dates with larger offsets, if necessary.
*/
#include "scdtl_2.h"
#include "scdtl_4.h"
struct DATE_INFO DOW_closest_to_date (int dow_index, struct DATE_INFO *date_in)
{
int diff1, diff2;
double JD0, jd1, jd2;
struct DATE_INFO date0, date1, date2;
date0 = make_date (0, 0, 0);
if ( (dow_index < 0) || (dow_index > 6) )
return (date0);
JD0 = date_to_JD0 (date_in);
date1 = Nth_DOW_before_date (1, dow_index, date_in);
jd1 = date_to_JD0 (&date1);
diff1 = (int) (JD0 - jd1);
/*
If the difference between the JD#'s of the input date and the 1st
DOW on or before the date is 0, then then the 1st DOW is ON the
input date, and we can return the 1st DOW.
*/
if (diff1 == 0)
return (date1);
date2 = Nth_DOW_after_date (1, dow_index, date_in);
jd2 = date_to_JD0 (&date2);
diff2 = (int) (jd2 - JD0);
/*
The variables 'diff1' and 'diff2' can only be equal in the event that the
desired DOW is ON the input date, since both 'Nth_DOW' routines return for the
date ON. This condition should already be detected by the 'if' above, and if it
recurs again below, it is considered an internal error.
*/
if (diff2 > diff1)
return (date1); else
{
if (diff2 < diff1)
return (date2);
else
return (date0);
}
}
|
|
|
|
|
|
|
|