Convert a string date to a timestamp

后端 未结 5 1758
无人及你
无人及你 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 22:39

    The code below does this except that it does not handle timezones:

    s="Sat, 29 Oct 1994 19:43:31 GMT"
    p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) (%a+)"
    day,month,year,hour,min,sec,tz=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]
    print(os.time({tz=tz,day=day,month=month,year=year,hour=hour,min=min,sec=sec}))
    

    But it prints 783467011. The code below tells us that 1288374211 is a different date:

    print(os.date("%c",1288374211))
    --> Fri Oct 29 15:43:31 2010
    print(os.date("%c",783467011))
    --> Sat Oct 29 19:43:31 1994
    

提交回复
热议问题