在Win32时间类型有这样几种类型 FILETIME/
很大,总共16个字节。你可以调用GetSystemTime来取得当前的时间
2. FILETIME
FILETIME的定义是这样的
FILETIME是一个__int64。可以这样转化成__int64
转化为__int64是一个以100纳秒为单位的值
补习一下时间单位( http://blog.vckbase.com/zaboli/archive/2005/06/29/8969.aspx)
SYSTEMTIME可以通过SystemTimeToFileTime来转化为FILETIME
3.Window Time
::GetTickCount()可以返回当前的以微秒为单位的时间,用在精度要求不高的场合,返回的只是一个DWORD,四字节。高一点的使用timeGetTime
如何计算当前时间100天以后的时间,在win32中。
应该这样写
最后:附上我写的一个小工具,删除指定文件夹下面的过期文件的一个工具,并可以备份,支持子文件夹嵌套。
SYSTEMTIME/WINDOWSTIME
先提个问题,如何计算当前时间100天以后的时间,在win32中。
1.SYSTEMTIME
SYSTEMTIME的定义是这样
typedef
struct
_SYSTEMTIME
{
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
{
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
很大,总共16个字节。你可以调用GetSystemTime来取得当前的时间
SYSTEMTIME st;
::GetSystemTime( & st);
但是如果你调用 st.wDay += 100;是达不到效果的,将会得到一个无效的时间格式
::GetSystemTime( & st);
2. FILETIME
FILETIME的定义是这样的
typedef
struct
_FILETIME
{
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME;
FILETIME这个名字很奇怪,其实他跟FILE并没有直接关系,只是很多File的API中,都以这个为时间的类型,比如你可以调用GetFileTime来取得File的创建/修改/访问时间。
{
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME;
FILETIME是一个__int64。可以这样转化成__int64
__int64 d
=
*
(__int64
*
)
&
st;
// or
__int64 d2;
memcpy( & d2, & st, sizeof (d2));
// or
__int64 d2;
memcpy( & d2, & st, sizeof (d2));
转化为__int64是一个以100纳秒为单位的值
补习一下时间单位( http://blog.vckbase.com/zaboli/archive/2005/06/29/8969.aspx)
1ms (毫秒) | 1毫秒=0.001秒=10-3秒(millisecond) |
1μs (微秒) | 1微秒=0.000001=10-6秒(microsecond) |
1ns (纳秒) | 1纳秒=0.000000001秒=10-9秒(nanosecond) |
1ps (皮秒) | 1皮秒=0.000000000001秒=10-12秒 |
1fs (飞秒) | 1飞秒=0.00000000000001秒=10-15秒 |
SYSTEMTIME可以通过SystemTimeToFileTime来转化为FILETIME
3.Window Time
::GetTickCount()可以返回当前的以微秒为单位的时间,用在精度要求不高的场合,返回的只是一个DWORD,四字节。高一点的使用timeGetTime
如何计算当前时间100天以后的时间,在win32中。
应该这样写
const
__int64 nano100SecInDay
=
(__int64)
10000000
*
60
*
60
*
24
;
SYSTEMTIME st;
::GetSystemTime( & st);
FILETIME f;
::SystemTimeToFileTime( & st, & f);
ULARGE_INTEGER now;
memcpy( & now, & f, sizeof (now));
now += 100 * nano100SecInDay;
memcpy( & f, & now, sizeof (f));
FileTimeToSystemTime( & f, & st);
SYSTEMTIME st;
::GetSystemTime( & st);
FILETIME f;
::SystemTimeToFileTime( & st, & f);
ULARGE_INTEGER now;
memcpy( & now, & f, sizeof (now));
now += 100 * nano100SecInDay;
memcpy( & f, & now, sizeof (f));
FileTimeToSystemTime( & f, & st);
最后:附上我写的一个小工具,删除指定文件夹下面的过期文件的一个工具,并可以备份,支持子文件夹嵌套。
#include
<
windows.h
>
#include < cstdio >
#include < string >
using namespace std;
const __int64 nano100SecInDay = (__int64) 10000000 * 60 * 60 * 24 ;
ULARGE_INTEGER now;
int days = 14 ;
bool backup = false ;
string dst_dir;
string backup_dir;
void make_sure_dir_exist( string & file)
{
int tag = 2 ;
while ( true )
{
tag = file.find_first_of( " \\ " ,tag + 1 );
if (tag !=- 1 )
{
string tmp = file.substr( 0 ,tag);
::CreateDirectory(tmp.c_str(),NULL);
}
else
{
break ;
}
}
}
void xdel( const char * dir)
{
char tempFileFind[ 1024 ];
sprintf(tempFileFind, " %s\\*.* " ,dir);
WIN32_FIND_DATA ffd;
HANDLE hFind;
hFind = ::FindFirstFile(tempFileFind, & ffd);
if (hFind == INVALID_HANDLE_VALUE)
{
printf( " can't find %s\n " ,dir);
return ;
}
while ( true )
{
// printf("find %s\n",ffd.cFileName);
FILETIME & ft = ffd.ftLastWriteTime;
ULARGE_INTEGER ui;
memcpy( & ui, & ft, sizeof (ui));
__int64 t = now.QuadPart - ui.QuadPart;
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(ffd.cFileName, " . " ) && strcmp(ffd.cFileName, " .. " ))
{
char temp[ 1024 ];
sprintf(temp, " %s\\%s " ,dir,ffd.cFileName);
xdel(temp);
if (t >= nano100SecInDay * days)
{
if (::RemoveDirectory(temp))
{
printf( " del dir %s ok\n " ,temp);
}
else
{
printf( " del dir %s failed\n " ,temp);
}
}
}
}
else
{
char temp[ 1024 ];
sprintf(temp, " %s\\%s " ,dir,ffd.cFileName);
if (t >= nano100SecInDay * days)
{
if (backup)
{
string backup_file = temp;
backup_file.replace( 0 ,dst_dir.length(),backup_dir.c_str());
make_sure_dir_exist(backup_file);
if (::MoveFile(temp,backup_file.c_str()))
{
printf( " backup file %s ok\n " ,temp);
}
else
{
printf( " backup file %s failed\n " ,temp);
}
}
else
{
SetFileAttributes(temp,FILE_ATTRIBUTE_NORMAL);
if (::DeleteFile(temp))
{
printf( " del file %s ok\n " ,temp);
}
else
{
printf( " del file %s failed\n " ,temp);
}
}
}
}
if ( ! FindNextFile(hFind, & ffd))
{
break ;
}
}
FindClose(hFind);
}
int main( int argc, char ** argv)
{
if (argc < 2 )
{
printf( " usage: xdel directory /d= /m=\n " );
printf( " [optional] /d: config the expired days,default is 14 days\n " );
printf( " [optional] /m: config the backup directory\n " );
printf( " for example: xdel d:\\test /d=10 /m=d:\\backup " );
return 1 ;
}
for ( int i = 1 ;i < argc; ++ i)
{
string tmp = argv[i];
if (tmp.find( " /d= " ) !=- 1 )
{
int d = atoi(tmp.substr( 3 ).c_str());
if (d != 0 )
{
days = d;
}
}
else if (tmp.find( " /m= " ) !=- 1 )
{
backup = true ;
backup_dir = tmp.substr( 3 );
}
else
{
dst_dir = tmp;
}
}
// Get system time
SYSTEMTIME st;
::GetSystemTime( & st);
FILETIME f;
::SystemTimeToFileTime( & st, & f);
memcpy( & now, & f, sizeof (now));
xdel(argv[ 1 ]);
return 0 ;
}
#include < cstdio >
#include < string >
using namespace std;
const __int64 nano100SecInDay = (__int64) 10000000 * 60 * 60 * 24 ;
ULARGE_INTEGER now;
int days = 14 ;
bool backup = false ;
string dst_dir;
string backup_dir;
void make_sure_dir_exist( string & file)
{
int tag = 2 ;
while ( true )
{
tag = file.find_first_of( " \\ " ,tag + 1 );
if (tag !=- 1 )
{
string tmp = file.substr( 0 ,tag);
::CreateDirectory(tmp.c_str(),NULL);
}
else
{
break ;
}
}
}
void xdel( const char * dir)
{
char tempFileFind[ 1024 ];
sprintf(tempFileFind, " %s\\*.* " ,dir);
WIN32_FIND_DATA ffd;
HANDLE hFind;
hFind = ::FindFirstFile(tempFileFind, & ffd);
if (hFind == INVALID_HANDLE_VALUE)
{
printf( " can't find %s\n " ,dir);
return ;
}
while ( true )
{
// printf("find %s\n",ffd.cFileName);
FILETIME & ft = ffd.ftLastWriteTime;
ULARGE_INTEGER ui;
memcpy( & ui, & ft, sizeof (ui));
__int64 t = now.QuadPart - ui.QuadPart;
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(ffd.cFileName, " . " ) && strcmp(ffd.cFileName, " .. " ))
{
char temp[ 1024 ];
sprintf(temp, " %s\\%s " ,dir,ffd.cFileName);
xdel(temp);
if (t >= nano100SecInDay * days)
{
if (::RemoveDirectory(temp))
{
printf( " del dir %s ok\n " ,temp);
}
else
{
printf( " del dir %s failed\n " ,temp);
}
}
}
}
else
{
char temp[ 1024 ];
sprintf(temp, " %s\\%s " ,dir,ffd.cFileName);
if (t >= nano100SecInDay * days)
{
if (backup)
{
string backup_file = temp;
backup_file.replace( 0 ,dst_dir.length(),backup_dir.c_str());
make_sure_dir_exist(backup_file);
if (::MoveFile(temp,backup_file.c_str()))
{
printf( " backup file %s ok\n " ,temp);
}
else
{
printf( " backup file %s failed\n " ,temp);
}
}
else
{
SetFileAttributes(temp,FILE_ATTRIBUTE_NORMAL);
if (::DeleteFile(temp))
{
printf( " del file %s ok\n " ,temp);
}
else
{
printf( " del file %s failed\n " ,temp);
}
}
}
}
if ( ! FindNextFile(hFind, & ffd))
{
break ;
}
}
FindClose(hFind);
}
int main( int argc, char ** argv)
{
if (argc < 2 )
{
printf( " usage: xdel directory /d= /m=\n " );
printf( " [optional] /d: config the expired days,default is 14 days\n " );
printf( " [optional] /m: config the backup directory\n " );
printf( " for example: xdel d:\\test /d=10 /m=d:\\backup " );
return 1 ;
}
for ( int i = 1 ;i < argc; ++ i)
{
string tmp = argv[i];
if (tmp.find( " /d= " ) !=- 1 )
{
int d = atoi(tmp.substr( 3 ).c_str());
if (d != 0 )
{
days = d;
}
}
else if (tmp.find( " /m= " ) !=- 1 )
{
backup = true ;
backup_dir = tmp.substr( 3 );
}
else
{
dst_dir = tmp;
}
}
// Get system time
SYSTEMTIME st;
::GetSystemTime( & st);
FILETIME f;
::SystemTimeToFileTime( & st, & f);
memcpy( & now, & f, sizeof (now));
xdel(argv[ 1 ]);
return 0 ;
}