Article Title: linux tips -- set the clock and time. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This document explains how to set a computer clock in linux, how to set your time zone, and other materials related to how linux saves time.
Your computer has two clocks. One is a always-running, battery (hardware, BIOS, or CMOS) clock, the other is the (system) Clock maintained by the operating system running on your computer. The hardware clock is usually only used to set the system clock when the operating system is started, and then record the time until the system is restarted or shut down. In Linux, you can choose to use UTC/GMT time or local time to record the hardware clock. The recommended option is to use UTC for record, because it can be automatically recorded during timeout. The only disadvantage of using UTC to record the hardware clock is that if you use a dual system and other operating systems, such as DOS, require that the hardware clock be set with local time, the time in that operating system will be incorrect.
Set the time zone:
The Time Zone in Linux is implemented by establishing a symbolic link between the/etc/localtime  and/usr/share/zoneinfo  directories that match your time zone.. For example, since I am in South Australia,/etc/localtime is the symbolic link to/usr/share/zoneinfo/Australia/South. To establish this link, run:
Replace your/zone with a file in the form of Australia/NSW or Australia/Perth. Check the time zone of the/usr/share/zoneinfo directory.
 Here we assume that/usr/share/zoneinfo is the prerequisite for link to/etc/localtime is Redhat Linux
 in earlier versions, you will find that/usr/lib/zoneinfo is used instead of/usr/share/zoneinfo. Refer to "time errors in some applications ".
Set UTC or local time:
When Linux is started, a STARTUP script runs the/sbin/hwclock program to copy the current hardware clock time to the system clock. Hwclock assumes that the hardware clock is set to the local time unless it uses the -- utc parameter. In RedHat Linux, you do not edit the startup script, but edit the/etc/sysconfig/clock file. The corresponding change in UTC format is UTC = true or UTC = false.
Set the system clock:
In Linux, set the system clock to use the date command. For example, set the current time and date to July 07312316, 11: 16 pm, and run date 1998 (note that the time here is in the 24-hour format). If you want to set the year,
Run date 073123161998. If you want to set the second, run date 07312316.30 or date 073123161998.30. To view the current Linux local time, use date without parameters.
Set the hardware clock:
To set the hardware clock, I like to first set the system clock and then set the hardware clock to the current system clock time, run the/sbin/hwclock -- systohc (or/sbin/hwclock -- systohc -- utc) command to save the hardware clock in UTC ). To view the current hardware clock settings, run hwclock without parameters. If the hardware clock is saved in UTC, and you want to view the local time, run/sbin/hwclock -- utc.
Time errors in some applications:
If some applications, such as date, display the correct time while others are incorrect, and you are running RedHat Linux 5.0/5.1, you may encounter a bug caused by moving the time zone information from/usr/lib/zoneinfo to/usr/share/zoneinfo. The solution is to create a symbolic link from/usr/lib/zoneinfo to/usr/share/zoneinfo:
*/Etc/sysconfig/clock: Set the hardware clock, whether saved in UTC or locally
* Set the time zone by concatenating/etc/localtime to/usr/share/zoneinfo /...
* Run date MMDDhhmm to set the current system date/time
* Run/sbin/hwclock -- systohc [-- utc] to set the hardware clock.
Other interesting Annotations:
Linux kernel stores and computes time based on the number of seconds since midnight, January 1, January 1, 1970 UTC time, regardless of whether your hardware clock is saved in UTC. The task of converting to local time is completed at runtime. In this way, if someone uses your computer from different time zones, he can set the time zone environment variable, and all the dates and times can be correctly displayed by their time zone.
If the number of seconds since January 1, January 1, 1972 in UTC is saved as a 32-bit integer with plus or minus signs, your clock will stop working on January 1, 2038, just like on your Linux/Intel System. Linux has no Y2K problem, but it does have problems in 2038. It is expected that at that time we will all use 64-bit systems to run Linux. A 64-bit integer will keep our clock running for about 292.271 billion years.
Other programs worth reading:
* Rdate -- get the current time from the remote machine; it can be used to set the system time
* Xntpd-similar to rdata, but it is quite accurate, and you need to have a permanent network connection to run xntpd continuously, records network latency, clock drift, and other events, but there is also a program (ntpdate) included, set the current time like rdate.
* Date (1)
* Hwclock (8)
Linux, Clocks, and Time
This document explains how to set your computer's clock from Linux, how to set your timezone, and other stuff related to Linux and how it does its time-keeping.
Your computer has two timepieces; a battery-backed one that is always running (the ''hardware '', ''bios'', or ''cmos ''clock ), and another that is maintained by the operating system currently running on your computer (the 'system' clock ). the hardware clock is generally only used to set the system clock when your operating system boots, and then from that point until you reboot or turn off your system, the system clock is the one 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. the only disadvantage with keeping the hardware clock in UTC 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.
Setting your timezone
The timezone under Linux is set by a symbolic link from/etc/localtime  to a file in the/usr/share/zoneinfo  directory that corresponds with what timezone you are in. for example, since I'm in South Australia,/etc/localtime is a symlink to/usr/share/zoneinfo/Australia/South. to set this link, type:
Replace your/zone with something like Australia/NSW or Australia/Perth. Have a look in the directories under/usr/share/zoneinfo to see what timezones are available.
 This assumes that/usr/share/zoneinfo is linked to/etc/localtime as it is under Red Hat Linux.
 On older systems, you'll find that/usr/lib/zoneinfo is used instead of/usr/share/zoneinfo. see also the later section ''the time in some applications is wrong ''.
