< previous page page_214 next page >

Page 214
Listing 8.10 iso_week_number ()
/* iweeknbr.c


   +-------------------------------------------+
   |   Copyright (C) 1995 by Lance W. Latham.  |
   |         All rights reserved.              |
   +-------------------------------------------+


   This routine returns the week number of the year in which a given date
   falls, as defined by the International Standards Organization (ISO).


   INPUTS:
   1. date_in, a pointer to a DATE_INFO structure, containing the input
      date for which the week number is desired.


   OUTPUTS:
   1. returned by routine, type int, the week number in which the
      input date falls. Valid values are 1 to 53.


   NOTES:
   1. There are two definitional rules, as follow:
      1.1. The first day of the week is assumed to be Monday.
      1.2. The first Thursday of the year always falls in week 1.


   1. This code is a direct translation from the COBOL routine
      supplied by Constance Veening of the Netherlands.


   2. This definition of week number corresponds to the definition of
      week number determined by the ISO in document 8601, 1988 version.
*/


#include "scdtl_2.h"


int iso_week_number (struct DATE_INFO *date_in)
{
   double jd1, jd2;
   int dow, week_number, y, m, d;
   struct DATE_INFO date2;


   jd1 = date_to_JD0 (date_in);
   dow = day_of_week_index_JD0 (jd1);


   y = date_in->year;
   m = date_in->month;
   d = date_in->day;


   if (m == 12)
      {
      if ( (d == 29) && (dow == DOW_MONDAY) )
         return (1);
      else
         {
         if ( (d == 30) && ( (dow == DOW_MONDAY) || (dow == DOW_TUESDAY) ))
            return (1);
         else
            {
            if ( (d == 31) && ( (dow == DOW_MONDAY) || (dow == DOW_TUESDAY) ||
                   (dow == DOW_WEDNESDAY) ))
               return (1);
            }
         }
      }

 
< previous page page_214 next page >