About This Code
Brief Description:
LotusScript Class for Determining Elapsed Time and Business Days
Contributor:
Sean Burgess
Type:
Date/Time functions
Notes Version:
R6.x, R7.x
Last Modified:
18 Jun 2007
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
Public Class elapsedtime
Private startdt As NotesDateTime
Private enddt As NotesDateTime
Private tstring As String
Private bstring As String
Private tdays As Long
Private bdays As Long
Private thours As Integer
Private bhours As Integer
Private tminutes As Integer
Private bminutes As Integer
Private tseconds As Integer
Private bseconds As Integer
Private nondays () As Integer
Private nondates () As NotesDateTime
Private startofwd As String
Private endofwd As String
Sub New(dt1 As String, dt2 As String, holidays As String)
Dim tmp As Variant
Dim x As Integer
Set Me.startdt = New NotesDateTime(dt1)
Set Me.enddt = New NotesDateTime(dt2)
If dt1 = "" Or dt2 = "" Then Exit Sub
' Weekend/Non-working days are set to Sun (1) and Sat (7)
Redim nondays(1)
nondays(0) = 1
nondays(1) = 7
' Business Hours can be adjusted here
Me.startofwd = "8:00 AM"
Me.endofwd = "5:00 PM"
' holidays contains a comma separated list of dates that are not work days
tmp = Split(holidays,",")
Redim nondates(Ubound(tmp))
For x = 0 To Ubound(tmp)
Set nondates(x) = New NotesDateTime(tmp(x))
Next
Call GetTElapsed()
Call GetBElapsed()
End Sub
Private Sub GetTElapsed()
On Error Goto gteerror
Dim diff As Long, tmp As Long
If Me.startdt.localtime = Me.enddt.localtime Then
Me.tdays = 0
Me.thours = 0
Me.tminutes = 0
Me.tseconds = 0
Else
diff = Me.enddt.TimeDifference(Me.startdt)/60
Me.tdays = Fix(diff/1440)
Me.thours = Fix((diff Mod 1440)/60)
Me.tminutes = Fix(diff Mod 60)
Me.tseconds = Fix(Me.enddt.TimeDifference(Me.startdt) Mod 60)
End If
gtefinish :
Exit Sub
gteerror :
Call LogError()
Resume gtefinish
End Sub
Private Sub GetBElapsed()
On Error Goto bteerror
Dim dt1 As NotesDateTime
Dim dt2 As NotesDateTime
Dim dt3 As NotesDateTime
Dim hdt As NotesDateTime
Dim diff As Long, tmp As Long
Dim stepcount As Integer
Dim addit As Boolean
Me.bdays = 0
If Me.startdt.localtime = Me.enddt.localtime Then
Me.bhours = 0
Me.bminutes = 0
Me.bseconds = 0
Exit Sub
Elseif Me.enddt.TimeDifference(Me.startdt) < 0 Then
stepcount = -1
' Based on the time of the startdt, the values are recalculated to fall within work hours
If Hour(Me.enddt.TimeOnly) >= 18 Then
Set dt1 = New NotesDateTime(Me.enddt.DateOnly & " " & startofwd)
Call dt1.AdjustDay(stepcount)
Elseif Hour(Me.enddt.TimeOnly) < 8 Then
Set dt1 = New NotesDateTime(Me.enddt.DateOnly & " " & startofwd)
Else
Set dt1 = New NotesDateTime(Me.enddt.LocalTime)
End If
' if the start date falls on a weekend or holiday, move it
While CheckIfWorkDay(dt1) = False
Call dt1.AdjustDay(1)
Wend
' Based on the time of the enddt, the values are recalculated to fall within work hours
If Hour(Me.startdt.TimeOnly) < 8 Then
Set dt2 = New NotesDateTime(Me.startdt.DateOnly & " " & endofwd)
Call dt1.AdjustDay(-1)
Elseif Hour(Me.startdt.TimeOnly) >= 18 Then
Set dt2 = New NotesDateTime(Me.startdt.DateOnly & " " & endofwd)
Else
Set dt2 = New NotesDateTime(Me.startdt.LocalTime)
End If
' if the end date falls on a weekend or holiday, move it
While CheckIfWorkDay(dt2) = False
Call dt2.AdjustDay(-1)
Wend
Else
stepcount = 1
' Based on the time of the startdt, the values are recalculated to fall within work hours
If Hour(Me.startdt.TimeOnly) >= 18 Then
Set dt1 = New NotesDateTime(Me.startdt.DateOnly & " " & startofwd)
Call dt1.AdjustDay(stepcount)
Elseif Hour(Me.startdt.TimeOnly) < 8 Then
Set dt1 = New NotesDateTime(Me.startdt.DateOnly & " " & startofwd)
Else
Set dt1 = New NotesDateTime(Me.startdt.LocalTime)
End If
' if the start date falls on a weekend or holiday, move it
While CheckIfWorkDay(dt1) = False
Call dt1.AdjustDay(1)
Wend
' Based on the time of the enddt, the values are recalculated to fall within work hours
If Hour(Me.enddt.TimeOnly) < 8 Then
Set dt2 = New NotesDateTime(Me.enddt.DateOnly & " " & endofwd)
Call dt2.AdjustDay(-1)
Elseif Hour(Me.enddt.TimeOnly) >= 18 Then
Set dt2 = New NotesDateTime(Me.enddt.DateOnly & " " & endofwd)
Else
Set dt2 = New NotesDateTime(Me.enddt.LocalTime)
End If
' if the end date falls on a weekend or holiday, move it
While CheckIfWorkDay(dt2) = False
Call dt2.AdjustDay(-1)
Wend
End If
While dt2.dateonly <> dt1.DateOnly And dt2.TimeDifference(dt1) > 0
If CheckIfWorkDay(dt1) = True Then
Me.bdays = Me.bdays + 1
End If
Call dt1.AdjustDay(1)
Wend
If Hour(dt1.TimeOnly) > Hour(dt2.TimeOnly) Then
'If the time of the start is later in the day then the time of the end, got back to yesterday and calculate the time differently and subtract a business day
'diff = (seconds from start to end of work day) + (seconds from start of workday to end)
Call dt1.AdjustDay(-1)
Me.bdays = Me.bdays - 1
Set dt3 = New NotesDateTime(dt1.DateOnly & " " & endofwd)
diff = dt3.TimeDifference(dt1)/60
Set dt3 = New NotesDateTime(dt2.DateOnly & " " & startofwd)
diff = diff + (dt2.TimeDifference(dt3)/60)
Else
diff = dt2.TimeDifference(dt1)/60
End If
Me.bdays = Me.bdays * stepcount
Me.bhours = Fix(diff/60)
Me.bminutes = Fix(diff Mod 60)
Me.bseconds = Fix((dt2.TimeDifference(dt1)) Mod 60)
btefinish :
Exit Sub
bteerror :
Call LogError()
Resume btefinish
End Sub
Private Function CheckIfWorkDay(dt) As Boolean
Dim i As Integer
CheckIfWorkDay = True
Forall x In Me.nondays
If Weekday(dt.DateOnly) = x Then
CheckIfWorkDay = False
Exit Forall
End If
End Forall
If CheckIfWorkDay = True Then
For i = 0 To Ubound(nondates)
If dt.DateOnly = Me.nondates(i).DateOnly Then
CheckIfWorkDay = False
i = Ubound(Me.nondates)
End If
Next
End If
End Function
Public Function GetElapsedString() As String
On Error Goto geserror
Dim tmp As Integer
tmp = 0
If Me.tdays = 1 Then
GetElapsedString = "1 Day"
Elseif Me.tdays = -1 Then
GetElapsedString = "-1 Day"
Elseif Me.tdays <> 0 Then
GetElapsedString = Cstr(Me.tdays) & " Days"
End If
If GetElapsedString = "" Then tmp = Me.thours Else tmp = Abs(Me.thours)
If tmp = 1 Then
GetElapsedString = GetElapsedString & " 1 Hour"
Elseif tmp = -1 Then
GetElapsedString = GetElapsedString & " -1 Hour"
Elseif tmp <> 0 Then
GetElapsedString = GetElapsedString & " " & Cstr(tmp) & " Hours"
End If
If GetElapsedString = "" Then tmp = Me.tminutes Else tmp = Abs(Me.tminutes)
If tmp = 1 Then
GetElapsedString = GetElapsedString & " 1 Minute"
Elseif tmp = -1 Then
GetElapsedString = GetElapsedString & " -1 Minute"
Elseif tmp <> 0 Then
GetElapsedString = GetElapsedString & " " & Cstr(tmp) & " Minutes"
End If
GetElapsedString = Ltrim(GetElapsedString)
gesfinish :
Exit Function
geserror :
Call LogError()
Resume gesfinish
End Function
Public Function GetBElapsedString() As String
On Error Goto gbeserror
Dim tmp As Integer
tmp = 0
If Me.bdays = 1 Then
GetBElapsedString = "1 Day"
Elseif Me.bdays = -1 Then
GetBElapsedString = "-1 Day"
Elseif Me.bdays <> 0 Then
GetBElapsedString = Cstr(Me.bdays) & " Days"
End If
If GetBElapsedString = "" Then tmp = Me.bhours Else tmp = Abs(Me.bhours)
If tmp = 1 Then
GetBElapsedString = GetBElapsedString & " 1 Hour"
Elseif tmp = -1 Then
GetBElapsedString = GetBElapsedString & " -1 Hour"
Elseif tmp <> 0 Then
GetBElapsedString = GetBElapsedString & " " & Cstr(tmp) & " Hours"
End If
If GetBElapsedString = "" Then tmp = Me.bminutes Else tmp = Abs(Me.bminutes)
If tmp = 1 Then
GetBElapsedString = GetBElapsedString & " 1 Minute"
Elseif tmp = -1 Then
GetBElapsedString = GetBElapsedString & " -1 Minute"
Elseif tmp <> 0 Then
GetBElapsedString = GetBElapsedString & " " & Cstr(tmp) & " Minutes"
End If
GetBElapsedString = Ltrim(GetBElapsedString)
gbesfinish :
Exit Function
gbeserror :
Call LogError()
Resume gbesfinish
End Function
Public Function GetTimeString(elapsetype As String) As String
On Error Goto gtserror
Dim thisdays As Long, thishours As Integer, thisminutes As Integer, thisseconds As Integer
Dim tmp As Integer
If elapsetype = "B" Then
thisdays = Me.bdays
thishours = Me.bhours
thisminutes = Me.bminutes
thisseconds = Me.bseconds
Else
thisdays = Me.tdays
thishours = Me.thours
thisminutes = Me.tminutes
thisseconds = Me.tseconds
End If
tmp = 0
If thisdays = 1 Then
GetTimeString = "1 Day"
Elseif thisdays = -1 Then
GetTimeString = "-1 Day"
Elseif thisdays <> 0 Then
GetTimeString = Cstr(thisdays) & " Days"
End If
If GetTimeString = "" Then tmp = thishours Else tmp = Abs(thishours)
If tmp = 1 Then
GetTimeString = GetTimeString & " 1 Hour"
Elseif tmp = -1 Then
GetTimeString = GetTimeString & " -1 Hour"
Elseif tmp <> 0 Then
GetTimeString = GetTimeString & " " & Cstr(tmp) & " Hours"
End If
If GetTimeString = "" Then tmp = thisminutes Else tmp = Abs(thisminutes)
If tmp = 1 Then
GetTimeString = GetTimeString & " 1 Minute"
Elseif tmp = -1 Then
GetTimeString = GetTimeString & " -1 Minute"
Elseif tmp <> 0 Then
GetTimeString = GetTimeString & " " & Cstr(tmp) & " Minutes"
End If
GetTimeString = Ltrim(GetTimeString)
gtsfinish :
Exit Function
gtserror :
Call LogError()
Resume gtsfinish
End Function
Public Function GetDays(thistype As String) As Long
If thistype = "B" Then
GetDays = Me.bdays
Else
GetDays = Me.tdays
End If
End Function
Public Function GetHours(thistype As String) As Long
If thistype = "B" Then
GetHours = Me.bhours
Else
GetHours = Me.thours
End If
End Function
Public Function GetMinutes(thistype As String) As Long
If thistype = "B" Then
GetMinutes = Me.bminutes
Else
GetMinutes = Me.tminutes
End If
End Function
Public Function GetSeconds(thistype As String) As Long
If thistype = "B" Then
GetSeconds = Me.bseconds
Else
GetSeconds = Me.tseconds
End If
End Function
Public Function GetBDays() As Long
GetBDays = Me.bdays
End Function
Public Function GetTDays() As Long
GetTDays = Me.tdays
End Function
End Class
Usage / Example
This class has a New function that is used to instantiate the class. A start date/time, end date/time, and list of holidays is passed when the object is created. A number of public functions are available for retrieving total number of seconds elapsed as well as a string of days, hours, minutes and seconds elapsed, for both total and business days.
Error checking is being done using OpenLog.
Comments
Posted by Robert E McDonald on 01/14/2010 09:59:53 AMRendering Total Business Minutes & Addressing the Class & Function
Holiday1 = "5/31/2010,"
Holiday2 = "7/5/2010,"
Holiday3 = "9/6/2010,"
Holiday4 = "11/25/2010,"
Holiday5 = "11/26/2010,"
Dim Holi As String
Holi = Holiday1+Holiday2+Holiday3+Holiday4+Holiday5
Dim Date1 As String
Dim Date2 As String
Dim SLADayM As Long
Dim SLAHourM As Long
Dim SLATot As Long
Date1 = Cstr(Doc.SubmitDate(0))
Date2 = Cstr(Doc.ResolvedDate(0))
Set ET = New elapsedtime(Date1, Date2, Holi)
Dim ET As elapsedtime
SLADayM = ET.GetDays("B") * 12 * 60
SLAHourM = ET.GetHours("B") * 60
SLATot = ET.GetMinutes("B") + SLADayM + SLAHourM