Page 1 of 1

Easy Time Difference

Posted: Thu Sep 30, 2010 11:44 pm
by kaeus
So, I used to make a timer and do some annoying stuff to get the time difference between two events. I decided to just make it easier so I can call a function at the start of an event and the end to get the difference. Here is what I have.

Basically for some event, lets say...cheese:
storeTime("cheese")

then you can do this

diffTime("cheese") --Returns a string with the time in each increment that has changed, destroys the key
diffTime("cheese", true) --Same thing, but wont remove that event. Allows you to check elapsed time and keep it
diffTime("cheese", true, true) --Same as above, but returns a numerical difference in seconds elapsed
Code: [show] | [select all] lua

function storeTime(key)
	if not ksys.time then
		ksys.time = {}
	end

	ksys.time[key] = {
		h = getTime().hour,
		m = getTime().min,
		s = getTime().sec,
		z = getTime().msec
	}

	return true
end

function diffTime(key, check, numeric)
	if not ksys.time[key] then
		if not check then
			cecho("\nTime not stored for that event.")
		end
		return 0
	end

	local curtime = {
		h = getTime().hour,
		m = getTime().min,
		s = getTime().sec,
		z = getTime().msec
	}
	
	if numeric then
		return ((curtime.h - ksys.time[key].h) * 360) + ((curtime.m - ksys.time[key].m) * 60) + (curtime.s - ksys.time[key].s)
	end

	local outstring = ""

	if curtime.h > ksys.time[key].h then
		outstring = outstring .. (curtime.h - ksys.time[key].h) .. " hr "
	end
	if curtime.m > ksys.time[key].m then
		outstring = outstring .. (curtime.m - ksys.time[key].m) .. " min "
	end
	if curtime.s > ksys.time[key].s then
		outstring = outstring .. (curtime.s - ksys.time[key].s) .. " sec "
	end
	if curtime.z > ksys.time[key].z then
		outstring = outstring .. (curtime.z - ksys.time[key].z) .. " ms "
	end

	if not check then
		ksys.time[key] = nil
	end

	return outstring:gsub(".$", "")
end

Re: Easy Time Difference

Posted: Fri Oct 01, 2010 11:05 pm
by Vadi
Nice. Here is also something related for anyone looking: http://forums.mudlet.org/viewtopic.php? ... work#p3289