annotate src/win32/7zip/7z/CPP/Windows/Time.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 // Windows/Time.cpp
rlm@1 2
rlm@1 3 #include "StdAfx.h"
rlm@1 4
rlm@1 5 #include "Time.h"
rlm@1 6 #include "Windows/Defs.h"
rlm@1 7
rlm@1 8 namespace NWindows {
rlm@1 9 namespace NTime {
rlm@1 10
rlm@1 11 bool DosTimeToFileTime(UInt32 dosTime, FILETIME &fileTime)
rlm@1 12 {
rlm@1 13 return BOOLToBool(::DosDateTimeToFileTime((UInt16)(dosTime >> 16), (UInt16)(dosTime & 0xFFFF), &fileTime));
rlm@1 14 }
rlm@1 15
rlm@1 16 static const UInt32 kHighDosTime = 0xFF9FBF7D;
rlm@1 17 static const UInt32 kLowDosTime = 0x210000;
rlm@1 18
rlm@1 19 bool FileTimeToDosTime(const FILETIME &fileTime, UInt32 &dosTime)
rlm@1 20 {
rlm@1 21 WORD datePart, timePart;
rlm@1 22 if (!::FileTimeToDosDateTime(&fileTime, &datePart, &timePart))
rlm@1 23 {
rlm@1 24 dosTime = (fileTime.dwHighDateTime >= 0x01C00000) ? kHighDosTime : kLowDosTime;
rlm@1 25 return false;
rlm@1 26 }
rlm@1 27 dosTime = (((UInt32)datePart) << 16) + timePart;
rlm@1 28 return true;
rlm@1 29 }
rlm@1 30
rlm@1 31 static const UInt32 kNumTimeQuantumsInSecond = 10000000;
rlm@1 32 static const UInt64 kUnixTimeStartValue = ((UInt64)kNumTimeQuantumsInSecond) * 60 * 60 * 24 * 134774;
rlm@1 33
rlm@1 34 void UnixTimeToFileTime(UInt32 unixTime, FILETIME &fileTime)
rlm@1 35 {
rlm@1 36 UInt64 v = kUnixTimeStartValue + ((UInt64)unixTime) * kNumTimeQuantumsInSecond;
rlm@1 37 fileTime.dwLowDateTime = (DWORD)v;
rlm@1 38 fileTime.dwHighDateTime = (DWORD)(v >> 32);
rlm@1 39 }
rlm@1 40
rlm@1 41 bool FileTimeToUnixTime(const FILETIME &fileTime, UInt32 &unixTime)
rlm@1 42 {
rlm@1 43 UInt64 winTime = (((UInt64)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
rlm@1 44 if (winTime < kUnixTimeStartValue)
rlm@1 45 {
rlm@1 46 unixTime = 0;
rlm@1 47 return false;
rlm@1 48 }
rlm@1 49 winTime = (winTime - kUnixTimeStartValue) / kNumTimeQuantumsInSecond;
rlm@1 50 if (winTime > 0xFFFFFFFF)
rlm@1 51 {
rlm@1 52 unixTime = 0xFFFFFFFF;
rlm@1 53 return false;
rlm@1 54 }
rlm@1 55 unixTime = (UInt32)winTime;
rlm@1 56 return true;
rlm@1 57 }
rlm@1 58
rlm@1 59 bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
rlm@1 60 unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds)
rlm@1 61 {
rlm@1 62 resSeconds = 0;
rlm@1 63 if (year < 1601 || year >= 10000 || month < 1 || month > 12 ||
rlm@1 64 day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59)
rlm@1 65 return false;
rlm@1 66 UInt32 numYears = year - 1601;
rlm@1 67 UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400;
rlm@1 68 Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
rlm@1 69 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
rlm@1 70 ms[1] = 29;
rlm@1 71 month--;
rlm@1 72 for (unsigned i = 0; i < month; i++)
rlm@1 73 numDays += ms[i];
rlm@1 74 numDays += day - 1;
rlm@1 75 resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec;
rlm@1 76 return true;
rlm@1 77 }
rlm@1 78
rlm@1 79 void GetCurUtcFileTime(FILETIME &ft)
rlm@1 80 {
rlm@1 81 SYSTEMTIME st;
rlm@1 82 GetSystemTime(&st);
rlm@1 83 SystemTimeToFileTime(&st, &ft);
rlm@1 84 }
rlm@1 85
rlm@1 86 }}