• Seeking help with a time calculation

    From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.tcl on Fri Nov 29 17:10:57 2024
    From Newsgroup: comp.lang.tcl

    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.
    --
    Men rarely (if ever) manage to dream up a God superior to themselves.
    Most Gods have the manners and morals of a spoiled child.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Gerald Lester@Gerald.Lester@gmail.com to comp.lang.tcl on Sat Nov 30 21:47:24 2024
    From Newsgroup: comp.lang.tcl

    On 11/29/24 11:10, 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.

    The [clock add], [clock seconds], and maybe the [clock format] commands
    are your friends.


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Sun Dec 1 15:43:28 2024
    From Newsgroup: comp.lang.tcl

    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
    }



    --- Synchronet 3.20a-Linux NewsLink 1.114