计算现在到1970年过了多少秒 mktime函数 自己写的 ^_^

#include
#include

#include


#define MINUTE 60
#define HOUR (60*MINUTE)
#define DAY (24*HOUR)
#define YEAR (365*DAY)

//判断是不是闰年
int isYunNian(int year)
{
 if(((year%4==0) && (year%100!=0))||(year%400==0))
  return 1;
 else
  return 0;
}

//得出从1970 到现在闰年多出来的天数
//每4年多一天,每400年少3天,但是少于400年,就要另算
int daysInYunNian(int yearsBetween,int ceturyBetween)
{
 return  (yearsBetween/4 - ((ceturyBetween)/4*3+(ceturyBetween)%4-1));
 
}
//long型的数据只能保存六十多年的妙数不够用的,所以使用的是long long

long long getSecondsFromNowTo1970()
{
 long long llseconds = 0;
 
 struct tm *local;
   int year = 0;
 int mon = 0;
 int mday = 0;
 int hour = 0;
 int min = 0;
 int sec = 0;
 int years =0;
 int yunYueDays = 0;
 static int month[12] = {
  0,
  DAY*(31),
  DAY*(31+29),
  DAY*(31+29+31),
  DAY*(31+29+31+30),
  DAY*(31+29+31+30+31),
  DAY*(31+29+31+30+31+30),
  DAY*(31+29+31+30+31+30+31),
  DAY*(31+29+31+30+31+30+31+31),
  DAY*(31+29+31+30+31+30+31+31+30),
  DAY*(31+29+31+30+31+30+31+31+30+31),
  DAY*(31+29+31+30+31+30+31+31+30+31+30)
 };
 
 time_t t;
 t=time(NULL);

 local=localtime(&t);
 year = local->tm_year;
 mon = local->tm_mon;
 mday = local->tm_mday;
 hour = local->tm_hour;
 min = local->tm_min;
 sec = local->tm_sec;
 years = year-70;
 yunYueDays = daysInYunNian(years+2,((year+1900)/100 - 1970/100));
 
 
 llseconds = YEAR*(long long)years + DAY*(long long)yunYueDays;
 
 llseconds += month[mon];
 llseconds += DAY*(mday-1); // 再加上本月过去的天数的秒数时间。
 llseconds += HOUR*hour; // 再加上当天过去的小时数的秒数时间。
 llseconds += MINUTE*min; // 再加上1 小时内过去的分钟数的秒数时间。
 llseconds += sec;
 
 if (mon>1 && !isYunNian(year))
  llseconds -= DAY;
 //因为时制的问题快了八个小时要减去(北京时区)
 llseconds -= 8*HOUR; 
 
 
 printf("the year of now is %d/n",local->tm_year);
 printf("the month of now is %d/n",local->tm_mon);
 printf("the mday of now is %d/n",local->tm_mday);
 printf("the hour of now is %d/n",local->tm_hour);
 printf("the sec of now is %d/n",local->tm_sec);
 
 printf("~~~~~the secs of now between 70  is %ld/n",t);
 printf("~~~~~the secs of now between 70  is %lld/n",llseconds);
 
 
 printf("__LONG_MAX__ is %ld/n",__LONG_MAX__);
 printf("__INT_MAX__ is %ld/n",__INT_MAX__);
 printf("__LONG_LONG_MAX__ is %lld/n",__LONG_LONG_MAX__);
 printf("DBL_MAX is %0.10lf/n",DBL_MAX);
 
 return 0;
}

int main()
{
 getSecondsFromNowTo1970(); 
 return 0;
}


 

你可能感兴趣的:(linux)