본문 바로가기

C, C++

time 함수

int Api_leap(int year)

{

int month[] ={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if((year % 4 == 0) & (year % 100 != 0) || (year % 400 == 0)){

month[1] = 29;

}else{

month[1] = 28;

}


return month[1];

}



long long Api_Time(struct tm t)

{

#define HOUR 24

#define MIN 60

#define SEC 60

int month[] ={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int all_day = 0, minus_day;

int year = 0, mon = 0, a = 0, i = 0;

long long time_secs = 0;


for(i = 0; i < t.tm_year + 1900; ++i){

year += 365;

if(Api_leap(i) == 29){

year++;

}

}


for(i = 0; i < t.tm_mon; ++i){

mon += month[i];

}


if(Api_leap(t.tm_year+1900) == 29 && t.tm_mon > 2){

a = 1;

}

// 하루가 다 지난것은 아니기에 하루 minus

all_day = year + mon + t.tm_mday + a - 1;

// 1970년 1월 1일 기준으로 일수 삭감 

for(i = 0; i < 1970; ++i){

minus_day += 365;

if(Api_leap(i) == 29){

minus_day++;

}

}

time_secs = ((long long)(all_day-minus_day)) * HOUR * MIN * SEC; 

time_secs += t.tm_hour * MIN * SEC;

time_secs += t.tm_min * SEC;

time_secs += t.tm_sec;

// UTC 기준이기에 9시간 minus

time_secs -= 9 * MIN * SEC;


return time_secs;

}