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.

calculating days

Help for those learning Tcl or writing their own scripts.
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

calculating days

Post by iamdeath »

I am trying to make a script for someone here but I am stuck in the middle I want to know how to calculate this:

Code: Select all

[14:32] -NickServ- Time registered   : Thu 23-Sep-2004 01:00:59 UTC
[14:32] -NickServ- Time now          : Fri 05-Oct-2007 09:31:06 UTC
I want to calculate that how many days ago that nickname was registered from Time now. I mean the reply should come in days, let's say 12 20 45 days. So that then I can easily use if command if numbers are > 7 then body.

Thanks
iamdeath
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

set then [clock scan "Thu 23-Sep-2004 01:00:59 UTC"]
set now  [clock scan "Fri 05-Oct-2007 09:31:06 UTC"]
set secondsInADay 86400
set daysAgo [expr {($now-$then)/$secondsInADay}]
Have you ever read "The Manual"?
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Thanks alot I will give it a try and let you know.
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Code: Select all

### Event
bind join - "#channel *!*@*" join-ignore
bind notc - "*is not a valid nickname*" notice-ignore
bind notc - "*Time registered*" notice-ignore
bind notc - "*Time now*" notice-ignore

### Command
proc join-ignore {nick uhost handle chan} {
  putquick "PRIVMSG nickserv@services.dal.net :info $nick"
}

proc notice-ignore {nick uhost hand text dest} {
if {[string match "*is not a valid nickname*" $text]} {return}
if {[string match "*Time registered*" $text]} {
set then [split $text]
set then [lrange $text 3 end]
putlog "$then Before"
}
if {[string match "*Time now*" $text]} {
set now [split $text]
set now [lrange $text 3 end]
putlog "$now After"
}
set then [clock scan "$then"]
set now  [clock scan "$now"]
set secondsInADay 86400
set daysAgo [expr {($now-$then)/$secondsInADay}]
putlog "$daysAgo Days ago"
}
This is just half of my code but at the moment I am getting this error:

Code: Select all

Thu 23-Sep-2004 01:00:59 UTC Before
Tcl error [notice-ignore]: can't read "now": no such variable
Sat 06-Oct-2007 05:16:15 UTC After
Tcl error [notice-ignore]: can't read "then": no such variable
any suggestions please? :)

Thanks
iamdeath
|AmDeAtH @ Undernet
Death is only the *Beginning*...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Rethink your code.
You only set "now" when the notice matches "Time now", and you only set "then" when the notice matches "Time registered". In both cases you extract the 4th and further words as the time.
Obviously, upon execution, only one of them will be set at any time, and thus trying to use them both as in

Code: Select all

set daysAgo [expr {($now-$then)/$secondsInADay}]
will fail.

What you'll have to do, is store the value inbetween invocations, and do proper cleanup once you've gathered all the data and produced the output.
NML_375
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Thanks for the reply, but the thing is I have

Code: Select all

set then [lrange $text 3 end] 
which is ==

Code: Select all

Thu 23-Sep-2004 01:00:59 UTC Before 
So is the case with now I don't understand how to calculate more or store it within inbetween invocations, if you please give me a little hint more I will be thankful to you

Thanks
iamdeath
|AmDeAtH @ Undernet
Death is only the *Beginning*...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

One way would be to use global variables, with proper cleanup inbetween requests...
NML_375
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Ive tried all the possible ways it's too much for me sorry for bothering you. I just cant solve it on my own
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Take a look here: http://members.dandy.net/~fbn/date.2.tcl.txt

Use that for examples of doing time calculations if you like.
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Thanks rosc actually the script works well when I give it

Code: Select all

set then [clock scan "Thu 23-Sep-2004 01:00:59 UTC"] 
set now  [clock scan "Fri 05-Oct-2007 09:31:06 UTC"] 
set secondsInADay 86400 
set daysAgo [expr {($now-$then)/$secondsInADay}]
but the problem is the date always has to get changed for ever user so iused $text and $now instead of any specific date. But it;'s not recognizing that variable I wonder why
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Code: Select all

### Event
bind join - "#Islamabad *!*@*" join-ignore
bind notc - "*is not a valid nickname*" notice-ignore
bind notc - "*Time registered*" notice-ignore
bind notc - "*Time now*" notice-ignore

### Command
proc join-ignore {nick uhost handle chan} {
  putquick "PRIVMSG nickserv@services.dal.net :info $nick"
}

