Home > Software design >  convert varying timestamps to a uniform format
convert varying timestamps to a uniform format

Time:01-09

I am trying to convert a timestamp to milliseconds in a script using an external program. The external program outputs the time in the shortest possible format Example the external program can output any of the following formats:

"1:33.823" #1min:33 seconds.823 - shortest
"11:33.823" #11min:33 seconds.823 - short
"1:11:33.823" #1 hour 11min:33 seconds.823 - long

I need this value converted in milliseconds. so I tried [timestamp] $mystring but it complains about the format in the first case. I thought about parsing the format manually with something like

$format='h\:mm\:ss\.ff'
[timespan]::ParseExact($mystring,$format , $null) 

But the problem with this approach is that I have to predict every possible output format of the external program and implement all the cases manually (if shortest then $format=m\:ss\.fff else if short then $format=...

Or I can possibly split and define the string, loop from the back and define the attributes of the TimeSpan object manually.

My question is: are there any standard (good practice) conversion methods for my case provided in the language or are my "quick and dirty" solutions common?

Thanks

CodePudding user response:

If you ensure that your time-span strings have at least 3 :-separated components, you can cast them directly to [timespan] (which, behind the scenes, delegates to [timespan]::Parse($inputString, [cultureinfo]::InvariantCulture))[1]

@(
  "1:33.823" #1min:33 seconds.823 - shortest
  "11:33.823" #11min:33 seconds.823 - short
  "1:11:33.823" #1 hour 11min:33 seconds.823 - long
) | ForEach-Object {
  $colonCount = ($_ -replace '[^:]').Length
  [timespan] ('0:' * [math]::Max(0, (3 - $colonCount - 1))   $_)
}

The above transforms the input strings to 0:1:33.823, 0:11:33.823, and 1:11:33.823 before casting, i.e. prepends 0: components as needed.


[1] PowerShell by design uses the invariant culture when possible - see this answer for more information.

  • Related