Hello!
So, I’m wondering if there’s a function where you can get os.time (Unix time) from os.date. For example, if I inputted seconds,minutes,hours, … and it would be os.time.
So, basically, opposite of os.date(“!*t”,102359385)
Thank you!
Hello!
So, I’m wondering if there’s a function where you can get os.time (Unix time) from os.date. For example, if I inputted seconds,minutes,hours, … and it would be os.time.
So, basically, opposite of os.date(“!*t”,102359385)
Thank you!
I found this on another website, credits to them guys:
s="Sun, 23 Feb 2020 16:36:54 GMT"
p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
day,month,year,hour,min,sec=s:match(p)
MON={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
month=MON[month]
offset=os.time()-os.time(os.date("!*t"))
print(os.time({day=day,month=month,year=year,hour=hour,min=min,sec=sec})+offset)
It’s a little bit messy, but it does what it’s intended for.
Thank you so much!
I actually thought there was just a function that does that.
How does this function work though?
Variables are stupidly named, we’ll use this as a better example:
date = "Sun, 23 Feb 2020 16:36:54 GMT"
format ="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
day,month,year,hour,min,sec=date:match(format)
months = {Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
month = months[month]
offset = os.time() - os.time(os.date("!*t"))
print(os.time({day=day,month=month,year=year,hour=hour,min=min,sec=sec}) + offset)
How does day,month,year,hour,min,sec=date:match(format)
actually work, since there is “Sun, 23”.
Wouldn’t be like something like day,day2, etc.?
Here’s what I tried, and I noticed that the time was slightly off (only by like an hour or so) but it could do with correcting:
I’ve noticed, I can also just use that:
print(os.time({day=23,month=2,year=2020,hour=16,min=36,sec=54}))
I also tested this, and this works too. This may be more accurate?
print(os.time({wday = 1, yday = 54, day=23,month=2,year=2020,hour=17,min=40,sec=54}))
Well, it actually returns accurate number.
That was the reason for that theory. I realised (kinda late) that it’s using os.date() fields, and if we used more, we could get a better result. os.date() returns an array and the array we’re inputting is just custom, as opposed to using the current time via os.time()
Also, is there an optimal way to loop through days in a week? For example a list of days, and if today is let’s say Monday (2), the list would start from monday.
We can get that by using os.date(), but what’s the best was to do this for loop?
Not sure if I understand correctly. Are you talking about organising the list so that the list starts from the day that it is?
Yeah, like a circle of days and it would start on the today’s day.