proc notice-ignore {nick uhost hand text dest} {
if {[string match "*is not a valid nickname*" $text]} {return}
if {[string match "*Time registered*" $text]} {
set then [split $text]
set then [lrange $text 3 end]
set then [clock scan "$then"]
if {[string match "*Time now*" $text]} {
set now [split $text]
set now [lrange $text 3 end]
set now [clock scan "$now"]
set secondsInADay 86400
set daysAgo [expr {($now-$then)/$secondsInADay}]
putlog "$daysAgo Days ago"
}
}
}
putlog "Nickname ignore tcl Loaded!"
I even tried that its not working :(
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Code: Select all

### Event
bind join - "#Islamabad *!*@*" join-ignore
bind notc - "*is not a valid nickname*" notice-ignore
bind notc - "*Time registered*" notice-ignore
bind notc - "*Time now*" notice-ignore1

### Command
proc join-ignore {nick uhost handle chan} {
  putquick "PRIVMSG nickserv@services.dal.net :info $nick"
}

proc notice-ignore {nick uhost hand text dest} {
global then
set then [split $text]
set then [lrange $text 3 end]
set then [clock scan "$then"]
putlog "$then"
}

proc notice-ignore1 {nick uhost hand text dest} {
global now then
set now [split $text]
set now [lrange $text 3 end]
set now [clock scan "$now"]
set secondsInADay 86400
set daysAgo [expr {($now-$then)/$secondsInADay}]
putlog "$daysAgo Days ago"
putlog "$now"
}
putlog "Nickname ignore tcl Loaded!"
That works fine :D
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Usually you can set the time for "now" using the tcl 'clock' command, or the eggdrop strftime, ctime or unixtime commands, there's a few different ways to go about it, no real need to depend on chanserv to get it (although you might have to account for the different timezones between what your shell and [unixtime] and chanserv are using.

As far as how to keep the $then var across proc's, just declare it as a global var at the top of each proc that needs it.

Anyway, the logic in your script won't set the vars properly.

The errors you're getting is because this proc is being run for each and every notice the bot is receiving, so maybe nest all further processing under the 1st test for if {[string match "*Time registered*" $text]} {

eg:

Code: Select all

proc notice-ignore {nick uhost hand text dest} {
     if {[string match "*is not a valid nickname*" $text]} {return}
     if {[string match "*Time registered*" $text]} {
           set then [split $text]
           set then [lrange $text 3 end]
           set then [clock scan "$then"]
           set now  [unixtime]
           putlog "debug then '$then'"
           putlog "debug now '$now'"
           set secondsInADay 86400
           set daysAgo [expr {($now-$then)/$secondsInADay}]
           putlog "someone registered $daysAgo Days ago"
     }
}
One last piece of advice - INDENT YOUR CODE =)
Makes it much easier to debug..
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

rosc2112 wrote:Usually you can set the time for "now" using the tcl 'clock' command, or the eggdrop strftime, ctime or unixtime commands, there's a few different ways to go about it, no real need to depend on chanserv to get it (although you might have to account for the different timezones between what your shell and [unixtime] and chanserv are using.

As far as how to keep the $then var across proc's, just declare it as a global var at the top of each proc that needs it.

Anyway, the logic in your script won't set the vars properly.

The errors you're getting is because this proc is being run for each and every notice the bot is receiving, so maybe nest all further processing under the 1st test for if {[string match "*Time registered*" $text]} {

eg:

Code: Select all

proc notice-ignore {nick uhost hand text dest} {
     if {[string match "*is not a valid nickname*" $text]} {return}
     if {[string match "*Time registered*" $text]} {
           set then [split $text]
           set then [lrange $text 3 end]
           set then [clock scan "$then"]
           set now  [unixtime]
           putlog "debug then '$then'"
           putlog "debug now '$now'"
           set secondsInADay 86400
           set daysAgo [expr {($now-$then)/$secondsInADay}]
           putlog "someone registered $daysAgo Days ago"
     }
}
One last piece of advice - INDENT YOUR CODE =)
Makes it much easier to debug..
Thank your very very much it solved the problem actually I was trying to help this user thought I would learn something out of it too and I did learn many things, thanks alot to all of you guys for your great support and patience to stand me :D love you guys

HELPED

Thanks once again
iamdeath
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Time registered: Mar 07 21:22:37 2006 EST

If the format is like that, should'nt it work?
|AmDeAtH @ Undernet
Death is only the *Beginning*...
Post Reply