I have a function I have written in VB for use in SSRS 2012. It takes an integer and converts it to full text duration - for example 900 becomes "15 minutes", 9000 becomes "2 hours 30 minutes", and 7201 is "2 hours 1 second".
The code works, and is:
Public Function SecondsFullText(ByVal TotalSeconds As Integer) As String
Dim hours As Integer = new Integer()
Dim minutes As Integer = new Integer()
Dim seconds As Integer = new Integer()
Dim hourString as String = ""
Dim minuteString as String = ""
Dim secondString as String = ""
hours = floor(TotalSeconds / 3600)
minutes = floor((TotalSeconds mod 3600) / 60)
seconds = (TotalSeconds mod 3600) mod 60
If hours = 1 Then
hourString = Cstr(hours) & " hour"
Else If hours > 1 Then
hourString = Cstr(hours) & " hours"
End If
If minutes = 1 Then
minuteString = Cstr(minutes) & " minute"
Else If minutes > 1 Then
minuteString = Cstr(minutes) & " minutes"
End If
If seconds = 1 Then
secondString = Cstr(seconds) & " second"
Else If seconds > 1 Then
secondString = Cstr(seconds) & " seconds"
End If
If hours > 0 and (minutes > 0 or seconds > 0) Then
hourString = hourString & " "
End If
If minutes > 0 and seconds > 0 Then
minuteString = minuteString & " "
End If
return hourString & minuteString & secondString
End Function
My question is: how do I make this better? It seems very clunky, with too many IFs to handle the different possibilities for single/plural, spaces, etc. I feel that this can be better, but I'm not sure how.
This is called in Report Builder 3.0 / SSRS 2012 with this expression: =Code.SecondsFullText(Parameters!Integer.Value)