时间相关指令

用于终端时间设置

  • 查看当前系统时间状态
1
2
3
4
5
6
7
8
9
10
timedatectl status
输出示例:
Local time: Tue 2023-03-14 15:10:24 CST
Universal time: Tue 2023-03-14 07:10:24 UTC
RTC time: Tue 2023-03-14 07:10:23
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
systemd-timesyncd.service active: yes
RTC in local TZ: no

  • 时区
1
2
3
4
// 列出时区
timedatectl list-timezones
// 修改时区
timedatectl set-timezone "Asia/Shanghai"
  • 设置时间同步(关闭时间同步才能设置时间)
1
timedatectl set-ntp no
  • 修改系统时间
1
2
timedatectl set-time '2088-12-18 18:08:08'

用于RTC时钟测试

  • 写入硬件
1
hwclock --systohc
  • 其他
1
2
3
4
5
6
# 显示硬件时钟与日期
hwclock –r
# 将系统时钟调整为与目前的硬件时钟一致。
hwclock –s
# 将硬件时钟调整为与目前的系统时钟一致
hwclock –w

用于时间格式转换

  • date
1
2
3
4
5
6
7
8
9
10
11
12
13
# 显示当前系统时间
date

# 将Unix时间戳转换为日期 秒数从 1970-1-1 0点开始算起
date -d @seconds

# 字符串转时间 指定转换格式 为 年-月-日 时:分:秒
date -d '20200512 12:12:12' +"%Y-%m-%d %H:%M:%S"

# 时间转秒
date -d "2020-05-12 12:12:12" +%s