Date Compression & Year 2000 Challenges

by Robert L. Moore and D. Gregory Foley





Example 1: 



MMDDYYYY to CYYDDD



Represent YYYY as (Y1)(Y2)(Y3)(Y4). Then C = (Y1*10+Y2)-19. The "YY" in the CYYDDD are Y3 and Y4. Use MMDDtoDDD.c (Listing Three; available electronically; see "Resources," page 3) to complete the conversion.



Example 2: 



CYYDDD to MMDDYYYY



Assuming the CYYDDD count begins with 1900 A.D., YYYY = (19+C)*100+YY. Use DDDtoMMDD.c (Listing Five; available electroincally) to complete the conversion.



Example 3:



MMDDYYYY to DDDDDD



Use MMDDToDDD.c (Listing Three; available electronically) to convert the MMDD part of MMDDYYYY to DDD--the day count within a year. Then use DDDYYYYToDDDDDD.c (Listing Six; available electronically) to convert DDD and the YYYY part of MMDDYYYY to DDDDDD.



Example 4: 



DDDDDD to MMDDYYYY



Use DDToDDD4Y.c (Listing Four; available electronically) to convert from DDDDDD to DDDYYYY.  This is a four digit year and a count of days starting at January 1 within that year. Use DDDToMMDD.c (Listing Five; available electronically) on the DDD part of DDDYYYY to convert DDD to MMDD. The combined result is MMDDYYYY.





Listing One

/* routine:  sliding_window

*  Author:  Robert Moore

*  Takes a reference four digit year (year), a window width above the 

*  reference year (upperwinwith), and a one or two-digit year being queried 

*  (qyear), and returns a four-digit representation (ryear) of the query 

*  year based on the window and the reference year if the query year is one 

*  or two digits. Query years greater than two digits are just returned. 

*  Setting reference year to a constant would make this a fixed window.

*/

int sliding_window(int year, int upperwinwidth, int qyear)

{

  int yeardate, centurydate, ryear, temp;

  ryear = -1;  /* status that will be returned on error */

  if (qyear < 100)  /* the query year is a two digit year -- 

                       convert to four */

    {               /* bounds check */

      if ((year >= 0) && (0 < upperwinwidth) && (0 <= qyear)) 

    {  /* everything a proper bound */

      yeardate = year % 100;  /* convert year to a year */ 

      centurydate = year - yeardate;  /* and century */

      temp = yeardate + upperwinwidth;  /* calculate upper window limit */

      if (temp < 100)

        {

          if (temp <= qyear )

        ryear = centurydate - 100 + qyear; /* year wraps to previous */

          else                                 /* century:  case 1b */

        ryear = centurydate + qyear; /* same century:  case 1a or 1c*/

        }

      else

        {

          if (qyear < (temp - 100))

       ryear = centurydate + 100 + qyear; /* year in next century: case 2b */

          else

       ryear = centurydate + qyear; /* same century:  case 2a or 2c */

        }    

    }

    }

  else

    ryear = qyear; /* three or more digit year--don't convert */

  return ryear;

}



Listing Two

/*---------------------------------------------------------------

/*  Authors:  D. Greg Foley, Robert L. Moore

/*  Description: This function determines if a four digit year is a leap

/*   year. A year is a leap year if it is divisible by 4 but not by 100, 

/*   except years that are divisible by 400.  

/*  Parameter Description:

/*  int Year - a four-digit year

/*  returns 1 if the year is a Leap Year

/*          0 if the year is NOT a Leap Year

/*  Notes: For clarity of the algorithm no error checking is included.

/*--------------------------------------------------------------*/

int LEAPYEAR(int Year)

{

    if ((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0)

      return 1;   /* This is a LEAP year */

    else

      return 0;   /* This is NOT a LEAP year */

}



3



