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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Elric
Newbie


Joined: 18 Jul 2004
Posts: 3

PostPosted: Mon Jul 19, 2004 12:02 am   

Clock Arithmetic
 
I have a working alias called "offset" that shows what the time would be if you add or subtract a certain number of minutes from the current time, using the %time(h:nn AM/PM) format.

#alias offset {#math time_total %number(%time(hh))*60+%number(%time(nn))+%1
#if (@time_total >= 0) {#math time_range @time_total-24*60*(@time_total/(24*60))}
#if (@time_total < 0) {#math time_range @time_total-24*60*(@time_total/(24*60))+24*60}
#no time_range has a limit between 0 to 1440, inclusive
#math time_hour @time_range/60
#math time_minute @time_range\60
#if ((@time_hour >= 12) and (@time_hour <= 23)) {#var time_period PM} {#var time_period AM}
#if ((@time_hour >= 13) and (@time_hour <= 24)) {#math time_hour @time_hour-12}
#if (@time_hour = 0) {#var time_hour 12}
#echo {Time(%1) = @time_hour:%if(%len(@time_minute)=1,"0")@time_minute @time_period}
#unv time_total
#unv time_range
#unv time_hour
#unv time_minute
#unv time_period}

Usage: offset <negative or positive number>

If your 24-hour clock reads 12:00 then

offset -1 would echo -> Time(-1) = 11:59 AM
offser 0 would echo -> Time(0) = 12:00 PM
offset 1 would echo -> Time(1) = 12:01 PM
offset 90 would echo -> Time(90) = 1:30 PM
offset 720 would echo -> Time(720) = 12:00 AM

Question:

Is it possible to make this alias into a user-defined function? If so, what commands should I use to construct it?

I want a function like @offset(45) would output "12:45 PM" using the above example.

Thanks, Elric SlothMUD III
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5187

PostPosted: Mon Jul 19, 2004 2:59 am   
 
This is possible. It gets long enough that it should be considered next to impossible to do with out an intermediate variable. Since the creation or assignment of a variable is done by a command in zScript, it isn't meant to be done within a function. Still not impossible though. To make it shorter I used 2 #FUNCTIONs
#FUNCTION offset1 {%concat(%eval(%1/60),":",%if(%mod(%1,60)<10),"0"),%mod(%1,60),%if(%eval(%1/60)>12," PM"," AM"))}
#FUNCTION offset {@offset1(%eval(%time("hh*60+nn")+%1))}
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Mon Jul 19, 2004 3:12 am   
 
In 24-hour format. I'll leave you the conversion to 12-hour format.
#FUN offset {%eval((%time( hh) + ((%time( nn) + %1) / 60)) \ 24):%if( %eval((%time( nn) + %1) \ 60) < 10, 0)%eval((%time( nn) + %1) \ 60)}

I see Vijilante beat me on the reply. This function will not handle negatives if they are greater than the current minutes. It does fine with positive numbers.
_________________
LightBulb
Senior member

Most scripts in this forum are written for Command Line entry.
Don't even open the Settings Editor unless its use is specified or obvious.
Reply with quote
Elric
Newbie


Joined: 18 Jul 2004
Posts: 3

PostPosted: Mon Jul 19, 2004 10:54 pm   
 
Ahhh I can use nested functions Shocked that is a *BIG* help

Here is a working script based on the above posts, maybe someone else will find it useful:

#function time_fn {@time_fn_range(%eval(%time(hh*60+nn)+%1))}
#function time_fn_range {@time_fn_format(%eval(%1-24*60*(%1/(24*60))+%if(%1<0,%eval(24*60),0)))}
#function time_fn_format {%concat(%if(%eval(%1/60)=0,12,%if((%eval(%1/60)>=13) and (%eval(%1/60)<=24),%eval(%1/60-12),%eval(%1/60))),":",%if(%mod(%1,60)<10,"0"),%mod(%1,60),%if((%eval(%1/60)>=12) and (%eval(%1/60)<=23)," PM"," AM"))}

There is no limits to input, any positive or negative number will work.

If your 24-hour clock reads 12:00 then

#echo @time_fn(-1) -> 11:59 AM
#echo @time_fn(0) -> 12:00 PM
#echo @time_fn(1) -> 12:01 PM
#echo @time_fn(90) -> 1:30 PM
#echo @time_fn(720) -> 12:00 AM

Thanks for help guys, Elric
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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