C – time, difftime, localtime, gmtime, gettimeofday
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
time
time()
함수는 현재 시간을 1970-01-01 00:00:00 UTC
로부터 경과된 초(second) 를 반환한다.
리턴값으로도 반환하고, 동시에 참조로 전달한 파라미터에도 입력해 준다.
#include <stdio.h>
#include <time.h>
int main() {
time_t seconds;
printf("since 1970-01-01 00:00:00 : %ld (sec)\n", time(&seconds));
printf("seconds: %ld\n", seconds);
return 0;
}
difftime
두 시간의 차이값을 반환한다.(seconds)
window 에는 Sleep() 함수를 써야한다.
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main() {
time_t start, end;
time(&start);
sleep(3);
time(&end);
printf("time diff%.2lf (sec)\n", difftime(end, start));
return 0;
}
localtime, gmtime, localtime_r
struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};
함수명 localtime()
에 맞게 UTC 가 아닌 지역시간(KST) 로 시간을 반환한다.
UTC 가 필요한 경우 gmtime()
을 사용한다.
연도는 1900년도부터 경과된 년도를 반환하므로 1900 을 더해 주어야 한다.
월 또한 0월부터 시작하므로 1을 더해 주어야 한다.
멀티스레드 환경에서는 localtime_r() 을 사용한다.
#include <stdio.h>
#include <time.h>
int main() {
time_t now;
struct tm *t_now, t_now_multithread;
time(&now);
t_now = (struct tm *) localtime(&now);
printf("year : %d, month : %d, day : %d, hour : %d\n", (t_now->tm_year + 1900), (t_now->tm_mon + 1), t_now->tm_mday, t_now->tm_hour);
localtime_r(&now, &t_now_multithread);
printf("year : %d, month : %d, day : %d, hour : %d\n", (t_now_multithread.tm_year + 1900), (t_now_multithread.tm_mon + 1), t_now_multithread.tm_mday, t_now_multithread.tm_hour);
return 0;
}
localtime 오류
멀티스레드 환경이 아니더라도, 변수를 두개 이상 동시에 사용하려면 localtime_r() 을 사용해야 한다.
char start_dt_str[30];
char end_dt_str[30];
struct tm *start_dt_tm_info = localtime(&client_list[i].start_dt);
struct tm *end_dt_tm_info = localtime(&client_list[i].end_dt);
strftime(start_dt_str, sizeof(start_dt_str), "%Y-%m-%d %H:%M:%S", start_dt_tm_info);
strftime(end_dt_str, sizeof(end_dt_str), "%Y-%m-%d %H:%M:%S", end_dt_tm_info);
printf("%s ~ %s\n", start_dt_str, end_dt_str);
localtime 은 하나의 변수로 값을 반환하므로 두개의 포인터는 동일한 데이타를 바라보게 된다.
2025-04-01 15:28:13 ~ 2025-04-01 15:28:13
char start_dt_str[30];
char end_dt_str[30];
struct tm start_dt_tm_info;
struct tm end_dt_tm_info;
// localtime_r 사용 (스레드 안전 버전)
localtime_r(&client_list[i].start_dt, &start_dt_tm_info);
localtime_r(&client_list[i].end_dt, &end_dt_tm_info);
strftime(start_dt_str, sizeof(start_dt_str), "%Y-%m-%d %H:%M:%S", &start_dt_tm_info);
strftime(end_dt_str, sizeof(end_dt_str), "%Y-%m-%d %H:%M:%S", &end_dt_tm_info);
printf("%s ~ %s\n", start_dt_str, end_dt_str);
gettimeofday (milisecond 구하기)
window 에는 gettimeofday() 함수가 없다.
gettimeofday() 의 두번째 파라미터는 무시된다.
즉, NULL 이외의 다른 값이 입력되어도 작동하지 않는다.
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main() {
struct timeval now;
struct tm *t_now;
gettimeofday(&now, NULL);
t_now = localtime(&now.tv_sec);
float miliseconds = t_now->tm_sec + now.tv_usec / 1000000.0;
printf("miliseconds : %0.3f\n", miliseconds);
return 0;
}
strptime (문자열에서 시간 구하기)
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main() {
struct tm api_tm;
strptime("2022-03-25 14:42:24", "%Y-%m-%d %H:%M:%S", &api_tm);
return 0;
}
struct tm to time_t
초기화를 안해주면 오작동할 수 있다.
값을 설정하지 않은 모든 필드는 랜덤값이 할당되고,
그로인해 mktime() 이 -1 을 return 할 수 있다.
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
int main() {
struct tm api_tm = {0}; // 초가화하는 것이 중요
strptime("2022-03-25 14:42:24", "%Y-%m-%d %H:%M:%S", &api_tm);
// localtime 을 기준으로 변환합니다.
time_t api_t = mktime(&api_tm);
return 0;
}
mktime
https://www.it-note.kr/143
%H:%M:%S
%Y-%m-%d
%H:%M:%
https://www.it-note.kr/m/145