RTC hwclock 和 date 系統時間 - 通達人驛站

RTC hwclock 和 date 系統時間

最近客戶遇到有關 hwclock 的問題,發現設備因為斷電時間過長,進入 Linux 系統後,Linux 的系統時間又回到 1970年1月1日了。

因為對 hwclock 不了解,所以就趁這個機會熟悉一下 Linux 內和 clock 有關的命令,包括:hwclock、date。

安裝 Linux 的嵌入式系統

嵌入式設備內通常有 2 個時鐘:實時時鐘(RTC, Real Time Clock)和系統時間(system time)。

至於系統時間(system time),則由 Linux Kernel 維護,是以UTC1970年1月1日0時0分0秒起至現在的總秒數來表示,據說這個時間就是傳說中代表 Unix 時代開啟的時刻。

在啟動 Linux 時,它靠讀取 RTC 獲得計時起點,之後系統時間即獨立運作;而在關閉時,也會記錄時間並寫入檔案,以供下次開機時使用。

hwclock 修改實時時鐘 RTC

hwclock 命令常見的功能:
  • hwclock -r or hwclock --show 顯示目前RTC;
  • hwclock -w or hwclock --systohc 把系統時間寫入RTC時間;
  • hwclock -s or hwclock --hctosys 以 RTC 時間重設系統時間;
  • hwclock -a or hwclock --adjust 根據先前的記錄來估算硬體時鐘的偏差校正RTC,RTC的校正值設定在/etc/adjtime文件中;
  • hwclock -v or hwclock --version 顯示hwclock的版本信息;
  • hwclock --set --date=newdate 設定硬體時鐘。
root@BSP-A:~# hwclock --help
hwclock - query and set the hardware clock (RTC)

Usage: hwclock [function] [options...]

--help show this help
--show read hardware clock and print result
--set set the rtc to the time given with --date
--hctosys set the system time from the hardware clock
--systohc set the hardware clock to the current system time
--adjust adjust the rtc to account for systematic drift since
the clock was last set or adjusted
--getepoch print out the kernel's hardware clock epoch value
--setepoch set the kernel's hardware clock epoch value to the
value given with --epoch
--version print out the version of hwclock to stdout

--utc the hardware clock is kept in coordinated universal time
--localtime the hardware clock is kept in local time
--directisa access the ISA bus directly instead of /dev/rtc
--badyear ignore rtc's year because the bios is broken
--date specifies the time to which to set the hardware clock
--epoch=year specifies the year which is the beginning of the
hardware clock's epoch value
--noadjfile do not access /etc/adjtime. Requires the use of
either --utc or --localtime
root@BSP-A:~# hwclock -v
hwclock from util-linux-2.13-pre7
root@BSP-A:~# hwclock -r
Wed Aug 3 11:48:39 2011 -0.716106 seconds
root@BSP-A:~# hwclock -w

date 修改系統時間 system time

date 命令常見的功能:
  • date 顯示目前 system time
  • date -r 顯示目前 system time
  • date MMDDhhmmYYYY 第一種設定 system time 的方法
  • date YYYY.MM.DD-hh:mm:ss 第二種設定 system time 的方法
root@BSP-A:~# date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

-d, --date=STRING display time described by STRING, not `now'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-r, --reference=FILE display the last modification time of FILE
-R, --rfc-2822 output date and time in RFC 2822 format.
Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC output date and time in RFC 3339 format.
TIMESPEC=`date', `seconds', or `ns' for
date and time to the indicated precision.
Date and time components are separated by
a single space: 2006-08-07 12:34:56-06:00
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time
--help display this help and exit
--version output version information and exit

FORMAT controls the output. The only valid option for the second form
specifies Coordinated Universal Time. Interpreted sequences are:

%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 21)
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour ( 0..23)
%l hour ( 1..12)
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric timezone (e.g., -0400)
%:z +hh:mm numeric timezone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':

- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible

After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.

Report bugs to .
root@BSP-A:~# date -R
Wed, 03 Aug 2011 09:27:49 +0000
root@BSP-A:~# date 013122302011
root@BSP-A:~# date
Mon Jan 31 22:30:05 UTC 2011
root@BSP-A:~# date 2011.08.03 09:28:00
root@BSP-A:~# date
Wed Aug 03 09:28:05 UTC 2011

用 date 和 hwclock 測試 RTC 是否正常

以下步驟,可以測試 RTC 是否工作正常:
root@BSP-A:~# date #顯示目前 Linux 系統時間
Wed Aug 03 10:01:05 UTC 2011
root@BSP-A:~# date 2011.10.03 10:00:00
root@BSP-A:~# hwclock #顯示目前 RTC 時間
Wed Aug 3 10:01:39 2011 -0.626106 seconds
root@BSP-A:~# hwclock -w #將 Linux 系統時間寫到 RTC 中
root@BSP-A:~# hwclock #再次顯示目前 RTC 時間
Wed Aug 3 10:02:20 2011 -0.262006 seconds
root@BSP-A:~# hwclock #再次顯示目前 RTC 時間,應該比上一個步驟增加了幾秒
Wed Aug 3 10:02:26 2011 -0.153264 seconds

如果 RTC Driver 工作正常的話,最後 hwclock 顯示的時間應該和 date 一致。如果不同,就表示 RTC Driver 工作不正常,沒有正確地把系統時間寫進入 RTC 中。


0 意見 :


Related Posts with Thumbnails