'programming'에 해당되는 글 387건

  1. 2017.08.09 time_t 구조체, mktime(), localtime() 함수

반응형

 


1) time_t 구조체

time_t는 결과적으로 long int를 나타낸다.


2) mktime()을 이용하여 time_t 구조체로 변경하고
   time_t 값을 localtime() 함수를 이용하여 tm 구조체로 변경.


//           struct tm {
//             int tm_sec;         /* seconds */
//             int tm_min;         /* minutes */
//             int tm_hour;        /* hours */
//             int tm_mday;        /* day of the month */
//             int tm_mon;         /* month */
//             int tm_year;        /* year */
//             int tm_wday;        /* day of the week */
//             int tm_yday;        /* day in the year */
//             int tm_isdst;       /* daylight saving time */
//         };

//         time.h:typedef __time_t time_t;
//         time.h:# include <bits/types.h> /* This defines __time_t for us.  */

//         types.h:# define __STD_TYPE             typedef
//         types.h:__STD_TYPE __TIME_T_TYPE __time_t;      /* Seconds since the Epoch.  */
//         typesizes.h:#define __TIME_T_TYPE               __SLONGWORD_TYPE
//         types.h:#define __SLONGWORD_TYPE        long int
//
//         result
//         YYYY=2017 MM=8 DD=9 hh=10 mi=30 ss=45
//         nLTime=1502242245
//         nLTime=1502242245
//         YYYY=2017 MM=8 DD=9 hh=10 mi=30 ss=45
//

#include <stdio.h>
#include <time.h>

time_t MakeTimeT(int YYYY, int MM, int DD, int hh, int mi, int ss);

int main(int argc, char *argv[])
{

    int YYYY=2017;
    int MM=8;
    int DD=9;
    int hh=10;
    int mi=30;
    int ss=45;

    time_t nLTime;

    struct tm* pLocalTime;

    nLTime = MakeTimeT(YYYY, MM, DD, hh, mi, ss);

    fprintf(stdout, "YYYY=%d MM=%d DD=%d hh=%d mi=%d ss=%d\n", YYYY, MM, DD, hh, mi, ss);
    fprintf(stdout, "nLTime=%ld\n", nLTime);

    pLocalTime = localtime(&nLTime);

    fprintf(stdout, "nLTime=%ld\n", nLTime);
    
    fprintf(stdout, "YYYY=%d MM=%d DD=%d hh=%d mi=%d ss=%d\n", pLocalTime->tm_year + 1900, pLocalTime->tm_mon + 1, pLocalTime->tm_mday, pLocalTime->tm_hour, pLocalTime->tm_min, pLocalTime->tm_sec);

    return 1;
}


time_t MakeTimeT(int YYYY, int MM, int DD, int hh, int mi, int ss)
{
struct tm st_tm;

st_tm.tm_year = YYYY - 1900;
st_tm.tm_mon =  MM - 1;
st_tm.tm_mday = DD;
st_tm.tm_hour = hh;
st_tm.tm_min =  mi;
st_tm.tm_sec =  ss;

return mktime( &st_tm );

}


 

반응형
Posted by 공간사랑
,