Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

quick way of converting seconds to HH:MM:SS ?

2 views
Skip to first unread message

Mel

unread,
Mar 30, 2005, 10:00:26 AM3/30/05
to
i have a number line 457.000 seconds that i need to convert to HH:MM:SS

i am doing it the hard way devide/remainer etc.

was wondering if clock format or something similar could help ?

thanks


c0ldbyte

unread,
Mar 30, 2005, 10:33:33 AM3/30/05
to

Well you arent going to beable to convert that to HH:MM:SS but you can convert
that to MM:SS.*** since the seconds that you listed there only compute to
7.6166667 minutes your output in the original format would look like
00:08:02 thats minutes and seconds. But if your number was larger in the first
place the hours could be taken out of it.

--
( When in doubt, use brute force. -- Ken Thompson 1998 )

Donal K. Fellows

unread,
Mar 30, 2005, 10:44:37 AM3/30/05
to
Mel wrote:
> i have a number line 457.000 seconds that i need to convert to HH:MM:SS
> was wondering if clock format or something similar could help ?

Not really; you're formatting a duration and [clock format] is for
formatting time instants. Not the same thing at all (as you'd know when
you had more than 24 hours worth of duration!) Try this:

proc duration secs {
set hours [expr {int($secs) / 3600}]
set mins [expr {int($secs) / 60 % 60}]
set secs [expr {int($secs) % 60}]
return [format "%02d:%02d:%02d" $hours $mins $secs]
}

Donal.

Glenn Jackman

unread,
Mar 30, 2005, 11:44:37 AM3/30/05
to

Let's see:
% set time 457.000
457.000
% clock format $time -format {%T}
expected integer but got "457.000"

Oops, convert time to an int first:

% clock format [expr {int($time)}] -format {%T}
19:07:37

Hmmm, time zone issues. How about:

% clock format [expr {int($time)}] -format {%T} -gmt true
00:07:37

Maybe you want to keep the milliseconds:

set time 12345.678
foreach {seconds fraction} [split $time "."] {break}
set time "[clock format $seconds -format {%T} -gmt true].$fraction"
puts $time ;# ==> 03:25:45.678

In Tcl 8.5, the "foreach" line could become:
lassign [split $time "."] seconds fraction

--
Glenn Jackman
NCF Sysadmin
gle...@ncf.ca

Donald Arseneau

unread,
Mar 30, 2005, 7:03:55 PM3/30/05
to
"Mel" <mel.m....@hp.com> writes:

I immediately thought "I have an old delta-time function", but it is
for the opposite conversion.

I think your conversion is indeed easy...if you are limited to
time intervals of less than a day.

clock format [expr {round($s)+[clock scan 0:00:00]}] -format {%H:%M:%S}

You may want to add an explicit (arbitrary) time zone specification
because I suspect this may give a 1-hour error twice a year, when
the switches to/from daylight saving time happen.

clock format [expr {round($s)+[clock scan 0:00:00 -gmt 1]}] \
-gmt 1 -format {%H:%M:%S}


For reference, here is the opposite conversion.


# Convert a delta-time spec to seconds.
# The time spec can be in many formats; all of the following are equal:
# 1:30, 5400 s, 90 min, 1.5hr, 01:30:00, 5400
# Units are any strings that begin with "s", "m", or "h".
#
proc deltaTime { spec } {
if { [string first ":" $spec] >= 0 } {
return [expr { [clock scan $spec] - [clock scan 0:00:00] }]
} else {
set mult 0.0
if { [scan "$spec s" { %f %1s} t u] == 2 } {
switch -- $u {
s { set mult 1. }
m { set mult 60. }
h { set mult 3600. }
}
}
if { $mult > 0.0 } {
return [expr { round($t*$mult) }]
} else {
return -code error "unable to convert delta-time string \"$spec\""
}
}
}


--
Donald Arseneau as...@triumf.ca

Sean Woods

unread,
Mar 31, 2005, 9:29:17 AM3/31/05
to
I have to second Donal on that. This is a math problem more than
anything.

BTW, I've been looking for a better way to format leading zeros. Thanx.

-Sean

0 new messages