简体   繁体   English

在 Bash 中计算 PowerShell 滴答

[英]Calculating PowerShell ticks in Bash

I am translating a PowerShell script in Bash.我正在 Bash 中翻译 PowerShell 脚本。

This is how the ticks for current datetime are obtained in PowerShell:这是在 PowerShell 中获取当前日期时间刻度的方式:

[System.DateTime]::Now.Ticks;

By following the definition of Ticks , this is how I am trying to approximate the same calculation using the date command in bash:按照Ticks定义,这就是我尝试使用 bash 中的date命令近似相同计算的方式:

echo $(($(($(date -u '+%s') - $(date -d "0001-01-01T00:00:00.0000000 UTC" '+%s'))) * 10000000 ))

This is what I got the last time I tried:这是我最后一次尝试得到的结果:

$ echo $(($(($(date -u '+%s') - $(date -d "0001-01-01T00:00:00.0000000 UTC" '+%s'))) * 10000000 )) ; pwsh -c "[System.DateTime]::Now.Ticks;"
637707117310000000
637707189324310740

In particular, the first 7 digits are identical, but digits in position 8 and 9 are still too different between the two values.特别是,前 7 位数字是相同的,但是这两个值之间第 8 位和第 9 位的数字仍然相差太大。

I calculated that this means there is just a 2 hours difference between the 2 values.我计算出这意味着两个值之间只有 2 小时的差异。 But why?但为什么? It cannot be the timezone, since I specified UTC timezone in both date commands, right?它不能是时区,因为我在两个date命令中都指定了 UTC 时区,对吗? What do you think?你怎么认为?

Note: my suspects about the timezone are increasing, since I am currently based in UTC+2 (therefore 2 hours difference from UTC), but how is this possible since I explicitly specified UTC as timezone in the date commands?注意:我对时区的怀疑正在增加,因为我目前基于 UTC+2(因此与 UTC 相差 2 小时),但是这怎么可能,因为我在date命令中明确指定了 UTC 作为时区?

Solved it!解决了! The problem wasn't in the date commands, it was in the PowerShell command, which was using the +2 Timezone (CEST time).问题不在于date命令,而在于使用 +2 时区(CEST 时间)的 PowerShell 命令。 To fix this, I am now using UtcNow instead of Now .为了解决这个问题,我现在使用UtcNow而不是Now

This is what I am getting now:这就是我现在得到的:

$ echo $(($(($(date -u '+%s') - $(date -d "0001-01-01T00:00:00.0000000 UTC" '+%s'))) * 10000000 )) ; pwsh -c "[System.DateTime]::UtcNow.Ticks;"
637707132410000000
637707132415874110

As you can see, now all the digits are identical, except for the last 7th digits, since I added zeros on purpose to convert from seconds to ticks, as I am not interested in fractions of seconds (for now) and I consider them negligible.如您所见,现在所有数字都相同,除了最后的第 7 位数字,因为我故意添加了零以将秒转换为刻度,因为我对几分之一秒(目前)不感兴趣,我认为它们可以忽略不计.

Alternative way替代方式
Another way to make the two values identical (still excluding fractions of seconds), is to remove the -u option in the first date command in order to use the current time zone, and replace UTC with +0200 in the second date command.使这两个值相同(仍不包括秒的小数部分)的另一种方法是删除第一个date命令中的-u选项以使用当前时区,并在第二个date命令+0200 UTC替换为+0200 If I do this, I can leave Now on the PowerShell command (instead of replacing it with UtcNow ).如果我这样做,我可以将Now留在 PowerShell 命令上(而不是用UtcNow替换它)。

By doing this, I am getting:通过这样做,我得到:

$ echo $(($(($(date '+%s') - $(date -d "0001-01-01T00:00:00.0000000 +0200" '+%s'))) * 10000000)) ; pwsh -c "[System.DateTime]::Now.Ticks;"
637707218060000000
637707218067248090

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM