How can you get the current OS 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
knuxyl
Citizen
Posts: 52
Joined: Sat Aug 13, 2016 4:40 am

How can you get the current OS time?

Post by knuxyl »

Is there a way to get the operating systems time that is cross platform? (ie Mac,Windows,Linux,Android)
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

Re: How can you get the current OS time?

Post by Ikroth »

os.date, or os.time, depending on what you need.

https://www.lua.org/pil/22.1.html

Code: Select all

os.time() => 1476852730 -- seconds since epoch
os.date("%X", os.time()) => 00:50:02
os.date("%c", os.time()) => "Wed Oct 19 00:50:02 2016"
Edit: I don't know if these are cross-platform, but I haven't encountered any issues with them. Additionally, the results will conform to the current locale.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How can you get the current OS time?

Post by ivan »

The timestamp IS platform independent - it counts the number of seconds since a specific point in time (1970),
whereas a 'readable' time string like "00:50:02" or "Wed Oct 19 00:50:02 2016" can be affected by timezone offsets and daylight savings.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: How can you get the current OS time?

Post by darkfrei »

Is it possible to write date/time according to ISO 8601? For example date: 2024-09-17 or date and time: 2024-09-17T18:16:00Z
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: How can you get the current OS time?

Post by pgimeno »

Sure.

Code: Select all

print(os.date('%Y-%m-%d')) -- local date
print(os.date('!%Y-%m-%d')) -- zulu date
print(os.date('%Y-%m-%dT%H:%M:%S%z')) -- local time + time offset
print(os.date('!%Y-%m-%dT%H:%M:%SZ')) -- zulu time
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest