Time Validation Procedure
On one of my asp pages, I was passing a time value to a SQL Server stored procedure. The procedure, sp_help_jobhistory, would not accept a time value greater than the integer 235959. My time value was calculated, so there where situations where the integer would be something like 248434. I wrote this code to convert the value to one that would be accepted by the procedure:
<%
'Validate that intEndTime is not greater than 235959.
'Adjust intEndTime to an acceptable value if it is not.
intUVEndTime = intStartTime + intRunTime
Dim intTime, intHour, intMinute, intSecond
Dim intNewMinute, intAdditionalHour, intAdditionalMinute
Dim intFinalHour, intFinalMinute, intFinalSecond
intTime = intUVEndTime
'The "Fix()" function returns the integer part of a number
intHour = Fix((intTime / 10000))
intMinute = Fix((intTime Mod 10000) / 100)
intSecond = Fix((intTime Mod 100))
'The "Mod" operator returns the modulus of two numbers
intNewMinute = (intMinute Mod 60)
intAdditionalHour = Fix(intMinute / 60)
intAdditionalMinute = Fix(intSecond / 60)
intFinalHour = intHour + intAdditionalHour
intFinalMinute = intNewMinute + intAdditionalMinute
intFinalSecond = (intSecond Mod 60)
'The maximum time value accepted by the "sp_help_jobhistory" stored procedure is 235959. If intFinalHour is greater than 23, intEndTime will be set to 235959.
If intFinalHour > 23 then
intFinalHour = 23
intFinalMinute = 59
intFinalSecond = 59
End If
'The "sp_help_jobhistory" stored procedure requires a leading zero for minute or second values between 0 and 9. The following code adds the leading zero and converts the new values to strings.
strHour = CStr(intFinalHour)
If intFinalMinute >= 0 and intFinalMinute <= 9 then
strMinute = "0" & CStr(intFinalMinute)
Else
strMinute = CStr(intFinalMinute)
End If
If intFinalSecond >= 0 and intFinalSecond <= 9 then
strSecond = "0" & CStr(intFinalSecond)
Else
strSecond = CStr(intFinalSecond)
End If
'Concatenate the strings
strTime = strHour & strMinute & strSecond
'Convert the concatenated string to a Long
intNewTime = CLng(strTime)
'Assign the new value to intEndTime
intEndTime = intNewTime
'Print the new time
Response.Write "<br><br>intNewTime = " & intNewTime
%>
|