計算從1970年到現在累計的秒數


沒啥技術含量,只不過是在沒事干,就把這個也記上,Windows下好像有這個api函數,但是在wince下用不了,所以還得自己封裝一個。大體代碼如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM