将不同的时间戳转换为统一的格式。

huangapple go评论59阅读模式
英文:

convert varying timestamps to a uniform format

问题

我正在尝试在脚本中使用外部程序将时间戳转换为毫秒。外部程序以最短可能的格式输出时间。

例如,外部程序可以输出以下任何格式之一:

"1:33.823" #1分钟33秒.823 - 最短
"11:33.823" #11分钟33秒.823 - 短
"1:11:33.823" #1小时11分钟33秒.823 - 长

我需要将这个值转换为毫秒。所以我尝试了 [timestamp] $mystring 但是在第一种情况下它抱怨格式不正确。我考虑手动解析格式,类似于:

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

但是这种方法的问题在于我必须预测外部程序的每种可能的输出格式,并手动实现所有情况(如果是最短的,那么 $format=m\:ss\.fff,如果是短的,那么 $format=...)。

或者我可以可能拆分和定义字符串,从后往前循环,并手动定义TimeSpan对象的属性。

我的问题是:在语言中是否提供了任何标准(良好实践)的转换方法适用于我的情况,还是我的“快速脏”解决方案常见?

谢谢

英文:

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

答案1

得分: 2

如果您确保您的时间跨度字符串至少有 3: 分隔的组件,您可以将它们直接转换为 [timespan](在幕后委托给 [timespan]::Parse($inputString, [cultureinfo]::InvariantCulture))<sup>[1]</sup>

@(
  "1:33.823" #1分钟:33秒.823 - 最短
  "11:33.823" #11分钟:33秒.823 - 短
  "1:11:33.823" #1小时11分钟:33秒.823 - 长
) | ForEach-Object {
  $colonCount = ($_ -replace '[^:]').Length
  [timespan] ('0:' * [math]::Max(0, (3 - $colonCount - 1)) + $_)
}

上述在转换之前将输入字符串转换为 0:1:33.8230:11:33.8231:11:33.823,即根据需要添加 0: 组件。


<sup>[1] PowerShell 在可能的情况下使用 不变的文化 - 有关更多信息,请参阅 此答案。</sup>

英文:

<!-- language-all: sh -->

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))<sup>[1]</sup>

@(
  &quot;1:33.823&quot; #1min:33 seconds.823 - shortest
  &quot;11:33.823&quot; #11min:33 seconds.823 - short
  &quot;1:11:33.823&quot; #1 hour 11min:33 seconds.823 - long
) | ForEach-Object {
  $colonCount = ($_ -replace &#39;[^:]&#39;).Length
  [timespan] (&#39;0:&#39; * [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.


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

huangapple
  • 本文由 发表于 2023年1月9日 04:37:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定