如何在shell脚本中从这种格式的时间中减去毫秒:“2020-01-06 11:27:12.280”

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

How do I subtract milliseconds from time in this format: "2020-01-06 11:27:12.280" in shell script

问题

我有时间格式2020-01-06 11:27:12.280,其中.280表示毫秒。
想要从这个时间中减去500毫秒,并再次以相同的时间格式获得结果。

我尝试过使用以下命令将日期时间转换为纪元时间:

  1. date +%s -d "2020-01-06 11:27:12.280"

但它不考虑毫秒。

英文:

I have time format 2020-01-06 11:27:12.280 where .280 is milliseconds.
Want to subtract 500 milliseconds from this time and get result in same time format again.

I have tried to convert date time in epoch time using this command:

  1. date +%s -d"2020-01-06 11:27:12.280

but is does not consider milliseconds.

答案1

得分: 3

GNU: 使用GNU date,可以直接在date命令内部进行减法运算。这在Léa Gris的评论中有演示:

  1. $ date -d "2020-01-06 11:27:12.280 - 0.5 sec" '+%F %T.%3N'
  2. 2020-01-06 11:27:11.780

这在这里有效,因为GNU date有使用-d标志查询任何日期字符串的可能性,并且具有纳秒的知识。

BSD/MacOS: BSD date的行为不同。在这里需要更多的工作:

  1. $ d="2020-01-06 11:27:12.280"
  2. $ d=$(( $(date -j -f "%F %T" "${d%.*}" "+%s")${d#*.} - 500 ))
  3. $ date -r "${d%${d: -3}}" "+%F %T.${d: -3}"
  4. 2020-01-06 11:27:11.780

POSIX: 如果要遵循POSIX标准,情况就不同了。根据POSIX标准,不能使用date命令,因为无法使用-d标志设置自己的日期时间,也没有指定毫秒或纳秒的格式。因此,需要使用不同的工具。有很多可能的方法,但我们将在这里使用POSIX awk的一个版本:

  1. awk -v d='2020-01-06 11:27:12.280' '
  2. BEGIN{ gsub(/[-:]/," ",d); strftime_posix(mktime_posix(d)-0.5) }
  3. # 从"天文算法"中的算法J.Meeus
  4. function mktime_posix(datestring, a,t) {
  5. split(datestring,a," ")
  6. if (a[1] < 1970) return -1
  7. if (a[2] <= 2) { a[1]--; a[2]+=12 }
  8. t=int(a[1]/100); t=2-t+int(t/4)
  9. t=int(365.25*a[1]) + int(30.6001*(a[2]+1)) + a[3] + t - 719593
  10. return t*86400 + a[4]*3600 + a[5]*60 + a[6]
  11. }
  12. function strftime_posix(epoch, JD,yyyy,mm,dd,HH,MM,SS,A,B,C,D,E ) {
  13. if (epoch < 0 ) return "0000 00 00 00 00 00.000000"
  14. JD=epoch; SS=JD%60; JD-=SS; JD/=60; MM=JD%60;
  15. JD-=MM; JD/=60; HH=JD%24; JD-=HH; JD/=24;
  16. JD+=2440588
  17. A=int((JD - 1867216.25)/(36524.25))
  18. A=JD+1+A-int(A/4)
  19. B=A+1524; C=int((B-122.1)/365.25); D=int(365.25*C); E=int((B-D)/30.6001)
  20. dd=B-D-int(30.6001*E)
  21. mm = E < 14 ? E-1 : E - 13
  22. yyyy=mm>2?C-4716:C-4715
  23. return sprintf("%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%06.3f",yyyy,mm,dd,HH,MM,SS)
  24. }
  25. '
英文:

GNU: Using GNU date, it is possible to do the subtraction directly within the date-command. This is demonstrated in the comment of Léa Gris:

  1. $ date -d &quot;2020-01-06 11:27:12.280 - 0.5 sec&quot; &#39;+%F %T.%3N&#39;
  2. 2020-01-06 11:27:11.780

This works here because GNU date has the possibility to query any date-string using the -d flag and has knowledge of nanoseconds.

BSD/MacOS: BSD date is behaving differently. You need a bit more work here:

  1. $ d=&quot;2020-01-06 11:27:12.280&quot;
  2. $ d=$(( $(date -j -f &quot;%F %T&quot; &quot;${d%.*}&quot; &quot;+%s&quot;)${d#*.} - 500 ))
  3. $ date -r &quot;${d%${d: -3}}&quot; &quot;+%F %T.${d: -3}&quot;
  4. 2020-01-06 11:27:11.780

POSIX: If you want to be POSIX compliant, it is a different story. According to the POSIX standard, the date command cannot be used here as you cannot set your own date-time with the -d flag, and neither is there a format that specifies mili or nanoseconds. Hence, one is required to use different tools. There are many possible ways, but we will use here a version of POSIX awk:

  1. awk -v d=&#39;2020-01-06 11:27:12.280&#39; &#39;
  2. BEGIN{ gsub(/[-:]/,&quot; &quot;,d); strftime_posix(mktime_posix(d)-0.5) }
  3. # Algorithm from &quot;Astronomical Algorithms&quot; By J.Meeus
  4. function mktime_posix(datestring, a,t) {
  5. split(datestring,a,&quot; &quot;)
  6. if (a[1] &lt; 1970) return -1
  7. if (a[2] &lt;= 2) { a[1]--; a[2]+=12 }
  8. t=int(a[1]/100); t=2-t+int(t/4)
  9. t=int(365.25*a[1]) + int(30.6001*(a[2]+1)) + a[3] + t - 719593
  10. return t*86400 + a[4]*3600 + a[5]*60 + a[6]
  11. }
  12. function strftime_posix(epoch, JD,yyyy,mm,dd,HH,MM,SS,A,B,C,D,E ) {
  13. if (epoch &lt; 0 ) return &quot;0000 00 00 00 00 00.000000&quot;
  14. JD=epoch; SS=JD%60; JD-=SS; JD/=60; MM=JD%60;
  15. JD-=MM; JD/=60; HH=JD%24; JD-=HH; JD/=24;
  16. JD+=2440588
  17. A=int((JD - 1867216.25)/(36524.25))
  18. A=JD+1+A-int(A/4)
  19. B=A+1524; C=int((B-122.1)/365.25); D=int(365.25*C); E=int((B-D)/30.6001)
  20. dd=B-D-int(30.6001*E)
  21. mm = E &lt; 14 ? E-1 : E - 13
  22. yyyy=mm&gt;2?C-4716:C-4715
  23. return sprintf(&quot;%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%06.3f&quot;,yyyy,mm,dd,HH,MM,SS)
  24. }
  25. &#39;

huangapple
  • 本文由 发表于 2020年1月6日 19:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611108.html
匿名

发表评论

匿名网友

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

确定