From: tleylan@pegasus.com (Tom Leylan)
Subject: Re: Pls help: algorithm wanted (xbase, pref foxpro)
Organization: Pegasus,  Honolulu
Date: Sun, 3 Jan 93 10:34:06 GMT

durai@ortta.umn.edu (Durai Venkatasubramanian) writes:

>I need an algorithm to accomplish a date comparison task.  Any help will be 
>appreciated and gratefully remembered for life.

The following will do what you want.  I started with the other poster's
code and "structured" it (a little better (in my opinion)) and as you
will notice the code resides in a function which is called repeatedly
until the function sends back a signal saying it is done.

It is in Clipper 5.0 but I don't think there is anything that won't
translate into FoxPro.  I left some of the Clipper-only initializations
and stuff out to facilitate it.

I wrote another version which adds a little more complexity but also
adds flexibility in that the PrintRate() function didn't actually print
the results but rather returned them to Main() which could then print
the BegDate, EndDate and Status or do whatever else it might want to
like write it to a file, send it through a fax card or whatever.

Oh... you should add append one extra record to your database which is
a dummy that covers all dates from the last "real" one to the end of
time or 01/01/95 - 12/31/99 in your sample and figuring that the end
of time is the end of this century.  By adding the record some code is
eliminated which has to check if the EOF() condition was a false hit
on the last record.  Personally I'd make the status "unknown" rather
than "uncovered" on that dummy record because it indicates you shot
past the scope of the system.  The person would be uncovered but it
might serve as a warning that you typed a date in wrong or something.

No big deal and you know what you're doing better than I do.

Oh... the first line is just a Clipper header file which I reference
in PrintRate() to SET SOFTSEEK on and off again without having to know
what the status is in the rest of the application.

tom


#include "set.ch"

FIELD START_DATE, END_DATE, STATUS

MEMVAR getlist

FUNCTION Main
   LOCAL dBeg, dEnd
   LOCAL aDates[ 2 ]

   USE RATE EXCLUSIVE NEW ALIAS Rate
   INDEX ON Rate->START_DATE TO START

   CLS

   aDates[ 1 ] := CTOD( "" )
   aDates[ 2 ] := CTOD( "" )

   @ 0, 0 SAY "Beg Date : " GET aDates[ 1 ] PICT "@D"
   @ 2, 0 SAY "End Date : " GET aDates[ 2 ] PICT "@D"
   READ

   ?
   ? "Subcontract Period: " +;
      DTOC( aDates[ 1 ] ) + " - " + DTOC( aDates[ 2 ] )
   ?

   WHILE PrintRate( aDates )
   ENDDO

   CLOSE Rate

   RETURN NIL


FUNCTION PrintRate( aDates )
   LOCAL lRet := .F.

   LOCAL xSoft := SET( _SET_SOFTSEEK, .T. )

   SEEK aDates[ 1 ]

   IF ( Rate->( EOF() ) )
      OutStat( aDates[ 1 ], aDates[ 2 ], "Uncovered" )

   ELSE

      IF ! ( aDates[ 1 ] == Rate->START_DATE )
         SKIP -1
      ENDIF

      IF ( aDates[ 2 ] <= Rate->END_DATE )
         OutStat( aDates[ 1 ], aDates[ 2 ], Rate->STATUS )

      ELSE
         OutStat( aDates[ 1 ], Rate->END_DATE, Rate->STATUS )
         aDates[ 1 ] := ( Rate->END_DATE + 1 )
         lRet := .T.

      ENDIF

   ENDIF

   SET( _SET_SOFTSEEK, xSoft )

   RETURN lRet


FUNCTION OutStat( dBeg, dEnd, cMsg )
   ? DTOC( dBeg ) + " - " + DTOC( dEnd ) + " = " + cMsg

   RETURN NIL

Newsgroups: comp.databases