问题
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