編集の要約なし |
細 文字列「</source>」を「</syntaxhighlight>」に置換 |
||
| (同じ利用者による、間の2版が非表示) | |||
| 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> | ||
| 449行目: | 449行目: | ||
<br> | <br> | ||
ctime関数は、timerが指す暦時刻を文字列形式の地方時に変換して返す関数である。<br> | ctime関数は、timerが指す暦時刻を文字列形式の地方時に変換して返す関数である。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
char *ctime(const time_t *timer); | char *ctime(const time_t *timer); | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
以下の例では、ctime関数を使用して、現在の時刻を文字列形式の地方時として表示している。<br> | 以下の例では、ctime関数を使用して、現在の時刻を文字列形式の地方時として表示している。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 475行目: | 475行目: | ||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''実行例'''</u><br> | <u>'''実行例'''</u><br> | ||
| 487行目: | 487行目: | ||
gmtime関数は、timerが指す暦時刻を協定世界時(UTC)に変換して、tm構造体に格納して、そのポインタを返す関数である。<br> | gmtime関数は、timerが指す暦時刻を協定世界時(UTC)に変換して、tm構造体に格納して、そのポインタを返す関数である。<br> | ||
gmtime関数は、暦時刻の変換に失敗した場合はNULLを返す。<br> | gmtime関数は、暦時刻の変換に失敗した場合はNULLを返す。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <time.h> | #include <time.h> | ||
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> | ||
再度、gmtime関数が返すtm構造体を下表に示す。<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> | <br> | ||
以下の例では、gmtime関数を使用して、暦時刻を協定世界時(UTC)に変換している。<br> | 以下の例では、gmtime関数を使用して、暦時刻を協定世界時(UTC)に変換している。<br> | ||
< | <syntaxhighlight lang="c"> | ||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
| 524行目: | 548行目: | ||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
<u>'''実行例'''</u><br> | <u>'''実行例'''</u><br> | ||