mIRC Home    About    Download    Register    News    Help

Print Thread
$time of the world #168005 02/01/07 05:08 PM
Joined: Dec 2005
Posts: 54
B
Bundy Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Dec 2005
Posts: 54
Would it be relatively easy to do this:

<bundy> !time
<bot> -
*bot lewks at his watch...
<bot> -
<bot> Johannesburg - 18:40 Tuesday 2 January 2007
<bot> Auckland - 06:40 Wednesday 3 January 2007
<bot> Vancouver - 08:40 Tuesday 2 January 2007
<bot> Tokyo - 01:40 Wednesday 3 January 2007
<bot> -

Re: $time of the world [Re: Bundy] #168007 02/01/07 05:36 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Easy enough:

Code:
on *:load:{
  hmake timezones
  ;; Add offsets from GMT in seconds
  hadd timezones _list Johhannesburg Auckland Vancouver Tokyo
  hadd timezones Johhannesburg 0
  hadd timezones Auckland 43200
  hadd timezones Vancouver -36000
  hadd timezones Tokyo 32400
}

on *:text:!time:#:{
  var %list = $hget(timezones,_list)
  var %n = $numtok(%list,32), %i = 1
  while (%i <= %n) {
    var %place = $gettok(%list,%i,32), %offset = $hget(timezones,%place)
    var %time = $gmt + %offset
    msg $chan %place - $asctime(%time)
    inc %i 
  }
}


I'm fairly certain of the calculations here, although my offset values might not be quite right...

To extend, just add an entry to the hash with the appropriate offset in seconds from GMT, and add the name to the _list item. (The latter is strictly not necessary, but it does mean you can store many different offsets, and only display a few of them, instead of cycling throught the entire hash)



Sais
Re: $time of the world [Re: Sais] #168009 02/01/07 05:42 PM
Joined: Dec 2005
Posts: 54
B
Bundy Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Dec 2005
Posts: 54
Thanx, ill test it and let you know ;-)

(how the hell can you guys code so quickly!)

Regards,

b

Re: $time of the world [Re: Bundy] #168012 02/01/07 06:24 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Originally Posted By: Bundy
(how the hell can you guys code so quickly!)


By knowing what's available ($ctime, $gmt, hash tables...); seeing a potential solution ($ctime + %offset); seeing the issues with the solution and coding for them ($timezone and $ctime/$gmt are in seconds, so need the offset in seconds; see my use of $gmt instead of $ctime); and then trying it. smile

BTW, I was thinking on the way home that I missed the ob.sanity: Might be wise to put some flood protection around that, and perhaps limit it to certain channels only.



Sais
Re: $time of the world [Re: Sais] #168019 02/01/07 08:18 PM
Joined: Dec 2005
Posts: 54
B
Bundy Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Dec 2005
Posts: 54
Tx Sais, yeah - I'm still getting the hang of things - scripting.

I like it when people explain what they're doing above the line of code, like you do.
And yeah, I have limited it to a channel - not quite sure how to flood protect it, but ill read in /help.

Is there a way to 'refresh' mIRC remotes without closing mIRC and reload the file again?
When I make changes and reload it, mIRC only remembers the first version I've loaded.

Regarding hash tables, info gets pulled from the .ini file where the code is stored - correct?

Many thanks,

b

Re: $time of the world [Re: Bundy] #168021 02/01/07 09:04 PM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Originally Posted By: Bundy
I like it when people explain what they're doing above the line of code, like you do.

Self-documenting code is something that is good programming practice. I tend only to do it when writing code for others, though...doesn't help 2 months later when I come to look at the code and try to unravel what it was I was trying to do!

Quote:
And yeah, I have limited it to a channel - not quite sure how to flood protect it, but ill read in /help.

There's nothing specific in the help file that will tell you that. There are a few ways to do it, but all involve setting some sort of flag on a time-out which you check before allowing the code to run again.

Quote:
Is there a way to 'refresh' mIRC remotes without closing mIRC and reload the file again?
When I make changes and reload it, mIRC only remembers the first version I've loaded.

Just press ctrl-s or file->save from the menu. As you say, if you reload it, you lose your changes because you're reverting to the version of the file on disk.

Quote:
Regarding hash tables, info gets pulled from the .ini file where the code is stored - correct?

Not quite. I read that as 'reading from an ini' which isn't the same as saving the script in an ini file. You can also save scripts in a plain text file (usually with the .mrc extension).

All I've done here is to hard code the values into the script itself, and have the hash table created and populated when the script loads.

It would certainly be possible to populate the hash table from an ini file, or indeed just read the values directly from an ini file.


Sais
Re: $time of the world [Re: Sais] #168024 02/01/07 10:37 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Originally Posted By: Sais
var %time = $gmt + %offset


Just thinking about this... It's a pain to manually enter all of the offsets in seconds. It would be much easier to use -5 or whatever.

