Home | History | Annotate | Download | only in Windows
      1 // Windows/Time.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include "Windows/Defs.h"
      6 
      7 #include "Time.h"
      8 
      9 namespace NWindows {
     10 namespace NTime {
     11 
     12 static const UInt32 kNumTimeQuantumsInSecond = 10000000;
     13 static const UInt32 kFileTimeStartYear = 1601;
     14 static const UInt32 kDosTimeStartYear = 1980;
     15 static const UInt32 kUnixTimeStartYear = 1970;
     16 static const UInt64 kUnixTimeStartValue = ((UInt64)kNumTimeQuantumsInSecond) *
     17     60 * 60 * 24 * (89 + 365 * (kUnixTimeStartYear - kFileTimeStartYear));
     18 
     19 bool DosTimeToFileTime(UInt32 dosTime, FILETIME &ft)
     20 {
     21   #if defined(_WIN32) && !defined(UNDER_CE)
     22   return BOOLToBool(::DosDateTimeToFileTime((UInt16)(dosTime >> 16), (UInt16)(dosTime & 0xFFFF), &ft));
     23   #else
     24   ft.dwLowDateTime = 0;
     25   ft.dwHighDateTime = 0;
     26   UInt64 res;
     27   if (!GetSecondsSince1601(kDosTimeStartYear + (dosTime >> 25), (dosTime >> 21) & 0xF, (dosTime >> 16) & 0x1F,
     28       (dosTime >> 11) & 0x1F, (dosTime >> 5) & 0x3F, (dosTime & 0x1F) * 2, res))
     29     return false;
     30   res *= kNumTimeQuantumsInSecond;
     31   ft.dwLowDateTime = (UInt32)res;
     32   ft.dwHighDateTime = (UInt32)(res >> 32);
     33   return true;
     34   #endif
     35 }
     36 
     37 static const UInt32 kHighDosTime = 0xFF9FBF7D;
     38 static const UInt32 kLowDosTime = 0x210000;
     39 
     40 #define PERIOD_4 (4 * 365 + 1)
     41 #define PERIOD_100 (PERIOD_4 * 25 - 1)
     42 #define PERIOD_400 (PERIOD_100 * 4 + 1)
     43 
     44 bool FileTimeToDosTime(const FILETIME &ft, UInt32 &dosTime)
     45 {
     46   #if defined(_WIN32) && !defined(UNDER_CE)
     47 
     48   WORD datePart, timePart;
     49   if (!::FileTimeToDosDateTime(&ft, &datePart, &timePart))
     50   {
     51     dosTime = (ft.dwHighDateTime >= 0x01C00000) ? kHighDosTime : kLowDosTime;
     52     return false;
     53   }
     54   dosTime = (((UInt32)datePart) << 16) + timePart;
     55 
     56   #else
     57 
     58   unsigned year, mon, day, hour, min, sec;
     59   UInt64 v64 = ft.dwLowDateTime | ((UInt64)ft.dwHighDateTime << 32);
     60   Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     61   unsigned temp;
     62   UInt32 v;
     63   v64 += (kNumTimeQuantumsInSecond * 2 - 1);
     64   v64 /= kNumTimeQuantumsInSecond;
     65   sec = (unsigned)(v64 % 60);
     66   v64 /= 60;
     67   min = (unsigned)(v64 % 60);
     68   v64 /= 60;
     69   hour = (unsigned)(v64 % 24);
     70   v64 /= 24;
     71 
     72   v = (UInt32)v64;
     73 
     74   year = (unsigned)(kFileTimeStartYear + v / PERIOD_400 * 400);
     75   v %= PERIOD_400;
     76 
     77   temp = (unsigned)(v / PERIOD_100);
     78   if (temp == 4)
     79     temp = 3;
     80   year += temp * 100;
     81   v -= temp * PERIOD_100;
     82 
     83   temp = v / PERIOD_4;
     84   if (temp == 25)
     85     temp = 24;
     86   year += temp * 4;
     87   v -= temp * PERIOD_4;
     88 
     89   temp = v / 365;
     90   if (temp == 4)
     91     temp = 3;
     92   year += temp;
     93   v -= temp * 365;
     94 
     95   if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
     96     ms[1] = 29;
     97   for (mon = 1; mon <= 12; mon++)
     98   {
     99     unsigned s = ms[mon - 1];
    100     if (v < s)
    101       break;
    102     v -= s;
    103   }
    104   day = (unsigned)v + 1;
    105 
    106   dosTime = kLowDosTime;
    107   if (year < kDosTimeStartYear)
    108     return false;
    109   year -= kDosTimeStartYear;
    110   dosTime = kHighDosTime;
    111   if (year >= 128)
    112     return false;
    113   dosTime = (year << 25) | (mon << 21) | (day << 16) | (hour << 11) | (min << 5) | (sec >> 1);
    114   #endif
    115   return true;
    116 }
    117 
    118 void UnixTimeToFileTime(UInt32 unixTime, FILETIME &ft)
    119 {
    120   UInt64 v = kUnixTimeStartValue + ((UInt64)unixTime) * kNumTimeQuantumsInSecond;
    121   ft.dwLowDateTime = (DWORD)v;
    122   ft.dwHighDateTime = (DWORD)(v >> 32);
    123 }
    124 
    125 bool FileTimeToUnixTime(const FILETIME &ft, UInt32 &unixTime)
    126 {
    127   UInt64 winTime = (((UInt64)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
    128   if (winTime < kUnixTimeStartValue)
    129   {
    130     unixTime = 0;
    131     return false;
    132   }
    133   winTime = (winTime - kUnixTimeStartValue) / kNumTimeQuantumsInSecond;
    134   if (winTime > 0xFFFFFFFF)
    135   {
    136     unixTime = 0xFFFFFFFF;
    137     return false;
    138   }
    139   unixTime = (UInt32)winTime;
    140   return true;
    141 }
    142 
    143 bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
    144   unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds)
    145 {
    146   resSeconds = 0;
    147   if (year < kFileTimeStartYear || year >= 10000 || month < 1 || month > 12 ||
    148       day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59)
    149     return false;
    150   UInt32 numYears = year - kFileTimeStartYear;
    151   UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400;
    152   Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    153   if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
    154     ms[1] = 29;
    155   month--;
    156   for (unsigned i = 0; i < month; i++)
    157     numDays += ms[i];
    158   numDays += day - 1;
    159   resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec;
    160   return true;
    161 }
    162 
    163 void GetCurUtcFileTime(FILETIME &ft)
    164 {
    165   SYSTEMTIME st;
    166   GetSystemTime(&st);
    167   SystemTimeToFileTime(&st, &ft);
    168 }
    169 
    170 }}
    171