對Linux系統中的時鐘和時間的探討 概要1)介紹Linux系統中時鐘的基本概念2)探討hwclock命令的工作方式。3)系統啟動過程中Linux系統對系統時鐘和硬體時鐘的同步。 www.2cto.com 主要術語和背景知識 UTC: Coordinated Universal Time, 一種是件標準,用以規範世界各地的時間。Time Zone: 時區,表示方式是:UTC-xx:xx, UTC+xx:xx。比如中國的時區表示是:UTC+08:00. 其他一些相關術語,比如CST,DST等,我們並不需要關心。 典型Linux對時鐘和時間的管理 一個典型的Linux系統主要有兩種時鐘:系統時鐘(System Clock)和硬體時鐘(Hardware Clock)。 www.2cto.com 硬體時鐘獨立運行於作業系統之外,有自己的電源供給,即使當系統關機時,它也依然在跑。Hardware Clock有時也叫BIOS Clock, CMOS Clock, RTC 等。但是只有hardware clock這個詞彙不容易引起誤解。 系統時鐘就是由作業系統維護的一個時鐘。在Linux系統中,是由kernel維護,由timer的中斷進行驅動的一個時鐘(因為它是由計時器的中斷驅動的,所以可以認為是一個軟體時鐘)。 有兩個時鐘,就需要有同步。這個同步功能由hwclock來實現。在此僅作簡要介紹,詳情請查詢手冊(man hwclock). hwclock在各個系統上的工作方式可能並不一樣,但是,在大多數情況下,它通過訪問/dev/rtc來進行硬體時鐘的控制。在先行的發行版上,/dev/rtc很多時候是一個symlink,指向/dev/rtcX。影響hwclock工作的還有兩個因素,一個是TZ環境變數,一個是/etc下的設定檔(這個檔案位置不一,也可能沒有,要看系統)。 一個典型的Linux系統,對時間的同步會自動發生在系統啟動和系統關閉的時候。系統啟動時,將Hardware Clock中的內容同步到System Clock;系統關閉時,將System Clock中的內容同步到Hardware Clock。當然,這種同步也可以自己手動進行,或者利用指令碼進行(自己編寫的,或者該版本系統中內建的)。 如下是一個典型的啟動和關閉系統時,對系統時鐘和硬體時鐘進行同步的指令碼,通常存在於/etc/init.d/目錄下。01#!/bin/sh02### BEGIN INIT INFO03# Provides: hwclock04# Required-Start: 05# Required-Stop: $local_fs06# Default-Start: S07# Default-Stop: 0 608# Short-Description: Set system clock09# Description: Set system clock to hardware clock, according to the UTC10# setting in /etc/default/rcS (see also rcS(5)).11### END INIT INFO12#13# WARNING: If your hardware clock is not in UTC/GMT, this script14# must know the local time zone. This information is15# stored in /etc/localtime. This might be a problem if16# your /etc/localtime is a symlink to something in17# /usr/share/zoneinfo AND /usr isn't in the root18# partition! The workaround is to define TZ either19# in /etc/default/rcS, or in the proper place below.20 21[ ! -x /sbin/hwclock ] && exit 022 23. /etc/default/rcS24[ "$UTC" = "yes" ] && tz="--utc" || tz="--localtime"25 26case "$1" in27 start)28 if [ "$VERBOSE" != no ]29 then30 echo "System time was `date`."31 echo "Setting the System Clock using the Hardware Clock as reference..."32 fi33 34 if [ "$HWCLOCKACCESS" != no ]35 then36 if [ -z "$TZ" ]37 then38 hwclock $tz --hctosys39 else40 TZ="$TZ" hwclock $tz --hctosys41 fi42 fi43 44 if [ "$VERBOSE" != no ]45 then46 echo "System Clock set. System local time is now `date`."47 fi48 ;;49 stop|restart|reload|force-reload)50 #51 # Updates the Hardware Clock with the System Clock time.52 # This will *override* any changes made to the Hardware Clock.53 #54 # WARNING: If you disable this, any changes to the system55 # clock will not be carried across reboots.56 #57 if [ "$VERBOSE" != no ]58 then59 echo "Saving the System Clock time to the Hardware Clock..."60 fi61 if [ "$HWCLOCKACCESS" != no ]62 then63 hwclock $tz --systohc64 fi65 if [ "$VERBOSE" != no ]66 then67 echo "Hardware Clock updated to `date`."68 fi69 exit 070 ;;71 show)72 if [ "$HWCLOCKACCESS" != no ]73 then74 hwclock $tz --show75 fi76 ;;77 *)78 echo "Usage: hwclock.sh {start|stop|show|reload|restart}" >&279 echo " start sets kernel (system) clock from hardware (RTC) clock" >&280 echo " stop and reload set hardware (RTC) clock from kernel (system) clock" >&281 exit 182 ;;83esac hwclock 和 date 命令中--utc選項 這兩個命令雖然都有--utc選項,但是其含義是不同的。首先來看一段命令和結果。 1chenqi@chenqi-OptiPlex-760:/etc/default$ date2Wed Oct 31 16:00:23 CST 20123chenqi@chenqi-OptiPlex-760:/etc/default$ date --utc4Wed Oct 31 08:00:24 UTC 20125chenqi@chenqi-OptiPlex-760:/etc/default$ sudo hwclock --localtime -r6Wed 31 Oct 2012 08:00:30 AM CST -0.594038 seconds7chenqi@chenqi-OptiPlex-760:/etc/default$ sudo hwclock --utc -r8Wed 31 Oct 2012 04:00:37 PM CST -0.937780 seconds由上可見date --utc得到的時間和hwclock --localtime得到的時間一致,而date得到的時間和hwclock --utc得到的時間一致。這一點,容易讓人困擾。實際上,date命令中--utc選項表示“對標準UTC時間進行操作”,所以date --utc是輸出當前的標準UTC時間;而hwclock中--utc選項的意思是,這次操作要考慮時區上的換算,比如,如果是讀取hardware clock的話,那麼讀出的資料(從本地讀出的時間),要考慮到時區,換算出當前時區的時間後,再做輸出。同理,--localtime表示不用進行時區上的考慮和換算,讀出什麼,就輸出什麼。 由此,上面的困擾就比較容易解釋了,hardware clock儲存的時UTC標準時間,所以用--localtime去讀取,得到的就是標準UTC時間,即和date --utc一致。另外一種情況就是考慮了時區換算,道理一樣。 參考材料 hwclock manualdate manualhttp://en.wikipedia.org/wiki/Time_zone