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.

Calculate time

Help for those learning Tcl or writing their own scripts.
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Calculate time

Post by w00f »

hey, i have a little prob. I'm trying to calc the "wait time" between "now" and some "date".
The date format is monthname/day/year (%b/%d/%Y).
i.e. date = "Jul/04/2007".
im using clock scan , but since its free-form scan i cant use -format in way to set the expected input. only -gmt and -base options are available.

atm i have this

Code: Select all

set waitime [duration [expr [clock scan $date -format {%b/%d/%Y}] - [clock seconds]]]
i get this , Tcl error: bad switch "-format": must be -base or -gmt :/
any way to fix this ?


ty in advance.
r
r0t3n
Owner
Posts: 507
Joined: Tue May 31, 2005 6:56 pm
Location: UK

Post by r0t3n »

Maybe something like this?

On my bot's tcldebug:

Code: Select all

[22:29:37] <@r0t3n> ?vip? return "[duration [expr {[clock scan 07/06/2007] - [clock seconds]}]]"
[22:29:37] -exotic- » TCL: Return: 4 weeks 5 days 30 minutes 35 seconds - 0.183 ms «
r0t3n @ #r0t3n @ Quakenet
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

the problem is the month , if the month was in number format instead of name the code certainly works.

Code: Select all

.tcl [duration [expr {[clock scan 07/06/2007] - [clock seconds]}]]
ew : Tcl: 4 weeks 5 days 1 hour 13 minutes 44 seconds
.tcl [duration [expr {[clock scan Jul/06/2007] - [clock seconds]}]]
ew : Tcl: unable to convert date-time string "Jul/06/2007"
if i convert the Jul/06/2007 to 07/06/2007 should work. but how i do that ?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

One approach might be "string map". Perhaps not the prettiest solution, but should do the trick.
NML_375
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Code: Select all

set date [string map "{Jan} {01} {Feb} {02} {Mar} {03} {Apr} {04} {May} {05} {Jun} {06} {Jul} {07} {Aug} {08} {Sep} {09} {Oct} {10} {Nov} {11} {Dec} {12}" $date]
Like he said, not the prettiest.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

The mapping is supposed to be a list, so it's better to use:

Code: Select all

set date [string map {Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12} $date]
instead.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Re: Calculate time

Post by Sir_Fz »

w00f wrote:hey, i have a little prob. I'm trying to calc the "wait time" between "now" and some "date".
The date format is monthname/day/year (%b/%d/%Y).
i.e. date = "Jul/04/2007".
im using clock scan , but since its free-form scan i cant use -format in way to set the expected input. only -gmt and -base options are available.

atm i have this

Code: Select all

set waitime [duration [expr [clock scan $date -format {%b/%d/%Y}] - [clock seconds]]]
i get this , Tcl error: bad switch "-format": must be -base or -gmt :/
any way to fix this ?


ty in advance.
The [clock scan] command does not have a -format switch.

Edit: Suggested [clock format] but the [string map] seems to be the only (best) solution.
w
w00f
Halfop
Posts: 49
Joined: Wed Oct 04, 2006 6:50 pm

Post by w00f »

thanks guys , ill try string map :)
A
AskMe
Voice
Posts: 23
Joined: Wed Apr 11, 2007 4:22 pm
Location: Canada

Post by AskMe »

Sir_Fz wrote:The mapping is supposed to be a list, so it's better to use:

Code: Select all

set date [string map {Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12} $date]
instead.
i try this one (in french)

Code: Select all

set date [string map {Jan 01 Fév 02 Mars 03 Avr 04 Mai 05 Juin 06 Juil 07 Aout 08 Sept 09 Oct 10 Nov 11 Déc 12} $date] 
and i got this error when rehash

Code: Select all

can't read "date": no such variable
while executing
"string map {Jan 01 Fév 02 Mars 03 Avr 04 Mai 05 Juin 06 Juil 07 Aout 08 Sept 09 Oct 10 Nov 11 Déc 12} $date"
invoked from within
"set date [string map {Jan 01 Fév 02 Mars 03 Avr 04 Mai 05 Juin 06 Juil 07 Aout 08 Sept 09 Oct 10 Nov 11 Déc 12} $date] "
what i do wrong?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Post the whole script, as it would seem you've placed that line of code incorrectly...
NML_375
A
AskMe
Voice
Posts: 23
Joined: Wed Apr 11, 2007 4:22 pm
Location: Canada

