Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
theOneAndOnlyMrT
Newbie


Joined: 15 May 2008
Posts: 3

PostPosted: Thu May 22, 2008 6:02 pm   

Time Difference...
 
Does anyone know how to do a time difference?

I'm storing a time value in the format of %time(c) in a variable called @pwlogtime.

So for example it currently contains: 5/22/2008 12:43:16 PM

What I'm trying to do is get the number of minutes passed between the variable and now.

I've tried this but it doesn't seem to work and I'm all out of ideas: %eval(%time(c)-@pwlogtime)

Anyone know what I might be doing wrong?
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Thu May 22, 2008 6:10 pm   
 
You can't subtract one string from another like this. It's not PHP (though you could use PHPscript to do it!).

You could use something like %subregex to parse it into its components (day, month, year, etc) and then do your own math using those pieces.
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu May 22, 2008 6:25 pm   
 
I seam to recall actually doing just that at some point, and using Zscript. I'll look into it.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Thu May 22, 2008 7:44 pm   
 
I don't have a client handy ATM so this is %100 untested and guaranteed to have a flaw somewhere.

Code:
#FUNCTION ElapsedMinutes($t1,$t2) {#return %eval((%mss(%concat("Date.parse(""",$t2,""")- Date.parse(""",$t1,""")"),"JScript")/1000)/60)}


This should echo 10
Code:
#echo @ElapsedMinutes("5/22/2008 12:43:16 PM","5/22/2008 12:53:16 PM")
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu May 22, 2008 8:39 pm   
 
Nice one Dharkael Smile I had cheated when I tried this before. So mine wouldn't have worked here.

There is No way I could write this up off the top of my head. Shocked
Corrected function below

Code:
#FUNCTION ElapsedMinutes($t1,$t2) {#return {%eval(%mss("Date.parse('"$t2"')"-"Date.parse('"$t1"')","JScript")/1000/60)}}
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu May 22, 2008 8:48 pm   
 
I have wrote this small script for my settings, though not heavily tested yet, but seems that it work properly. It's just an idea for you, modify it for your needs.

Code:

    #IF ((%int(%number(%time("mmddhhnnss"))) - @sdLastDamageTime) > 5)
    {
      // We did not take any serious damage for more than 5 seconds: we idling now
      CombatState = @StateIdle;
    }


You may run the following command:
#SAY %int(%number(%time("mmddhhnnss")))

in CMUD cmdline several times just to see that it works: last 2 digits in aech number shown will be seconds.

UPDATE: ah, and you can change time format for %time() if you think that maximum interval between measurements ==1 year will be too long for you Laughing
UPDATE 2: I see what a problem this script has, yes.


Last edited by Arde on Thu May 22, 2008 9:46 pm; edited 1 time in total
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu May 22, 2008 9:10 pm   
 
Dharkael's system is quite good actually, it holds up for a little over a month.
If you start messing with intervals of a few months worth of minutes it breaks down.

#echo @ElapsedMinutes(@pwlogtime,%time(c))

Shows 208
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu May 22, 2008 9:36 pm   
 
As I said, it was just an idea. Smile

For the maximum interval of 1 minute it will be easy... almost Smile %int(%number(%time("nn")))*60+%int(%number(%time("ss"))) + somehow track hour change Smile

UPDATE: deleted some...
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Thu May 22, 2008 9:54 pm   
 
I updated the function a bit so that all the math is done in JScript.
Code:
#FUNCTION ElapsedMinutes($t1,$t2) {#return {%mss("(Date.parse('"$t2"')"-"Date.parse('"$t1"'))/1000/60","JScript")}}


Arminas the format of short time strings has to follow month/day/year - JScript requires it.
For me at least on this computer %time(c) returns it in day/month/year so %time(c) wont work for me.
however if I want to keep it in short format %time("mm/dd/yyyy t") will do the trick.
The shortdate format depends on your computers regional settings.
If you keep it in long format %time() which would return something like Thursday May 22, 2008 3:47:47 pm
then there is no problems with localization.


Anyways If the format is correct then there's no problems with several months difference that I can find.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Zhiroc
Adept


Joined: 04 Feb 2005
Posts: 246

PostPosted: Fri May 23, 2008 1:19 am   
 
If all you want is an elapsed timer, I would simply store %ctime, which is the number of seconds you have been connected to the MUD. Then you can subtract any two timestamps to give you the number of seconds between the two. The math from there is simple. Of course, it only holds as long as you don't disconnect, but doing such elapsed time calculations are probably limited to a single login session.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Fri May 23, 2008 1:05 pm   
 
I needed a way to tell elapsed time for potentially long periods, and wrote the following function called parsetime:
Code:
#local $timestring $curtime $x $year $month $day $hour $minute $second $seconds2000
$timestring = %1
$x = %match($timestring, "(%d)/(%d)/(%d) (%d):(%d):(%d)", $year, $month, $day, $hour, $minute, $second)
$seconds2000 = $second + ($minute * 60) + ($hour * 3600) + ($day * 86400) + ($year * 31536000)
#switch ($month = 1) {}
  ($month = 2) {$seconds2000 = $seconds2000 + (31 * 86400)}
  ($month = 3) {$seconds2000 = $seconds2000 + (59 * 86400)}
  ($month = 4) {$seconds2000 = $seconds2000 + (90 * 86400)}
  ($month = 5) {$seconds2000 = $seconds2000 + (120 * 86400)}
  ($month = 6) {$seconds2000 = $seconds2000 + (151 * 86400)}
  ($month = 7) {$seconds2000 = $seconds2000 + (181 * 86400)}
  ($month = 8) {$seconds2000 = $seconds2000 + (212 * 86400)}
  ($month = 9) {$seconds2000 = $seconds2000 + (243 * 86400)}
  ($month = 10) {$seconds2000 = $seconds2000 + (273 * 86400)}
  ($month = 11) {$seconds2000 = $seconds2000 + (304 * 86400)}
  ($month = 12) {$seconds2000 = $seconds2000 + (335 * 86400)}
#if (%mod($year, 4) = 0 and $month > 2) {$seconds2000 = $seconds2000 + 86400}
#return $seconds2000

@parsetime(%time("yy/mm/dd hh:nn:ss") will return the number of seconds since midnight Jan 1, 2000.
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Fri May 23, 2008 2:20 pm   
 
Yeah, I it hit me late last night at a random moment that it was probably because I was doing the math in Zscript and I hadn't taken it into consideration.

Oddly though it let me plug in %time(c) and it worked with the format of 5/22/2008 12:43:16 PM. Is that what was causing the errors and not the math in the wrong place?
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Fri May 23, 2008 2:37 pm   
 
The reason I moved the math to JScript is because I remembered that CMUD/ZMUD flakes out with large integer math operations.
But even after making that change I noticed as you did the results was a little wonky.
After reading the JScript Date docs and switching my %time format to mm/dd/yyyy t everything worked out, so I'm going to say that was the problem.
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
charneus
Wizard


Joined: 19 Jun 2005
Posts: 1876
Location: California

PostPosted: Fri Jun 20, 2008 10:53 am   
 
Hmmm... so I'm bumping this up, hoping to get an answer.

Dharkael, I tried using your script, and it returned "JScript is not supported!" -- any clues how to fix that? :P

Charneus
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net