This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Abbreviate Time

Help for those learning Tcl or writing their own scripts.
Post Reply
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Abbreviate Time

Post by intel »

Is there a way when my bot announces the time instead of hours it will display hrs (same for minutes and seconds)?
set time1 [unixtime]
incr time1 -$timestamp
set ago [duration $time1]
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

set ago [string map {"year" "yr" "week" "wk" "day" "dy" "hour" "hr" "minute" "min" "second" "sec"} [duration $time1]]
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Post by intel »

Thanx, but will that do hrs or just hr. Like 5 hrs ago or 5 hr ago?
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

intel wrote:Thanx, but will that do hrs or just hr. Like 5 hrs ago or 5 hr ago?
It will change any occurence of "hour" to "hr" which is part of both words, hour and hours. So "hours" will be changed to "hrs", yes. The same goes for the rest of the abbreviations.
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Post by intel »

awesome... thanx :lol:
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Post by intel »

oh would you know how to get rid of the space? It says 1 hr 15 min ago would like 1hr 15min ago


Thanks.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

You must include the space character in the mapped items to be replaced as follows :-

Code: Select all

set ago [string map {" year" "yr" " week" "wk" " day" "dy" " hour" "hr" " minute" "min" " second" "sec"} [duration $time1]]
For example :-

[12:19] <@arfer> % return [string map {" year" "yr" " week" "wk" " day" "dy" " hour" "hr" " minute" "min" " second" "sec"} [duration [unixtime]]]
[12:19] <@Baal> 39yrs 3wks 12hrs 19mins 6secs
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Post by intel »

Great. It works. Thanx appreciate the all the help.
Just curious if I wanted say 12h 15m 10s would that just be
" hour" "" " minutes" "" or will just put either nothing or a s at the end (like 12 or 12s instead of 12hr or 12hrs)?
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

How about you just fiddle with it until it works the way you want..

http://www.tcl.tk/man/tcl8.2.3/TclCmd/string.htm#M34
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Post by intel »

lol been tryin... used /m /s [m] [s] not working... so obviously doing something wrong.
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

You are mapping "{from} {to}", so if you want to replace hour with h, ...

{hour} {h} {minute} {m} etc.. you might need to do extra ones for the s, or try with {hour*} but that might end up replacing the whole string <_<. maybe hour? to check for the 1 additional char.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

set ago [string map {" years" "y" " weeks" "w" " days" "d" " hours" "h" " minutes" "m" " seconds" "s" " year" "y" " week" "w" " day" "d" " hour" "h" " minute" "m" " second" "s"} [duration $time1]] 
Make a big long string map in that case. Incith was correct, test these things on your own, so you learn intuitively and interactively.
i
intel
Halfop
Posts: 57
Joined: Tue Feb 26, 2008 11:51 pm

Post by intel »

Thanx. I did try that really long way but I didnt have it in that order so it did not work. It was still doing ms (for minutes) and ss (for seconds)

Thanx. Appreciate the help.
Post Reply