Os.time returns what system time?

the os.time library states in the wiki that it returns the system time. I tried it with the string format “*t” and it seemed to return the time on my PC. That said, I live very close to Roblox HQ.

What os time is it returning? My own PC? The server that is running the game? The time at Roblox HQ?

The wiki should be more clear.

I need it to be consistent, not based on the time on my PC.

Thanks!

7 Likes

I believe it returns the time relative to the machine the function is called on. So it could be fairly inconsistent. I’m not sure how/where Roblox servers are run but I wouldn’t be surprised if they ran in AWS or something like that. If you use os.time locally it will return the time the client’s machine. if you do it on the server it will return the time on the machine the server is running on.

6 Likes

yes well, wont any servers in the cloud all have different os.time based on where they are located?

If this is the case, how is this function useful at all?

I noticed that there was another string pattern option like this: “!*t” but I am not sure what that does, again the wiki is not helpful in explaining what these parameters do.

1 Like

Well, this function is from the Lua library, it isn’t implemented by Roblox specifically, although they may have made modifications to the way it operates.

It’s mostly useful for getting formatted times, although tick is effectively the same thing with less functionality. I wouldn’t say it was designed for a server-side implementation but rather a client side one. It could also be useful for logging purposes on the server/client side. There are really numerous applications.

The string pattern options are for formatting purposes I believe. Those are actually extremely useful for getting day of the week and things like that. It’s actually quite a bit of work to tell whether it’s Wednesday or Friday without something like that since the number of seconds since 1970 is a bit more complicated to convert into an actual day of a week.

I’ve used tick() as a way of syncing actions between the client and server without actually needing to communicate between client and server constantly (just an initial time offset check when the player joins).

1 Like

i am making a Twitter Codes module that has codes that expire on certain dates, I wish it would just return like Greenwich Mean Time, or some other constant instead of return values based on whatever machine it was running. Inconsistencies can lead to problems.

1 Like

In this case I would just convert all of the times to iso format (or whatever format you prefer) and sync all of the times that way. Not sure if this works in Roblox or not but this StackOverflow post might be helpful: nginx - Timestamp to ISO 8601 in Lua - Stack Overflow

It may be better to convert to whatever format Twitter is using. Time is a bit tricky to handle in some cases but I’m fairly certain that the os.time API is sufficient to do what you’re trying to do.

1 Like

oh no i think you misunderstand, I am not doing anything with Twitter. When i say Twitter codes, i mean I post a code on twitter, this is a string that they enter in to the GUI and they get an in-game reward.

My need for accurate times is for example, I have a code that will expire on June 1st. Whenever a person tries a code, it will check if that code has an expiration date before giving them the reward.

I guess if i just go with the date, it doesn’t matter too much what time it is. Some servers will experience codes that expire before other servers as the date line moves across the globe.

1 Like

I don’t think I misunderstood. I may not have been as clear though. I’m saying if you put all of the dates into a common format, the date of the twitter code’s expiration based on the twitter timestamp, and the date on the server you can get the exact time it should expire from the server’s time offset. Otherwise there could be some weird inconsistency in when it ends.

Also, I’m not certain how/where the servers are hosted. They may all share a common time offset so this may be a relatively simple problem to solve, although the dynamic approach is going to be more scalable with Roblox as eventually (assuming they aren’t already) they will need to spool up servers all over the globe.

2 Likes

os.time is synced to UTC time, and returns the time in seconds since January 1st 1970, 00:00:00

5 Likes

Are you planning on hitting Twitter’s API from the game server? If so it will probably already be converted into the timezone of the server making the request in UTC format anyway so this shouldn’t be too difficult to do. Seems like os.difftime would make this pretty trivial.

If you’re doing it manually and os.time is already in UTC then you wouldn’t really have to worry about all of the stuff I said. Just add the number of days to the time in UTC when the code started to get the correct end date.

1 Like

That’s for os.date, not os.time. os.time’s only argument is a date table. There’s a better tutorial on the Lua Users Wiki, but it contains functions that don’t exist in Roblox. (Ignore everything but os.date, os.time, and os.difftime)

2 Likes

Ok i think you do misunderstand. I am not doing anything with Twitter besides posting the code on my feed for people to get. This code is just a string they enter into my games GUI.

1 Like

Although this could be quite a lot of work you could set a DataStore value to the os.time() you update the code in and then have a script check if X seconds have passed.

1 Like

Don’t get me wrong, os.time() isn’t it a stop watch that started in 1970 it’s a time that keeps on going.

1 Like

os.time returns the number of seconds since January 1, 1970, 12:00 AM UTC. It can be confusing when you’re testing this out yourself because os.date("*t") actually internally converts the value to your local timezone. You have to call os.date("!*t") to get a date formatted for UTC. In both cases, os.date expects you to pass the parameter in the same format as os.time, which is in UTC.

tick is similar to os.time, but returns the time since January 1, 1970, 12:00 AM in your local timezone. time returns a number of seconds relative to some arbitrary point, like the number of seconds since the client started running.

Update (May 2022): The DateTime API is much easier to understand and to work with, and I’d recommend using it going forward.

19 Likes

It returns to the current local time on your system coded as number, you will need to break it off using tables so it can be readable.

So, os.date(“!*t”) will give me a table with days and time that are not relative to my local machine or the server that is running the code?

I made this script to get the UTC time. I included a variable so I could activate/deactivate a “DST” function so the script adapted the time to “summer time”. For storing the hours and minutes I use some number values because I need that information for other scripts but you can change them to a local variable if you want.

local h= script.h
local m= script.m
local dayV= script.d
local date = os.date("!*t",os.time())
dayV.Value= date.wday
local DST= true --activates/deactivates summer time

while true do
    date = os.date("!*t",os.time())
    dayV.Value= date.wday
    if DST==true then
        if date.hour==24 then
            h.Value=1
            dayV.Value= date.wday+1
            if dayV.Value==8 then dayV.Value=1 end
            else
                h.Value=(date.hour)+1
            end
        else
            h.Value=(date.hour)
        end
    m.Value=(date.min)
    wait(1)
end

For more info on both os.time() and os.date(), I recommend you check this page: How to use os.time() & os.date() and what it does? - Scripting Helpers

3 Likes

Yeah, that will return the time in UTC, which will be the same regardless of what timezone the client or the server are in.

In order to implement your usecase of expiring Twitter codes, you can use this:

local timeValue = os.time({
    year = 2019,
    month = 5, -- May
    day = 5,
    hour = 13,
    minute = 16
})

to get the time value for when it will expire. Note that the time values passed into this function are in UTC. Then, you can just use os.time() < timeValue to tell whether the code is still valid. I recommend performing this check server-side, because players can adjust their local clocks to any value they want.

To show the user how much time is left before the code expires, you could do:

local delta = timeValue - os.time()
local seconds = math.floor(delta) % 60
local minutes = math.floor(delta / 60) % 60
local hours = math.floor(delta / 60 / 60) % 60
local days = math.floor(delta / 60 / 60 / 24) % 60
local prettyString = string.format("%d days and %02d:%02d:%02d left!", days, hours, minutes, seconds)

print(prettyString)
26 Likes

I just wanna tell that it is indeed hosted in AWS. Basically, try downloading ROBLOX Studio, it’ll launch a downloading from s3.amazonaws.com.

sorry for reviving.

1 Like