C言語/標準ライブラリ/time.h

出典: フリー教科書『ウィキブックス(Wikibooks)』
このページ「C言語/標準ライブラリ/time.h」は、まだ書きかけです。加筆・訂正など、協力いただける皆様の編集を心からお待ちしております。また、ご意見などがありましたら、お気軽にトークページへどうぞ。


ヘッダー <time.h> では、いくつかのマクロが定義されており、時間を操作するための型や関数が宣言されています[1]

多くの関数は、(グレゴリオ暦に基づく)現在の日付と時刻を表すカレンダータイムを扱います。いくつかの関数は、特定のタイムゾーンで表現されるカレンダータイムであるローカルタイムや、ローカルタイムを決定するアルゴリズムの一時的な変更である夏時間を扱います。ローカルタイムゾーンと夏時間は実装で定義されます。

C2xでは、機能テストマクロ __STDC_VERSION_TIME_H__ は、 yyyymmL というトークンに展開されます。その他のマクロはNULLが定義されています(§7.19 Common definitions <stddef.h>に記載)[2]

ヘッダー <time.h> で定義されているマクロと型と、宣言されている関数[3]

マクロ[編集]

NULL
CLOCKS_PER_SEC
TIME_UTC

[編集]

size_t
clock_t
time_t
struct timespec
struct tm

関数[編集]

必須
clock_t clock(void);
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm *timeptr);
time_t time(time_t *timer);
int timespec_get(struct timespec *ts, int base); // struct が抜けている
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
size_t strftime(char * restrict s, size_t maxsize, const char * restrict format, const struct tm * restrict timeptr);

境界チェックインターフェース[編集]

処理系が __STDC_LIB_EXT1__ を定義し、ユーザが __STDC_WANT_LIB_EXT1__ を 1 に停止した場合、以下の境界チェックインターフェースが有効になります。

マクロ[編集]

__STDC_LIB_EXT1__
__STDC_WANT_LIB_EXT1__

[編集]

errno_t
rsize_t

関数[編集]

境界チェックインターフェース
errno_t asctime_s(char *s, rsize_t maxsize, const struct tm *timeptr);
errno_t ctime_s(char *s, rsize_t maxsize, const time_t *timer);
struct tm *gmtime_s(const time_t * restrict timer, struct tm * restrict result);
struct tm *localtime_s(const time_t * restrict timer, struct tm * restrict result);

Unixの拡張機能[編集]

POSIX(IEEE Std 1003.1)では、time.hに2つの関数を追加しています。asctime_r[4]ctime_rです。[5] これらはasctimectimeのリエントラント版( reentrant version )です。どちらの関数も、呼び出し側が、ある瞬間のテキスト表現を格納するバッファを提供することを必要とします。 以下のサンプルでは、リエントラント版のlocaltimeとasctimeの使用方法を示しています。

#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
  time_t rawtime;
  struct tm *timeinfo;
  struct tm timeinfoBuffer;
  char *result;

  time(&rawtime);
  /* call localtime */
  timeinfo = localtime_r(&rawtime, &timeinfoBuffer);
  /* allocate memory for the result of asctime call*/
  result = malloc(26 * sizeof(char));
  /* call reentrant asctime function */
  result = asctime_r(timeinfo, result);
  printf("The current date/time is: %s", result);
  /* free allocated memory */
  free(result);

  return 0;
}

これらの関数はC++の標準にはないので、C++の名前空間stdには属していません。

脚註[編集]

  1. ^ N2176 C17 ballot ISO/IEC 9899:2017. ISO/IEC JTC1/SC22/WG14. p. 284, §7.27 Date and time <time.h>. オリジナルの2018-12-30時点によるアーカイブ。. https://web.archive.org/web/20181230041359/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. 
  2. ^ N2596 working draft — December 11, 2020 ISO/IEC 9899:202x (E). ISO/IEC JTC1/SC22/WG14. p. 284, §7.27 Date and time <time.h>. オリジナルの2018-12-30時点によるアーカイブ。. https://web.archive.org/web/20181230041359/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. 
  3. ^ N2176 C17 ballot ISO/IEC 9899:2017. ISO/IEC JTC1/SC22/WG14. p. 360, §B.26 Date and time <time.h>. オリジナルの2018-12-30時点によるアーカイブ。. https://web.archive.org/web/20181230041359/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf. 
  4. ^ asctime. The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008.
  5. ^ ctime. The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008.

参考文献[編集]