#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
struct tm * my_localtime_r(const time_t *srctime,struct tm *tm_time)
{
long int n32_Pass4year,n32_hpery;
const static char Days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const static int ONE_YEAR_HOURS = 8760;
time_t time = *srctime;
time=time+28800;
tm_time->tm_isdst=0;
if(time < 0)
{
time = 0;
}
tm_time->tm_sec=(int)(time % 60);
time /= 60;
tm_time->tm_min=(int)(time % 60);
time /= 60;
tm_time->tm_wday=(time/24+4)%7;
n32_Pass4year=((unsigned int)time / (1461L * 24L));
tm_time->tm_year=(n32_Pass4year << 2)+70;
time %= 1461L * 24L;
tm_time->tm_yday=(time/24)%365;
for (;;)
{
n32_hpery = ONE_YEAR_HOURS;
if ((tm_time->tm_year & 3) == 0)
{
n32_hpery += 24;
}
if (time < n32_hpery)
{
break;
}
tm_time->tm_year++;
time -= n32_hpery;
}
tm_time->tm_hour=(int)(time % 24);
time /= 24;
time++;
if ((tm_time->tm_year & 3) == 0)
{
if (time > 60)
{
time--;
}
else
{
if (time == 60)
{
tm_time->tm_mon = 1;
tm_time->tm_mday = 29;
return tm_time;
}
}
}
for (tm_time->tm_mon = 0;Days[tm_time->tm_mon] < time;tm_time->tm_mon++)
{
time -= Days[tm_time->tm_mon];
}
tm_time->tm_mday = (int)(time);
return tm_time;
}
int main(){
char tmp[25] = {0};
time_t tt = time(NULL);
struct tm t;
printf("tt is %d\n",tt);
my_localtime_r(&tt,&t);
printf("%d-%02d-%02d %02d:%02d:%02d\t",
t.tm_year + 1900,
t.tm_mon + 1,
t.tm_mday,
t.tm_hour,
t.tm_min,
t.tm_sec);
return 0;
}