将字符串日期转换为时间戳 [英] Convert a string date to a timestamp

查看:73
本文介绍了将字符串日期转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以将RFC HTTP日期转换为Lua中的时间戳?

Is there any simple way to convert a RFC HTTP date into a timestamp in Lua?

"1994年10月29日星期六,格林尼治标准时间"

进入

783467011

推荐答案

更正lhf的示例代码以解决时区问题,因为os.time()无法指定时区.还要假设所有输入都以GMT结尾,因为这仅适用于GMT.

Correcting lhf's example code to account for timezone since os.time() does not have a way to specify the timezone. Also assume all input ends in GMT since this only works with GMT.

s="Sat, 29 Oct 1994 19:43:31 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)

哪个给了我们783477811.我们将使用os.date(!%c")进行验证,因为!将以UTC而不是本地时区进行输出.

Which gives us 783477811. And we will verify with os.date("!%c") because the ! will make the output in UTC instead of local timezone.

print(os.date("!%c",783477811))
--> Sat Oct 29 19:43:31 1994

这篇关于将字符串日期转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