0

I need to compute time in 2 different instants and return the difference in the format days, hours, minutes, seconds.

However, when I try doing that, the difference is not correct as it keeps adding 1hour to the result. Instead of 00:00:07, it returns 01:00:07. On top of that, I can't understand the default date from 1 Jan 1970.

Any ideas? Thanks, Pedro

set t1 [clock seconds]

1634735417

set t2 [clock seconds]

1634735424

set t [expr $t2 - $t1]

7

clock format $t

Thu Jan 01 01:00:07 CET 1970

  • Instants are not durations. Tcl measures instants in offsets from the Unix epoch (i.e., start of 1970 in GMT) and provides nothing explicitly for rendering durations (though it does have code for the arithmetic you'd do with them when combining a duration with an instant). Perhaps it should? – Donal Fellows Oct 20 '21 at 14:21

1 Answers1

1

Please see this previous question:
Convert seconds to hours minutes seconds format

clock format $seconds will return a formatted time string that represents the time/date of the moment $seconds after the beginning of the Unix epoch (https://en.wikipedia.org/wiki/Unix_time).

Your local timezone matters. I'm guessing you're in Spain, one hour ahead of London GMT midnight. A computer in California returns this for Pacific Standard Time, one second into the Unix epoch.

tcl8.6.8> clock format 1
Wed Dec 31 16:00:01 PST 1969

To convert a simple seconds duration into days/hours/minutes/seconds, please see the answer in the link I shared. A time duration vs a specific moment in time are two slightly different problems.

Chris Heithoff
  • 1,787
  • 1
  • 5
  • 14
  • Hi, Thanks for the reply. When I enter the command above, it returns: Thu Jan 01 01:00:01 CET 1970 – Pedro Cardoso Oct 20 '21 at 14:12
  • In other words, `clock format` works with instants and not durations. – Donal Fellows Oct 20 '21 at 14:18
  • Hi, I've followed the link above (Convert seconds to hours minutes seconds format) and tried the following suggestion: `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]` Can someone explain to me why the % (mod) is used? How can I modify this functions to show microseconds as well? – Pedro Cardoso Oct 22 '21 at 10:50
  • Use `clock microseconds` instead of `clock seconds`. Also look up `clock clicks` – Chris Heithoff Oct 22 '21 at 13:34
  • @ChrisHeithoff Hi, Yes, I am using clock microseconds, but how do I convert to days, hours, minutes, seconds, ms, us ? I get really confused with the MODs when converting. Thanks, Pedro\ – Pedro Cardoso Oct 22 '21 at 15:23
  • If you have 2001ms, you probably know it's 2 seconds + 1 ms without thinking hard. `expr {2001 / 1000}` will tell you how many 1000's fit into 2001, and `expr {2001 % 1000}` will tell you how many ms are leftover after subtracting multiples of 1000. In Tcl with integers, `/` will do floor division, meaning it will return the integer part only. – Chris Heithoff Oct 22 '21 at 15:39