I've found this script on the internet. It's a bit old, and the developers page is long gone. The code works, but I'm trying to clean it up a bit. For example: If weeks = 0, then don't display it. I'm getting errors from the console about extra tokens. I've tried to google this error, but nothing solid is coming up. Looking for some insight.
The commented section is my attempt at "cleaning it up". Here's the error I'm receiving.
Tcl error [uptime]: syntax error in expression "$::time(week) = 0": extra tokens at end of expression
With the section commented, it works fine. Just trying to clean things up so it doesn't return "I've been online for 0 weeks 0 days 0 hours 1 minute".
bind pub "-|-" !uptime uptime
proc uptime { nick host handle channel text } {
putquick "PRIVMSG $channel :I have been online for [eggtime]."
}
proc eggtime {} {
set ::time(uptime) [expr [clock seconds]-$::uptime]
set ::time(week) [expr $::time(uptime)/604800]
set ::time(uptime) [expr $::time(uptime)-$::time(week)*604800]
set ::time(days) [expr $::time(uptime)/86400]
set ::time(uptime) [expr $::time(uptime)-$::time(days)*86400]
set ::time(hour) [expr $::time(uptime)/3600]
set ::time(uptime) [expr $::time(uptime)-$::time(hour)*3600]
set ::time(mins) [expr $::time(uptime)/60]
set ::time(uptime) [expr $::time(uptime)-$::time(mins)*60]
set ::time(secs) $::time(uptime)
#if {$::time(week) = 0} {
# set ::time(return) "$::time(days) day\(s\), $::time(hour) hour\(s\), $::time(mins) minute\(s\) and $::time(secs) second\(s\)"
#} else if {$::time(days) = 0} {
# set ::time(return) "$::time(hour) hour\(s\), $::time(mins) minute\(s\) and $::time(secs) second\(s\)"
#} {
# set ::time(return) "$::time(week) week\(s\), $::time(days) day\(s\), $::time(hour) hour\(s\), $::time(mins) minute\(s\) and $::time(secs) second\(s\)"
#}
set ::time(return) "$::time(week) week\(s\), $::time(days) day\(s\), $::time(hour) hour\(s\), $::time(mins) minute\(s\) and $::time(secs) second\(s\)"
return $::time(return)
}
The equality operator is ==.
You are missing an else
in the final section of your commented block. You should also be using elseif
and not else if
. This is not C.
However, here is one I wrote previously that takes an elapsed interval and returns a string in English. Its roughly what you are aiming at.
# paste::delta --
#
# Returns the time difference between the given and current time
# as an english string.
#
proc ::paste::delta {time} {
set r $time
catch {
set delta [expr {[clock seconds] - $time}]
if {$delta < 60} {
set r "$delta secs ago"
} elseif {$delta < 3600} {
set r "[expr {$delta / 60}] mins ago"
} elseif {$delta < 86400} {
set r "[expr {$delta / 3600}] hours ago"
} else {
set r "[expr {$delta / 86400}] days ago"
}
}
return $r
}
So you get things like [paste::delta 7260] giving "2 hours ago" or 310 giving "5 minutes ago".
One other thing - with expr
statements, always brace the body of the expression with curly braces. Otherwise the parser has to parse the body again and it can have significant performance cost.