在 Debian(以及所有 Linux 系统)里,“时间戳”通常指 Unix 时间戳(自 1970-01-01 00:00:00 UTC 起的秒数)。
“校验时间戳”一般有三种常见含义,我分别说明:
ts=1700000000
if [[ "$ts" =~ ^[0-9]+$ ]]; then
echo "合法时间戳"
else
echo "非法时间戳"
fi
now=$(date +%s)
if [ "$ts" -ge 0 ] && [ "$ts" -le "$now" ]; then
echo "时间戳合理"
fi
date +%s
date -d @1700000000
或
date -d @1700000000 -u # UTC 时间
now=$(date +%s)
diff=$((now - ts))
if [ "$diff" -lt 60 ]; then
echo "时间戳在 1 分钟内"
fi
stat filename
# 修改时间在 1 天内的文件
find /path -type f -mtime -1
touch -d @1700000000 filename
如果时间戳“莫名其妙不对”,可能是系统时间漂移:
timedatectl status
同步时间:
apt install ntpdate
ntpdate pool.ntp.org
或(推荐):
apt install systemd-timesyncd
timedatectl set-ntp true
exp=1700000000
now=$(date +%s)
if [ "$now" -gt "$exp" ]; then
echo "已过期"
fi
如果你说的“校验”是指:
可以告诉我具体场景,我可以给你更精确的命令或脚本。