Home > database >  How to convert a string timestamp into an array in Lua (or other languages)
How to convert a string timestamp into an array in Lua (or other languages)

Time:11-02

I'm having this problem for a while and I can't find a good way to resolve it. So I have a timestamp like this for example : local t = "00:00:0.031" and I'm trying to convert it into an array like this :

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

This is pretty much the same no matter the language so if you have an idea on how to solve this but don't know Lua you can submite your answer anyway in any language. I tried solving it myself using regex and I'm quite sure it's possible this way ...
Thank you for the interest to my question, have a good day (:

CodePudding user response:

My Simplest Answer Will Be Just Split the given string by your regex, in This Case For HOUR:MIN:SEC.MS first Split By (:) To Get HOUR MIN & SEC MS, Then Split SEC MS by (.) To separate Both seconds And milliseconds

Below is my answer in java

import java.util.*;

class timeX {
    long hours = 0,
    minutes = 0,
    seconds = 0,
    milliseconds = 31;
    
    //Convert Given Time String To Vars
    timeX(String input) {
        //Split Input By (:) For Hour, Minutes & Seconds Miliseconds
        String[] splitted=input.split(":");
        this.hours=Long.parseLong(splitted[0]);
        this.minutes=Long.parseLong(splitted[1]);
        
        //Split Again For Seconds And Miliseconds By (.)
        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);
    }
}

CodePudding user response:

Here is a not bad way using string.gmatch which is splitting by regex in Lua. Here the value is being split by either ":" or ".". Then there is a counter in place to match the index for the resulting table.

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

You can do a printing loop afterwards to check the results

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

Output:

0
0
0
31

The main problem I have found with my solution is that it does not save the values under the keys listed but instead the numbers 1 2 3 4.

CodePudding user response:

You could use string.match to extract the substrings in a first place. In a second time, you could use the function tonumber to convert it into numbers.

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

With the following code, you could get the results you want:

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

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

This should returns:

> Result = ParseTimestampString("00:00:0.031")
>
> print(Result.hours)
0
> print(Result.minutes)
0
> print(Result.seconds)
0
> print(Result.milliseconds)
31
  • Related