view src/win32/7zip/7z/CPP/Windows/Time.cpp @ 5:8fe0c57e53d2

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