Time.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Windows/Time.h
  2. #ifndef __WINDOWS_TIME_H
  3. #define __WINDOWS_TIME_H
  4. #include "Common/Types.h"
  5. #include "Windows/Defs.h"
  6. namespace NWindows {
  7. namespace NTime {
  8. inline bool DosTimeToFileTime(UInt32 dosTime, FILETIME &fileTime)
  9. {
  10. return BOOLToBool(::DosDateTimeToFileTime(UInt16(dosTime >> 16),
  11. UInt16(dosTime & 0xFFFF), &fileTime));
  12. }
  13. const UInt32 kHighDosTime = 0xFF9FBF7D;
  14. const UInt32 kLowDosTime = 0x210000;
  15. inline bool FileTimeToDosTime(const FILETIME &fileTime, UInt32 &dosTime)
  16. {
  17. WORD datePart, timePart;
  18. if (!::FileTimeToDosDateTime(&fileTime, &datePart, &timePart))
  19. {
  20. if (fileTime.dwHighDateTime >= 0x01C00000) // 2000
  21. dosTime = kHighDosTime;
  22. else
  23. dosTime = kLowDosTime;
  24. return false;
  25. }
  26. dosTime = (((UInt32)datePart) << 16) + timePart;
  27. return true;
  28. }
  29. const UInt32 kNumTimeQuantumsInSecond = 10000000;
  30. const UInt64 kUnixTimeStartValue = ((UInt64)kNumTimeQuantumsInSecond) * 60 * 60 * 24 * 134774;
  31. inline void UnixTimeToFileTime(UInt32 unixTime, FILETIME &fileTime)
  32. {
  33. UInt64 v = kUnixTimeStartValue + ((UInt64)unixTime) * kNumTimeQuantumsInSecond;
  34. fileTime.dwLowDateTime = (DWORD)v;
  35. fileTime.dwHighDateTime = (DWORD)(v >> 32);
  36. }
  37. inline bool FileTimeToUnixTime(const FILETIME &fileTime, UInt32 &unixTime)
  38. {
  39. UInt64 winTime = (((UInt64)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
  40. if (winTime < kUnixTimeStartValue)
  41. {
  42. unixTime = 0;
  43. return false;
  44. }
  45. winTime = (winTime - kUnixTimeStartValue) / kNumTimeQuantumsInSecond;
  46. if (winTime > 0xFFFFFFFF)
  47. {
  48. unixTime = 0xFFFFFFFF;
  49. return false;
  50. }
  51. unixTime = (UInt32)winTime;
  52. return true;
  53. }
  54. }}
  55. #endif