Setting UTC or local time
When Linux boots, one of the initialisation scripts will run the/sbin/hwclock program to copy the current hardware clock time to the system clock. hwclock will assume the hardware clock is set to local time unless it is run with the -- utc switch. rather than editing the startup script, under Red Hat Linux you shoshould edit the/etc/sysconfig/clock file and change the ''utc' line to either ''utc = true' or ''utc = false' 'As appropriate.
Setting the system clock
To set the system clock under Linux, use the date command. as an example, to set the current time and date to July 31, 11: 16 pm, type ''date 07312316 ''(note that the time is given in 24 hour notation ). if you wanted to change the year as well, you cocould type ''date 073123161998 ''. to set the seconds as well, type ''date 07312316.30 ''or ''date 073123161998.30 ''. to see what Linux thinks the current local time is, run date with no arguments.
Setting the hardware clock
To set the hardware clock, my favorite way is to set the system clock first, and then set the hardware clock to the current system clock by typing ''/sbin/hwclock -- systohc'' (or ''/sbin/hwclock -- systohc -- utc'' if you are keeping the hardware clock in UTC ). to see what the hardware clock is currently set to, run hwclock with no arguments. if the hardware clock is in UTC and you want to see the local equivalent, type ''/sbin/hwclock -- utc''
The time in some applications is wrong
If some applications (such as date) display the correct time, but others don't, and you are running Red Hat Linux 5.0 or 5.1, you most likely have 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: ''ln-s .. /share/zoneinfo/usr/lib/zoneinfo ''.
*/Etc/sysconfig/clock sets whether the hardware clock is stored as UTC or local time.
* Symlink/etc/localtime to/usr/share/zoneinfo/... to set your timezone.
* Run ''date MMDDhhmm ''to set the current system date/time.
* Type ''/sbin/hwclock -- systohc [-- utc]'' to set the hardware clock.
Other interesting notes
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 run-time. one neat thing about this is that if someone is using your computer from a different timezone, they can set the TZ environment variable and all dates and times will appear correct for their timezone.
If the number of seconds since the 1st of January 1970 UTC is stored as an signed 32-bit integer (as it is on your Linux/Intel system ), your clock will stop working sometime on the year 2038. linux has no inherent Y2K problem, but it does have a year 2038 problem. hopefully we'll all be running Linux on 64-bit systems by then. 64-bit integers will keep our clocks running quite well until aproximately the year 292271-million.
Other programs worth looking
* Rdate-get the current time from a remote machine; can be used to set the system time.
* Xntpd-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, but there's also a program (ntpdate) included that just sets the current time like rdate does.
* Date (1)
* Hwclock (8)