Is there a way to get os time from os.date?

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.
image

Thank you so much!
I actually thought there was just a function that does that.

1 Like

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)
  1. The date is pretty obvious, put in the date there.
  2. The format specifiers. %d = integers and the + just makes a + come before a number
  3. It then matches the format specifiers to the date
  4. The months are their equivalent number (e.g. July = 7, September = 9, June = 6, etc)
  5. It selects the month from the months based on the format.
    As for the 6th line, it looks like it’s a way of calculating something. And then the last line is the last calculation, and then displaying it.
1 Like

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}))

1 Like

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.

1 Like

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()

1 Like

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.