我想以人类可读的格式获取自上次更新文件以来的时间。在互联网上,我发现:
now=$(date +%s) # to get timestamp of now
last=$(stat -c %Y /var/cache/apt/) # to get the timestamp of the last update
但是现在我想用last
划分now
,并在 Debian shell 中以人类可读的格式打印出来?
你可以编写一个函数:
time_since() {
file=$1
now=$(date +%s)
file_time=$(stat -c %Y "$file")
seconds=$((now - file_time))
hh=$(( $seconds / 3600 ))
mm=$(( ($seconds - $hh * 3600) / 60 ))
ss=$(( $seconds - $hh * 3600 - $mm * 60 ))
printf '%d:%02d:%02dn' $hh $mm $ss
}
time_since /var/cache/apt
# output => 332:17:07
使用 bash
& printf
:
time_since() {
eval "$(stat -s "$1")" # st_mtime=1490029104
TZ=UTC printf "%(%T)Tn" $(( $(printf "%(%s)T") - st_mtime ))
}
time_since file.txt
#out -> 02:07:02
某些持续时间值(以秒为单位的时差(与纪元时间相同。(例如,与纪元 (UTC( 的秒差(