Sujet : Re: Seeking help with a time calculation
De : saitology9 (at) *nospam* gmail.com (saito)
Groupes : comp.lang.tclDate : 01. Dec 2024, 21:43:28
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <viihpg$2p17e$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 11/29/2024 12:10 PM, Kenny McCormack wrote:
Platform is Linux.
In an Expect script, I want to calculate the number of seconds until the
next "X minutes after the hour". E.g., if X is 37 and the current time is
15:30:30, then the result I seek would be: 390. If X is 37, and the
current time is 15:50, then the result would be: 2820.
It's the number of seconds I have to wait (sleep) until that time arrives.
I have a method in AWK, which I can (and do) call from my Expect script,
but I am curious if there is a (simple) native Tcl way to do it.
I think this does the calculation you want. There is an interesting "octal" problem left as an exercise :-)
proc wait_how_long {x} {
set now [clock seconds]
set mins [clock format $now -format "%M"]
set secs [clock format $now -format "%S"]
if {$mins == $x} {
# in the minute requested
set wait 0
} elseif {$mins < $x} {
# minute has passed
# add one hour
set wait [expr {($x - $mins - 1) * 60 + (60 - $secs)}]
} else {
# some minutes ahead, within the same hour
set mindiff [expr {(60 - $mins - 1) + $x}]
set wait [expr {$mindiff * 60 + (60 - $secs)}]
}
return $wait
}