hwclock - query and set the hardware clock (RTC) Usage: hwclock [function] [options...] Functions: -h | --help show this help -r | --show read hardware clock and print result --set set the rtc to the time given with --date -s | --hctosys set the system time from the hardware clock -w | --systohc set the hardware clock to the current system time --systz set the system time based on the current timezone --adjust adjust the rtc to account for systematic drift since the clock was last set or adjusted -c | --compare periodically compare the system clock with the CMOS clock --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 -v | --version print out the version of hwclock to stdout Options: -u | --utc the hardware clock is kept in UTC --localtime the hardware clock is kept in local time -f | --rtc=path special /dev/... file to use instead of default --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 --adjfile=path specifies the path to the adjust file (default is /etc/adjtime) --test do everything except actually updating the hardware clock or anything else -D | --debug debug mode
hwclock - query and set the hardware clock (RTC) Usage: hwclock [function] [options...] Functions: -h | --help show this help -r | --show read hardware clock and print result --set set the rtc to the time given with --date -s | --hctosys set the system time from the hardware clock -w | --systohc set the hardware clock to the current system time --systz set the system time based on the current timezone --adjust adjust the rtc to account for systematic drift since the clock was last set or adjusted -c | --compare periodically compare the system clock with the CMOS clock --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 -v | --version print out the version of hwclock to stdout Options: -u | --utc the hardware clock is kept in UTC --localtime the hardware clock is kept in local time -f | --rtc=path special /dev/... file to use instead of default --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 --adjfile=path specifies the path to the adjust file (default is /etc/adjtime) --test do everything except actually updating the hardware clock or anything else -D | --debug debug mode
HWCLOCK(8) HWCLOCK(8) 名前 hwclock - ハードウェア・クロック (RTC) の読み取りと設定を行う 書式 hwclock -r or hwclock --show hwclock -w or hwclock --systohc hwclock -s or hwclock --hctosys hwclock -a or hwclock --adjust hwclock -v or hwclock --version hwclock --set --date=newdate hwclock --getepoch hwclock --setepoch --epoch=year その他のオプション: [-u|--utc] --localtime --noadjfile --directisa --test [-D|--debug] DEC Alpha 用の知る人ぞ知るオプション: [-A|--arc] [-J|--jensen] [-S|--srm] [-F|--funky-toy] すべてのオプションは他と区別がつく範囲において短縮することができる。 また -h はヘルプメッセージを表示する。 説明 hwclock はハードウェア・クロックにアクセスするためのツールである。現在 の時刻の表示、指定した時刻へのハードウェア・クロックの設定、ハードウ ェ ア・クロックをシステム時刻に合わせる (およびその逆)、といった機能を持つ 。 hwclock を定期的に実行し、ハードウェア・クロックの時間を増減して、時 計 の 規 則 的なずれ (systematic drift) を補償することもできる (systematic drift とは、クロックが放っておかれたとき、経過時間に比例して時刻がず れ る現象のこと)。 オプション 以下のオプションは hwclock にどの機能を実行するかを伝えるもので、必ず一 つだけを指定する。 --show ハードウェア・クロックを読んで時刻を標準出力に表示する。ここで表 示される時刻は常にローカル・タイムである。ハードウェア・クロック を協定世界時にしていても表示はローカル・タイムである。 --utc オ プションの部分を参照すること。 --set ハードウェア・クロックを --date オプションによって指定した時刻に 設定する。 --hctosys システム・クロックをハードウェア・クロックに合わせる。 同時にカーネルが持つタイムゾーンの値もローカルのタイムゾーンにセ ッ ト す る。このとき TZ 環境変数や /usr/share/zoneinfo の内容を tzset(3) と同じように解釈して参照する。カーネルのタイムゾーン の obsolete なフィールドである tz_dsttime は DST_NONE に設定される 。 (このフィールドがかつて意味していた内容に関しては settimeof- day(2) を参照のこと。) このオプションはシステムの起動スクリプトの一部で用いるとよい。 --systohc ハードウェア・クロックを現在のシステム・クロックに合わせる。 --adjust 最後にハードウェア・クロックを合わせた時点からの経過時間に対して 生じる、時計の規則的なずれを補償するために、一定の時間をハードウ ェア・クロックの時刻から増減する。詳細は以下の議論を参照のこと。 --getepoch 標準出力に、カーネルが保持しているハードウェア・クロックの紀元年 (epoch value) を表示する。これは西暦の何年が、ハードウェア・クロ ックの 0 年として参照されるかを示す数値である。例えば、ハード ウ ェアクロックの年カウンタに 1952 年以降の経過年数を用いている場合 には、カーネルでのハードウェア・クロック紀元年は 1952 でなければ ならない。 こ の紀元年の値は、 hwclock がハードウェア・クロックを読み書きす るとき常に用いられる。 --setepoch カーネルのハードウェア・クロック紀元年の値を --epoch オプショ ン で指定した値に設定する。詳細は --getepoch オプションの説明を見よ 。 --version hwclock のバージョンを標準出力に表示する。 --date=date_string --set オプションを指定した場合は、このオプションも指定しなければ な らない。 --set オプションが指定されていなければ、このオプショ ンは無視される。ハードウェア・クロックを合わせる時刻を指定する。 こ のオプションに与える値は date(1) プログラムの引数と同じである 。例えば以下のようにする。 hwclock --set --date="9/22/96 16:45:05" 引数はローカルタイムで与える。ハードウェア・クロックを協定世界時 にしている場合でも、である。 --utc オプションの部分を見よ。 --setepoch オプションを指定した場合は次のオプションも必要である 。 --epoch=year ハードウェア・クロックの紀元年を指定する。すなわち西暦年のいつが 、 ハードウェア・クロックの年カウンタの 0 に対応するかを指定する 。このオプションは、--setepoch オプションとともに使った場合、 カ ー ネ ル の 概念であるハードウェア・クロックの紀元年を設定する。 --setepoch オプションとともに使わない場合は、直接 ISA アクセスに 用いられる紀元年を指定する。 例えば、Digital Unix マシンでは以下のようにする。 hwclock --setepoch --epoch=1952 次のオプションはほとんどの機能と同時に用いることができる。 --utc --localtime ハ ードウェア・クロックを協定世界時 (Universal Coordinated Time: UTC) とローカルタイムのどちらにするか (しているか) を指定する 。 UTC にするかローカルタイムにするかはユーザの選択しだいだが、時計 の内部にはどちらを選択したかを記録する場所はない。したがって、ユ ー ザーはこのオプションで自分の選択を hwclock に伝えなければなら ない。 これらの指定を間違ったほうにしたり (あるいはデフォルトを勘違いし て両方とも指定しなかったり) すると、ハードウェア・クロックの設定 やクロックへの問い合わせの結果はめちゃめちゃになってしまうだろう 。 --utc も --localtime も指定しなかった場合のデフォルトは、最後に hwclock を使って時計を合わせたとき (つ ま り --set, --systohc, --adjust オプションを指定しての実行が成功したとき) に指定してい た方になる。このときの選択は adjtime ファイルに記録されている 。 adjtime ファイルがなかったときのデフォルトはローカルタイムになる 。 --noadjfile /etc/adjtime によって提供される機能を無効にする。このオプショ ン を 使うと、 hwclock は /etc/adjtime の読み込みも書き込みもしない 。このオプションを使うときは、 --utc または --localtime を指定し なければならない。 --directisa こ のオプションは、ISA マシンまたは (hwclock から充分 ISA マシン に見える程度 ISA の仕様を実装した) Alpha マシンでのみ意味を持 つ 。 他のマシンでは効果がない。このオプションは hwclock に指令して 、ハードウェア・クロックへのアクセスに直接 I/O 命令を用いるよ う に させる。このオプションを指定しないと、 hwclock は /dev/rtc デ バイスを用いようとする (/dev/rtc が rtc デバイスドライバで駆動さ れ ていることを仮定する)。デバイスを読み込みオープンできない場合 は、いずれにせよ直接 I/O 命令を用いる。 rtc デバイスドライバは Linux リリース 2 から現れた。 --badyear ハードウェア・クロックが、1994-1999 年の外側の年を保持できないこ と を示す。ある種の BIOS には問題があり (4/26/94 から 5/31/95 の 間に生産されたほとんどの Award BIOS がそうである)、 1999 年以 降 の年を扱うことができないのである。世紀内の年の部分を 94 未満 (場 合によっては 95 未満) に設定しようとすると、実際には 94 (また は 95) が 設定されてしまう。このようなマシンでは、 hwclock は年を 1999 以降に設定できず、またクロックの値を通常のように正しい値 と しては用いることができない。 本当は BIOS を更新するのが絶対に良いが、そうできない場合にこの問 題を補償するには、これらのマシンを用いるとき、常に --badyear オ プ ションを指定すること。 hwclock は、自分が頭のイカれたクロック を扱っていることを知ると、ハードウェア・クロックの年の部分を無視 し 、 adjtime ファイルの「最終時計合わせ日付」から現在の年を推定 しようとする。この動作を行わせたい場合には、 hwclock --set ま た は hwclock --systohc を少なくとも年に一回は実行するほうが良いだ ろう! hwclock は、ハードウェア・クロックの読み込み時には年の値を無視す るが、設定時には年も設定する。これは 1995, 1996, 1997, 1998 のい ずれかとなり、閏年のサイクルに合う年が選択される。このようにして 、ハードウェア・クロックに閏日を挿入させるのである。繰り返すが、 ハードウェア・クロックを設定せずに一年以上動作させつづけると、こ の機能が動作せず、一日を失うことになる。 ハ ー ド ウ ェ ア ・クロックが 1994 または 1995 になっていると、 hwclock は --badyear が必要ではないか、という警告を発する。 --srm このオプションは --epoch=1900 と等しく、 SRM コンソールの Alpha で最も一般的な紀元年を指定するのに使われる。 --arc このオプションは --epoch=1980 と等しく、 ARC コンソールの Alpha で最も一般的な紀元年を指定するのに使われる (ただし Ruffians では 1900 を紀元年にしている)。 --jensen --funky-toy これら 2 つのオプションは、使っている Alpha マシンがどのような種 類のものであるか指定する。 Alpha 以外では無効だし、Alpha でも 実 際 には指定しなくても良いだろう。 hwclock は自分が動作しているマ シンの種類を自分で決定できるはずである (最低でも /proc がマウ ン ト されていれば)。 (hwclock が正しく動作しないことがわかった場合 には、メンテナに連絡して、あなたのシステムを自動検知できるように プログラムを改良できないか相談してみてほしい。 ‘hwclock --debug’ と ‘cat /proc/cpuinfo’ の出力が役立つかもしれない。) --jensen は、Jensen モデルを動作させていることを意味する。 --funky-toy は、そのマシンでは時間の遷移の検知にハードウェア・ク ロ ックの UIP ビットではなく UF ビットが使われていることを意味す る。オプション名の "Toy" は、マシンの "Time Of Year" 機能から と ったものである。 --test 実際のハードウェア・クロックの更新 (およびそれに類する) 作業をの ぞき、すべての動作を行う。このオプションは --debug と組み合わ せ ると hwclock の動作を理解する上で有用であろう。 --debug hwclock が内部で行っている動作に関して大量の情報を表示する。一部 の機能は複雑であるが、この出力はプログラムの動作を理解する上で助 けになるだろう。 注意 Linux システムにおける時計 Linux システムには主要な時計が 2 つ存在する。 ハードウェア・クロック: これは CPU 内部で動作しているすべてのコントロー ルプログラムから独立しており、マシンの電源が OFF のときにも動作している 。 ISA システムでは、このクロックは ISA 規格の一部として定義されている。コ ントロールプログラムはこの時計に対して 1 秒単位で読み書きできるが、秒針 の 変化を検出することもできるので、実際には仮想的に無限大の精度を持って いることになる。 この時計は一般にハードウェア・クロック、リアルタイム・クロック 、RTC 、 BIOS クロック、CMOS クロックなどと呼ばれる。 hwclock では「ハードウェア ・クロック (原文では Hardware Clock)」を用いる。他の名前は不正確だっ た り誤解のもとになるからである。 システム・クロック: これは Linux カーネルの内部に存在している時計で、タ イマ割り込みによって駆動されている (ISA システムでは、タイマ割り込み は ISA 標準の一部である)。すなわち Linux が起動している間しか動作しない。 システム時刻は UTC 1970/01/01 00:00:00 からの経過秒数である (より簡単に 言えば 1969 年終了後の経過秒数である)。しかしシステム時刻は整数ではなく 、仮想的に無限大の精度を持っている。 Linux ではシステム・クロックがすべての基準となる時計である。ハードウ ェ ア ・クロックの基本的な役割は、システムが動いていない間にも時計を動かし つづけることである。 Linux システムは起動時に一度だけハードウェア・クロ ッ クを参照し、システム・クロックを設定する。その後はハードウェア・クロ ックは用いない。 ISA システムの設計対象であった DOS においては、ハー ド ウェア・クロックがただ一つの実時間時計であることに注意すること。 シ ステム・クロックには不連続が存在してはならない。これはシステムが走っ ている間に date(1L) プログラムを実行して時計を合わせるような場合でも 同 様 である。一方ハードウェア・クロックには、システムの実行中にでも何を行 ってもよい。次回 Linux が起動したときに、ハードウェア・クロックからこの 調 整された時間が使用される。システムが走っている間にシステム・クロック をスムースに修正するには adjtimex(8) を用いることもできる。 Linux カーネルは、システムのローカルなタイムゾーンという概念を持って い る 。しかし注意してほしい -- 「カーネルが自分をどのタイムゾーンにいると 思っているか」など、誰も気にしていないのである。代わりに、タイムゾー ン に 関るプログラム (おそらくローカルな時間を表示しようとしているもの) は 、ほぼ間違いなく従来用いられてきた方法でタイムゾーンを決定する。つま り TZ 環境変数や /usr/share/zoneinfo ディレクトリを、 tzset(3) で説明され ているようなやり方で参照するのである。しかしカーネルのタイムゾーンの 値 を見るプログラムも存在するし、カーネルの周辺部分 (ファイルシステムなど) もこちらを参照する。 vfat ファイルシステムなどがそうである。カーネル の タイムゾーンの値が間違っていると、vfat ファイルシステムはファイルのタイ ムスタンプの設定・取得を間違ってしまう。 hwclock は --hctosys オプションでシステム・クロックをセットするとき、カ ーネルのタイムゾーンも TZ や /usr/share/zoneinfo の値に設定する。 タ イムゾーンの値は実際には 2 つの部分からなる。 1) tz_minuteswest フィ ールド: (DST でない) ローカルタイムが UTC から何分遅れているかを表す 。 2) tz_dsttime: 夏時間 (DST) の形式を表し、現在地の現在時刻に影響する。 この 2 番目のフィールドは Linux では用いられず、常に 0 となる 。 (set- timeofday(2) も参照のこと。) hwclock がハードウェア・クロックへアクセスする方法 hwclock はハードウェア・クロック時刻の取得や設定に、いろいろな方法を用 いる。もっとも普通のやり方は、デバイススペシャルファイル /dev/rtc に 対 して I/O を行う方法である。しかしこの方法が常に利用できるとは限らない。 そもそも rtc ドライバが Linux へ追加されたのは比較的最近のことである 。 古 いシステムには存在しない。 DEC Alpha で動作する rtc ドライバもあるが 、このドライバが使えない Alpha マシンもたくさんあるようである (症状とし ては hwclock がハングする)。 古 いシステムでは、ハードウェア・クロックへのアクセス方法はシステムのハ ードウェアに依存している。 ISA システムでは、 hwclock は時計を構成していた「CMOS メモリ」のレジ ス タ に直接アクセスすることができた (ポート 0x70 と 0x71 に I/O を行う)。 これを行うには hwclock の実効ユーザー ID がスーパーユーザーでなければな らない。 (Jensen Alpha の場合は、このような I/O 命令を hwclock に実行さ せることはできない。したがってこの場合はデバイススペシャ ル フ ァ イ ル /dev/port が用いられる。これは I/O サブシステムへの低レベルインターフェ ースのほとんどを与えるものである。) これは時計にアクセスする方法としては実に情けない方法である。ユーザー 空 間のプログラムでは、このように直接 I/O を叩いたり、割り込みを禁止したり することは通常想定されていないのだから。 hwclock でこれが使えるようにし て あるのは、古い Linux カーネルで ISA マシンを使う場合には、これが唯一 の方法だからである。 m68k システムでは、 hwclock はコンソールドライバとデバイススペシャル フ ァイル /dev/tty1 を通して時計にアクセスすることができる。 hwclock は /dev/rtc を用いようとする。この機能を持たないカーネル向けに コンパイルされていたり、/dev/rtc をオープンできない場合には 、 hwclock は 他の方法を (可能であれば) 試そうとする。 ISA や Alpha のマシンでは、 /dev/rtc を試さずに、最初から hwclock に CMOS レジスタを直接操作する よ うに強制することもできる。これには --directisa オプションを指定する。 時刻合わせ機能 通 常ハードウェア・クロックはそれほど正確なものではない。しかし、その「 不正確さ」は完全に予測できるものである。すなわち、時計は一日あたり同 じ 時 間だけ進む(あるいは遅れる)のである。これを規則的なずれ (systematic drift) と呼ぶことにする。 hwclock の時刻合わせの機能は、この規則的な ず れに対応する補正量を求め、適用するものである。 以下に動作原理を述べる。 hwclock は /etc/adjtime というファイルを管理し 、そこに履歴情報を保管する。このファイルを adjtime ファイルと呼ぶ。 adjtime ファイルがない状態から話をはじめる。 hwclock --set コマンドを用 い て ハ ードウェア・クロックを現在の正しい値に合わせたとする。このとき hwclock は adjtime ファイルを作成し、そこに現在の時刻を「最後に時計合わ せ (calibration) が行われた時刻」として記録する。五日後に時計は 10 秒進 んだとし、それを修正するために再び hwclock --set が実行されたとす る 。 hwclock は adjtime ファイルを更新し、現在の時刻を最後に時計合わせが行わ れた時刻として記録、同時に 2 秒/日という値を規則的なずれの値として記 録 す る。 24 時間が経過したときに hwclock --adjust コマンドを実行すると、 hwclock は adjtime ファイルを参照し、放っておかれた時計は一日に 2 秒 進 む こ と 、 時 計はちょうど一日だけ放置されていたことを読みとる。そこで hwclock はハードウェア・クロックから 2 秒を差し引き、現在の時刻を時計の 補 正 (adjustment) が行われた時刻として記録する。さらに 24 時間が経過し たときに hwclock --adjust を実行すれば、 hwclock はまた同じことを行う。 つまり 2 秒を差し引き、現在の時刻を adjtime ファイルに書き込む。 (--set または --systohc を用いて) 時計を合わせるごとに、 hwclock は規則 的なずれを再計算する。このときには、最後に時計合せが行われた時点から の 経 過、途中で行われた補正で用いられていたずれの量、最後に補正を行った時 刻からの経過時間などが参照される。 hwclock が時計を設定するときには、常に小さなずれが生じる可能性がある 。 これが 1 秒に満たない場合には、時計の補正量からは切り捨てられる。後に再 び補正を行う際に、このずれが蓄積して 1 秒を越えていれば、その分はその時 に補正される。 システムの起動時に (あるいはシステムの動作中に cron で定期的に) hwclock --hctosys を行う時には、常にその前に hwclock --adjust を行うと良いだ ろ う。 adjtime ファイルは、当初は修正量 (adjustments) だけを目的としていたため にこの名前がつけられたが、現在では他の情報も書き込まれており、 hwclock が一度起動され、次に起動されるまでにその情報を保持する。 adjtime は ASCII ファイルであり、フォーマットは以下の通り: 一行目は三つの数値からなり、それぞれ空白で区切られる: 1) 一日あたりに生 じる時刻ずれを秒で表したもの (浮動小数点型 10 進): 2) 最後に時計合わ せ あるいは補正を行った時刻を 1969 UTC からの経過秒数で表したもの (10 進整 数): 3) ゼロ (clock(8) との互換性のためのもの) 二行目: 数値が一つ: 最後に時計を合わせた時刻を 1969 UTC からの経過秒 数 で 表したもの。時計合わせが一度もされていなかったり、以前の時計あわせに 問題があった (例えばその時計あわせ以降にハードウェア・クロックの時刻 が 不正なことがわかったとか) 場合には 0 が入る。これは 10 進の整数である。 三行目: "UTC" または "LOCAL"。ハードウェア・クロックが協定世界時かロ ー カルタイム化を示す。この値は hwclock にコマンドラインを指定すればいつで も上書き可能である。 以前 clock(8) で使っていた adjtime ファイルは hwclock でもそのまま使 う ことができる。 カーネルによるハードウェアクロックの自動合わせ ハ ードウェアクロックを正しい値に同期させるのに、別法が取れるようなシス テムもある。 Linux カーネルには、11 分ごとにシステムクロックをハード ウ ェ アクロックにコピーするようなモードが存在する。これは、何らかの洗練さ れた方法 (ntp など) でシステムクロックを同期できている時には、よいモ ー ド であろう。 (ntp とは、ネットワークのどこかにあるタイムサーバーか、シ ステムに付属した電波時計にシステム・クロックを同期させる手法である。RFC 1305 を見よ。) このモード (「11 分モード」と呼ぶ) は、何かによって有効にされるまではオ フになっている。例えば ntp デーモンである xntpd はこのモードを有効に で き る も の の ひとつである。オフにするのも何かを実行すればよく、例えば hwclock --hctosys を実行して、システム・クロックを古い方法で設定すれ ば 、11 分モードはオフになる。 モ ードがオンかオフかを調べるには、 adjtimex --print コマンドを実行して "status" の値を見ればよい。この数値の第 64 ビットが (2 進数表示で) 0 な らば、 11 分モードはオンになっている。それ以外の場合はオフである。 シ ステムが 11 分モードで動作している場合に hwclock --adjust や hwclock --hctosys を実行してはならない。システムをおかしくしてしまう。 hwclock --hctosys を起動時だけに用いるならかまわない。これを用いれば、システム ・クロックが外部の値に同期して 11 分モードが開始されるまで、システム ク ロックを妥当な値にできる。 ISA ハードウェア・クロックの「世紀値 (Century value)」 その手の標準の中には、ISA マシンの CMOS 50 バイト目を、現在の世紀の指標 として定義しているものがある。 hwclock は、このバイトの読み書きを行わな い 。なぜならこのバイトをそのようには利用していないマシンが存在するし、 いずれにしてもこれは実際には必要ないからである。年の世紀の部分を使え ば 、現在の世紀を特定するには充分である。 も しこの CMOS の世紀バイトの利用ルーチンを開発した (したい) 方がいたら 、 hwclock のメンテナに連絡してほしい。オプションを付加することは望まし いことであるから。 こ の セクションが意味を持つのは、ハードウェア・クロックに "direct ISA" によってアクセスしている場合だけであることに注意。 環境変数 TZ ファイル /etc/adjtime /usr/share/zoneinfo/ (古いシステムでは /usr/lib/zoneinfo) /dev/rtc /dev/port /dev/tty1 /proc/cpuinfo 関連項目 adjtimex(8), date(1), gettimeofday(2), settimeofday(2), crontab(1), tzset(3) 著者 hwclock は 1996 年 9 月に Bryan Henderson (bryanh@giraffe-data.com) が clock を も とに書いた。 clock は Charles Hendrick, Rob Hooft, Haraid Koenig によって書かれた。完全な履歴と謝辞はソースに書かれている。 02 March 1998 HWCLOCK(8)
HWCLOCK(8) HWCLOCK(8) NAME hwclock - query and set the hardware clock (RTC) SYNOPSIS hwclock [functions] [options] DESCRIPTION hwclock is a tool for accessing the Hardware Clock. You can display the current time, set the Hardware Clock to a specified time, set the Hardware Clock to the System Time, and set the System Time from the Hardware Clock. You can also run hwclock periodically to insert or remove time from the Hardware Clock to compensate for systematic drift (where the clock con- sistently gains or loses time at a certain rate if left to run). FUNCTIONS You need exactly one of the following options to tell hwclock what function to perform: -r, --show Read the Hardware Clock and print the time on Standard Output. The time shown is always in local time, even if you keep your Hardware Clock in Coordinated Universal Time. See the --utc option. -c, --compare Periodically compare the Hardware Clock to the System Time and output the difference every 10 seconds. This will also print the frequency offset and tick. --set Set the Hardware Clock to the time given by the --date option. -s, --hctosys Set the System Time from the Hardware Clock. Also set the kernel’s timezone value to the local timezone as indicated by the TZ environment variable and/or /usr/share/zone- info, as tzset(3) would interpret them. The obsolete tz_dsttime field of the kernel’s timezone value is set to DST_NONE. (For details on what this field used to mean, see settimeofday(2).) This is a good option to use in one of the system startup scripts. -w, --systohc Set the Hardware Clock to the current System Time. --systz Set the kernel’s timezone and reset the System Time based on the current timezone. The system time is only reset on the first call after boot. The local timezone is taken to be what is indicated by the TZ environment variable and/or /usr/share/zoneinfo, as tzset(3) would interpret them. The obsolete tz_dsttime field of the kernel’s timezone value is set to DST_NONE. (For details on what this field used to mean, see settimeofday(2).) This is an alternate option to --hctosys that does not read the hardware clock, and may be used in system startup scripts for recent 2.6 kernels where you know the System Time contains the Hardware Clock time. If the Hardware Clock is already in UTC, it is not reset. --adjust Add or subtract time from the Hardware Clock to account for sys- tematic drift since the last time the clock was set or adjusted. See discussion below. --getepoch Print the kernel’s Hardware Clock epoch value to standard out- put. This is the number of years into AD to which a zero year value in the Hardware Clock refers. For example, if you are using the convention that the year counter in your Hardware Clock contains the number of full years since 1952, then the kernel’s Hardware Counter epoch value must be 1952. This epoch value is used whenever hwclock reads or sets the Hardware Clock. --setepoch Set the kernel’s Hardware Clock epoch value to the value speci- fied by the --epoch option. See the --getepoch option for details. -v, --version Print the version of hwclock on Standard Output. --date=date_string You need this option if you specify the --set option. Other- wise, it is ignored. This specifies the time to which to set the Hardware Clock. The value of this option is an argument to the date(1) program. For example, hwclock --set --date="9/22/96 16:45:05" The argument is in local time, even if you keep your Hardware Clock in Coordinated Universal time. See the --utc option. --epoch=year Specifies the year which is the beginning of the Hardware Clock’s epoch. I.e. the number of years into AD to which a zero value in the Hardware Clock’s year counter refers. It is used together with the --setepoch option to set the kernel’s idea of the epoch of the Hardware Clock, or otherwise to specify the epoch for use with direct ISA access. For example, on a Digital Unix machine: hwclock --setepoch --epoch=1952 OPTIONS The following options apply to most functions. -u, --utc --localtime Indicates that the Hardware Clock is kept in Coordinated Univer- sal Time or local time, respectively. It is your choice whether to keep your clock in UTC or local time, but nothing in the clock tells which you’ve chosen. So this option is how you give that information to hwclock. If you specify the wrong one of these options (or specify nei- ther and take a wrong default), both setting and querying of the Hardware Clock will be messed up. If you specify neither --utc nor --localtime , the default is whichever was specified the last time hwclock was used to set the clock (i.e. hwclock was successfully run with the --set, --systohc, or --adjust options), as recorded in the adjtime file. If the adjtime file doesn’t exist, the default is local time. --noadjfile disables the facilities provided by /etc/adjtime. hwclock will not read nor write to that file with this option. Either --utc or --localtime must be specified when using this option. --adjfile=filename overrides the default /etc/adjtime. -f, --rtc=filename overrides the default /dev file name, which is /dev/rtc on many platforms but may be /dev/rtc0, /dev/rtc1, and so on. --directisa is meaningful only on an ISA machine or an Alpha (which imple- ments enough of ISA to be, roughly speaking, an ISA machine for hwclock’s purposes). For other machines, it has no effect. This option tells hwclock to use explicit I/O instructions to access the Hardware Clock. Without this option, hwclock will try to use the /dev/rtc device (which it assumes to be driven by the rtc device driver). If it is unable to open the device (for read), it will use the explicit I/O instructions anyway. The rtc device driver was new in Linux Release 2. --badyear Indicates that the Hardware Clock is incapable of storing years outside the range 1994-1999. There is a problem in some BIOSes (almost all Award BIOSes made between 4/26/94 and 5/31/95) wherein they are unable to deal with years after 1999. If one attempts to set the year-of-century value to something less than 94 (or 95 in some cases), the value that actually gets set is 94 (or 95). Thus, if you have one of these machines, hwclock can- not set the year after 1999 and cannot use the value of the clock as the true time in the normal way. To compensate for this (without your getting a BIOS update, which would definitely be preferable), always use --badyear if you have one of these machines. When hwclock knows it’s working with a brain-damaged clock, it ignores the year part of the Hardware Clock value and instead tries to guess the year based on the last calibrated date in the adjtime file, by assuming that that date is within the past year. For this to work, you had better do a hwclock --set or hwclock --systohc at least once a year! Though hwclock ignores the year value when it reads the Hardware Clock, it sets the year value when it sets the clock. It sets it to 1995, 1996, 1997, or 1998, whichever one has the same position in the leap year cycle as the true year. That way, the Hardware Clock inserts leap days where they belong. Again, if you let the Hardware Clock run for more than a year without set- ting it, this scheme could be defeated and you could end up los- ing a day. hwclock warns you that you probably need --badyear whenever it finds your Hardware Clock set to 1994 or 1995. --srm This option is equivalent to --epoch=1900 and is used to specify the most common epoch on Alphas with SRM console. --arc This option is equivalent to --epoch=1980 and is used to specify the most common epoch on Alphas with ARC console (but Ruffians have epoch 1900). --jensen --funky-toy These two options specify what kind of Alpha machine you have. They are invalid if you don’t have an Alpha and are usually unnecessary if you do, because hwclock should be able to deter- mine by itself what it’s running on, at least when /proc is mounted. (If you find you need one of these options to make hwclock work, contact the maintainer to see if the program can be improved to detect your system automatically. Output of ‘hwclock --debug’ and ‘cat /proc/cpuinfo’ may be of interest.) --jensen means you are running on a Jensen model. --funky-toy means that on your machine, one has to use the UF bit instead of the UIP bit in the Hardware Clock to detect a time transition. "Toy" in the option name refers to the Time Of Year facility of the machine. --test Do everything except actually updating the Hardware Clock or anything else. This is useful, especially in conjunction with --debug, in learning about hwclock. --debug Display a lot of information about what hwclock is doing inter- nally. Some of its function is complex and this output can help you understand how the program works. NOTES Clocks in a Linux System There are two main clocks in a Linux system: The Hardware Clock: This is a clock that runs independently of any con- trol program running in the CPU and even when the machine is powered off. On an ISA system, this clock is specified as part of the ISA standard. The control program can read or set this clock to a whole second, but the control program can also detect the edges of the 1 second clock ticks, so the clock actually has virtually infinite precision. This clock is commonly called the hardware clock, the real time clock, the RTC, the BIOS clock, and the CMOS clock. Hardware Clock, in its capitalized form, was coined for use by hwclock because all of the other names are inappropriate to the point of being misleading. So for example, some non-ISA systems have a few real time clocks with only one of them having its own power domain. A very low power exter- nal I2C or SPI clock chip might be used with a backup battery as the hardware clock to initialize a more functional integrated real-time clock which is used for most other purposes. The System Time: This is the time kept by a clock inside the Linux ker- nel and driven by a timer interrupt. (On an ISA machine, the timer interrupt is part of the ISA standard). It has meaning only while Linux is running on the machine. The System Time is the number of sec- onds since 00:00:00 January 1, 1970 UTC (or more succinctly, the number of seconds since 1969). The System Time is not an integer, though. It has virtually infinite precision. The System Time is the time that matters. The Hardware Clock’s basic purpose in a Linux system is to keep time when Linux is not running. You initialize the System Time to the time from the Hardware Clock when Linux starts up, and then never use the Hardware Clock again. Note that in DOS, for which ISA was designed, the Hardware Clock is the only real time clock. It is important that the System Time not have any discontinuities such as would happen if you used the date(1L) program to set it while the system is running. You can, however, do whatever you want to the Hard- ware Clock while the system is running, and the next time Linux starts up, it will do so with the adjusted time from the Hardware Clock. A Linux kernel maintains a concept of a local timezone for the system. But don’t be misled -- almost nobody cares what timezone the kernel thinks it is in. Instead, programs that care about the timezone (per- haps because they want to display a local time for you) almost always use a more traditional method of determining the timezone: They use the TZ environment variable and/or the /usr/share/zoneinfo directory, as explained in the man page for tzset(3). However, some programs and fringe parts of the Linux kernel such as filesystems use the kernel timezone value. An example is the vfat filesystem. If the kernel timezone value is wrong, the vfat filesystem will report and set the wrong timestamps on files. hwclock sets the kernel timezone to the value indicated by TZ and/or /usr/share/zoneinfo when you set the System Time using the --hctosys option. The timezone value actually consists of two parts: 1) a field tz_min- uteswest indicating how many minutes local time (not adjusted for DST) lags behind UTC, and 2) a field tz_dsttime indicating the type of Day- light Savings Time (DST) convention that is in effect in the locality at the present time. This second field is not used under Linux and is always zero. (See also settimeofday(2).) How hwclock Accesses the Hardware Clock hwclock uses many different ways to get and set Hardware Clock values. The most normal way is to do I/O to the device special file /dev/rtc, which is presumed to be driven by the rtc device driver. However, this method is not always available. For one thing, the rtc driver is a relatively recent addition to Linux. Older systems don’t have it. Also, though there are versions of the rtc driver that work on DEC Alphas, there appear to be plenty of Alphas on which the rtc driver does not work (a common symptom is hwclock hanging). Moreover, recent Linux systems have more generic support for RTCs, even systems that have more than one, so you might need to override the default by speci- fying /dev/rtc0 or /dev/rtc1 instead. On older systems, the method of accessing the Hardware Clock depends on the system hardware. On an ISA system, hwclock can directly access the "CMOS memory" regis- ters that constitute the clock, by doing I/O to Ports 0x70 and 0x71. It does this with actual I/O instructions and consequently can only do it if running with superuser effective userid. (In the case of a Jensen Alpha, there is no way for hwclock to execute those I/O instruc- tions, and so it uses instead the /dev/port device special file, which provides almost as low-level an interface to the I/O subsystem). This is a really poor method of accessing the clock, for all the rea- sons that user space programs are generally not supposed to do direct I/O and disable interrupts. Hwclock provides it because it is the only method available on ISA and Alpha systems which don’t have working rtc device drivers available. On an m68k system, hwclock can access the clock via the console driver, via the device special file /dev/tty1. hwclock tries to use /dev/rtc. If it is compiled for a kernel that doesn’t have that function or it is unable to open /dev/rtc (or the alternative special file you’ve defined on the command line) hwclock will fall back to another method, if available. On an ISA or Alpha machine, you can force hwclock to use the direct manipulation of the CMOS registers without even trying /dev/rtc by specifying the --direc- tisa option. The Adjust Function The Hardware Clock is usually not very accurate. However, much of its inaccuracy is completely predictable - it gains or loses the same amount of time every day. This is called systematic drift. hwclock’s "adjust" function lets you make systematic corrections to correct the systematic drift. It works like this: hwclock keeps a file, /etc/adjtime, that keeps some historical information. This is called the adjtime file. Suppose you start with no adjtime file. You issue a hwclock --set com- mand to set the Hardware Clock to the true current time. Hwclock cre- ates the adjtime file and records in it the current time as the last time the clock was calibrated. 5 days later, the clock has gained 10 seconds, so you issue another hwclock --set command to set it back 10 seconds. Hwclock updates the adjtime file to show the current time as the last time the clock was calibrated, and records 2 seconds per day as the systematic drift rate. 24 hours go by, and then you issue a hwclock --adjust command. Hwclock consults the adjtime file and sees that the clock gains 2 seconds per day when left alone and that it has been left alone for exactly one day. So it subtracts 2 seconds from the Hardware Clock. It then records the current time as the last time the clock was adjusted. Another 24 hours goes by and you issue another hwclock --adjust. Hwclock does the same thing: subtracts 2 seconds and updates the adjtime file with the current time as the last time the clock was adjusted. Every time you calibrate (set) the clock (using --set or --systohc), hwclock recalculates the systematic drift rate based on how long it has been since the last calibration, how long it has been since the last adjustment, what drift rate was assumed in any intervening adjustments, and the amount by which the clock is presently off. A small amount of error creeps in any time hwclock sets the clock, so it refrains from making an adjustment that would be less than 1 second. Later on, when you request an adjustment again, the accumulated drift will be more than a second and hwclock will do the adjustment then. It is good to do a hwclock --adjust just before the hwclock --hctosys at system startup time, and maybe periodically while the system is run- ning via cron. The adjtime file, while named for its historical purpose of controlling adjustments only, actually contains other information for use by hwclock in remembering information from one invocation to the next. The format of the adjtime file is, in ASCII: Line 1: 3 numbers, separated by blanks: 1) systematic drift rate in seconds per day, floating point decimal; 2) Resulting number of seconds since 1969 UTC of most recent adjustment or calibration, decimal inte- ger; 3) zero (for compatibility with clock(8)) as a decimal integer. Line 2: 1 number: Resulting number of seconds since 1969 UTC of most recent calibration. Zero if there has been no calibration yet or it is known that any previous calibration is moot (for example, because the Hardware Clock has been found, since that calibration, not to contain a valid time). This is a decimal integer. Line 3: "UTC" or "LOCAL". Tells whether the Hardware Clock is set to Coordinated Universal Time or local time. You can always override this value with options on the hwclock command line. You can use an adjtime file that was previously used with the clock(8) program with hwclock. Automatic Hardware Clock Synchronization By the Kernel You should be aware of another way that the Hardware Clock is kept syn- chronized in some systems. The Linux kernel has a mode wherein it copies the System Time to the Hardware Clock every 11 minutes. This is a good mode to use when you are using something sophisticated like ntp to keep your System Time synchronized. (ntp is a way to keep your Sys- tem Time synchronized either to a time server somewhere on the network or to a radio clock hooked up to your system. See RFC 1305). This mode (we’ll call it "11 minute mode") is off until something turns it on. The ntp daemon xntpd is one thing that turns it on. You can turn it off by running anything, including hwclock --hctosys, that sets the System Time the old fashioned way. If your system runs with 11 minute mode on, don’t use hwclock --adjust or hwclock --hctosys. You’ll just make a mess. It is acceptable to use a hwclock --hctosys at startup time to get a reasonable System Time until your system is able to set the System Time from the external source and start 11 minute mode. ISA Hardware Clock Century value There is some sort of standard that defines CMOS memory Byte 50 on an ISA machine as an indicator of what century it is. hwclock does not use or set that byte because there are some machines that don’t define the byte that way, and it really isn’t necessary anyway, since the year-of-century does a good job of implying which century it is. If you have a bona fide use for a CMOS century byte, contact the hwclock maintainer; an option may be appropriate. Note that this section is only relevant when you are using the "direct ISA" method of accessing the Hardware Clock. ACPI provides a standard way to access century values, when they are supported by the hardware. ENVIRONMENT VARIABLES TZ FILES /etc/adjtime /usr/share/zoneinfo/ (/usr/lib/zoneinfo on old systems) /dev/rtc /dev/rtc0 /dev/port /dev/tty1 /proc/cpuinfo SEE ALSO date(1), gettimeofday(2), settimeofday(2), crontab(1), tzset(3) AUTHORS Written by Bryan Henderson, September 1996 (bryanh@giraffe-data.com), based on work done on the clock program by Charles Hedrick, Rob Hooft, and Harald Koenig. See the source code for complete history and cred- its. AVAILABILITY The hwclock command is part of the util-linux-ng package and is avail- able from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/. 06 August 2008 HWCLOCK(8)
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa