Convert a string date to a timestamp

后端 未结 5 1636
无人及你
无人及你 2021-01-31 22:34

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

\"Sat, 29 Oct 1994 19:43:31 GMT\"

into

783467011

5条回答
  •  余生分开走
    2021-01-31 23:03

    Arrowmaster's answer above would be perfect if daylight saving time is considered. Also using array seems cleaner.

    s="Sat, 29 Oct 1994 19:43:31 GMT"
    p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
    date={}
    date['day'],date['month'],date['year'],date['hour'],date['min'],date['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}
    date['month']=MON[date['month']]
    date["isdst"] = false  -- new code
    offset=os.time()-os.time(os.date("!*t"))
    print(os.time(date)+offset)
    

提交回复
热议问题