如何将 Lua(或其他语言)中的字符串时间戳转换为数组

我遇到了这个问题很长时间了,但一直无法找到一个好的解决方法。例如,我有一个时间戳:local t = "00:00:0.031",我正在尝试将其转换为以下数组:

local ft = {
    hours = 0,
    minutes = 0,
    seconds = 0,
    milliseconds = 31
}

这在任何语言中都是大致相同的,因此,如果您知道如何解决此问题但不知道 Lua,则可以用任何语言提交您的答案。我尝试使用正则表达式来解决它,并且我非常确定这种方式是可行的……

感谢您对我的问题的关注,祝您有个愉快的一天。 (:

原文链接 https://stackoverflow.com/questions/69804375

点赞
stackoverflow用户17304565
stackoverflow用户17304565

我的最简单答案就是通过你的正则表达式将给定的字符串分割开来,在这种情况下,按小时:分:秒.毫秒先按(:)分割以获得小时、分钟和秒+毫秒,然后通过(.)分割秒+毫秒,以分离出秒和毫秒。

以下是我的java答案示例

import java.util.*;

class timeX {
    long hours = 0,
    minutes = 0,
    seconds = 0,
    milliseconds = 31;

    //将给定的时间字符串转换为变量
    timeX(String input) {
        //按(:)拆分输入为小时、分钟和秒+毫秒
        String[] splitted=input.split(":");
        this.hours=Long.parseLong(splitted[0]);
        this.minutes=Long.parseLong(splitted[1]);

        //再次按(.)拆分秒和毫秒
        String[] splittedMandS=splitted[2].split("\\.");
        this.seconds=Long.parseLong(splittedMandS[0]);
        this.milliseconds=Long.parseLong(splittedMandS[1]);
    }
}

public class Main
{
    public static void main(String[] args)
    {
        timeX tmp = new timeX("30:20:2.031");
        System.out.println("H: "+tmp.hours+" M: "+tmp.minutes+" S: "+tmp.seconds+" MS: "+tmp.milliseconds);
    }
}
2021-11-02 01:36:18
stackoverflow用户10915018
stackoverflow用户10915018

以下是一种使用 string.gmatch 来在 Lua 中通过正则表达式进行分割的不错方法。在这里,值将通过 ":""." 进行分割。然后,使用计数器来匹配结果表的索引。

local t = "00:00:0.031"

local ft = {
  hours = 0,
  minutes = 0,
  seconds = 0,
  milliseconds = 0
}
local count = 1
for str in string.gmatch(t, "([^:|.]+)") do
  ft[count] = tonumber(str)
  count = count + 1
end

您可以执行后续循环来检查结果

for i = 1, 4 do
  print(ft[i])
end

输出:

0
0
0
31

我发现我的解决方案的主要问题是它没有将值保存在列出的键下,而是保存在数字1、2、3、4下。

2021-11-02 03:21:28
stackoverflow用户10953006
stackoverflow用户10953006

你可以使用 string.match 来提取子字符串。然后,你可以使用 tonumber 函数将其转换为数字。

function ParseTimestampString (TimestampString)
  local Hours, Minutes, Seconds, Milliseconds = string.match(TimestampString, "(%d+)%:(%d+)%:(%d+)%.(%d+)")
  local Result

  if Hours and Minutes and Seconds and Milliseconds then
    Result = {
      hours        = tonumber(Hours),
      minutes      = tonumber(Minutes),
      seconds      = tonumber(Seconds),
      milliseconds = tonumber(Milliseconds)
    }
  end

  return Result
end

通过以下代码,你可以获得想要的结果:

Result = ParseTimestampString("00:00:0.031")

print(Result.hours)
print(Result.minutes)
print(Result.seconds)
print(Result.milliseconds)

这应该会返回:

> Result = ParseTimestampString("00:00:0.031")
>
> print(Result.hours)
0
> print(Result.minutes)
0
> print(Result.seconds)
0
> print(Result.milliseconds)
31
2021-11-02 05:08:49
stackoverflow用户11740758
stackoverflow用户11740758

使用Lua可以做到...

os.date()可以是一个秒的格式化工具,但要依赖于操作系统。

它在Linux中可以使用,但是(据我所知)在MS-Windows中不可用。

print(os.date('%H:%M:%S',0-3600)) -- 输出:00:00:00
print(os.date('%H:%M:%S',300-3600)) -- 输出:00:05:00

还可以将日期/时间输出为一个表格。

> tshow=function(tab) for k,v in pairs(tab) do print(k,'=',v) end end
> tshow(os.date('*t'))
day =   4
year    =   2021
month   =   11
hour    =   11
yday    =   308
isdst   =   false
min =   23
wday    =   5
sec =   51

...不幸的是,它没有毫秒。

如果将os.date()的表格输出保存为表格...

> ttable=os.date('*t')
> os.time(ttable)
1636021672
> os.date(_,os.time(ttable))
Thu Nov  4 11:27:52 2021
> os.date('%H:%M:%S',os.time(ttable))
11:27:52

...然后它的键/值对可以用于:os.time()

当在ttable键1中,您的时间是带毫秒的字符串时,进一步的代码几乎可以做到您所期望的那样...

local tshow=function(tab) for k,v in pairs(tab) do print(k,'=',v) end end
local ttable=os.date('*t') -- 创建一个时间表
ttable[1]='0:0:0.31' -- 数字键按顺序忽略os.tim()
ttable[2]=ttable[1]:gsub('(%d+):(%d+):(%d+)[.](%d+)','return {year=ttable.year,month=ttable.month,day=ttable.day,hour=%1,min=%2,sec=%3,milliseconds=%4}')
-- 这会创建ttable [2]的值:
--- return {year=ttable.year,month=ttable.month,day=ttable.day,hour=0,min=0,sec=0,milliseconds=31}
-- 现在让我们使用load()将其转换为一个表...
ttable[2]=load(ttable[2])()
-- 使用tshow()查看内部
tshow(ttable[2])

这将输出...

milliseconds    =   31
day =   4
year    =   2021
hour    =   0
month   =   11
min =   0
sec =   0

并且可以使用os.date()格式化输出。

print(os.date('%H:%M:%S.'..ttable[2].milliseconds,os.time(ttable[2])))
-- 输出:00:00:00.31
2021-11-04 10:32:17