Tuesday, August 13, 2013

Set your PC's Hardware date and time


The clock that is managed by Linux kernel (usually called software clock) is not the same as the hardware clock (the result returned by the date command). Hardware clock is also called as BIOS clock. Hardware clock runs even when you shutdown your system. You can change the date and time of the hardware clock from the BIOS. However, when the system is up and running, you can still view and set the hardware date and time using Linux hwclock command.

Hardware clock is battery driven. The battery ensures that the clock will work even if the rest of the computer is without electricity. During the server booting, Linux sets its own clock to the same time as the hardware clock. After this, both clocks run independently. The reason that Linux maintains its own clock because looking at the hardware is slow and complicated.

OS: CentOS 6.x

1. Display Hardware Clock Date and Time, you have to be root otherwise you will get "Cannot access the Hardware Clock via any known method." message.

# hwclock
Sat 10 Aug 2013 08:26:12 AM PDT  -0.312862 seconds
# hwclock; date
Tue 13 Aug 2013 02:51:11 PM EDT  -0.906702 seconds
Tue Aug 13 14:51:11 EDT 2013


Note: The output of the date command might be same as the output of the hwclock command (actually it should be the same).

2. Copy System Time to Hardware Time
# hwclock -w
# hwclock; date
Tue 13 Aug 2013 02:51:11 PM EDT  -0.906702 seconds
Tue Aug 13 14:51:11 EDT 2013


Note: Both -w and –systohc option does the same. I like to use –systohc as it is easy to remember. –systohc stands for “system to hardware clock”, which copies the time from system to hardware clock.

3. Set Hardware Clock Date and Time Manually
Instead of copying the system date and time to the hardware clock, you can also manually set the value using –set and –date option as shown below.
# hwclock --set --date 8/11/2013
# hwclock; date
Sun 11 Aug 2013 12:00:02 AM EDT  -0.219273 seconds
Tue Aug 13 14:53:01 EDT 2013


You can also set both date and time at the same time as shown below. Please note that it will use the local time by default.

# hwclock --set --date "8/11/2013 23:10:45"
# hwclock
Sun 11 Aug 2013 11:10:48 PM PDT  -0.562862 seconds


You can also take the output of the date command, and pass it to the –set and –date option as shown below.
# hwclock --set --date "Sat Aug 10 08:31:24 PDT 2013"

4. Copy Hardware Time to System Time
# hwclock -s

Note: Both -s and –hctosys option does the same. I like to use –hctosys as it is easy to remember. –hctosys stands for “hardware clock to system”, which copies the time from hardware clock to system clock.
# hwclock -hctosys


5. hwclock Debug Mode
You can pass the "--debug" option to hwclock command, to see extacly what it does:
# hwclock -w --debug
hwclock from util-linux-ng 2.17.2
Using /dev interface to clock.
Last drift adjustment done at 1376419995 seconds after 1969
Last calibration done at 1376419995 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2013/08/13 18:54:58
Hw clock time : 2013/08/13 18:54:58 = 1376420098 seconds since 1969
Time elapsed since reference time has been 0.004028 seconds.
Delaying further to reach the new time.
Setting Hardware Clock to 18:54:58 = 1376420098 seconds since 1969
ioctl(RTC_SET_TIME) was successful.
Not adjusting drift factor because it has been less than a day since the last calibration.



6. hwclock Test Mode
When you change things using hwclock, using –test option, you can execute the command without really making any changes.

The –systz option will change the system timezone accordingly. But, since we gave –test it will not really make the change, but it will still execute the command without any issues.
# hwclock --systz --test
Not setting system clock because running in test mode.

The best use of test mode is when you combine it with debug option. I.e When you want to see what the hwclock command does, but without making any changes.

No comments: