특정 날, 월 및 연도에 대해 C에서 UTC 시간을 어떻게 만드나요?
문제
다음 날짜의 C에서 UTC 시간을 어떻게 만드나요?
2038 년 7 월 1 일
표준 ANSI C 함수 호출을 사용합니다 (TM 구조의 TM_YEAR 요소가 137보다 클 수 없음)?
해결책
당신은 그렇지 않습니다. 32 비트 ANSI C TIME_T는 2038 년에 롤오버됩니다. 2003 년 7 월 23 일 이전 2 자리 COBOL 시스템에서 만드는 방법을 묻는 것과 같습니다.
다른 팁
다른 사람들은 예제로 제공 한 특정 날짜가 32 비트 시간이 표시되는 최대 날짜/시간을 넘어서는 경우가 많으며, 종종 2038 년 문제. 한 가지 해결책은 64 비트 Time_t를 사용하는 것입니다. mktime
.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
struct tm future; /* as in future date */
time_t t;
future.tm_sec = 0;
future.tm_min = 0;
future.tm_hour = 0;
future.tm_mday = 1; /* 1st */
future.tm_mon = 6; /* July */
future.tm_year = 2038 - 1900; /* 2038 in years since 1900 */
future.tm_isdst = 0; /* Daylight Saving not in affect (UTC) */
#ifdef _BSD_SOURCE
future.tm_zone = "UTC";
#endif
t = mktime( &future );
if ( -1 == t ) {
printf("Error converting 1 July 2038 to time_t time since Epoch\n");
return EXIT_FAILURE;
}
printf("UTC time and date: %s\n", asctime( &future ) );
return EXIT_SUCCESS;
}
다음 예를 사용하여 시도 할 수 있습니다.
#include <time.h>
#include <stdio.h>
int main(void)
{
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
printf("Local time and date: %s\n", asctime(local));
local = gmtime(&t);
printf("UTC time and date: %s\n", asctime(local));
return 0;
}
예상 결과를 제공해야합니다.
제휴하지 않습니다 StackOverflow