Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you explain os.time()?

Asked by 8 years ago

The wiki doesn't help me too much and the forums are filled with the wrong type of people, so I resorted into coming here.

I'm working on a daily rewards system and I hear you use os.time(), but I don't understand what os.time() does.

0
Note: os.time() can vary slightly based on the specific server you're on (in tests, I've found it can vary by over 20 seconds, though most servers are within a second of each other). Further, 'tick' includes milliseconds, whereas 'os.time' does not. chess123mate 5873 — 8y

2 answers

Log in to vote
3
Answered by 8 years ago

os.time()

os.time() always returns Unix time. If you are unsure what Unix time is, Unix time is defined as the number of seconds that have passed since 00:00:00 Coordinated Universal Time, Thursday, 1 January 1970.

Since tick() always returns what the users local time is, it is an inefficient way to keep track of time over multiple servers. The best thing to do is use os.time() which chooses one timezone [Unix (which is equivalent to GMT)] and records that. This way you can keep the standard of keeping time universal for all players.

If you want to see a neat trick to figure out a timezone, take the difference between the local users time, and os.time() and compare with a list:

print(os.difftime(tick(),os.time())/3600)

For me, since I am in Eastern Timezone which is also GMT -04:00, the result that got printed was -4. Using this, you can try and guess what timezone the player is in with a list such as:

http://unattended.sourceforge.net/timezones.php

Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
8 years ago

So I'm assuming you know about tick(), which comes with the following warning on the wiki:

Warning: tick() returns local time, and time zones between servers may vary. A reliable option to get the same time on every server is to call os.time().

Basically, it's a method of getting the time, except os.time() can be used as a standard time, as each machine and server could potentially return different numbers for tick() because of time zone differences.

os.time() is like tick(), except it is the same no matter which device it is called on.

More information here

Answer this question