Re: Date Subtraction Routines
- From: Evenbit <nbaker2328@xxxxxxxxxxx>
- Date: Tue, 10 Jul 2007 22:07:27 -0700
On Jul 6, 9:48 am, "r...@xxxxxxxxxx" <r...@xxxxxxxxxx> wrote:
Hi All,
Here are some date subtraction routines that handle subtracting days,
months, and years from a date. As with the add functions posted
earlier, these routines properly handle differences that result in
invalid dates (by truncation, and returning the number of truncated
days in EAX).
hLater,
Randy Hyde
Strange... You post source code but don't recieve any comments...
must be the wrong newsgroup?? ;)
Well, let's take a look...
// date.subDays-
//
// This function subtracts the specified number of days from
// the date passed by reference.
So, we are not passing the 'date' information to this routine - we are
only passing a 'pointer to the date' so that this routine will know
where to find it - and the routine subtracts some amount of 'days'
from it to return us to a previous time. Okay, somebody has been
reading w-a-y too many Jules Verne novels... ;)
procedure date.subDays
(
days: uns32;
var theDate:date.daterec
);
@nodisplay;
@noframe;
These last two lines tell the compiler not to generate any 'fluff'
"behind our back".
var
dt :date.daterec;
begin subDays;
push( ebp );
mov( esp, ebp );
sub( _vars_, esp );
push( eax );
push( ebx );
mov( theDate, ebx );
mov( [ebx], eax );
mov( eax, dt.date );
// Optimization: most of the time, the difference of the days
minus
// the specified date falls within the current month. A quick
// check for this allows for a *very* fast date computation,
// so it's worth making this check because the situation is
// so common.
Sounds reasonable... unless someone uses this routine in a program
which is _always_ run on the First of the Month! :)
//
// Note: the following assumes that the dt.day value is in
// the L.O. byte of the daterec structure.
.... and then some smartypants makes one tiny, tinsee, little,
"innocent" minor change...
movzx( al, eax );
sub( days, eax );
cmp( eax, 28 );
jg fullDateArith;
cmp( eax, 0 );
jle fullDateArith;
// Okay, we've got the special case here.
// First, verify that the original date
// was valid.
What? You don't BELIEVE me? I swear!
pushd( [ebx] );
call date._validate;
// Now store away the new day value:
Well they say that for every cig that I _dont_ smoke, it adds some
number of days...
mov( al, (type date.daterec [ebx]).day );
pop( ebx );
pop( eax );
leave();
ret( _parms_ );
fullDateArith:
// Okay, if the difference of the days parameter and theDate
might not
// leave us in the same month, then we're going to do this the
// slow way -- we'll convert the date to a Julian day number,
// add in the days value, then convert the result back to a
// Gregorian date:
Oh, did Gregory write a time-machine novel too?
pushd( [ebx] );
call date._toJulian; // Also checks validity of date.
To Julian: Wherefore art though?
[okay, I'll stop...]
Nathan.
.
- References:
- Date Subtraction Routines
- From: rhyde@xxxxxxxxxx
- Date Subtraction Routines
- Prev by Date: Re: Blair, another one bites the dust !
- Next by Date: Re: MASM v8 is available on Microsoft's Web Site
- Previous by thread: Date Subtraction Routines
- Next by thread: MASM v8 is available on Microsoft's Web Site
- Index(es):
Relevant Pages
|