.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Sat Jul 24 19:49:27 1993 by Rik Faith (faith@cs.unc.edu) .\" Modified Fri Apr 26 12:38:55 MET DST 1996 by Martin Schulze (joey@linux.de) .\" Modified 2001-11-13, aeb .\" Modified 2001-12-13, joey, aeb .\" Modified 2004-11-16, mtk .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 Hiroaki Nagoya .\" all rights reserved. .\" Translated 1997-02-10, Hiroaki Nagoya .\" Updated 1999-09-01, Kentaro Shirakata .\" Updated 2001-10-15, Kentaro Shirakata .\" Updated 2001-12-13, Kentaro Shirakata .\" Updated 2002-01-03, Kentaro Shirakata .\" Updated 2005-02-24, Akihiro MOTOKI .\" Updated 2008-04-04, Akihiro MOTOKI , LDP v2.79 .\" Updated 2008-08-08, Akihiro MOTOKI , LDP v3.05 .\" Updated 2008-11-10, Akihiro MOTOKI , LDP v3.13 .\" .TH CTIME 3 2014\-08\-19 "" "Linux Programmer's Manual" .SH 名前 asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r \- 日付と時刻を要素別の時刻や ASCII に変換する .SH 書式 .nf \fB#include \fP .sp \fBchar *asctime(const struct tm *\fP\fItm\fP\fB);\fP .br \fBchar *asctime_r(const struct tm *\fP\fItm\fP\fB, char *\fP\fIbuf\fP\fB);\fP .sp \fBchar *ctime(const time_t *\fP\fItimep\fP\fB);\fP .br \fBchar *ctime_r(const time_t *\fP\fItimep\fP\fB, char *\fP\fIbuf\fP\fB);\fP .sp \fBstruct tm *gmtime(const time_t *\fP\fItimep\fP\fB);\fP .br \fBstruct tm *gmtime_r(const time_t *\fP\fItimep\fP\fB, struct tm *\fP\fIresult\fP\fB);\fP .sp \fBstruct tm *localtime(const time_t *\fP\fItimep\fP\fB);\fP .br \fBstruct tm *localtime_r(const time_t *\fP\fItimep\fP\fB, struct tm *\fP\fIresult\fP\fB);\fP .sp \fBtime_t mktime(struct tm *\fP\fItm\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .ad l .sp \fBasctime_r\fP(), \fBctime_r\fP(), \fBgmtime_r\fP(), \fBlocaltime_r\fP(): .RS _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE .RE .ad .SH 説明 関数 \fBctime\fP(), \fBgmtime\fP(), \fBlocaltime\fP() は \fItime_t\fP 型のカレンダー時刻を引き数にとる。 引き数が絶対値として解釈される場合は、時刻紀元 (Epoch; 1970\-01\-01 00:00:00 +0000 (UTC)) からの経過秒数と解釈される。 .PP 関数 \fBasctime\fP() と \fBmktime\fP() は 年・月・日などに分離された要素別の時刻を引き数とする。 .PP 要素別の時刻は \fI\fP で以下のように定義されている \fItm\fP 構造体に保持される。 .sp .in +4n .nf struct tm { int tm_sec; /* 秒 (0\-60) */ int tm_min; /* 分 (0\-59) */ int tm_hour; /* 時間 (0\-23) */ int tm_mday; /* 月内の日付 (1\-31) */ int tm_mon; /* 月 (0\-11) */ int tm_year; /* 年 \- 1900 */ int tm_wday; /* 曜日 (0\-6, 日曜 = 0) */ int tm_yday; /* 年内通算日 (0\-365, 1 月 1 日 = 0) */ int tm_isdst; /* 夏時間 */ }; .fi .in .PP \fItm\fP 構造体のメンバーは以下の通り: .TP 10 \fItm_sec\fP 秒数、ふつうは 0 から 59 までの値、 しかし閏秒のため 60 までの値は許される。 .TP \fItm_min\fP 分数、0 から 59 までの値。 .TP \fItm_hour\fP 真夜中からの通算時間、0 から 23 までの値。 .TP \fItm_mday\fP 月はじめからの日数、1 から 31 までの値。 .TP \fItm_mon\fP 1月からの通算月数、0 から 11 までの値。 .TP \fItm_year\fP 1900 年からの通算年数。 .TP \fItm_wday\fP 日曜日からの通算日数(曜日)。0 から 6 までの値。 .TP \fItm_yday\fP 1 月 1 日からの通算日数、0 から 365 までの値。 .TP \fItm_isdst\fP 夏時間が有効かどうかのフラグ。 正の値ならば夏時間は有効になり、0 ならば無効、負の値ならばこの情報には 意味がない。 .PP \fBctime(\fP\fIt\fP\fB)\fP 関数は、 \fBasctime(localtime(\fP\fIt\fP\fB))\fP と等価である。 カレンダー時刻 \fIt\fP を .sp .RS "Wed Jun 30 21:49:08 1993\en" .RE .sp という形式のヌル終端された文字列へ変換する。 曜日の略称は "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" である。 月の略称は "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" である。 返り値は、静的 (static) に割り当てられた文字列へのポインターである。 この文字列は、日付・時刻関数のいずれかが呼び出されると上書きされることがある。 またこの関数は大域変数 \fItzname\fP, \fItimezone\fP, \fIdaylight\fP に現在のタイムゾーンの情報を設定する (\fBtzset\fP(3) 参照)。 リエントラント版である \fBctime_r\fP() も同様だが、 文字列はユーザーが用意したバッファーに格納される。バッファーのサイズは 少なくとも 26 バイト以上でなければならない。 この関数は \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP を設定する必要はない。 .PP 関数 \fBgmtime\fP() は、カレンダー時刻 \fItimep\fP を 協定世界時 (UTC) での要素別の時刻へ変換する。 年が整数型に収まらない場合、NULL を返す。 返り値は静的に確保された構造体を指しており、この後で 日付や時刻に関する関数のいずれかが呼び出されると 上書きされる可能性がある。 \fBgmtime_r\fP() も同様だが、 データはユーザーが用意した構造体に格納される。 .PP 関数 \fBlocaltime\fP() は、カレンダー時刻 \fItimep\fP を ユーザーが指定したタイムゾーンでの時刻要素別の表現へ変換する。 この関数は \fBtzset\fP(3) を呼び出したかのように振舞い、 大域変数 \fItzname\fP に現在のタイムゾーンの情報を設定する。 また、\fItimezone\fP に協定世界時 (UTC) とローカル標準時との 時差の秒数を設定し、 一年の一部で夏時間が適用される場合は \fIdaylight\fP に 0 が設定される。 返り値は静的に確保された構造体を指しており、この後で 日付や時刻に関する関数のいずれかが呼び出されると 上書きされる可能性がある。 \fBlocaltime_r\fP() も同様だが、 データはユーザーが用意した構造体に格納される。 この関数は \fItzname\fP, \fItimezone\fP, and \fIdaylight\fP を設定する必要はない。 .PP 関数 \fBasctime\fP() は、要素別の時刻 \fItm\fP を \fBctime\fP() と同じ形式のヌル終端された文字列へ変換する。 返り値は静的に割り当てられた文字列へのポインターである。この文字列は、 日付・時刻関数のいずれかが呼び出されると上書きされることがある。 リエントラント版である \fBasctime_r\fP() も同様だが、 文字列はユーザーが用意したバッファーに格納される。バッファーのサイズは 少なくとも 26 バイト以上でなければならない。 .PP 関数 \fBmktime\fP() は、(ローカルタイムで記述されている) 要素別の時刻を カレンダー時刻へ変換する。この際、呼び出し元がフィールド \fItm_wday\fP と \fItm_yday\fP で指定した値は無視される。 \fBmktime\fP() は、フィールド \fItm_isdst\fP で指定された値により、 \fItm\fP 構造体で渡された時刻で夏時間 (daylight saving time; DST) が有効になって いるかを知る。 正の値は夏時間が有効であることを意味する。 負の値であれば、 \fBmktime\fP() は (タイムゾーン情報とシステムのデータベースを使って) 指定された時刻で夏時間が有効かどうかを判断する必要があることを意味する。 \fBmktime\fP() は \fItm\fP 構造体の各フィールドを以下のように修正する。 \fItm_wday\fP と \fItm_yday\fP には他のフィールドの内容から求めた値を設定する。 構造体の要素が有効な範囲にない場合、正規化される (例えば、10 月 40 日は 11 月 9 日に変更される)。 \fItm_isdst\fP には (最初の値にかかわらず) 正の値か 0 が設定される。 正の値は指定された時間で夏時間が有効であることを示し、 0 は無効であることを示す。 関数 \fBmktime\fP() を呼び出すと、 大域変数 \fItzname\fP が現在のタイムゾーンに設定される。 要素別の時刻をカレンダー時刻 (紀元 (Epoch) からの秒数) で表現できない場合、 \fBmktime\fP() は \fI(time_t)\ (\-1)\fP を返し、要素別の時刻の構造体メンバーを変更しない。 .SH 返り値 各関数はそれぞれ前述した値を返す。エラーの場合は NULL (\fBmktime\fP() では \-1) を返す。 .SH 準拠 POSIX.1\-2001. C89 と C99 では \fBasctime\fP(), \fBctime\fP(), \fBgmtime\fP(), \fBlocaltime\fP(), \fBmktime\fP() が規定されている。 POSIX.1\-2008 は、 \fBasctime\fP(), \fBasctime_r\fP(), \fBctime\fP(), \fBctime_r\fP() を廃止予定としている。 代わりに、 \fBstrftime\fP(3) の使用が推奨されている。 .SH 注意 \fBasctime\fP(), \fBctime\fP(), \fBgmtime\fP(), \fBlocaltime\fP() の 4 つの関数は静的データへのポインターを返すので、スレッドセーフではない。 これらの関数のスレッドセーフ版である \fBasctime_r\fP(), \fBctime_r\fP(), \fBgmtime_r\fP(), \fBlocaltime_r\fP() は SUSv2 で規定されている。 POSIX.1\-2001 では、「関数 \fBasctime\fP(), \fBctime\fP(), \fBgmtime\fP(), \fBlocaltime\fP() は、要素別の時刻の構造体か \fIchar\fP 型の配列かのどちらかの静的オブジェクトを返すものとする。 これらの関数のいずれかを実行すると、他の関数のどれかがこれらの 静的オブジェクトのどちらかに格納して返した情報が上書きされるかも しれない。」となっている。 このことは glibc の実装で起こりうる。 .LP glibc を含む多くの実装では、 \fItm_mday\fP に 0 を指定すると前月の最終日を意味していると解釈される。 .LP glibc では、 \fI\fP がインクルードされる前に \fB_BSD_SOURCE\fP が定義されると、 \fIstruct tm\fP に以下のフィールドが追加される。 .sp .RS .nf long tm_gmtoff; /* Seconds east of UTC */ const char *tm_zone; /* Timezone abbreviation */ .fi .RE .sp これは BSD 拡張であり、4.3BSD\-Reno から現れた。 .\" See http://thread.gmane.org/gmane.comp.time.tz/2034/ POSIX.1\-2004 によると、 \fBlocaltime\fP() はあたかも \fBtzset\fP(3) が呼ばれたかのように振舞うことが要求されているが、 \fBlocaltime_r\fP() にはこの要件はない。 移植性が必要なコードでは、 \fBlocaltime_r\fP() の前に \fBtzset\fP(3) を呼び出しておくべきである。 .SH 関連項目 \fBdate\fP(1), \fBgettimeofday\fP(2), \fBtime\fP(2), \fButime\fP(2), \fBclock\fP(3), \fBdifftime\fP(3), \fBstrftime\fP(3), \fBstrptime\fP(3), \fBtimegm\fP(3), \fBtzset\fP(3), \fBtime\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。