Most of processing of time in Linux system are "check the time" or "set the time". Regardless of whether you are viewing the time or setting the time, you can use the date
command to complete it. If you want to see the calendar, use the cal
command (short for calendar).
This is generally the case. The following is a simple record of some common usage methods, right as an example.
Check The Time
The time on the system is divided into two types. One is the time on the system, which is the clock that we can adjust at will through the graphical interface. It is also the clock that we usually view on the computer; the other is the hardware clock, which is set in the computer, but we can also adjust it through the BIOS.
Check System Time
As mentioned above, we can check the system time via the date
command.
date -R
Output:
Thu, 17 Sep 2020 16:30:44 +0800
Check Hardware Time
If you want to check the hardware time, you can use hwclock
command to view.
要查看硬體時間,我們可以使用 hwclock
指令來查看。
sudo hwclock -r
Output:
2020-09-17 16:28:57.023147+0800
Check Calendar
In Linux, we can also view the calendar at any time through commands:
cal
Output:
Set The Time
Manually Set The System Time
If we have a requirements related to testing different times, we can manually adjust the system time artificially.
Suppose we want to adjust the current time to 04:30 pm on December 31, 2006, we can set the system time via the following command.
sudo date -s "20061231 16:30:00"
Also remember to write in the hardware time.
sudo hwclock -w
Automatically set system time (adjust time zone)
You can use the following commands to see which different time zones can be set. (Here I take Asia as an example)
timedatectl list-timezones | grep Asia
Output:
...
Asia/Taipei
Asia/Tashkent
Asia/Tbilisi
Asia/Tehran
Asia/Thimphu
Asia/Tokyo
Asia/Tomsk
Asia/Ulaanbaatar
Asia/Urumqi
Asia/Ust-Nera
Asia/Vientiane
Asia/Vladivostok
Asia/Yakutsk
Asia/Yangon
Asia/Yekaterinburg
Asia/Yerevan
For example, if I want to set the time zone in Taipei, I can use the following command:
sudo timedatectl set-timezone Asia/Taipei
References
- https://www.cyberciti.biz/faq/howto-set-date-time-from-linux-command-prompt/
- https://unix.stackexchange.com/questions/151547/linux-set-date-through-command-line
- https://www.garron.me/en/linux/set-time-date-timezone-ntp-linux-shell-gnome-command-line.html