< previous page page_113 next page >

Page 113
Listing 6.3 Nth_DOW_after_date ()
/* nthdowaf.c
   +-------------------------------------------+
   |   Copyright (C) 1995 by Lance W. Latham.  |
   |         All rights reserved.              |
   +-------------------------------------------+


   Returns the date corresponding to the Nth day of the week, on or after
   a given date. I.e., this routine returns the date for "the fourth
   Thursday on or after 02 March 1994", or "the first Tuesday on or after
   Christmas of 1866".
   INPUTS:
   1. n, type int, the number indicating the sequence number desired
      for the chosen day of the week. Valid values cannot be less than 1.
   2. dow_index, type int, the index indicating which day of the week is
      desired. Valid values are 0 (Sunday) through 6 (Saturday).
   3. date_in, pointer to a DATE_INFO structure containing the desired
      starting date.
   OUTPUTS:
   1. Returned by routine, a DATE_INFO structure containing the date
      corresponding to the 'Nth' day of the week (DOW) on or after the
      date given. In the event of an error, the routine returns
      a date consisting of the year, month and day set to zero.
   NOTES:
   1. The routine returns an error indication, of a date consisting
      entirely of zeroes, in the following events:


      1.1. The value of 'n' is invalid.
      1.2. The value of 'dow_index' is invalid.
      1.3. The value of 'month' of 'date_in' is invalid.
   2. Note that this routine, unlike the 'Nth_DOW_of_month ()' routine
      in this series, does NOT intercept result dates outside the month and
      year of the input date. Thus, the 'thirty-seventh Tuesday on or after
      the date of 31 December 1998" will return a legitimate date, not an error.
   3. This routine works in three basic steps:
      3.1. Find the DOW of the given date.
      3.2. Find the date of the first DOW matching the input DOW.
      3.3. Find the date of the 'n' th DOW matching the input DOW.
   4. Note that this routine returns dates ON or after a date. Therefore,
      if 'n' is 1, and the DOW of the input date 'date_in' matches the
      DOW specified by 'dow_index', then the routine will return the
      input date as its result.*/


#include "scdtl_2.h"


struct DATE_INFO Nth_DOW_after_date (int n, int dow_index, struct DATE_INFO *date_in)
{
   int index, diff;
   double jd;
   struct DATE_INFO date;


   date = make_date (0, 0, 0);


   if (n < 1)
      return (date);


   if ( (dow_index < 0) || (dow_index > 6) )
      return (date);

 
< previous page page_113 next page >