diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/Windows/Time.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,86 @@
     1.4 +// Windows/Time.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "Time.h"
     1.9 +#include "Windows/Defs.h"
    1.10 +
    1.11 +namespace NWindows {
    1.12 +namespace NTime {
    1.13 +
    1.14 +bool DosTimeToFileTime(UInt32 dosTime, FILETIME &fileTime)
    1.15 +{
    1.16 +  return BOOLToBool(::DosDateTimeToFileTime((UInt16)(dosTime >> 16), (UInt16)(dosTime & 0xFFFF), &fileTime));
    1.17 +}
    1.18 +
    1.19 +static const UInt32 kHighDosTime = 0xFF9FBF7D;
    1.20 +static const UInt32 kLowDosTime = 0x210000;
    1.21 +
    1.22 +bool FileTimeToDosTime(const FILETIME &fileTime, UInt32 &dosTime)
    1.23 +{
    1.24 +  WORD datePart, timePart;
    1.25 +  if (!::FileTimeToDosDateTime(&fileTime, &datePart, &timePart))
    1.26 +  {
    1.27 +    dosTime = (fileTime.dwHighDateTime >= 0x01C00000) ? kHighDosTime : kLowDosTime;
    1.28 +    return false;
    1.29 +  }
    1.30 +  dosTime = (((UInt32)datePart) << 16) + timePart;
    1.31 +  return true;
    1.32 +}
    1.33 +
    1.34 +static const UInt32 kNumTimeQuantumsInSecond = 10000000;
    1.35 +static const UInt64 kUnixTimeStartValue = ((UInt64)kNumTimeQuantumsInSecond) * 60 * 60 * 24 * 134774;
    1.36 +
    1.37 +void UnixTimeToFileTime(UInt32 unixTime, FILETIME &fileTime)
    1.38 +{
    1.39 +  UInt64 v = kUnixTimeStartValue + ((UInt64)unixTime) * kNumTimeQuantumsInSecond;
    1.40 +  fileTime.dwLowDateTime = (DWORD)v;
    1.41 +  fileTime.dwHighDateTime = (DWORD)(v >> 32);
    1.42 +}
    1.43 +
    1.44 +bool FileTimeToUnixTime(const FILETIME &fileTime, UInt32 &unixTime)
    1.45 +{
    1.46 +  UInt64 winTime = (((UInt64)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
    1.47 +  if (winTime < kUnixTimeStartValue)
    1.48 +  {
    1.49 +    unixTime = 0;
    1.50 +    return false;
    1.51 +  }
    1.52 +  winTime = (winTime - kUnixTimeStartValue) / kNumTimeQuantumsInSecond;
    1.53 +  if (winTime > 0xFFFFFFFF)
    1.54 +  {
    1.55 +    unixTime = 0xFFFFFFFF;
    1.56 +    return false;
    1.57 +  }
    1.58 +  unixTime = (UInt32)winTime;
    1.59 +  return true;
    1.60 +}
    1.61 +
    1.62 +bool GetSecondsSince1601(unsigned year, unsigned month, unsigned day,
    1.63 +  unsigned hour, unsigned min, unsigned sec, UInt64 &resSeconds)
    1.64 +{
    1.65 +  resSeconds = 0;
    1.66 +  if (year < 1601 || year >= 10000 || month < 1 || month > 12 ||
    1.67 +      day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59)
    1.68 +    return false;
    1.69 +  UInt32 numYears = year - 1601;
    1.70 +  UInt32 numDays = numYears * 365 + numYears / 4 - numYears / 100 + numYears / 400;
    1.71 +  Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    1.72 +  if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
    1.73 +    ms[1] = 29;
    1.74 +  month--;
    1.75 +  for (unsigned i = 0; i < month; i++)
    1.76 +    numDays += ms[i];
    1.77 +  numDays += day - 1;
    1.78 +  resSeconds = ((UInt64)(numDays * 24 + hour) * 60 + min) * 60 + sec;
    1.79 +  return true;
    1.80 +}
    1.81 +
    1.82 +void GetCurUtcFileTime(FILETIME &ft)
    1.83 +{
    1.84 +  SYSTEMTIME st;
    1.85 +  GetSystemTime(&st);
    1.86 +  SystemTimeToFileTime(&st, &ft);
    1.87 +}
    1.88 +
    1.89 +}}