ID:195098
 
//Title: Time Estimation
//Credit to: Jtgibson
//Contributed by: Jtgibson


/*
This converts a number in ticks to an estimated time. Because it is an esti-
mate, it is intentionally supposed to lose accuracy based on how much time it
really is.

For example, the number '3960' is 396 seconds. This is estimated to be 7
minutes.

This is very useful in games where you have objects being repaired or
manufactured over an extremely long period of time, or other such things.
Instead of telling someone that their shirt will be ready in 3 days, 6 hours,
32 minutes, and 18 seconds, you can tell them that their shirt will be ready
in about three days. That is the purpose of this proc.

Because it rounds to the nearest, it will automatically either under- or over-
shoot the target time. Thus, using the above example, the client will discover
that the shirt isn't ready if s/he comes back at the same time three days
later.
*/




//The time bound specifies how far over the bound of the upper
// time length the lesser time length extends. For example,
// with an ESTTIME_BOUND of 3, any number up to three minutes
// will be displayed in seconds instead of minutes.
#define ESTTIME_BOUND 3


proc/estimate_time(num)
num = abs(num)

if(num >= 31536000000*ESTTIME_BOUND)
//estimate in centuries
var/amount = round(num/31536000000,1)
return "[amount] centur[amount != 1 ? "ies" : "y"]"

else if(num >= 3153600000*ESTTIME_BOUND)
//estimate in decades
var/amount = round(num/3153600000,1)
return "[amount] decade[amount != 1 ? "s" : ""]"

else if(num >= 315360000*ESTTIME_BOUND)
//estimate in years
var/amount = round(num/315360000,1)
return "[amount] year[amount != 1 ? "s" : ""]"

else if(num >= 25920000*ESTTIME_BOUND)
//estimate in months
var/amount = round(num/25920000,1)
return "[amount] month[amount != 1 ? "s" : ""]"

else if(num >= 6048000*ESTTIME_BOUND)
//estimate in weeks
var/amount = round(num/6048000,1)
return "[amount] week[amount != 1 ? "s" : ""]"

else if(num >= 864000*ESTTIME_BOUND)
//estimate in days
var/amount = round(num/864000,1)
return "[amount] month[amount != 1 ? "s" : ""]"

else if(num >= 36000*ESTTIME_BOUND)
//estimate in hours
var/amount = round(num/36000,1)
return "[amount] hour[amount != 1 ? "s" : ""]"

else if(num >= 600*ESTTIME_BOUND)
//estimate in minutes
var/amount = round(num/600,1)
return "[amount] minute[amount != 1 ? "s" : ""]"

else if(num >= 10)
//estimate in seconds
var/amount = round(num/10,1)
return "[amount] second[amount != 1 ? "s" : ""]"

else
return "1 second"


///*
//Testing code/sample implementation:

mob/verb/test_estimate_time()
usr << "BYOND has existed for about [estimate_time(world.realtime)]."
usr << "This world has been running for about [estimate_time(world.time)]."
usr << "There have been approximately [estimate_time(world.timeofday)] \
since midnight Greenwich Mean Time."


//*/