Post by AskMe »

Code: Select all

set date [string map {Jan 01 Fév 02 Mars 03 Avr 04 Mai 05 Juin 06 Juil 07 Aout 08 Sept 09 Oct 10 Nov 11 Déc 12} $date] 

##################
# Voice database #
##################

proc addvoicedb {nick host hand chan args} {
	global sql botnick mysqlink date UCSRegistered

  set username [UCS:Registered:IsRegistered $nick]
  set hostmask "*!*@[lindex [split $host @] 1]"

	mysqlexec $mysqlink "INSERT INTO voice
(nick,username,chan,host,ajouté_le) VALUES ('$nick','$username','$chan','$hostmask','$date')"
	putlog "$nick a été ajouté a la liste autovoice avec le username $username" 
 	putserv "NOTICE $nick :Vous etes maintenant ajouté à la liste auto voice sur $chan sous le username $username"
}
the whole script i a bit messy but this is the only procedure i use date
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

$date is not a predefined variable in globalspace, so you'll have to set it to some value before you attempt to read it.
It would seem to me that you're trying to insert current date/time into a mysql-database; in this case, I'd suggest you use the mysql-macro NOW(). Another option would be to use the "clock format" command.

Be adviced that $date will not automatically be updated to hold current time...
NML_375
A
AskMe
Voice
Posts: 23
Joined: Wed Apr 11, 2007 4:22 pm
Location: Canada

Post by AskMe »

It would seem to me that you're trying to insert current date/time into a mysql-database
your right



in fact i use

Code: Select all

set date [clock format [clock scan "now"] -format %Y.%m.%d]
but i try to replace it by the maping of Sir_Fz to have the month listed with a name and not with a number
...

as you can see im a not a pro un tcl ;)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Reading the manual page I linked on clock format, it should make it obvious you have no use for that string mapping, as the format used would already print the month in numbers.

Also, executing that code you posted once in globalspace will only result in $date holding the time for that particular moment, and not current date.

A proper example, you'll probably need to modify it to suite your needs:

Code: Select all

proc addvoicedb {nick host hand chan args} {
 global mysqlink UCSRegistered
 set username [UCS:Registered:IsRegistered $nick]
 set hostmask "*!*@[lindex [split $host @] 1]"
 mysqlexec $mysqlink "INSERT INTO voice (nick,username,chan,host,ajouté_le) VALUES ('$nick','$username','$chan','$hostmask',NOW())"
}
Or

Code: Select all

proc addvoicedb {nick host hand chan args} {
 global mysqlink UCSRegistered
 set username [UCS:Registered:IsRegistered $nick]
 set hostmask "*!*@[lindex [split $host @] 1]"
 mysqlexec $mysqlink "INSERT INTO voice (nick,username,chan,host,ajouté_le) VALUES ('$nick','$username','$chan','$hostmask','[clock [clock seconds] -format "%Y.%m.%d"]')"
}
NML_375
A
AskMe
Voice
Posts: 23
Joined: Wed Apr 11, 2007 4:22 pm
Location: Canada

Post by AskMe »

Also, executing that code you posted once in globalspace will only result in $date holding the time for that particular moment, and not current date.
thanks i didnt know that info

and the link have almost all info i need

Code: Select all

proc addvoicedb {nick host hand chan args} { 
 global mysqlink UCSRegistered 
 set username [UCS:Registered:IsRegistered $nick] 
 set hostmask "*!*@[lindex [split $host @] 1]" 
 mysqlexec $mysqlink "INSERT INTO voice (nick,username,chan,host,ajouté_le) VALUES ('$nick','$username','$chan','$hostmask','[clock [clock seconds] -format "%Y.%m.%d"]')" 
}
if i want the monthname i have to replace the %m by %b or %B right?

if i want to replace the monthname by the monthname but in french is it possible?

sorry if im not clear but my english is limited.. ;)
Post Reply