Hello! Today I am struggling with some dates, look at my code:
local date = os.time({year=2020, month=8, day=13, hour=13, min=45, sec=0})
print(date)
print(tick())
warn(date - tick())
The problem is that the difference is just too big and yes I am using UTC, here is my output:
1597319100
1597329670.9616
14:41:11.196 - -10570.962337494
as u can see tick is like 3 hours ahead and I dont understand why
Try putting nothing in the os. time parameters
It’s the 12 hour and 47th minute as I am writing this in UTC. So you’re an hour off on the date… tick() is based off of the UNIX epoch, which is based on the local users time in seconds since jan 1st of 1970. So you’re probably in NYC or something where the time zone is 3 hours behind the 13th hour to which you set in os.time()
1 Like
I have changed a code a bit:
local date = os.time({year=2020, month=8, day=13, hour=13, min=20, sec=0})
print(date)
print(os.time())
warn(date - os.time())
but there is still a big difference,
1597317600
1597324567
15:16:07.309 - -6967
I think you’re confused with how these functions work, I recommend reading the wiki page about them, they have specific use cases and are sensitive to different environments like whether they are run on a client or a server.
The reason the times are different in this picture is because of time zones, which are actually the worst thing to deal with because some countries desire to change their time zones because they felt like the day should be a tuesday instead of a wednesday. Don’t even get me started with that giant wonky line going through the ocean
why is this relevant? Isnt just UTC that I should bother?
Like I said, they have different use cases and vary based on the local machines time. Unless your computer is set to perfect UTC, not UTC -4h, not UTC -10h, you won’t get the same results.
int os.time ( table time = UTC time )
Returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00), under current UTC time. If provided a table formatted similarly to that returned by os.date
, it will return the number of seconds since that time instead.
- print(os.time()) → 1586982482 (ran at approx. April 15th, 2020 at 1:28 PM PST)
- print(os.time({
- year=2020, month=4, day=15, – Date components
- hour=16, min=28, sec=0 – Time components
- })) → 1586968080
number tick ( )
Returns how much time has elapsed, in seconds, since the UNIX epoch, on the current local session’s computer.
The UNIX epoch is represented by the date January 1st, 1970 .
I just figured it out that os.time() is refering to ur local time, so in my case UTC + 2, therefore the solution is:
local date = os.time({year=2020, month=8, day=13, hour=23, min=10, sec=0})
print(date)
print(os.time())
warn(os.difftime(date, os.time()))
I tested this code and it seems to be the right one