在 Ubuntu 系统中,可以使用多种方法来生成时间戳。以下是一些常用的方法:
使用 date
命令:
当前时间的时间戳(秒级):
date +%s
当前时间的时间戳(毫秒级):
date +%s%3N
或者
date +%s | xargs -I {} echo "{}$(printf '%03d' $(date +%N | cut -b1-3))"
指定时间的时间戳:
如果你想生成某个特定日期和时间的时间戳,可以使用 -d
选项。例如,生成 2023-10-01 12:00:00
的时间戳:
date -d "2023-10-01 12:00:00" +%s
使用 perl
命令:
Perl 也可以用来生成时间戳:
当前时间的时间戳(秒级):
perl -MTime::HiRes -e 'printf "%d\n", Time::HiRes::time()'
当前时间的时间戳(毫秒级):
perl -MTime::HiRes -e 'printf "%d\n", Time::HiRes::time() * 1000'
使用 python
命令:
如果你安装了 Python,也可以使用它来生成时间戳:
当前时间的时间戳(秒级):
python3 -c 'import time; print(int(time.time()))'
当前时间的时间戳(毫秒级):
python3 -c 'import time; print(int(time.time() * 1000))'
这些方法可以帮助你在 Ubuntu 系统中生成所需的时间戳。根据你的具体需求选择合适的方法即可。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Linux时间戳如何生成