【原创】计算从1970年到现在累计的秒数

没啥技术含量,只不过是在没事干,就把这个也记上,Windows下好像有这个api函数,但是在wince下用不了,所以还得自己封装一个。大体代码如下:

/*the seconds of round year = 3600*24*366 */
#define SECONDOFROUNDYEAR 31622400

/*the seconds of general year = 3600*24*365 */
#define SECONDOFYEAR 31536000

unsigned int SecondsFrom1970()
{
	SYSTEMTIME st;
	unsigned int tTemp=0;
	unsigned int tSecond=0;
	int month_s[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 nDays=0;
	int nCount=0;
	int i;
	int j;

	GetLocalTime(&st);
	tSecond=st.wHour*3600+st.wMinute*60+st.wSecond;

	for (i=1970;i<st.wYear;++i)
	{
		if (IsRound(i))
			++nCount;
	}

	tTemp+=(st.wYear-1970-nCount)*SECONDOFYEAR+nCount*SECONDOFROUNDYEAR;

	if (st.wMonth>1)
	{
		if (IsRound(st.wYear))
		{
			for (j=0;j<st.wMonth-1;++j)
			{
				tTemp+=month_s[1][j]*MAXSECONDOFDAY;
			}
			tTemp+=(st.wDay-1)*MAXSECONDOFDAY+tSecond;
		}
		else
		{
			for (j=0;j<st.wMonth-1;++j)
			{
				tTemp+=month_s[0][j]*MAXSECONDOFDAY;
			}
			tTemp+=(st.wDay-1)*MAXSECONDOFDAY+tSecond;
		}
	}
	else
	{
		tTemp+=(st.wDay-1)*MAXSECONDOFDAY+tSecond;
	}

	return tTemp;
}

bool IsRound(int year)
{
	/*is round year?*/
	if((year%100)&&(year%4==0)) return 1;
	if((year%100==0)&&(year%400==0)) return 1;
	return 0;
}

posted @ 2010-09-10 16:44  天堂大鸟  阅读(4943)  评论(3编辑  收藏  举报