gettimeofdayのヘルプ・マニュアル
日本語 英語
gettimeofday --help
man gettimeofday
GETTIMEOFDAY(2) Linux Programmer’s Manual GETTIMEOFDAY(2)
名前
gettimeofday, settimeofday - 時刻を取得/設定する
書式
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
settimeofday(): _BSD_SOURCE
説明
関 数 gettimeofday() と settimeofday() は時刻とタイムゾーンを取得または
設定する。 tv 引き数は struct timeval である ( で定義されて
いる):
struct timeval {
time_t tv_sec; /* 秒 */
suseconds_t tv_usec; /* マイクロ秒 */
};
これにより紀元 (the Epoch: time(2) を参照) からの秒とマイクロ秒が取得で
きる。 tz 引き数は struct timezone である:
struct timezone {
int tz_minuteswest; /* グリニッジ標準時との差 (西方に分単位) */
int tz_dsttime; /* 夏時間調整の型 */
};
tv や tz が NULL の場合、対応する構造体の設定/取得は行われない。
timezone 構造体を使うのは時代遅れ (obsolete) である: tz 引き数は通常 は
NULL に指定すべきである。 tz_dsttime フィールドは Linux では一切使われ
てこなかった; libc や glibc でもサポートされてこなかったし、これから も
サ ポートされることはない。カーネルのソース中の宣言以外で、このフィール
ドが存在するのは全てバグである。よってこれ以降の記述は、純粋に歴史的 興
味に基づいたものである。
tz_dsttime フィールドには (以下で与えられるような) シンボル定数が格納さ
れる。これは一年のうちでいつ夏時間 (Daylight Savings Time) を実施するか
を 示している (注意: その値は年間を通した定数である: 夏時間が実施中であ
ることを示すわけではなく、アルゴリズムを選択しているだけである)。夏時間
は以下のように定義される:
DST_NONE /* 夏時間を採用していない */
DST_USA /* アメリカ合衆国式夏時間 */
DST_AUST /* オーストラリア式夏時間 */
DST_WET /* 西ヨーロッパ式夏時間 */
DST_MET /* 中央ヨーロッパ式夏時間 */
DST_EET /* 東ヨーロッパ式夏時間 */
DST_CAN /* カナダ */
DST_GB /* グレートブリテンおよびアイルランド */
DST_RUM /* ルーマニア */
DST_TUR /* トルコ */
DST_AUSTALT /* 1986年に移行されたオーストラリア式 */
当 然のことながら、夏時間がどの期間に実施されるかを国ごとの簡単なアルゴ
リズムで導くことができないことが判明した。実際、夏時間の期間は予測不 可
能 な政治的決定で決まる。そのためこの方法でタイム・ゾーンを表すことは断
念された。 Linux において settimeofday() を呼び出すときは、 tz_dsttime
フィールドを 0 にするべきである。
Linux では、 settimeofday() システム・コールに関連して、独特の「クロッ
クのズレ (warp clock)」が存在する。これは (ブート後の) 最初の呼び出しで
tz 引き数が NULL でなく、 tv 引き数が NULL で tz_minuteswest フィールド
が 0 でない場合に起こる。このような場合、 settimeofday() は CMOS クロッ
ク が 地 方 時 (local time) であり、 UTC システム時間を得るためには、
tz_minuteswest の分だけ増加させなくてはならないとみなしてしまう。疑いも
なく、この機構を使うことは良い考えではない。
timeval 構造体を操作するためのマクロの説明は timeradd(3) にある。
返り値
gettimeofday() と settimeofday() は成功すると 0 を返し、失敗した場合は
-1 を返す (この場合は errno が適切に設定される)。
エラー
EFAULT tv か tz のどちらかがアクセス可能なアドレス空間外を指している。
EINVAL タイムゾーン (または他の何か) が不正である。
EPERM 呼び出し元プロセスに settimeofday() を呼び出すための十分な特権が
な い。 Linux では CAP_SYS_TIME ケーパビリティ (capability) が必
要である。
準拠
SVr4, 4.3BSD に準拠する。 POSIX.1-2001 は gettimeofday() については記述
し ているが、 settimeofday() については記述していない。 POSIX.1-2008 で
は gettimeofday() は廃止予定とされており、代わりに clock_gettime(2) の
使用が推奨されている。
注意
昔は struct timeval のフィールドは long 型であった。
関連項目
date(1), adjtimex(2), time(2), ctime(3), ftime(3), capabilities(7),
time(7)
Linux 2009-03-25 GETTIMEOFDAY(2)
GETTIMEOFDAY(2) Linux Programmer’s Manual GETTIMEOFDAY(2)
NAME
gettimeofday, settimeofday - get / set time
SYNOPSIS
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
settimeofday(): _BSD_SOURCE
DESCRIPTION
The functions gettimeofday() and settimeofday() can get and set the
time as well as a timezone. The tv argument is a struct timeval (as
specified in ):
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
and gives the number of seconds and microseconds since the Epoch (see
time(2)). The tz argument is a struct timezone:
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
If either tv or tz is NULL, the corresponding structure is not set or
returned.
The use of the timezone structure is obsolete; the tz argument should
normally be specified as NULL. The tz_dsttime field has never been
used under Linux; it has not been and will not be supported by libc or
glibc. Each and every occurrence of this field in the kernel source
(other than the declaration) is a bug. Thus, the following is purely
of historic interest.
The field tz_dsttime contains a symbolic constant (values are given
below) that indicates in which part of the year Daylight Saving Time is
in force. (Note: its value is constant throughout the year: it does
not indicate that DST is in force, it just selects an algorithm.) The
daylight saving time algorithms defined are as follows :
DST_NONE /* not on dst */
DST_USA /* USA style dst */
DST_AUST /* Australian style dst */
DST_WET /* Western European dst */
DST_MET /* Middle European dst */
DST_EET /* Eastern European dst */
DST_CAN /* Canada */
DST_GB /* Great Britain and Eire */
DST_RUM /* Rumania */
DST_TUR /* Turkey */
DST_AUSTALT /* Australian style with shift in 1986 */
Of course it turned out that the period in which Daylight Saving Time
is in force cannot be given by a simple algorithm, one per country;
indeed, this period is determined by unpredictable political decisions.
So this method of representing timezones has been abandoned. Under
Linux, in a call to settimeofday() the tz_dsttime field should be zero.
Under Linux there are some peculiar "warp clock" semantics associated
with the settimeofday() system call if on the very first call (after
booting) that has a non-NULL tz argument, the tv argument is NULL and
the tz_minuteswest field is non-zero. In such a case it is assumed
that the CMOS clock is on local time, and that it has to be incremented
by this amount to get UTC system time. No doubt it is a bad idea to
use this feature.
Macros for operating on timeval structures are described in timer-
add(3).
RETURN VALUE
gettimeofday() and settimeofday() return 0 for success, or -1 for fail-
ure (in which case errno is set appropriately).
ERRORS
EFAULT One of tv or tz pointed outside the accessible address space.
EINVAL Timezone (or something else) is invalid.
EPERM The calling process has insufficient privilege to call settime-
ofday(); under Linux the CAP_SYS_TIME capability is required.
CONFORMING TO
SVr4, 4.3BSD. POSIX.1-2001 describes gettimeofday() but not settimeof-
day(). POSIX.1-2008 marks gettimeofday() as obsolete, recomending the
use of clock_gettime(2) instead.
NOTES
Traditionally, the fields of struct timeval were of type long.
SEE ALSO
date(1), adjtimex(2), time(2), ctime(3), ftime(3), capabilities(7),
time(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2009-03-25 GETTIMEOFDAY(2)