mktime()을 사용하기 전에 1970을 빼고 값을 넣어준다면
localtime()으로 struct tm 형으로 받을 때에는 ts->tm_year + 1970 으로 출력을 해주어야 한다.
다른 시스템과의 혼용을 하지 않은다면은 1970을 빼지 않고 사용해도 되지만
유닉스 / 리눅스 시스템과 시간을 같이 사용하기 위해서는 1970 epoch를 계산해주어야 한다.
#include "stdio.h"
#include "time.h"
int main(void)
{
time_t now;
struct tm tmtm;
struct tm *ts;
char buf[80];
/* Get the current time */
//now = time(NULL);
time(&now);
/* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz" */
ts = localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", ts);
printf("%s\n", buf);
printf("%d\n",ts->tm_year);
return 0;
}
|
2009/12/09 - [Linux] - 시간관련 함수/구조체 - time API on linux
---
혹시나 해서 찾아봤는데 시스템에 따라서는 epoch가 1900일수도 있다고 한다.
유닉스 계열은 1970 / NTP에서는 1900을 epoch로 사용한다.
'Linux' 카테고리의 다른 글
| 리눅스를 위한 아이튠스 서버 만들기 (0) | 2012.07.08 |
|---|---|
| G840 cpuinfo (0) | 2012.04.14 |
| partitionless disk (2) | 2012.01.06 |
| sudo와 selinux (0) | 2011.12.25 |
| 조이스틱 / 조이패드 on ubuntu (2) | 2011.12.23 |