英文:
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.823
、0:11:33.823
和 1: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>
@(
"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.
<sup>[1] PowerShell by design uses the invariant culture when possible - see this answer for more information.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论