#include <time.h>
time_t time( time_t *inTT ) {
SYSTEMTIME sysTimeStruct;
FILETIME fTime;
ULARGE_INTEGER int64time;
time_t locTT = 0;
if ( inTT == NULL ) {
inTT = &locTT;
}
GetSystemTime( &sysTimeStruct );
if ( SystemTimeToFileTime( &sysTimeStruct, &fTime ) ) {
memcpy( &int64time, &fTime, sizeof( FILETIME ) );
/* Subtract the value for 1970-01-01 00:00 (UTC) */
int64time.QuadPart -= 0x19db1ded53e8000;
/* Convert to seconds. */
int64time.QuadPart /= 10000000;
*inTT = int64time.QuadPart;
}
return *inTT;
}
class date
{
int year;
int month;
int day;
public:
date(int y,int m,int d)
{
year=y;month=m;day=d;
}
void disp()
{
printf("%d.%d.%d",year,month,day );
}
friend int count_day(date &d,int);
friend int leap(int year);
friend int subs(date d1,date d2);
};
static int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int count_day(date &d,int flag)
{
int p,i,s;
if(leap(d.year))
p=1;
else
p=0;
if(flag)
{
s=d.day;
for(i=1;i<d.month;i++)
s+=day_tab[p][i-1];
}
else
{
s=day_tab[p][d.month]-d.day;
for (i=d.month+1;i<=12;i++)
s+=day_tab[p][i-1];
}
return(s);
}
int leap(int year)
{
if(year%4==0 && year%100!=0 || year%400==0)
return 1;
else
return 0;
}
int subs(date d1,date d2)
{
int days,day1,day2,y;
if(d1.year<d2.year)
{
days=count_day(d1,0);
for(y=d1.year+1;y<d2.year;y++)
if(leap(y))
days+=366L;
else
days+=365L;
days+=count_day(d2,1);
}
else if (d1.year==d2.year)
{
day1=count_day(d1,1);
day2=count_day(d2,1);
days=day2-day1;
}
else
{
days=count_day(d2,0);
for(y=d2.year+1;y<d1.year;y++)
if(leap(y))
days+=366L;
else
days+=365L;
days+=count_day(d1,1);
}
return days;
}
struct tm mylocaltime(time_t &t)
{
struct tm ret_tm;
int days = t/60/60/24;
int subsecs=t-(days*24*60*60);
//int years= days/365;
int years=0;
int i=365;
while(i<days)
{
years++;
if(leap(1970+years))
i+=1;
i+=365;
}
ret_tm.tm_year=years;
ret_tm.tm_mon=0;
int subdays = days-(i-365);
int nMonCounter=0;
int nMons=0;
int isleap=leap(1970+years);
while(nMonCounter<subdays)
{
ret_tm.tm_mon++;
nMonCounter+=day_tab[isleap][nMons];
if(nMonCounter + day_tab[isleap][nMons+1]>subdays)
break;
nMons++;
}
ret_tm.tm_mday=subdays-nMonCounter;
ret_tm.tm_mday+=1;
TIME_ZONE_INFORMATION zi;
GetTimeZoneInformation(&zi);
ret_tm.tm_hour=subsecs/3600 - zi.Bias/60;
int subsecs2 = subsecs - ((int)(subsecs/3600))*3600;
ret_tm.tm_min = subsecs2/60;
ret_tm.tm_sec = subsecs2- ((int)(subsecs2/60))*60;
return ret_tm;
}