How would I convert ISO 8601 durations into a seconds or milliseconds in Lua?

本小妞迷上赌 提交于 2020-01-05 13:11:23

问题


I need to implement a lua function to convert the time format in ISO 8601 to seconds or milliseconds, is there any built-in libraries available in Lua or we have to implement? Examples for ISO 8601 format :

PT1S, PT0.010S, PT0.001S---> to seconds or milliseconds.

回答1:


The code below converts strings of the form PTxxxS:

s="PT0.001S"
print(tonumber(s:match("PT([%d.]+)S")))

More generally, this code parses a duration string into a table, which you can then process easily:

s= "P3Y6M4DT12H30M5S" 
t = {Y=0, M=0, W=0, D=0, H=0, M=0, S=0}
for v,k in s:gmatch("([%d.,]+)(%u)") do
    t[k]=tonumber(v)
end


来源:https://stackoverflow.com/questions/48522313/how-would-i-convert-iso-8601-durations-into-a-seconds-or-milliseconds-in-lua

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!