Linux system clock

To display yesterday date:

date --date="1 days ago"
date --date="1 day ago"
date --date="yesterday"
date --date="-1 day"

To display tomorrow date:

date --date="-1 days ago"
date --date="next day"

To display a date in the future:

date --date='tomorrow'
date --date='1 day'
date --date='10 day'
date --date='10 week'
date --date='10 month'
date --date='10 year'

To display a date in the past:

date --date='yesterday'
date --date='1 day ago'
date --date='10 day ago'
date --date='10 week ago'
date --date='10 month ago'
date --date='10 year ago'

Your computer has two timepieces; a battery-backed one that is always running (even when your computer is powered-off, the "hardware", "BIOS", or "CMOS" clock), and another that is maintained by the operating system (the system clock). The hardware clock is generally only used to initialize the system clock when the operating system boots, and then from that point until you reboot or turn-off your system, the system clock is used to keep track of time.

On Linux systems, you have a choice of keeping the hardware clock in UTC/GMT time or local time. The preferred option is to keep it in UTC because then daylight savings can be automatically accounted for. The disadvantage is that if you dual boot with an operating system (such as DOS) that expects the hardware clock to be set to local time, the time will always be wrong in that operating system.

The timezone under Linux is set by a symbolic link /etc/localtime

When Linux boots, one of the initialization scripts will run the /sbin/hwclock program to copy the current hardware clock time to the system clock. hwclock will assume that the hardware clock is set to local time unless it is run with —utc switch. Rather than editing the start-up script, under Red Hat Linux, we should edit /etc/sysconfig/clock file to change the "UTC" line to either "UTC=true" or "UTC=false" as appropriate.

To set the system clock under Linux, use the "date" command.

To set the current time and date to July 31, 11:16pm (time is given in 24-hour notation):

date 07312316

If we want to change the year as well (the year is tacked on at the end):

date 073123161998

To set the seconds as well:

date 07312316.30
date 073123161998.30

To set your system clock:

date -s "2 OCT 2006 18:00:00"
date set="2 OCT 2006 18:00:00"
date +%Y%m%d -s "20081128"

To set time use the following syntax:

date +%T -s "10:13:13"

Use %p locale’s equivalent of either AM or PM, enter:

date +%T%p -s "6:10:30AM"
date +%T%p -s "12:10:30PM"

To set the hardware clock, we should set the system clock first, and then set the hardware clock to the current system clock:

/sbin/hwclock --systohc

If we are keeping the hardware clock in UTC:

/sbin/hwclock --systohc --utc

To see what the hardware clock is currently set to, run hwclock with no arguments. If the hardware clock is in UTC and we want to see the local equivalent: "/sbin/hwclock —utc"

If some application (such as date) display the correct time, but others don't, and we are running Red Hat, we most likely run into a bug caused by a move of the timezone information from /usr/lib/zoneinfo to /usr/share/zoneinfo. The fix is to create a symbolic link from /usr/lib/zoneinfo to /usr/share/zoneinfo.

The Linux kernel always stores and calculates time as the number of seconds since midnight of the 1st of January 1970 UTC regardless of whether your hardware clock is stored as UTC or not. Conversions to your local time are done at boot-time. The neat thing about this is that if we need to use our computer from a different timezone, we can set the TZ environment variable and all dates and times will appears correctly.

If the number of seconds since the epoch is stored as an signed 32-bit integer (as it is on Linux/Intel system), our clock will stop working sometime in year 2038. Hopefully we'll all be running Linux on 64-bit systems by then. 64-bit integers will keep our clocks running quite well until approximately year 292271-million.

xntpd is like rdate, but it's extremely accurate and you need a permanent net connection. xntpd runs continuously and accounts for things like network delay and clock drift. There is also a program (ntpdate) included that set the current time like rdate does.


Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License