Ticks => Hours: Minutes: Seconds?

  • Thread starter (PeteCresswell)
  • Start date
P

(PeteCresswell)

In the interest of not re-inventing this particular wheel....

Does anybody know of an API call that will accept a tick count
and return the corresponding hours/minutes/seconds?

Googling, I see some stuff for Java, and at least one proprietary
product... but nothing VBA-oriented jumps out at me.
 
P

PeteCresswell

Does what I have in my April, 2004 "Access Answers" column in Pinnacle
Publication's "Smart Access" help? You can download the column (and sample
database) for free athttp://www.accessmvp.com/DJSteele/SmartAccess.html

Searching on "Time", I found two items:
-----------------------------------------------------------------
October 2003: How to be regular (and about time!)

April 2004: Mind the gap, take your time, and how long is a string?
-----------------------------------------------------------------

The second one has conversion of ticks to seconds, but I couldn't
find anything about ticks to HH:MM:SS.

Off the top of your head, can you think of any other articles I should
have checked?

BTW: Thanks for all the great stuff in the other articles.
 
D

Douglas J. Steele

If you know seconds, converting to hh:mm:ss is pretty straightforward:

Function FormatSeconds(TotalSeconds As Long) As String
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long

lngHours = TotalSeconds \ 3600
lngMinutes = TotalSeconds Mod 3600
lngSeconds = lngMinutes Mod 60
lngMinutes = lngMinutes \ 60

FormatSeconds = lngHours & ":" & _
Format(lngMinutes, "00") & ":" & _
Format(lngSeconds, "00")

End Function

If TotalSeconds is less than 86400 (the number of seconds in a day), you can
use:

Format(TotalSeconds/86400, "hh:nn:ss")
 
P

PeteCresswell

If you know seconds, converting to hh:mm:ss is pretty straightforward:

Function FormatSeconds(TotalSeconds As Long) As String
Dim lngHours As Long
Dim lngMinutes As Long
Dim lngSeconds As Long

lngHours = TotalSeconds \ 3600
lngMinutes = TotalSeconds Mod 3600
lngSeconds = lngMinutes Mod 60
lngMinutes = lngMinutes \ 60

FormatSeconds = lngHours & ":" & _
Format(lngMinutes, "00") & ":" & _
Format(lngSeconds, "00")

End Function

If TotalSeconds is less than 86400 (the number of seconds in a day), you can
use:

Format(TotalSeconds/86400, "hh:nn:ss")


Thanks again.

I'm going to go with that.
e.g.
----------------------------------------------------------
mySeconds = tickCount/1000

If mySeconds < mySecondsPerDay then
strTime = Format$(mySeconds/mySecondsPerDay, "hh:nn:ss"
Else
(do the Mod thing and concatonate....)
End If
 
P

PeteCresswell

Here's what I came up with in the end:
=================================================================
Public Function TicksToTime(ByVal theTicks As Double) As String
debugStackPush mModuleName & ": TicksToTime"
On Error GoTo TicksToTime_err

' PURPOSE: To convert a tick count (thousandths of a second) into
' a time string in the format "HH:NN:SS"
' ACCEPTS: Number of ticks
' RETURNS: Formatted time string


Dim myHours As Long
Dim myMinutes As Long
Dim mySeconds As Long

Dim strTime As String

Const secondsPerDay As Long = 86400
Const secondsPerHour As Long = 3600
Const secondsPerMinute As Long = 60
Const minutesPerHour As Long = 60

mySeconds = theTicks * 0.001

If mySeconds < secondsPerDay Then
strTime = Format$(mySeconds / secondsPerDay, "hh:nn:ss")
Else
myHours = mySeconds / secondsPerHour
myMinutes = mySeconds Mod secondsPerHour
mySeconds = myMinutes Mod secondsPerMinute
myMinutes = myMinutes / secondsPerMinute

strTime = Format$(myHours, "#,#00") & ":" & Format$(myMinutes,
"00") & ":" & Format$(mySeconds, "00")
End If

TicksToTime = strTime

TicksToTime_xit:
debugStackPop
On Error Resume Next
Exit Function

TicksToTime_err:
bugAlert True, ""
Resume TicksToTime_xit
End Function
=================================================================
 
P

PeteCresswell

mySeconds = theTicks * 0.001


Oops! For readability/documentation, of course that shb:

Const ticksPerSecond = 1000

mySeconds = theTicks/ticksPerSecond
 
Top