| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

C gmtime

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

C gmtime

Programming C |

gmtime

time関数を呼び出して、低水準の時刻の値を取得し、gmtimeを利用して日付と時刻の表現に変換する。

  1. #include <time.h>
  2. 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 夏時間
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct tm *tm_ptr;
  8. time_t the_time;
  9.  
  10. (void) time(&the_time);
  11. tm_ptr = gmtime(&the_time);
  12.  
  13. printf("%02d/%02d/%02d %02d:%02d:%02d\n",
  14. tm_ptr->tm_year + 1900,
  15. tm_ptr->tm_mon + 1,
  16. tm_ptr->tm_mday,
  17. tm_ptr->tm_hour,
  18. tm_ptr->tm_min,
  19. tm_ptr->tm_sec);
  20. exit(0);
  21. }

localtime

  1. #include <time.h>
  2. struct tm *localtime(const time_t *timeval);
  • local time zone と夏時間に関する調整を加えた値を返す。
  • 使い方は、gmtimeと同様

mktime

  1. #include <time.h>
  2. time_t mktime(struct tm *timeptr);
  • tm構造体に格納した値から逆にtime_t型の値を求める。
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6. struct tm *tm_ptr;
  7. time_t the_time;
  8. time_t other_time;
  9.  
  10. (void) time(&the_time);
  11. tm_ptr = localtime(&the_time);
  12. printf("%d\n", the_time);
  13. printf("%02d/%02d/%02d %02d:%02d:%02d\n",
  14. tm_ptr->tm_year + 1900,
  15. tm_ptr->tm_mon + 1,
  16. tm_ptr->tm_mday,
  17. tm_ptr->tm_hour,
  18. tm_ptr->tm_min,
  19. tm_ptr->tm_sec);
  20.  
  21. other_time = mktime(tm_ptr);
  22.  
  23. printf("%d\n", other_time);
  24.  
  25. exit(0);
  26. }

asctime、ctime

  • asctime関数は、tm構造体timeptrで指定された日付と時刻を表す文字列を返す。
  • ctime関数は、asctime(localtime(timeval))と同じ。
  1. #include <time.h>
  2. char *asctime(const struct tm *timeptr);
  3. char *ctime(const time_t *timeval);

返される文字は次のような形式

  1. Sun Oct 14 01:46:51 2007\n\0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct tm *tm_ptr;
  8. time_t the_time;
  9.  
  10. (void) time(&the_time);
  11. tm_ptr = localtime(&the_time);
  12.  
  13. printf("%s", asctime(tm_ptr));
  14. printf("%s", ctime(&the_time)); exit(0);
  15. }

strftime

  • timeptrで指定した構造体tmが表す日付と時刻に書式を設定する。
  1. #include <time.h>
  2. 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 西暦年
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct tm *tm_ptr;
  8. time_t the_time;
  9. char buf[256];
  10. (void) time(&the_time);
  11. tm_ptr = localtime(&the_time);
  12. strftime(buf, 256, "%Y/%m/%d %A %H:%M:%S", tm_ptr);
  13. printf("%s\n", buf); exit(0);
  14. }
  1. 2007/10/15 Monday 22:50:48

strptime

日付を読み取るときには、strptimeを使う。 日付と時刻を表す文字列を受け取って、tm構造体を作成

  1. #include <time.h>
  2. char *strptime(const char *buf, const char *format, struct tm *timeptr);



この本からの覚書。