在 Ubuntu(Linux)里,“时间戳”通常指 Unix 时间戳(从 1970-01-01 00:00:00 UTC 起的秒数,或毫秒数)。对比时间戳常见有几种场景,下面按常见用法说明。
ts1=1700000000
ts2=1700003600
if [ "$ts1" -lt "$ts2" ]; then
echo "ts1 早于 ts2"
else
echo "ts1 晚于或等于 ts2"
fi
Shell 中时间戳就是整数,可直接比较。
date -d @1700000000
date -d "2023-11-15 00:00:00" +%s
date +%s%3N
now=$(date +%s)
ts=1700000000
if [ "$now" -gt "$ts" ]; then
echo "当前时间晚于该时间戳"
fi
ts1=1700000000
ts2=1700003600
diff=$((ts2 - ts1))
echo "相差 ${diff} 秒"
如果有一堆时间戳:
echo -e "1700003600\n1700000000\n1699999999" | sort -n
date -d @时间戳 显示的是本地时间ts_ms=1700000000000
ts_s=$((ts_ms / 1000))
如果你能说具体场景(比如:shell 脚本?Python?日志分析?数据库?),我可以给你更贴切的示例。