Difference between revisions of "Setting the System Time"

From wiki.emacinc.com
Jump to: navigation, search
Line 1: Line 1:
 
{{todo|Port and polish and finish|Klint Youngmeyer|project=oe 4,oe 5,ky}}
 
{{todo|Port and polish and finish|Klint Youngmeyer|project=oe 4,oe 5,ky}}
  
 +
There are two different clocks on a Linux system. The RTC, or hardware clock, is present on most systems and is used to provide a persistent clock using a battery backup and for other purposes like waking the processor up from suspend mode through an alarm interrupt. The hardware clock is generally only accessed to retrieve an initial time when Linux boots and is rarely accessed from a running system. The system clock is implemented in the kernel and is used for timekeeping purposes for the running system. On Linux, time is represented by the number of seconds after the Unix epoch: 00:00:00 UTC on January 1, 1970.
  
 
== Setting the System Time on EMAC OE Products ==
 
== Setting the System Time on EMAC OE Products ==
Line 37: Line 38:
  
 
On shutdown the system time is saved to the hardware clock automatically with the following command:<syntaxhighlight lang="console">/etc/init.d/hwclock.sh stop</syntaxhighlight>To do this manually after setting the date run the following command:<syntaxhighlight lang="console">/etc/init.d/hwclock.sh restart</syntaxhighlight>
 
On shutdown the system time is saved to the hardware clock automatically with the following command:<syntaxhighlight lang="console">/etc/init.d/hwclock.sh stop</syntaxhighlight>To do this manually after setting the date run the following command:<syntaxhighlight lang="console">/etc/init.d/hwclock.sh restart</syntaxhighlight>
 +
 +
== Hardware Clock ==
 +
 +
The hardware clock can be accessed through the ''hwclock'' command. Running ''hwclock'' with no arguments prints the current value stored in the hardware clock. The ''--systohc'' option sets the value of the hardware clock using the system clock. This command is called by the system shutdown scripts using ''/etc/init.d/hwclock.sh''. The ''--hctosys'' option can be used to set the system clock using the value stored in the hardware clock. This is used by ''/etc/init.d/hwclock.sh'' to initialize the system clock during the boot process. Note that the hardware clock is generally set to UTC regardless of the local timezone. On system shutdown, the current system time is saved to the RTC using the ''/etc/init.d/save-rtc.sh'' script.<br />
 +
 +
 +
{{Fmbox | type =  | text = On some boards you may receive a message such as ''hwclock: RTC_RD_TIME: Invalid or incomplete multibyte or wide character'' or a similar message when attempting to read the hardware clock. This generally means that the hardware clock has not yet been set, in which case running the ''hwclock --systohc'' command will correct the issue.}}<br />
 +
 +
