在 Ubuntu(Linux)里,“时间戳”一般指 Unix 时间戳(从 1970-01-01 00:00:00 UTC 起的秒数,或毫秒数)。对比时间戳常见有几种场景,下面按实用方式说明。
# 秒级时间戳
date +%s
# 毫秒级时间戳
date +%s%3N
# 秒级
date -d @1710000000
# 毫秒级
date -d @$(echo 1710000000000 / 1000 | bc)
时间戳本质是整数,可直接比大小:
t1=1710000000
t2=1710003600
if [ "$t1" -lt "$t2" ]; then
echo "t1 早于 t2"
fi
diff=$(( t2 - t1 ))
echo "相差 ${diff} 秒"
如果是毫秒:
diff=$(( (t2 - t1) / 1000 ))
stat file.txt
if [ file1.txt -nt file2.txt ]; then
echo "file1 更新"
fi
-nt:newer than-ot:older thannow=$(date +%s)
last=$(stat -c %Y file.txt) # 文件修改时间(秒)
if [ $((now - last)) -gt 3600 ]; then
echo "文件超过 1 小时未更新"
fi
import time
t1 = 1710000000
t2 = time.time()
print(t2 > t1)
如果你指的是:
可以告诉我具体场景,我可以给更针对性的例子。