如何在Lua中将ISO 8601持续时间转换为秒或毫秒? [英] How would I convert ISO 8601 durations into a seconds or milliseconds in Lua?
本文介绍了如何在Lua中将ISO 8601持续时间转换为秒或毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要实现一个lua函数,以将ISO 8601
中的时间格式转换为秒或毫秒,Lua
中是否有任何内置库,或者我们必须实现?
ISO 8601格式的示例:
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.
推荐答案
下面的代码转换PTxxxS
形式的字符串:
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
这篇关于如何在Lua中将ISO 8601持续时间转换为秒或毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文