The system can be configured to use either local time (see [[#local_timezone|Local Timezone]]) or UTC for the hardware clock. Although the hardware clock only holds an absolute time value and no timezone value, adjustments can be made to account for the timezone when reading and setting the clocks. By default, EMAC OE uses the local time value for the hardware clock. If UTC is used for the system clock or NTP or another method is used to synchronize the clocks, this will not cause an issue. However, if the hardware clock is the primary method used to initialize the system clock, their will be no way to automatically adjust for Daylight Saving Time changes that occur while the system is off. In these cases, it is best to keep the hardware clock in UTC. This can be done easily by adding the line ''UTC=yes'' to ''/etc/default/rcS''. Doing so will cause the ''/etc/init.d/hwclock.sh'' script to pass the ''utc'' option to the ''hwclock'' command when setting the hardware clock and when using it to set the system clock. The following example illustrates how the ''utc'' option allows the system to automatically account for the timezone when setting and reading the clocks:<syntaxhighlight lang="console">
 +
root@som9g20:~# date
 +
Tue Oct 18 10:34:44 CDT 2011
 +
root@som9g20:~# hwclock --utc --systohc
 +
root@som9g20:~# hwclock
 +
Tue Oct 18 15:34:53 2011  0.000000 seconds
 +
root@som9g20:~# hwclock --utc
 +
Tue Oct 18 10:34:58 2011  0.000000 seconds
 +
root@som9g20:~# hwclock --utc --hctosys
 +
root@som9g20:~# date
 +
Tue Oct 18 10:35:07 CDT 2011</syntaxhighlight>{{Fmbox | type =  | text =If the system is set to use UTC for the hardware clock setting, the ''utc'' option must be passed to ''hwclock'' if it is used to outside of the ''/etc/init.d/hwclock.sh'' script to synchronize the hardware and system clocks.}}
 +
 +
== Local Timezone ==
 +
 +
By default, EMAC OE systems are set to UTC timezone. Setting the timezone is done by creating a link from <code>/etc/localtime</code> to the desired timezone information file. The most common timezones are installed in the base image; contact EMAC for additional timezone definitions if required. Timezone information files are stored in the <code>/usr/share/zoneinfo</code> directory. For example, for Central Standard Time (CST), the file <code>/usr/share/zoneinfo/America/Chicago</code> or <code>/usr/share/zoneinfo/CST6CDT</code> would be used. This will also allow the system to automatically adjust for Daylight Savings Time.<br />
 +
 +
 +
 +
The example below illustrates the process of setting the timezone. Notice that it is necessary to log out and log back in for the new timezone setting to take effect. The command below that actually sets the timezone is <code>ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime</code>:
 +
 +
<syntaxhighlight lang="console">
 +
root@SOM9G20M:~# hwclock
 +
Thu Feb  4 22:54:48 2010  0.000000 seconds
 +
root@SOM9G20M:~# date
 +
Thu Feb  4 22:54:49 UTC 2010
 +
root@SOM9G20M:~# rm -rf /etc/localtime #only necessary if this file exists
 +
root@SOM9G20M:~# ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
 +
root@SOM9G20M:~# ls -l /etc/localtime
 +
lrwxrwxrwx    1 root    root          35 Feb  4 22:55 /etc/localtime -> /usr/share/zoneinfo/America/Chicago
 +
root@SOM9G20M:~# date
 +
Thu Feb  4 22:55:46 UTC 2010
 +
root@SOM9G20M:~# logout
 +
....
 +
SOM9G20M login: root
 +
Password:
 +
root@SOM9G20M:~# date
 +
Thu Feb  4 16:56:05 CST 2010
 +
root@SOM9G20M:~# hwclock
 +
Thu Feb  4 22:56:08 2010  0.000000 seconds
 +
root@SOM9G20M:~#
 +
</syntaxhighlight>

Revision as of 14:30, 4 October 2013

TODO: {{#todo:Port and polish and finish|Klint Youngmeyer|oe 4,oe 5,ky}}

There are two different clocks on a Linux system. The RTC, or hardware clock, is present on most systems and is used to provide a persistent clock using a battery backup and for other purposes like waking the processor up from suspend mode through an alarm interrupt. The hardware clock is generally only accessed to retrieve an initial time when Linux boots and is rarely accessed from a running system. The system clock is implemented in the kernel and is used for timekeeping purposes for the running system. On Linux, time is represented by the number of seconds after the Unix epoch: 00:00:00 UTC on January 1, 1970.

Setting the System Time on EMAC OE Products

During boot, they system will call the program ntpdate to set the time from an NTP server. This requires that the device has Internet access. To do this manually run the following command:

$ ntpdate pool.ntp.org

The example above uses the server pool.ntp.org which will retrieve a randomly chosen public NTP server. However, the choice of time server is arbitrary and can be set according to the users' needs. If the device does not have Internet access, then the system date can be set using the following command:

$ date -s "<date_format>"

The argument <date_format> can be any of the following:

"hh:mm[:ss]"
"[YYYY.]MM.DD-hh:mm[:ss]"
"YYYY-MM-DD hh:mm[:ss]"
"MMDDhhmm[[YY]YY][.ss]"

hh = hour
mm = minute
ss = second
YYYY = four-digit year
MM = month
DD = day
[YY]YY = four-digit year where the first two digits are optional



On shutdown the system time is saved to the hardware clock automatically with the following command:

/etc/init.d/hwclock.sh stop

To do this manually after setting the date run the following command:

/etc/init.d/hwclock.sh restart

Hardware Clock

The hardware clock can be accessed through the hwclock command. Running hwclock with no arguments prints the current value stored in the hardware clock. The --systohc option sets the value of the hardware clock using the system clock. This command is called by the system shutdown scripts using /etc/init.d/hwclock.sh. The --hctosys option can be used to set the system clock using the value stored in the hardware clock. This is used by /etc/init.d/hwclock.sh to initialize the system clock during the boot process. Note that the hardware clock is generally set to UTC regardless of the local timezone. On system shutdown, the current system time is saved to the RTC using the /etc/init.d/save-rtc.sh script.



The system can be configured to use either local time (see Local Timezone) or UTC for the hardware clock. Although the hardware clock only holds an absolute time value and no timezone value, adjustments can be made to account for the timezone when reading and setting the clocks. By default, EMAC OE uses the local time value for the hardware clock. If UTC is used for the system clock or NTP or another method is used to synchronize the clocks, this will not cause an issue. However, if the hardware clock is the primary method used to initialize the system clock, their will be no way to automatically adjust for Daylight Saving Time changes that occur while the system is off. In these cases, it is best to keep the hardware clock in UTC. This can be done easily by adding the line UTC=yes to /etc/default/rcS. Doing so will cause the /etc/init.d/hwclock.sh script to pass the utc option to the hwclock command when setting the hardware clock and when using it to set the system clock. The following example illustrates how the utc option allows the system to automatically account for the timezone when setting and reading the clocks:

root@som9g20:~# date
Tue Oct 18 10:34:44 CDT 2011
root@som9g20:~# hwclock --utc --systohc
root@som9g20:~# hwclock
Tue Oct 18 15:34:53 2011  0.000000 seconds
root@som9g20:~# hwclock --utc
Tue Oct 18 10:34:58 2011  0.000000 seconds
root@som9g20:~# hwclock --utc --hctosys
root@som9g20:~# date
Tue Oct 18 10:35:07 CDT 2011

Local Timezone

By default, EMAC OE systems are set to UTC timezone. Setting the timezone is done by creating a link from /etc/localtime to the desired timezone information file. The most common timezones are installed in the base image; contact EMAC for additional timezone definitions if required. Timezone information files are stored in the /usr/share/zoneinfo directory. For example, for Central Standard Time (CST), the file /usr/share/zoneinfo/America/Chicago or /usr/share/zoneinfo/CST6CDT would be used. This will also allow the system to automatically adjust for Daylight Savings Time.


The example below illustrates the process of setting the timezone. Notice that it is necessary to log out and log back in for the new timezone setting to take effect. The command below that actually sets the timezone is ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime:

root@SOM9G20M:~# hwclock
Thu Feb  4 22:54:48 2010  0.000000 seconds
root@SOM9G20M:~# date
Thu Feb  4 22:54:49 UTC 2010
root@SOM9G20M:~# rm -rf /etc/localtime #only necessary if this file exists
root@SOM9G20M:~# ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
root@SOM9G20M:~# ls -l /etc/localtime
lrwxrwxrwx    1 root     root           35 Feb  4 22:55 /etc/localtime -> /usr/share/zoneinfo/America/Chicago
root@SOM9G20M:~# date
Thu Feb  4 22:55:46 UTC 2010
root@SOM9G20M:~# logout
....
SOM9G20M login: root
Password:
root@SOM9G20M:~# date
Thu Feb  4 16:56:05 CST 2010
root@SOM9G20M:~# hwclock
Thu Feb  4 22:56:08 2010  0.000000 seconds
root@SOM9G20M:~#