将秒转换为小时分钟秒格式

Pra*_*ian 2 tcl

我需要将时间转换为HH:MM:SS.mm格式化.正在从嵌入式设备读取秒输入,它是一种double格式seconds.millseconds.我尝试了以下转换代码,但它失败了:

set cpu_time [function_that_fetches_the_time]
puts "[clock format $cpu_time -format {%H:%M:%S}]"
Run Code Online (Sandbox Code Playgroud)

这失败了,错误

expected integer but got "98.92"
Run Code Online (Sandbox Code Playgroud)

抛出

"ParseFormatArgs {*}$args"
    (procedure "::tcl::clock::format" line 6)
Run Code Online (Sandbox Code Playgroud)

我可以将double转换为整数,上面的方法也可以,但是输出显示中的毫秒数也很好.

此外,什么是时钟格式说明符为毫秒?

编辑:

似乎只转换double为a int也不起作用.我试过了

puts "[clock format [expr int($cpu_time)] -format {%H:%M:%S}]"
Run Code Online (Sandbox Code Playgroud)

这会导致一些奇怪的时间.例如,当嵌入式设备返回3.53(并将其转换为a 3)时,打印出来的时间是17:00:03.

Don*_*ows 5

格式化可靠工作的区间的最简单方法是手动完成所有操作.

set cpu_time [function_that_fetches_the_time]
set cpu_ms   [expr { int(fmod($cpu_time, 1.0) * 1000) }]
set cpu_secs [expr { int(floor($cpu_time)) % 60 }]
set cpu_hrs  [expr { int(floor($cpu_time / 3600)) }]
set cpu_mins [expr { int(floor($cpu_time / 60)) % 60 }]
puts [format "%d:%02d:%02d.%3d" $cpu_hrs $cpu_mins $cpu_secs $cpu_ms]
Run Code Online (Sandbox Code Playgroud)

你可能想把它包装成一个程序......