Run function at a specific time or after a set amount of days/weeks/months (long timer)

Has anyone here figured out how to run functions at a certain time on a certain day (ex: RunConsoleCommand("_restart") on Febuary 15th at 12:00or when the server is online anytime after that) or run functions on a long timer even after server restarts/crashes? For example, “RunConsoleCommand(…) 2 months and 1 day from now”.

I was thinking that I could write files to the data folder and having a script check there on some hook but I’d think there’s a more efficent way.

Thanks in advance for your input.

I Think you’re going to use something involving which will give you the date of the Operating System. There is a way to get what you recieve from that into a table to actually check it.

What do you need it for?

Demoting people with ulx, removing round end songs and some other things I can’t think of right now.

ULx TempAdd for the first one

You would be better saving some stuff in SQLite or in a text file so you can read it at a later date, Lua memory isn’t going to cut it because your server will eventually need to restart. You would use the aforementioned OS time values, but you would want to use os.time not date (date takes an os.time argument and a string argument and spits out a human readable form of time.)

So os.time() will return the current time in seconds. You can either add to the current time (like os.time() + 60 is one minute from now) or specify a specific date like this: os.time({year=2014, month=2, day=7}) which would be Febuary the 7th at 12:00:01.
(Also before anyone mentions it the epoch is like in 2038 or something so no need to worry about that.)

Okay now is there a way to see with os.time() if the current date is past the date in the text file?

I think you’d use:


os.difftime(t2, t1)

Calculate the number of seconds between time t1 to time t2.

> t1 = os.time()
> -- wait a little while then type....
> = os.difftime(os.time(), t1)
> = os.difftime(os.time(), t1)

Although I don’t know if it’d work for comparing two different dates, I haven’t tried it myself.

os.difftime() is literally a substraction operation lol