<time.h> | ||
|---|---|---|
| struct tm *localtime ( const time_t *timer ); | (1) | |
| struct tm *localtime_r( const time_t *timer, struct tm *buf ); | (2) | (since C23) |
| struct tm *localtime_s( const time_t *restrict timer, struct tm *restrict buf ); | (3) | (since C11) |
功能和gmtime類似,將UTC秒數轉為本地日歷時間,而gmtime是轉為UTC日歷時間,即不帶時區,
同樣執行緒不安全,
#define __STDC_WANT_LIB_EXT1__ 1
#define _XOPEN_SOURCE // for putenv
#include <time.h>
#include <stdio.h>
#include <stdlib.h> // for putenv
int main(void)
{
time_t t = time(NULL);
printf("UTC: %s", asctime(gmtime(&t)));
printf("local: %s", asctime(localtime(&t)));
// POSIX-specific
putenv("TZ=Asia/Singapore");
printf("Singapore: %s", asctime(localtime(&t)));
#ifdef __STDC_LIB_EXT1__
struct tm buf;
char str[26];
asctime_s(str,sizeof str,gmtime_s(&t, &buf));
printf("UTC: %s", str);
asctime_s(str,sizeof str,localtime_s(&t, &buf));
printf("local: %s", str);
#endif
}
Possible output:
UTC: Fri Sep 15 14:22:05 2017
local: Fri Sep 15 14:22:05 2017
Singapore: Fri Sep 15 22:22:05 2017
UTC: Fri Sep 15 14:22:05 2017
local: Fri Sep 15 14:22:05 2017
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/243933.html
標籤:其他
下一篇:單管發報機的神奇之處-身兼兩職
