반응형

안녕하세요.

코딩을 하다 보면 시간에 관련된 함수를 많이 사용하게 됩니다.

항상 구글링으로 찾아보았었는데, 이번 기회에 한번 정리해봅니다.

공통으로 헤더 파일은 time.h를 포함시켜줘야 합니다.

 

time

현재시간을 time_t로 리턴합니다.

함수 원형

time_t time(time_t*  _Time);

반환형도 있고, 리턴 값도 있습니다. 리턴 값으로 받아도 되고, 파라미터에 time_t변수의 포인트를 넘겨서 받을 수도 있습니다.

time_t는 32비트 플랫폼에서는 long으로 64비트 플랫폼에서는 __int64로 typedef 되어 있습니다.

예제

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <stdio.h>
#include <time.h>
 
void main()
{
    time_t t = 0;
    t = time(NULL);        // time(&t);로 해도 같습니다.
    printf("%lld\n", t);
}
cs

 

localtime

time_t타입을 이용해서 tm구조체로 만듭니다.

tm구조체는 멤버 변수로 년, 월, 날, 시간, 분, 초가 있어서 현재시간을 명확하게 알 수 있게 해 줍니다.

 

함수 원형

struct tm * localtime(const time_t * _Time)

예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <stdio.h>
#include <time.h>
 
void main()
{
    struct tm* pTimeInfo;
    time_t t = time(NULL);
    pTimeInfo = localtime(&t);
 
    int year = pTimeInfo->tm_year + 1900;    //연도에는 1900 더해줌
    int month = pTimeInfo->tm_mon + 1;    // 월에는 1 더해줌
    int day = pTimeInfo->tm_mday;
    int hour = pTimeInfo->tm_hour;
    int min = pTimeInfo->tm_min;
    int sec = pTimeInfo->tm_sec;
 
    printf("%d-%.2d-%.2d %.2d:%.2d:%.2d\n", year, month, day, hour, min, sec);
}
cs

asctime

tm 구조체를 읽어 들여서 Www Mmm dd hh:mm:ss yyyy 형식으로 스트링 리턴합니다.

함수 원형

char * asctime(_In_ const struct tm * _Tm);

예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <stdio.h>
#include <time.h>
void main()
{
    struct tm* pTimeInfo;
    time_t t = time(NULL);
    pTimeInfo = localtime(&t);
 
    printf("%s", asctime(pTimeInfo));
}
cs

 

gmtime

time_t를 파라미터로 받아 UTC 시간을 리턴합니다.

함수 원형

struct tm * gmtime(const time_t * _Time)

예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <stdio.h>
#include <time.h>
 
void main()
{
    struct tm* pTimeInfo;
    time_t t = time(NULL);
    pTimeInfo = gmtime(&t);
 
    int year = pTimeInfo->tm_year + 1900;    //연도에는 1900 더해줌
    int month = pTimeInfo->tm_mon + 1;    // 월에는 1 더해줌
    int day = pTimeInfo->tm_mday;
    int hour = pTimeInfo->tm_hour;
    int min = pTimeInfo->tm_min;
    int sec = pTimeInfo->tm_sec;
 
    printf("UTC %d-%.2d-%.2d %.2d:%.2d:%.2d\n", year, month, day, hour, min, sec);
    hour = hour + 9;
    if (hour >= 24)
    {
        day++;
        hour -= 24;
    }
        
    printf("KST %d-%.2d-%.2d %.2d:%.2d:%.2d\n", year, month, day, hour, min, sec);
}
cs

 

mktime

tm구조체를 이용해서 time_t타입으로 만듭니다.

함수 원형

time_t mktime(struct tm * _Tm)

예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <stdio.h>
#include <time.h>
 
void main()
{
    struct tm* pTimeInfo;
    time_t t1 = time(NULL);
    pTimeInfo = localtime(&t1);
 
    pTimeInfo->tm_mday = pTimeInfo->tm_mday + 7;
 
    time_t t2 = mktime(pTimeInfo);
 
    printf("time : %d\n", t1);
    printf("7 days later %d\n", t2);
    printf("t2-t1 : %d\n", t2 - t1);
}
cs

현재 시간과 7일 후 시간의 차를 계산해보는 예제입니다.

1분을 초로 변환하면 60 

시간을 초로 변환하면 60 X 60 = 3,600

하루를 초로 변환하면 60 X 60 X 24= 86,400

7일을 초로 계산하면 60 X 60 X 24 X 7= 604,800

 

기타로 mktime함수를 실제로 구현해보았습니다.

특정 시간 time_t로 지정한 후 tm구조체에 넣은 후 다시 time_t로 변경해보았습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <stdio.h>
#include <time.h>
 
 
int GetMinToSec(int nMin);
int GetHourToSec(int nHour);
int GetDayToSec(int nDay);
int GetMonthToSec(int nMonth);
int GetYearToSec(int nYear);
void main()
{
    time_t t;
    struct tm* pTimeInfo;
    //t = time(NULL);
    t = 1620716583;
    pTimeInfo = localtime(&t);
 
    int year = pTimeInfo->tm_year + 1900;    //연도에는 1900 더해줌
    int month = pTimeInfo->tm_mon + 1;    // 월에는 1 더해줌
    int day = pTimeInfo->tm_mday;
    int hour = pTimeInfo->tm_hour;
    int min = pTimeInfo->tm_min;
    int sec = pTimeInfo->tm_sec;
 
    time_t nTime = sec + GetMinToSec(min) + GetHourToSec(hour - 9+ GetDayToSec(day) + GetMonthToSec(month - 1+ GetYearToSec(year - 1970);
 
    printf("%d-%.2d-%.2d %.2d:%.2d:%.2d\n", year, month, day, hour, min, sec);
    printf("%lld", nTime);
}
 
int GetMinToSec(int nMin)
{
    return nMin * 60;
}
int GetHourToSec(int nHour)
{
    return nHour * 3600;    // nHour * 60 * 60
}
int GetDayToSec(int nDay)
{
    return nDay * 86400;        // nDay * 60 * 60 * 24
}
int GetMonthToSec(int nMonth)
{
    int nMonthDay[12= { 312831303130313130313031 };    // Number of days per month
 
    int nMonthDayCount = 0;
 
    for (int i = 0; i < nMonth; i++)
        nMonthDayCount += nMonthDay[i];
 
    return nMonthDayCount * 86400;    // nMonthDayCount * 60 * 60 * 24
}
 
int GetYearToSec(int nYear)
{
    int nAddDay = nYear / 4;
 
    return  GetMonthToSec(12* nYear + nAddDay * 86400;    // GetMonthToSec(12) * nYear + nAddDay * 60 * 60 * 24
}
cs

지정한 1620716583과 같은 값이 출력되는 것을 확인할 수 있습니다.

반응형

+ Recent posts