LoginSignup
1
0

More than 5 years have passed since last update.

time(), gettimeofday(), clock_gettime() のタイムゾーンについて

Posted at

結論

time_t time(time_t *t);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int clock_gettime(clockid_t clk_id, struct timespec *tp); // when clk_id == CLOCK_REALTIME

これらの関数が返す時刻の値は Epoch からの経過時間とマニュアルに書かれている。

Epoch の定義は https://en.wikipedia.org/wiki/Unix_time によると

The Unix epoch is the time 00:00:00 UTC on 1 January 1970.

なのでタイムゾーンに関わらず UTC からの経過時刻で間違いない。

備考

gettimeofday() の引数 struct timezone *tz は obsolete なので使わず NULL を指定するべき。

タイムゾーンを取得する C/C++ 標準の関数は c++ - How do I find the current system timezone? - Stack Overflow によるとないらしいが glibc では #include <time.h> の前に _BSD_SOURCE を定義することで struct tm に 2 つの拡張フィールドが追加される。

long tm_gmtoff;           /* Seconds east of UTC */
const char *tm_zone;      /* Timezone abbreviation */

tm_gmtoff は UTC から表される時間のオフセット(秒単位)で、正の値は本初子午線の東を示す。tm_zone はタイムゾーン名の短縮形を示す。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0