บทที่ 2 การตั้งค่า วันที่ และ เวลา Date and Time

ในระบบปฎิบัติปัจจุบันได้จำแนกเวลาออกเป็น สองชนิด

  1. Real-time Clock โดยทำการอ้างถึง hardware clock (วงจร บนบอร์ด) จะทำงานตลอดเวลา แม้ว่าเครื่องคอมพิวเตอร์จะหยุดทำงานก็ตาม
  2. System clock หรือที่เรียกว่า software clock จะทำดำเนินการในระดับของ kernel โดยทำการอ้างอิงเริ่มต้นจาก real-time ตอนเริ่มต้นทำงาน แต่หลังจากที่ได้เริ่มต้นการทำงานไปแล้ว การทำงานก็จะเป็นอิสระ

การใช้งานคำสั่ง timedatectl เพื่อแสดง ค่าของ local time และ universal time นอกจากนั้นยังแสดงสถานะของ NTP (Network time server)

# timedatectl
      Local time: Sat 2017-11-11 12:33:05 UTC
  Universal time: Sat 2017-11-11 12:33:05 UTC
        RTC time: Sat 2017-11-11 08:03:39
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

ตั้งค่า timezone

# timedatectl set-timezones  time_zone
# timedatectl list-timezones | grep Bangkok
Asia/Bangkok

# timedatectl set-timezone Asia/Bangkok
Local time: Sat 2017-11-11 19:42:37 +07
Universal time: Sat 2017-11-11 12:42:37 UTC
RTC time: Sat 2017-11-11 19:42:37
Time zone: Asia/Bangkok (+07, +0700)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a

เมื่อมีการเปลี่ยนแปลงสถานะของ chrony หรือ ntp ก็ยังไม่ได้รับรู้โดย timedatectl จะต้องทำการ restart

# systemctl restart systemd-timedated.service

การตั้งเวลา เอง

# timedatectl set-time HH:MM:SS

HH - เวลา ชั่วโมง MM - เวลานาที SS - เวลาวินาที

ตั้งค่า วัน เดือน ปี

timedatectl set-time YYYY-MM-DD

YYYY- ปี คศ MM -เดือน DD- วันที่

การตั้งค่าอัตโนมัติ ให้ system clock เชื่อมโยงกับ remote server

การตั้งค่านี้จะต้องติดตั้ง ใช้งาน Chrony ไปด้วยกัน

# yum install chrony
# vi /etc/chrony.conf

# systemctl restart chronyd

# timedatectl set-ntp yes

แสดงผลวัน เวลาปัจจุบัน Date/Time

# date
Sat Nov 11 20:16:49 +07 2017

# date --utc
Sat Nov 11 13:16:57 UTC 2017

# date +"%Y-%m-%d %H:%M"
2017-11-11 20:24 

# date +"%F %T"

คำสั่ง date +"format"

Control Sequence Description
%H เวลาชั่วโมง HH format
%M เวลานาที MM format
%S เวลาวินาที SS format
%d วันที่ของ เดือน DD format
%m เดือน MM format (หากต้องการแบบอักษร ใช้ %h)
%Y ปี คศ
%Z ตัวย่อของ timezone
%F แทนรูปแบบวันเดือนปี YYYY-MM-DD เช่นเดียวกับ "%Y-%m-%d"
%T แทนรูปแบบของวันเวลา HH:MM:SS

ตั้งค่าเวลา ด้วยคำสั่ง date

เช่น วันที่ 11 เดือน พฤศจิกายน 2560 เวลา 20:30

date --set "2017-11-11 20:30:00"

การแสดงผลเวลาด้วยคำสั่ง

# hwclock --utc
Sun 12 Nov 2017 04:21:24 AM +07  -0.510940 seconds

# hwclock --local
Sat 11 Nov 2017 09:22:30 PM +07  -0.135211 seconds

results matching ""

    No results matching ""