在Ubuntu系统中,你可以使用命令行工具date来处理时间戳和时间计算
date +%s
这将返回一个整数,表示从1970年1月1日(UTC)到现在的秒数。
date -d @<timestamp> "+%Y-%m-%d %H:%M:%S"
将<timestamp>替换为你想要转换的时间戳。
例如:
date -d @1633024800 "+%Y-%m-%d %H:%M:%S"
这将返回 “2021-10-01 00:00:00”(根据你输入的时间戳)。
echo $((<timestamp1> - <timestamp2>))
将<timestamp1>和<timestamp2>替换为你想要计算差值的两个时间戳。
例如:
echo $((1633024800 - 1632938400))
这将返回 “86400”,表示两个时间戳之间相差86400秒。
date -d @"$((<timestamp1> - <timestamp2>))" "+%d days, %H hours, %M minutes, %S seconds"
将<timestamp1>和<timestamp2>替换为你想要计算差值的两个时间戳。
例如:
date -d @"$((1633024800 - 1632938400))" "+%d days, %H hours, %M minutes, %S seconds"
这将返回 “1 days, 0 hours, 0 minutes, 0 seconds”,表示两个时间戳之间相差1天。