bash脚本 - 如何在两个Unix -Timestamp之间获得时间差(小时:分钟:秒)
我需要在小时,分钟和几秒钟内获得两个Unix-Timestamps之间的时差。我可以在几分钟和几秒钟内正确获得不同的情况,但是小时总是错的。
我有两个unix-timestamps,我减去有所不同。
1595455100-1595452147 = 2953 (这是同一天2:09:07 pm和2:58:20 pm之间的时间差)
然后我做日期-D @2953 +'%i:%m:%s'
我得到04:49:13
,但我希望获得0:49:13
我也尝试了date -d @2953 +'%H:%M:%M: %s'
和日期-d @2953 +'%t'
并获得16:49:13
。我还检查了大于一个小时的差异,分钟和几秒钟的差异仍然正确,但小时仍然错误。
我不了解小时格式,也不理解任何帮助。
I need to get the time difference between two unix-timestamps in hour, minute, and seconds. I can get the different in minutes and seconds correctly but the hour is always wrong.
I have two unix-timestamps and I subtract to get the difference.
1595455100 - 1595452147 = 2953
(That's the time difference between 2:09:07pm and 2:58:20pm on the same day)
Then I do date -d @2953 +'%I:%M:%S'
I get 04:49:13
but I expect to get 0:49:13
I'd also tried date -d @2953 +'%H:%M:%S'
and date -d @2953 +'%T'
and get 16:49:13
. I've also checked differences greater than an hour, the difference in minute and seconds is still correct but the hour is still wrong.
I don't understand the Hour format and appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误答案的原因是因为时间戳 @0不是某种绝对的0。这是固定的日期时间:
您可以做:
只要天数小于31
The reason for wrong answer is because the timestamp @0 is not an absolute 0 of some kind. It is the fixed datetime at:
You could do:
as long as number of days is less than 31
对于此转换,您不一定需要
日期
。您可以使用整数数学和printf
进行娱乐和利润。也可能有一种方法可以使用
date
,但是以上应该可靠地工作。You don't necessarily need
date
for this conversion. You can use integer math andprintf
for fun and profit.There may be a way to do it with
date
too, but the above should work reliably.