細 文字列「</source>」を「</syntaxhighlight>」に置換 |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 4行目: | 4行目: | ||
# localtime関数またはgmtime関数を使用して、取得した暦時刻を変換する。 | # localtime関数またはgmtime関数を使用して、取得した暦時刻を変換する。 | ||
<br> | <br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
time_t time(time_t *timer); | time_t time(time_t *timer); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''time関数'''</u><br> | <u>'''time関数'''</u><br> | ||
| 23行目: | 23行目: | ||
<br> | <br> | ||
localtime関数とgmtime関数は、暦時刻の変換に失敗した場合は、NULLを返す。<br> | localtime関数とgmtime関数は、暦時刻の変換に失敗した場合は、NULLを返す。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
| 29行目: | 29行目: | ||
struct tm *gmtime(const time_t *timer); | struct tm *gmtime(const time_t *timer); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''tm構造体'''</u><br> | <u>'''tm構造体'''</u><br> | ||
| 59行目: | 59行目: | ||
<br> | <br> | ||
以下の例では、time関数を使用して日付・時刻を取得し、localtime関数およびgmtime関数をそれぞれ使用して表示している。<br> | 以下の例では、time関数を使用して日付・時刻を取得し、localtime関数およびgmtime関数をそれぞれ使用して表示している。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 99行目: | 99行目: | ||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''実行例'''</u><br> | <u>'''実行例'''</u><br> | ||
| 115行目: | 115行目: | ||
difftime関数は、2つの暦時刻の差(time1 - time0)を求め、その差を秒単位で返す関数である。<br> | difftime関数は、2つの暦時刻の差(time1 - time0)を求め、その差を秒単位で返す関数である。<br> | ||
なお、一般的に、time1とtime2はtime関数を使用して取得する。<br> | なお、一般的に、time1とtime2はtime関数を使用して取得する。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
double difftime(time_t time1, time_t time0); | double difftime(time_t time1, time_t time0); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
具体的には、以下のように記述する。<br> | 具体的には、以下のように記述する。<br> | ||
< | <syntaxhighlight lang="c"> | ||
double diff; | double diff; | ||
clock_t time1, time0; | clock_t time1, time0; | ||
| 136行目: | 136行目: | ||
/* 差分を求める */ | /* 差分を求める */ | ||
diff = difftime(time1, time0); | diff = difftime(time1, time0); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''clock関数を使用して、2つの時刻の差を求める方法'''</u><br> | <u>'''clock関数を使用して、2つの時刻の差を求める方法'''</u><br> | ||
| 142行目: | 142行目: | ||
clock関数の呼び出しに失敗した場合は、(clock_t) - 1を返す。<br> | clock関数の呼び出しに失敗した場合は、(clock_t) - 1を返す。<br> | ||
2つの時刻の差を求めるには、clock関数を2回呼び出し、差分を求める。<br> | 2つの時刻の差を求めるには、clock関数を2回呼び出し、差分を求める。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
clock_t clock(void); | clock_t clock(void); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
具体的には以下のように記述する。<br> | 具体的には以下のように記述する。<br> | ||
< | <syntaxhighlight lang="c"> | ||
double diff; | double diff; | ||
clock_t start, stop; | clock_t start, stop; | ||
| 163行目: | 163行目: | ||
/* 差分を求める */ | /* 差分を求める */ | ||
diff = (double)(stop - start) / CLOCKS_PER_SEC; | diff = (double)(stop - start) / CLOCKS_PER_SEC; | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
以下の例では、difftime関数およびclock関数を使用して、2つの時刻の差を求めている。<br> | 以下の例では、difftime関数およびclock関数を使用して、2つの時刻の差を求めている。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 220行目: | 220行目: | ||
printf("\n"); | printf("\n"); | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 231行目: | 231行目: | ||
clock関数の呼び出しに失敗した場合は、(clock_t) - 1を返す。<br> | clock関数の呼び出しに失敗した場合は、(clock_t) - 1を返す。<br> | ||
経過時間を秒単位で取得する場合は、戻り値をCLOCKS_PRE_SECマクロの値で除算する。<br> | 経過時間を秒単位で取得する場合は、戻り値をCLOCKS_PRE_SECマクロの値で除算する。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
clock_t clock(void); | clock_t clock(void); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
以下の例では、clock関数を使用して、プログラム開始時点からの経過時間を取得している。<br> | 以下の例では、clock関数を使用して、プログラム開始時点からの経過時間を取得している。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 274行目: | 274行目: | ||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 284行目: | 284行目: | ||
変換の際には、tm_wday要素(日曜日からの日数)とtm_yday要素(1月1日からの日数)の元の値は無視されて、自動的に適切な値が格納される。<Br> | 変換の際には、tm_wday要素(日曜日からの日数)とtm_yday要素(1月1日からの日数)の元の値は無視されて、自動的に適切な値が格納される。<Br> | ||
この"適切な値"により、この性質を利用すると日付から曜日を求めることができる。<br> | この"適切な値"により、この性質を利用すると日付から曜日を求めることができる。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
time_t mktime(struct tm *timeptr); | time_t mktime(struct tm *timeptr); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
以下の例では、mktime関数を使用して、日付から曜日を取得している。<br> | 以下の例では、mktime関数を使用して、日付から曜日を取得している。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 368行目: | 368行目: | ||
printf("%s\n", weeks[wday]); | printf("%s\n", weeks[wday]); | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 377行目: | 377行目: | ||
<br> | <br> | ||
localtime関数は、暦時刻の変換に失敗した場合はNULLを返す。<br> | localtime関数は、暦時刻の変換に失敗した場合はNULLを返す。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
struct tm *localtime(const time_t *timer); | struct tm *localtime(const time_t *timer); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''tm構造体'''</u><br> | <u>'''tm構造体'''</u><br> | ||
| 411行目: | 411行目: | ||
<br> | <br> | ||
以下の例では、localtime関数を使用して、暦時刻を地方時に変換している。<br> | 以下の例では、localtime関数を使用して、暦時刻を地方時に変換している。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 438行目: | 438行目: | ||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''実行例'''</u><br> | <u>'''実行例'''</u><br> | ||
| 445行目: | 445行目: | ||
<br><br> | <br><br> | ||
== == | == 暦時刻を文字列形式の地方時に変換する == | ||
C言語で、暦時刻(Calendar Time)を文字列形式の地方時(Local Time)に変換するには、time.hのctime関数を使用する。<br> | |||
<br> | |||
ctime関数は、timerが指す暦時刻を文字列形式の地方時に変換して返す関数である。<br> | |||
<syntaxhighlight lang="c"> | |||
#include <time.h> | |||
char *ctime(const time_t *timer); | |||
</syntaxhighlight> | |||
<br> | |||
以下の例では、ctime関数を使用して、現在の時刻を文字列形式の地方時として表示している。<br> | |||
<syntaxhighlight lang="c"> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <time.h> | |||
int main(void) | |||
{ | |||
time_t timer; | |||
char *p = nullptr; | |||
/* 現在時刻を取得 */ | |||
timer = time(NULL); | |||
/* 文字列(地方時)への変換 */ | |||
p = ctime(&timer); | |||
printf("%s\n", p); | |||
return EXIT_SUCCESS; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<u>'''実行例'''</u><br> | |||
上記の実行結果は以下のようになる。(結果は実行環境により異なる)<br> | |||
Mon Dec 29 19:18:14 2008 | |||
<br><br> | |||
== 暦時刻を協定世界時(UTC)に変換する == | |||
C言語で、暦時刻(Calendar Time)を協定世界時(UTC)に変換するには、time.hのgmtime関数を使用する。<br> | |||
<br> | |||
gmtime関数は、timerが指す暦時刻を協定世界時(UTC)に変換して、tm構造体に格納して、そのポインタを返す関数である。<br> | |||
gmtime関数は、暦時刻の変換に失敗した場合はNULLを返す。<br> | |||
<syntaxhighlight lang="c"> | |||
#include <time.h> | |||
struct tm *gmtime(const time_t *timer); | |||
</syntaxhighlight> | |||
<br> | |||
<u>'''tm構造体'''</u><br> | |||
再度、gmtime関数が返すtm構造体を下表に示す。<br> | |||
<center> | |||
{| class="wikitable" | |||
|- | |||
! データ型 !! メンバ !! 説明 | |||
|- | |||
| int || tm_sec || 秒 (0 ~ 60) | |||
|- | |||
| int || tm_min || 分 (0 ~ 59) | |||
|- | |||
| int || tm_hour || 時 (0 ~ 23) | |||
|- | |||
| int || tm_mday || 日 (1 ~ 31) | |||
|- | |||
| int || tm_mon || 1月からの月数 (0 ~ 11) | |||
|- | |||
| int || tm_year || 1900年からの年数 | |||
|- | |||
| int || tm_wday || 日曜日からの日数 (0 ~ 6) | |||
|- | |||
| int || tm_yday || 1月1日からの日数 (0 ~ 365) | |||
|- | |||
| int || tm_isdst || <u>夏時間フラグ</u><br>夏時間を採用している場合 : 正<br>夏時間を採用していない場合 : 0<br>この情報が得られない場合 : 負 | |||
|} | |||
</center> | |||
<br> | |||
以下の例では、gmtime関数を使用して、暦時刻を協定世界時(UTC)に変換している。<br> | |||
<syntaxhighlight lang="c"> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <time.h> | |||
int main(void) | |||
{ | |||
time_t timer; | |||
struct tm *utc; | |||
/* 現在時刻を取得 */ | |||
timer = time(NULL); | |||
utc = gmtime(&timer); /* 協定世界時(UTC)に変換 */ | |||
/* 協定世界時 変換後表示 */ | |||
printf("協定世界時: "); | |||
printf("%4d/", utc->tm_year + 1900); | |||
printf("%2d/", utc->tm_mon + 1); | |||
printf("%2d ", utc->tm_mday); | |||
printf("%2d:", utc->tm_hour); | |||
printf("%2d:", utc->tm_min); | |||
printf("%2d", utc->tm_sec); | |||
printf(" %d\n", utc->tm_isdst); | |||
return EXIT_SUCCESS; | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<u>'''実行例'''</u><br> | |||
上記の実行結果は以下のようになる。(結果は実行環境により異なる)<br> | |||
協定世界時: 2008/12/29 10: 4:11 0 | |||
<br><br> | <br><br> | ||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:C]] | [[カテゴリ:C]] | ||