Calculate accurate differences between date/times
Posted: Thu Dec 21, 2006 8:13 pm
Code updated for 5.20+
I originally posted a version of this yesterday that seemed to work fine, until I noticed some problems. I fixed them, or so I thought, only to discover that more problems existed. It was so unreliable and my attempts at fixing it were so stupid I thought it best to take it down for the time being and see if I could get if working. I damn near despaired, I don't mind admitting. However, a minor brainwave washed over me this morning and I came up with a better approach. After a fair bit of testing I think I have something that can be relied upon to be accurate. If someone can break this, please do and post the results:
I originally posted a version of this yesterday that seemed to work fine, until I noticed some problems. I fixed them, or so I thought, only to discover that more problems existed. It was so unreliable and my attempts at fixing it were so stupid I thought it best to take it down for the time being and see if I could get if working. I damn near despaired, I don't mind admitting. However, a minor brainwave washed over me this morning and I came up with a better approach. After a fair bit of testing I think I have something that can be relied upon to be accurate. If someone can break this, please do and post the results:
Code: Select all
;===================================================
; Program: DateDiff library function
; Author: netmaestro
; Date: December 20, 2006
; Target OS: Windows All
; Target Compiler: PureBasic 4.0
; License: Free, unrestricted, credit
; appreciated but not required
;===================================================
Structure TimeDiff
totaldays.l
years.l
months.l
daysremaining.l
hours.l
minutes.l
seconds.l
EndStructure
Procedure DateDiff(dateearly, datelate, *diff.TimeDiff)
Protected totaldays,years,months,daysremaining,hours,minutes,seconds
curdate = dateearly
testdate = dateearly
startday = Day(dateearly)
totaldays = 0
daysremaining = 0
While testdate <= datelate
testdate = AddDate(curdate, #PB_Date_Day, 1)
If testdate <= datelate
curdate = testdate
totaldays+1
daysremaining+1
If Day(curdate) = startday
months+1
daysremaining=0
EndIf
EndIf
Wend
testdate = curdate
hours = 0
While testdate<datelate
testdate = AddDate(curdate, #PB_Date_Hour, 1)
If testdate <= datelate
curdate = testdate
hours+1
EndIf
Wend
testdate = curdate
minutes = 0
While testdate<datelate
testdate = AddDate(curdate, #PB_Date_Minute, 1)
If testdate <= datelate
curdate = testdate
minutes+1
EndIf
Wend
testdate = curdate
seconds = 0
While testdate<datelate
testdate = AddDate(curdate, #PB_Date_Second, 1)
If testdate <= datelate
curdate = testdate
seconds+1
EndIf
Wend
years = months/12
If years
months % 12
EndIf
*diff\totaldays = totaldays
*diff\years = years
*diff\months = months
*diff\daysremaining = daysremaining
*diff\hours = hours
*diff\minutes = minutes
*diff\seconds = seconds
EndProcedure
dateearly = ParseDate("%yyyy/%mm/%dd/%hh:%ii:%ss", "2005/9/9/12:30:00")
datelate = Date()
MyDiff.TimeDiff
DateDiff(dateearly,datelate,@MyDiff)
Debug "Total Days: "+Str(MyDiff\totaldays)
Debug "Years: "+Str(MyDiff\years)
Debug "Months: "+Str(MyDiff\months)
Debug "Days: "+Str(MyDiff\daysremaining)
Debug "Hours: "+Str(MyDiff\hours)
Debug "Minutes: "+Str(MyDiff\minutes)
Debug "Seconds: "+Str(MyDiff\seconds)