How to get your computer's time?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

How to get your computer's time?

Post by napco »

I wonder if there is or there will be a way to get current time (hh/mm/ss) from your computer... I know that this feature wouldn't be used from most programmers but it will really boost my pokemon game (day/night events, different pokemon encounters etc...). So, if you're planning to add it in the next love releases i would be glad!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to get your computer's time?

Post by Robin »

It's in the standard Lua library:

Code: Select all

Now = os.date('*t') --get the date/time
print(Now.hour)
print(Now.min)
print(Now.sec)
Help us help you: attach a .love.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: How to get your computer's time?

Post by TechnoCat »

Lua 5.1 Reference Manual - 5.8 Operating System Facilities
The Lua reference manual is very useful when coding for LOVE.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to get your computer's time?

Post by Jasoco »

os.time() is what you need. And if you want to format it into a date format use the os.date() function:

Code: Select all

os.date("%D %I:%M:%S%p", os.time() - timeOffset * 60 * 60)
Will return the date and time in the following format:
08/02/09 05:00:00PM

A function I created for taking a number (Usually seconds from the timer) and turning it into hours/minutes/seconds format: (00:00:00)

Code: Select all

function formatTime(t)
	return math.floor(math.mod(t/60/60,60)) .. ":" .. string.sub("0" .. math.floor(math.mod(t/60,60)),-2) .. ":" .. string.sub("0" .. math.floor(math.mod(t,60)),-2)
end
Will take a number, like love.timer.getTime() and turn it into the number of hours, minutes and seconds since LÖVE was started. And by setting a variable gameStartTime to this value in the load() function and using:

Code: Select all

formatTime(love.timer.getTime() - gameStartTime)
Will return the time your current game has been running. I'll probably use this as sort of a way of saving how long you've been playing the game like RPG's usually do. By writing love.timer.getTime() - gameStartTime into a game save, then loading it as the new gameStartTime the next time you load the game it will give you a running total of how long the current game has been going on.

You can pass any number into it if you want. You could create your own "accellerated year" if you are making a game that has day and night changes but at a faster rate with a "time" variable that increases more rapidly you could create a game like the 3D Zeldas do where time goes faster.

Of course my function goes up to hours, so if the hours go over 99 it will display 100, 101, 102, etc hours. I could go further and do days if I wanted to but whatever. Not like it'd be hard.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: How to get your computer's time?

Post by napco »

Thanks a lot! It seems that i skipped that section of the reference manual... I'm sorry for the stupid question!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 44 guests