「C gmtime」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==C gmtime== [Programming C] ====gmtime==== time関数を呼び出して、低水準の時刻の値を取得し、gmtimeを利用して日付と時刻の表現に変…」) |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 1行目: | 1行目: | ||
| − | ==C gmtime== | + | ==[[C gmtime]]== |
| − | [Programming C] | + | [[Programming C]] | |
====gmtime==== | ====gmtime==== | ||
time関数を呼び出して、低水準の時刻の値を取得し、gmtimeを利用して日付と時刻の表現に変換する。 | time関数を呼び出して、低水準の時刻の値を取得し、gmtimeを利用して日付と時刻の表現に変換する。 | ||
| − | #include | + | #include <time.h> |
struct tm *gmtime(const time_t timeval); | struct tm *gmtime(const time_t timeval); | ||
| 40行目: | 40行目: | ||
|} | |} | ||
| − | #include | + | #include <time.h> |
| − | #include | + | #include <stdio.h> |
| − | #include | + | #include <stdlib.h> |
int main() | int main() | ||
| 53行目: | 53行目: | ||
printf("%02d/%02d/%02d %02d:%02d:%02d\n", | printf("%02d/%02d/%02d %02d:%02d:%02d\n", | ||
| − | tm_ptr- | + | tm_ptr->tm_year + 1900, |
| − | tm_ptr- | + | tm_ptr->tm_mon + 1, |
| − | tm_ptr- | + | tm_ptr->tm_mday, |
| − | tm_ptr- | + | tm_ptr->tm_hour, |
| − | tm_ptr- | + | tm_ptr->tm_min, |
| − | tm_ptr- | + | tm_ptr->tm_sec); |
exit(0); | exit(0); | ||
} | } | ||
====localtime==== | ====localtime==== | ||
| − | #include | + | #include <time.h> |
struct tm *localtime(const time_t *timeval); | struct tm *localtime(const time_t *timeval); | ||
*local time zone と夏時間に関する調整を加えた値を返す。 | *local time zone と夏時間に関する調整を加えた値を返す。 | ||
| 69行目: | 69行目: | ||
====mktime==== | ====mktime==== | ||
| − | #include | + | #include <time.h> |
time_t mktime(struct tm *timeptr); | time_t mktime(struct tm *timeptr); | ||
*tm構造体に格納した値から逆にtime_t型の値を求める。 | *tm構造体に格納した値から逆にtime_t型の値を求める。 | ||
| − | #include | + | #include <time.h> |
| − | #include | + | #include <stdio.h> |
| − | #include | + | #include <stdlib.h> |
int main() | int main() | ||
{ | { | ||
| 87行目: | 87行目: | ||
printf("%d\n", the_time); | printf("%d\n", the_time); | ||
printf("%02d/%02d/%02d %02d:%02d:%02d\n", | printf("%02d/%02d/%02d %02d:%02d:%02d\n", | ||
| − | tm_ptr- | + | tm_ptr->tm_year + 1900, |
| − | tm_ptr- | + | tm_ptr->tm_mon + 1, |
| − | tm_ptr- | + | tm_ptr->tm_mday, |
| − | tm_ptr- | + | tm_ptr->tm_hour, |
| − | tm_ptr- | + | tm_ptr->tm_min, |
| − | tm_ptr- | + | tm_ptr->tm_sec); |
other_time = mktime(tm_ptr); | other_time = mktime(tm_ptr); | ||
| 105行目: | 105行目: | ||
*ctime関数は、asctime(localtime(timeval))と同じ。 | *ctime関数は、asctime(localtime(timeval))と同じ。 | ||
| − | #include | + | #include <time.h> |
char *asctime(const struct tm *timeptr); | char *asctime(const struct tm *timeptr); | ||
char *ctime(const time_t *timeval); | char *ctime(const time_t *timeval); | ||
| 112行目: | 112行目: | ||
Sun Oct 14 01:46:51 2007\n\0 | Sun Oct 14 01:46:51 2007\n\0 | ||
| − | #include | + | #include <time.h> |
| − | #include | + | #include <stdio.h> |
| − | #include | + | #include <stdlib.h> |
int main() | int main() | ||
| 129行目: | 129行目: | ||
====strftime==== | ====strftime==== | ||
*timeptrで指定した構造体tmが表す日付と時刻に書式を設定する。 | *timeptrで指定した構造体tmが表す日付と時刻に書式を設定する。 | ||
| − | #include | + | #include <time.h> |
size_t strftime(char *s, stize_t maxsize, const char *format, struct tm *timeptr); | size_t strftime(char *s, stize_t maxsize, const char *format, struct tm *timeptr); | ||
| 177行目: | 177行目: | ||
|} | |} | ||
| − | #include | + | #include <time.h> |
| − | #include | + | #include <stdio.h> |
| − | #include | + | #include <stdlib.h> |
int main() | int main() | ||
| 198行目: | 198行目: | ||
日付と時刻を表す文字列を受け取って、tm構造体を作成 | 日付と時刻を表す文字列を受け取って、tm構造体を作成 | ||
| − | #include | + | #include <time.h> |
char *strptime(const char *buf, const char *format, struct tm *timeptr); | char *strptime(const char *buf, const char *format, struct tm *timeptr); | ||
2020年2月16日 (日) 04:23時点における最新版
C gmtime
gmtime
time関数を呼び出して、低水準の時刻の値を取得し、gmtimeを利用して日付と時刻の表現に変換する。
#include <time.h> struct tm *gmtime(const time_t timeval);
| member | 説明 |
|---|---|
| int tm_setc | 秒(0-61) |
| 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 | 1年の中での日数(0-365) |
| int tm_isdst | 夏時間 |
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tm *tm_ptr;
time_t the_time;
(void) time(&the_time);
tm_ptr = gmtime(&the_time);
printf("%02d/%02d/%02d %02d:%02d:%02d\n",
tm_ptr->tm_year + 1900,
tm_ptr->tm_mon + 1,
tm_ptr->tm_mday,
tm_ptr->tm_hour,
tm_ptr->tm_min,
tm_ptr->tm_sec);
exit(0);
}
localtime
#include <time.h> struct tm *localtime(const time_t *timeval);
- local time zone と夏時間に関する調整を加えた値を返す。
- 使い方は、gmtimeと同様
mktime
#include <time.h> time_t mktime(struct tm *timeptr);
- tm構造体に格納した値から逆にtime_t型の値を求める。
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tm *tm_ptr;
time_t the_time;
time_t other_time;
(void) time(&the_time);
tm_ptr = localtime(&the_time);
printf("%d\n", the_time);
printf("%02d/%02d/%02d %02d:%02d:%02d\n",
tm_ptr->tm_year + 1900,
tm_ptr->tm_mon + 1,
tm_ptr->tm_mday,
tm_ptr->tm_hour,
tm_ptr->tm_min,
tm_ptr->tm_sec);
other_time = mktime(tm_ptr);
printf("%d\n", other_time);
exit(0);
}
asctime、ctime
- asctime関数は、tm構造体timeptrで指定された日付と時刻を表す文字列を返す。
- ctime関数は、asctime(localtime(timeval))と同じ。
#include <time.h> char *asctime(const struct tm *timeptr); char *ctime(const time_t *timeval);
返される文字は次のような形式
Sun Oct 14 01:46:51 2007\n\0
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tm *tm_ptr;
time_t the_time;
(void) time(&the_time);
tm_ptr = localtime(&the_time);
printf("%s", asctime(tm_ptr));
printf("%s", ctime(&the_time)); exit(0);
}
strftime
- timeptrで指定した構造体tmが表す日付と時刻に書式を設定する。
#include <time.h> size_t strftime(char *s, stize_t maxsize, const char *format, struct tm *timeptr);
| 変換指定子 | 説明 |
|---|---|
| %a | 曜日の省略 |
| %A | 曜日 |
| %b | 月名の省略 |
| %B | 月名 |
| %c | 日付と時刻 |
| %d | 日 |
| %H | 24時間制の時間 |
| %I | 12時間制の時間 |
| %m | 月 |
| %M | 分 |
| %p | AMまたはPM |
| %S | 秒 |
| %Y | 西暦年 |
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct tm *tm_ptr;
time_t the_time;
char buf[256];
(void) time(&the_time);
tm_ptr = localtime(&the_time);
strftime(buf, 256, "%Y/%m/%d %A %H:%M:%S", tm_ptr);
printf("%s\n", buf); exit(0);
}
2007/10/15 Monday 22:50:48
strptime
日付を読み取るときには、strptimeを使う。 日付と時刻を表す文字列を受け取って、tm構造体を作成
#include <time.h> char *strptime(const char *buf, const char *format, struct tm *timeptr);
この本からの覚書。
© 2006 矢木浩人