当我在awk中使用strftime时,它会增加一个小时。怎么修

时间:2013-11-20 12:14:46

标签: awk

当我尝试将秒转换为显示H:M:S时,strftime中的awk会增加一小时。
为什么以及如何解决?
EKS

echo "3600" | awk '{print strftime("%T",$1)}'
02:00:00  # Here i would like to see 01:00:00

echo "60" | awk '{print strftime("%T",$1)}'
01:01:00  # Here i would like to see 00:01:00

这给出了正确的结果,但这是正常的吗?

echo "72" | awk '{print strftime("%T",int($1)-3600)}'
00:01:12

编辑:这是一个解决方法,但我现在仍然喜欢为什么strftime没有给我正确答案。

echo "3958" | awk '{h=int($0/3600);m=int(($0-h*3600)/60);s=($0-h*3600-m*60);printf "%02d:%02d:%02d\n",h,m,s}'
01:05:58

echo "64" | awk '{h=int($0/3600);m=int(($0-h*3600)/60);s=($0-h*3600-m*60);printf "%02d:%02d:%02d\n",h,m,s}'
00:01:04

1 个答案:

答案 0 :(得分:3)

在GNU awk中,您可以添加utc-flag以使用通用时间码。

从手册页:

strftime([format [, timestamp[, utc-flag]]])
  Formats  timestamp  according  to  the  specification  in format.  If utc-flag is present and is non-zero or non-null, the result is in UTC, otherwise the
  result is in local time.  The timestamp should be of the same form as returned by systime().  If timestamp is missing, the current time of  day  is  used.
  If  format  is missing, a default format equivalent to the output of date(1) is used.  See the specification for the strftime() function in ANSI C for the
  format conversions that are guaranteed to be available.

示例:

echo "3600" | awk '{print strftime("%T",$1,1)}'
01:00:00