Unix UTC时间转化为本地时间的一个MFC实现

  1 #define YEAR_BEGIN      1970
  2 #define LEAPYEAR_DAY    366
  3 #define NORMALYEAR_DAY  365
  4 #define JANUARY         31
  5 #define MARCH           31
  6 #define APRIL           30
  7 #define MAY             31
  8 #define JUNE            30
  9 #define JULY            31
 10 #define AUGUST          31
 11 #define SEPTEMBER       30
 12 #define OCTOBER         31
 13 #define NOVEMBER        30
 14 #define DECEMBER        31
 15 
 16 bool CXXXClass::IsLeapYear(DWORD year)
 17 {
 18     return (((year % 4 == 0) && (year %100 != 0)) || (year % 400 == 0));
 19 }
 20 
 21 bool CXXXClass::UtcTimeToLocalTime(DWORD dwUtc, CString &strUtc)
 22 {
 23     int tmp_day;
 24     DWORD year, month, day;
 25     DWORD sec, mins, hour;
 26     TIME_ZONE_INFORMATION tzi;
 27     ZeroMemory(&tzi, sizeof(TIME_ZONE_INFORMATION));
 28     CString strTimeZone = _T("");
 29     DWORD dwTimeZoneID = GetTimeZoneInformation(&tzi);
 30     if (dwTimeZoneID == TIME_ZONE_ID_UNKNOWN) {
 31         dwUtc -= (tzi.Bias * 60);
 32         strTimeZone = tzi.StandardName;
 33     } else if (dwTimeZoneID == TIME_ZONE_ID_STANDARD) {
 34         dwUtc -= (tzi.Bias * 60 + tzi.StandardBias * 60);
 35         strTimeZone = tzi.StandardName;
 36     } else if ( dwTimeZoneID == TIME_ZONE_ID_DAYLIGHT) {
 37         dwUtc -= (tzi.Bias * 60 + tzi.DaylightBias * 60);
 38         strTimeZone = tzi.DaylightName;
 39     }
 40     hour = dwUtc / 3600;
 41     sec = dwUtc % 3600;
 42     mins = sec / 60;
 43     sec %= 60;
 44 
 45     tmp_day = hour / 24 + 1;
 46     hour %= 24;
 47 
 48     year = YEAR_BEGIN;
 49     while(1) {
 50         if(tmp_day < LEAPYEAR_DAY)
 51             break;
 52         if(IsLeapYear(year)) {
 53             tmp_day -= LEAPYEAR_DAY;
 54         } else {
 55             tmp_day -= NORMALYEAR_DAY;
 56         }
 57         year++;
 58     }
 59 
 60     DWORD feb = IsLeapYear(year) ? 29 : 28;
 61     day = 1;
 62     month = 1;
 63 
 64     for(int i = 0; i < 12; i++) {
 65         day = tmp_day;
 66         switch(i) {
 67             case 0:
 68                 tmp_day -= JANUARY;
 69                 break;
 70             case 1:
 71                 tmp_day -= feb;
 72                 break;
 73             case 2:
 74                 tmp_day -= MARCH;
 75                 break;
 76             case 3:
 77                 tmp_day -= APRIL;
 78                 break;
 79             case 4:
 80                 tmp_day -= MAY;
 81                 break;
 82             case 5:
 83                 tmp_day -= JUNE;
 84                 break;
 85             case 6:
 86                 tmp_day -= JULY;
 87                 break;
 88             case 7:
 89                 tmp_day -= AUGUST;
 90                 break;
 91             case 8:
 92                 tmp_day -= SEPTEMBER;
 93                 break;
 94             case 9:
 95                 tmp_day -= OCTOBER;
 96                 break;
 97             case 10:
 98                 tmp_day -= NOVEMBER;
 99                 break;
100             case 11:
101                 tmp_day -= DECEMBER;
102                 break;
103             default:
104                 break;
105         }
106         if(tmp_day > 0)
107             month++;
108         else
109             break;
110     }
111     if(strTimeZone.IsEmpty())
112         strUtc.Format(_T("%d-%d-%d %d:%2d:%2d"), year, month, day, hour, mins, sec);
113     else
114         strUtc.Format(_T("%s %d-%d-%d %d:%2d:%2d"), (LPCTSTR)strTimeZone, year, month, day, hour, mins, sec);
115 
116     return true;
117 }

需要做一个获取Android编译生成时间的功能,网上找了些,都不好用,于是自己写了个。现在放上来,做个笔记。

posted @ 2012-10-08 11:53  Jojodru  阅读(927)  评论(0编辑  收藏  举报