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

How to get Real-Life timing?

Asked by 9 years ago

As my Question title explains, I am unsure of how to code a function where it can get Real-Life time, like 10:30 or 2:15 in real life, or, to try and dumb it down a bit, kind of like the time where you live. Yes, I have checked the WIKI, but, sadly, it did not have anything on this, also, I do know if it probably has something to do with tick(), but, guessing, as it goes with real-time in a way, I'm thinking that it would have something to do with it, but, when I do a print, it does not do real-life time [Tick = 1424025566.5435, 1424025566.9337, ect.]. I am sorry if this question is considered Too Broad, or Not Constructive, but, I do not know how to complete this, as I do not know how to code it. Also, I forgot to mention, I have not created a script as of yet because I do not know how to code it, and what to use to correctly code it.

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

tick() is a "real life time". It returns Unix time which is the number of seconds since 1 January 1970.

tick() returns local time, meaning, local to the server's timezone. If you use it in a LocalScript, it will be local to that user's timezone.

If you want to use GMT time, use os.time() instead (this will only return whole numbers -- you can't use it to know the current fraction of a second).

Time is a very difficult monster. There are all sorts of crazy problems with time -- leap years, leap seconds, time zones, etc.

Here is an approximately correct way to handle it.

local time = os.time()

local seconds = time % 60 -- Remainder after division
time = time / 60
local minutes = time % 60
time = time / 60
local hours = time % 24
time = time / 24
local days = time

local niceHour = math.floor(hours)
local niceMinute = math.floor(minutes)
if niceMinute < 10 then
    niceMinute = "0" .. niceMinute
end
print(niceHour .. ":" .. niceMinute,  "GMT")

-- 1:59 GMT
-- Corresponds to 8:59 EST

Getting the date is much harder. See here for an answer regarding that


EDIT: CLARIFICATIONS

"GMT" means Greenwich Mean Time (this is what os.time() returns). That's a name for a timezone in England. Thus it is (about) five hours after EST/EDT (Eastern (Daylight) Time of USA) and (about) 8 hours after PST/PDT (Pacific (Daylight) Time of USA).

If you want a local time, use tick() instead (from a LocalScript) -- otherwise just add the appropriate hour offset (careful -- remember that Daylight Savings is a thing)


a % b. This is call the modulo operator (a misnomer; I digress) or the remainder operator.

For integers, it corresponds to the remainder after division. That means 5 % 3 = 2 because 5 = 1 * 3 + 2.

In general a % b means you'll remove b an integer-number-of-times from a until it's less than b (and at least 0):

5.9 % 1.3

(5.9 - 1.3) % 1.3
4.6 % 1.3

(4.6 - 1.3) % 1.3
3.3 % 1.3

(3.3 - 1.3) % 1.3
2 % 1.3

(2 - 1.3) % 1.3
0.7 % 1.3

0.7

This adds up: 0.7 + 1.3 * 4 = 5.9. (In practice, computers use a different algorithm to compute this, because 123482013 % 0.01 would be very slow to compute like this)

0
I am a bit confused on two things sadly; What is the '%'? As I do not see it allot, and, to me, it reverts to zero if the numbers are very close to eachother. I am also confused by the time; From the comments, the time is five-hours slow, or, five-hours fast, but, overall, this answers my question allot. :) Thanks man. :) TheeDeathCaster 2368 — 9y
2
5 hours slow: It uses GMT time, Greenwich Mean Time, which is London's time. BlueTaslem 18071 — 9y
0
Oh.. That explains the time difference. :) Thanks man! :D This helps allot! :D TheeDeathCaster 2368 — 9y
Ad

Answer this question