Code:
'###################################################################################### ' Difference between Date/Time combinations using Date$ and Time$ style data strings ' ' Thanks to Egbert for the Julian Date routines. ' I changed the routine declarations slightly to use LONGs rather than INTEGERs '###################################################################################### #COMPILE EXE #INCLUDE "WIN32API.INC" ' One TYPE declaration required TYPE ElapsedTimeStruc hours AS LONG minutes AS LONG seconds AS LONG END TYPE '###################################################################################### FUNCTION DateToJulian(inDate AS STRING) AS LONG ' Returns Julian Date (Long Integer) from "String" Date formatted as: MM/DD/YYYY. LOCAL year AS LONG, _ month AS LONG, _ day AS LONG, _ Elapsed AS LONG, _ InvalidLeaps AS LONG, _ ValidLeaps AS LONG, _ dMonths AS LONG month = VAL(LEFT$(inDate,2)) day = VAL(MID$(inDate,4,2)) year = VAL(MID$(inDate,7,4)) IF month < 3 THEN ' January or February? month = month + 12 ' 13th or 14th month .... DECR year ' .... of prev. year END IF Elapsed = INT((year + 4712) * 365.25) ' days in years elapsed InvalidLeaps = year \ 100 ' substract century leapdays ValidLeaps = year \ 400 ' re-add valid ones dMonths = INT(30.6 * (month - 1) + .2) ' days of months elapsed + adjustment FUNCTION = Elapsed - InvalidLeaps + ValidLeaps + dMonths + day END FUNCTION '###################################################################################### FUNCTION JulToDate(BYVAL Jul AS LONG) AS STRING ' Returns "String" Date formatted as: MM/DD/YYYY from Julian Date (Long Integer) LOCAL help AS LONG, _ Year AS LONG,_ Month AS LONG,_ Day AS LONG,_ oText AS STRING * 10 Jul = Jul + 68569 help = 4 * Jul \ 146097 Jul = Jul - ((146097 * help + 3) \ 4) Year = 4000 * (Jul + 1) \ 1461001 Jul = Jul - (1461 * Year \ 4) + 31 Month = 80 * Jul \ 2447 Day = Jul - (2447 * Month \ 80) Month = Month + 2 - (12 * (Month \ 11)) Year = 100 * (help - 49) + Year + (Month \ 11) oText = RIGHT$(STR$(Month),2) + "/" _ +RIGHT$(STR$(Day),2) + "/" _ +RIGHT$(STR$(Year),4) + "/" REPLACE " " WITH "0" IN oText FUNCTION = oText END FUNCTION '###################################################################################### FUNCTION SecondsSinceMidnight(inTime AS STRING) AS LONG ' accepts time "hh:mm:ss" and converts it to elapsed seconds since previous midnight LOCAL inHours AS LONG,_ inMinutes AS LONG,_ inSeconds AS LONG inHours = VAL(LEFT$(inTime,2)) inMinutes = VAL(MID$(inTime,4,2)) inSeconds = VAL(MID$(inTime,7,2)) FUNCTION = inHours * 3600 + inMinutes * 60 + inSeconds END FUNCTION '###################################################################################### SUB TimeDifference(inDate1 AS STRING, inTime1 AS STRING, _ inDate2 AS STRING, inTime2 AS STRING, _ tStruc AS elapsedTimeStruc) ' accepts 2 dates/times and calculates the absolute time elapsed. LOCAL Julian1 AS LONG,_ Julian2 AS LONG,_ Seconds1 AS LONG,_ Seconds2 AS LONG Julian1 = DateToJulian(inDate1) Julian2 = DateToJulian(inDate2) Seconds1 = SecondsSinceMidnight(inTime1) Seconds2 = SecondsSinceMidnight(inTime2) IF Julian1 > Julian2 OR (julian1 = Julian2 AND Seconds1 > Seconds2) THEN SWAP Julian1, Julian2 SWAP Seconds1, Seconds2 END IF IF Julian1 = Julian2 THEN tStruc.seconds = Seconds2 - Seconds1 ELSE tStruc.seconds = (60*60*24) - Seconds1 + Seconds2 tStruc.hours = (Julian2 - Julian1-1) * 24 END IF WHILE tStruc.seconds >= 60 INCR tStruc.minutes tStruc.seconds = tStruc.seconds - 60 LOOP WHILE tStruc.minutes >= 60 INCR tStruc.hours tStruc.minutes = tStruc.minutes - 60 LOOP END SUB '###################################################################################### FUNCTION PBMAIN() AS LONG LOCAL inDate1 AS STRING, _ inTime1 AS STRING, _ inDate2 AS STRING, _ inTime2 AS STRING, _ elapsedTime AS elapsedTimeStruc inDate1 = "05/09/2000" inTime1 = "18:11:35" inDate2 = "05/11/2000" inTime2 = "03:15:45" CALL TimeDifference(inDate1, inTime1, inDate2, inTime2, elapsedTime) CALL MessageBox (BYVAL %NULL,"From: " + inDate1 + " " + inTime1 + CHR$(13) + _ " To: " + inDate2 + " " + inTime2 + CHR$(13) + _ "elapsed time: " + CHR$(13) + _ "Hrs: " + STR$(elapsedTime.hours) + CHR$(13) + _ "Min: " + STR$(elapsedTime.minutes) + CHR$(13) + _ "Sec: " + STR$(elapsedTime.seconds), _ "Time Difference", %MB_ICONEXCLAMATION OR %MB_OK) END FUNCTION
[email protected]
Leave a comment: