Thanks for the bug report, Jason. Here is the modified procedure.
1. Modified to ignore AM/AP time formats when checking for thousandths
of a second.
2. Modified to allow more than 23 days when seconds are in thousandths
(an integer difference of 1,999,999,999 is 23 days, 3 hours, 33 minutes
and 20 seconds). As modified, I tested it to 100 years and it passed.
------------------------------------------------------------------------------
-- DateDiff.pro
-- Returns the difference between two datetime entries
-- in hours, minutes and seconds
-- Albert Berry 2012/10/17
------------------------------------------------------------------------------
{
BUG FIXES 20122/10/18
1. Jason Kramer reported that the procedure fails when the time format
includes the AM/PM parameter.
FIX: Modified procedure to check for the string ".S" not the length of
the time format.
Thanks, Jason!
2. Found by me, testing for Jason's bug find.
If the time setting includes fractions of a second, an integer value
does
not have enough digits to handle long stretches of time - it fails
before
one year has passed between the two date times and returns 0 as if both
values were the same..
FIX: I have modified the procedure to use a DOUBLE variable for the
difference, and it tests OK to 100 years plus. I have not
attempted
to calculate the years from the days because of leap years.
}
------------------------------------------------------------------------------
SET VAR vDateTime1 DATETIME
SET VAR vDateTime2 DATETIME
-- output variable
SET VAR vDateTimeDiff TEXT = NULL
-- working variables
CLEAR VAR vDifference, vHours, vDays, vMinutes, vSeconds
SET VAR vDifference DOUBLE = (.vDateTime2 - .vDateTime1)
SET VAR vTimeSetting TEXT = (CVAL("TIME"))
------------------------------------------------------------------------------
-- Checks for the existence of a fractional component to the time setting
-- and rounds to the nearest second if so.
------------------------------------------------------------------------------
IF vTimeSetting LIKE "%.S%" THEN
SET VAR vDifference = (NINT(.vDifference/1000)) -- round to nearest
second
ENDIF
------------------------------------------------------------------------------
SET VAR vDays INTEGER = (INT(.vDifference/86400))
SET VAR vDifference2 DOUBLE = (.vDifference - (.vDays * 86400))
SET VAR vHours INTEGER = (INT(.vDifference2 / 3600))
SET VAR vDifference3 DOUBLE = (.vDifference2 - (.vHours * 3600))
SET VAR vMinutes INTEGER = (INT(.vDifference3 / 60))
SET VAR vDifference4 DOUBLE = (.vDifference3 - (.vMinutes * 60))
SET VAR vSeconds INTEGER = (NINT(.vDifference4)) -- NINT rounds
SET VAR vDateTimeDiff = ( (CTXT(.vDays)) + "," + (CTXT(.vHours)) + +
"," + (CTXT(.vMinutes)) + "," + (CTXT(.vSeconds)))
------------------------------------------------------------------------------
LABEL Stop
SHOW VAR v%
RETURN