温馨提示×

debian时间戳如何校验

小樊
39
2026-10-16 16:37:46
栏目: 智能运维

在 Debian(以及所有 Linux 系统)里,“时间戳”通常指 Unix 时间戳(自 1970-01-01 00:00:00 UTC 起的秒数)。
“校验时间戳”一般有三种常见含义,我分别说明:


一、校验“时间戳是否合法(格式/范围)”

1. 判断是否为数字

ts=1700000000
if [[ "$ts" =~ ^[0-9]+$ ]]; then
    echo "合法时间戳"
else
    echo "非法时间戳"
fi

2. 判断是否在合理范围

now=$(date +%s)
if [ "$ts" -ge 0 ] && [ "$ts" -le "$now" ]; then
    echo "时间戳合理"
fi

二、校验“时间戳与系统时间是否一致”

1. 获取当前时间戳

date +%s

2. 将时间戳转成可读时间

date -d @1700000000

或

date -d @1700000000 -u   # UTC 时间

3. 校验某个时间戳是否“刚刚”

now=$(date +%s)
diff=$((now - ts))
if [ "$diff" -lt 60 ]; then
    echo "时间戳在 1 分钟内"
fi

三、校验“文件时间戳是否正确”(常用于日志/备份)

1. 查看文件时间戳

stat filename

2. 按时间戳查找文件

# 修改时间在 1 天内的文件
find /path -type f -mtime -1

3. 用时间戳设置文件时间(用于修复)

touch -d @1700000000 filename

四、NTP 时间校验(防止时间戳错乱)

如果时间戳“莫名其妙不对”,可能是系统时间漂移:

timedatectl status

同步时间:

apt install ntpdate
ntpdate pool.ntp.org

或(推荐):

apt install systemd-timesyncd
timedatectl set-ntp true

五、常见校验场景示例

✅ 校验接口返回的 token 时间戳是否过期

exp=1700000000
now=$(date +%s)
if [ "$now" -gt "$exp" ]; then
    echo "已过期"
fi

如果你说的“校验”是指:

  • 校验 证书时间戳
  • 校验 日志时间是否被篡改
  • 校验 签名里的时间戳(如 TSA)

可以告诉我具体场景,我可以给你更精确的命令或脚本。

0 踩