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.

Run a proc between specific times/days

Help for those learning Tcl or writing their own scripts.
Post Reply
a
adi2247
Voice
Posts: 10
Joined: Tue Nov 30, 2010 12:45 am

Run a proc between specific times/days

Post by adi2247 »

currently i have a proc similar to this:

Code: Select all

proc timer {min args} { 
 if {[scan $min %d] % 5 == 0} { 
  #Code goes here 
   } 
} 
which runs a proc every 5 min, but i would also like to have it check to see if the system time is between 9am-5pm mon-fri before running.

i think it may be somthing similar to

Code: Select all

proc timer {min args} { 
set systemTime [clock seconds]
 if { [clock format $systemTime -format %H] >= 9 && [clock format $systemTime -format %H] <= 17 &&
 [scan $min %d] % 5 == 0} { 
  #code goes here 
   } 
} 
which i would think would work but doesnt seem to be, inaddition im not sure how to equate day of the week to a numeric value to check against either. Anyhelp is greatly apreciated.

using this for reference http://www.tcl.tk/man/tcl/tutorial/Tcl41.html
w
willyw
Revered One
Posts: 1200
Joined: Thu Jan 15, 2009 12:55 am

Re: Run a proc between specific times/days

Post by willyw »

adi2247 wrote: ...
im not sure how to equate day of the week to a numeric value to check against either.

How about strftime ?
Try strftime %u

Ref:
http://www.eggheads.org/support/egghtml ... mands.html
and find strftime

Also:
http://linux.about.com/library/cmd/blcmdl3_strftime.htm


Is the .tcl command enabled on your bot? If so, you can do some quick experiments with the strftime command.
Example:

Code: Select all

<mynick> .tcl strftime %u
<botnick> Tcl: 2
(Today is Tuesday, and with strftime Monday =1)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

If you've got the 1.6.20 version of eggdrop, then you could use the new "CRON" binding rather than the "TIME" binding. In that case, the code would look like this:

Code: Select all

bind cron - {*/5 * * * 1-5} timer
proc timer {minute hour day month weekday} {
  #code goes here
}
If you'd rather use the "TIME" binding, and parse the various arguments passed to the proc as integers, you'll have to remember that single-digit numbers are zero-prefixed, causing 08 and 09 to be treated as invalid octal numbers. This is fairly easy to solve though, using something like this:

Code: Select all

bind time - * someproc {min hour day month year} {
  set min [string trimleft $min 0]
  set hour [string trimleft $hour 0]
  if {$min % 5 == 0 && $hour >= 9 && $hour <= 17} {
...
This won't solve the issue of weekdays though, so you'll have to resort to strftime for this, as suggested by willyw
NML_375
User avatar
username
Op
Posts: 196
Joined: Thu Oct 06, 2005 9:20 am
Location: Russian Federation, Podolsk
Contact:

Post by username »

nml375, but in http://www.eggheads.org/support/egghtml ... html#binda there is other arguments.

bind cron <flags> <mask> <proc>
proc-name <minute> <hour> <day> <weekday> <year>


And in bind description I read:
...It can contain up to five fields: minute, hour, day, month, weekday; delimited by whitespace....
So, this is mistake in this line proc-name <minute> <hour> <day> <weekday> <year>?
Архив TCL скриптов для ботов Eggdrop/Windrop:
http://egghelp.ru/
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

username:
If you instead open the tcl-commands.doc that's included with the source, you'll read <weekday> instead of <year>, so it's my best guess they've made a typo with the html-docs.
NML_375
p
pseudo
Halfop
Posts: 88
Joined: Mon Nov 23, 2009 4:52 am
Location: Bulgaria
Contact:

Post by pseudo »

Yeah, this was a mistake indeed. It's fixed in 1.8 cvs: http://cvs.eggheads.org/viewvc/eggdrop1 ... 1.2&r2=1.3
a
adi2247
Voice
Posts: 10
Joined: Tue Nov 30, 2010 12:45 am

Post by adi2247 »

ty all for the sugestions! i will try some of these and report back!
a
adi2247
Voice
Posts: 10
Joined: Tue Nov 30, 2010 12:45 am

Re: Run a proc between specific times/days

Post by adi2247 »

strftime seems to be working great for the most part. Sometimes i have to restart the bot completely vs a .rehash but that may or may not be related to the code below, and more related to numerous mysql connections, not sure yet. But if anyone sees anything glaringly wrong w the below please let me know

the two instances im using for this are:

Code: Select all

if { [strftime %u] >= 1 && [strftime %u] <= 6 && [strftime %H%M] >= 930 && [strftime %H%M] <= 1700 &&
    [scan $min %d] % 5 == 0} {
and

Code: Select all

if { [strftime %u] == 7 && [strftime %H] >= 18 && [scan $min %d] % 15 == 0 ||
     [strftime %u] >= 1 && [strftime %u] <= 5 && [scan $min %d] % 15 == 0} {
also what nml375 posted that strftime %H before noon will report back "01" or "02" for example vs just "1" or "2". However if you try to evaluate:

if { [strftime %H] >= 08 }

the bot will error and say this looks like an octal number just as nml375 mentioned.

** and i just realized that the first block of code i posted was NOT running before 10am today so the times will HAVE to be evaluated in the manner that nml375 posted as tcl/eggdrop aparently does not view 01 to == 1
thats a good finding

but the cron method if probably the the best way to run this proc m-f and ill look at the times seperately as mentioned in his post for simplicity

the second block of code i posted works fine since it only evaluates the hour if its greater than 2 digits anyway. cron may not work for this because it needs to look at different times if its a sunday
Post Reply