Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

os.time - How to use it?

I was looking in the blog and it started talking about os.time. I wondered how it outputted so I put in the command line in the in-game console and this happened: 03EFF218 So I tried it in studio and: 0x15af5670 Is this a table or something? Obviously it's not supposed to be a string. How would I use os.time?

1 comment

0

Pay close attention to the output: "function: 0D767DE0" the word before the numbers tells you that it's a function (not a table or something)

2 answers

✓ Accepted answer

Time is a very difficult to rigorously define.

The standard way that it is done is by counting the number of seconds from some particular, special moment in history.

tick() counts the number of seconds since the beginning of 1970 in your time zone.

os.clock() does a similar thing. However, it measures universal time meaning it references the beginning of 1970 GMT, regardless of which timezone the computer is in.

os.clock() is also an integer, while tick() provides a fraction of a second.


Here's a function that computes the day, year, etc, approximately, given the two functions:

-- The rule for whether or not `year` is a leap year.
function isleap(year)
	if year % 400 == 0 then
		return true;
	elseif year % 100 == 0 then
		return false;
	elseif year % 4 == 0 then
		return true;
	end
	return false;
end

function calendar( t )
	-- t is seconds since beginning of 1970
	local minutes = t / 60;
	local hours = minutes / 60;
	local days = hours / 24;
	local week = (days % 7 + 4) % 7;
	-- Weekday; 0 Su, 1 Mo, 6 Sa.
	-- Year / month is annoying because of leapyears:
	local dayStart = 0;
	local year;
	for y = 1970, 2100 do
		local len = 365;
		if isleap(y) then
			len = 366;
		end
		dayStart = dayStart + len;
		if dayStart > days then
			year = y;
			dayStart = dayStart - len;
			break;
		end
	end
	hours = hours % 24;
	minutes = minutes % 60;
	days = days - dayStart;
	local mos = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	local monthStart = 0;
	if isleap(year) then
		mos[2] = 29;
		days = days % 366;
	else
		days = days % 365;
	end
	for mo = 1, 12 do
		monthStart = monthStart + mos[mo];
		if monthStart > days then
			monthStart = monthStart - mos[mo];
			month = mo;
			break;
		end
	end
	local date = days - monthStart;
	print("Today's Date:");
	local monthName = {
		"January", "February", "March", "April", "May", "June", "July",
		"August", "September", "October", "November", "December"
	};
	print(math.floor(date) + 1, monthName[month], year);
	print(math.floor(hours), ":", math.floor(minutes));
end

calendar( os.time() );
--Today's Date:
-- 12 December 2014
-- 19 : 40

calendar( tick() );
-- Today's Date:
-- 12 December 2014
-- 14 : 40

os.time() results in the time in GMT (English time).

tick() results in my local time (EST)


Time is bad. If you can, avoid dealing with it. Using HTTP service to query some web service about time information might almost be a better option.

Or, finding a library that deals with it properly (I'm sure there is one, somewhere)

os.time is actually a function, not a readable value.

print(os.time()) --seconds passed since the UNIX epoch, UTC

If you call it with a table of parameters, you can get the seconds since the passed-in time.

2 comments

0

Could you give me an example?

1

It's a function. Methods are on instances of objects and thus usually use the :method() notation.

This discussion is preserved as it was. New answers, comments, and votes are closed.