Why not change this line to:
Code:
var %time = $calc(%offset * 3600 + $gmt)


Then he only has to enter the difference in hours instead of the difference in seconds.

Another option might be to have the timezone table have the offset be the itemname and the cities the data.

-5 = New York, Boston, Detroit, whatever else
-4 = Chicago, Green Bay, whatever else

That sort of thing. It would really make that much difference, but would reduce the number of hash table items. Not that it really matters as far as speed. Just thinking of how it might be done. This way, you could optionally output a list of cities for the timezone without looping.


For the OP, here is a simple way to prevent flooding. Insert this as the very first line after your on TEXT line.
Code:
  if (%time.flood) { return }
  else set -u10 %time.flood On


That will make it so the script will only run once every 10 seconds (-u10). If you want to change it to something else, just change the 10. Basically, anything 3 or higher will prevent you from being flooded off a network. But you may want higher if someone is just flooding the channel with it.


Invision Support
#Invision on irc.irchighway.net
Re: $time of the world [Re: Riamus2] #168050 03/01/07 08:40 AM
Joined: Dec 2005
Posts: 54
B
Bundy Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Dec 2005
Posts: 54
Thanx all,

I have one problem left, the refresh - I always save it like you mentioned Sais. But for some or other reason it still remembers my first version, until I close/open mIRC again.

This is the line that it gives me. For a start I've only made changes to the first and second entry in the hash table.

* /hmake: table 'timezones' exists (line 2, worldtime_v1.1.ini)

What would you suggest? a Refresh would be nice ;-)

Regards,

b

PS: Is there a special way to add colour to the table, or to code for that matter?

Last edited by Bundy; 03/01/07 08:49 AM.
Re: $time of the world [Re: Riamus2] #168052 03/01/07 09:10 AM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Yeah, there's no reason you can't put multiple entries into one item name.

There are a few awkward 1/2 hour time zones, and even one 3/4 hour timezone that I know of. Not much of a problem; 5.5 or 13.75 hours would work. It would also mean that you could show the GMT offset in the output - although you could do that with my version by dividing by 3600. Swings and roundabouts...

I just wanted to make the code as obvious as possible (and multiplying by 3600 isn't necessarily that obvious to some people smile



Sais
Re: $time of the world [Re: Bundy] #168053 03/01/07 09:29 AM
Joined: Oct 2003
Posts: 313
S
Sais Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Oct 2003
Posts: 313
Oh, you mean the timezone list?

Yes, perhaps it would be good to put that in an 'init' alias and call the init alias from on load/start.

You might also then have to test for the hash table being there and hfree'ing it:

Code:
alias timezones.init { 
  if ($hget(timezones)) { hfree timezones }
  hmake timezones
  hadd timezones ...
  ... 
}

on *:start:{
  timezones.init
}


Then you could call /timezones.init from the commandline without having to reload the script/restart mIRC. (I think I should have used on start instead of on load before, too...)

Last edited by Sais; 03/01/07 09:37 AM.

Sais
Re: $time of the world [Re: Sais] #168055 03/01/07 09:48 AM
Joined: Dec 2005
Posts: 54
B
Bundy Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Dec 2005
Posts: 54
This is wat I have now - if I add colour to the _list and/or hashtable, it doesn't show correctly, why not?

The init alias I add in aliases, and is it my whole table?

Code:
on *:load:{
  hmake timezones
  ;; Add offsets from GMT in seconds
  hadd timezones _list Johannesburg Afghanistan Amsterdam Auckland Bangkok Brazilia Chicago Iraq Melbourne Miami Moscow Paris Seattle Sydney Tokyo Vancouver Washington
  hadd timezones Johannesburg 7200
  hadd timezones Afghanistan 16200
  hadd timezones Amsterdam 3600
  hadd timezones Auckland 43200
  hadd timezones Bangkok 25200
  hadd timezones Brazilia -10800
  hadd timezones Chicago -21600
  hadd timezones Iraq 12600
  hadd timezones Melbourne 36000
  hadd timezones Miami -18000
  hadd timezones Moscow 10800
  hadd timezones Paris 3600
  hadd timezones Seattle -28800
  hadd timezones Sydney 36000
  hadd timezones Tokyo 32400
  hadd timezones Vancouver 28800
  hadd timezones Washington -18000
}

on *:text:!time:#test:{
  ;; This will make that the script only run once every 10 seconds
  if (%time.flood) { return }
  else set -u10 %time.flood On
  ;; Calculate the time and give output
  var %list = $hget(timezones,_list)
  var %n = $numtok(%list,32), %i = 1
  while (%i <= %n) {
    var %place = $gettok(%list,%i,32), %offset = $hget(timezones,%place)
    var %time = $gmt + %offset
    msg $chan %place - $asctime(%time)
    inc %i 
  }
}


Regards,

b