I’m terrible at os.time(), and I’m basically playing with numbers at this point trying to figure out how to find what week of the year it is. It’s for a shop system that will change weekly in the same order each year.
I’ve tried playing with day of the year and days since 1/1/1970 with no luck. Any help? (I’ll probably end up putting your name in the special thanks section)
Using tick() you can divide the number returned by 31622400 (the number of seconds in a year) to get how many years since 1/1/1970, then mod it by 1 and multiply it by 52.1429 and floor/ceil it to get the week of the year. This is relative to the UNIX epoch.
local year = tick() / 31622400
local week = (year % 1) * 52.1429
-- If you want whole numbers only:
year = year - year % 1
week = week - week % 1
A different solution similar to the above is using os.date(). It returns local time if you give it *t (only local scripts), and UTC date if you give it !*t.
For example:
local d = os.date("!*t")
print(d.year) -- 2018
print(d.month) -- 8 (August)
For your specific question, you can get the yday (year day) and figure out what day of the week it is by dividing by 7 and flooring the result.
local d = os.date("!*t")
print(math.floor(d.yday/7)) -- Returns 34 (Week 34)
Yeah, os.date() works on the server, but it won’t work if you give it *t, as that is local time. You’ll need to give it !*t, which is UTC time. local d = os.date("!*t") should work fine on the server.
Will this change weeks on weekends consistently throughout years, or will it change during the week on some years? It doesn’t really matter that much, but I’d like to have it on the weekend if possible.
I just thought about the math, and it looks like it will restart on the day that the year started on. Since this year started on a Monday, it will reset on Monday at 00:00. I hope that works…