//DateTime -> UnixTimestamp -> DateTime 변환

//DateTime -> UnixTimestamp -> DateTime 변환

 

#include<stdio.h>

#include<time.h>

 

int GetUnixTimeT(int year, int month, int day, int hour, int minute, int second)

{

  struct tm t = {0};

 

  t.tm_year = year - 1900;

  t.tm_mon = month - 1;

  t.tm_mday = day;

  t.tm_hour = hour;

  t.tm_min = minute;

  t.tm_sec = second;

 

  return(mktime(&t));

}

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

{

  time_t t = time(NULL);

  struct tm tm = *localtime(&t);

 

  int ____year = tm.tm_year + 1900;

  int ____month = tm.tm_mon = month + 1;

  int ____day = tm.tm_mday;

  int ____hour = tm.tm_hour;

  int ____minute = tm.tm_min;

  int ____second = tm.tm_sec;

 

  printf("DateTime(1):[%d-%d-%d %d:%d:%d]\n", ____year,

    ____month, ____day, ____hour, ____minute, ____second);

 

  time_t baseTime = GetUnixTimeT(____year,

            ____month, ____day, ____hour, ____minute, ____second);

 

  printf("UnixTimestamp:[%ld]\n", baseTime);

 

  struct tm *tmmm = (struct tm *)localtime(&baseTime);

 

  ____year = tmmm->tm_year + 1900;

  ____month = tmmm->tm_mon = month + 1;

  ____day = tmmm->tm_mday;

  ____hour = tmmm->tm_hour;

  ____minute = tmmm->tm_min;

  ____second = tmmm->tm_sec;

 

  printf("DateTime(2):[%d-%d-%d %d:%d:%d]\n", ____year,

    ____month, ____day, ____hour, ____minute, ____second);

 

  return(0);

}

 

 

 

 

  

+ Recent posts