Win32时间类型FILETIME/SYSTEMTIME/WINDOWSTIME(FILETIME这个名字很奇怪,其实他跟FILE并没有直接关系,只是很多File的API中,都以这个为时间的类型)

在Win32时间类型有这样几种类型 FILETIME/ 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;

很大,总共16个字节。你可以调用GetSystemTime来取得当前的时间
SYSTEMTIME st;
::GetSystemTime(
& st);
但是如果你调用 st.wDay += 100;是达不到效果的,将会得到一个无效的时间格式

2. FILETIME
FILETIME的定义是这样的
typedef  struct   _FILETIME
    {
    DWORD dwLowDateTime;
    DWORD dwHighDateTime;
    } FILETIME;
FILETIME这个名字很奇怪,其实他跟FILE并没有直接关系,只是很多File的API中,都以这个为时间的类型,比如你可以调用GetFileTime来取得File的创建/修改/访问时间。
FILETIME是一个__int64。可以这样转化成__int64
__int64 d  =   * (__int64  * ) & st;

// 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);


最后:附上我写的一个小工具,删除指定文件夹下面的过期文件的一个工具,并可以备份,支持子文件夹嵌套。

#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 ;
}



Feedback

# re: Win32的时间类型  回复  更多评论  

2006-04-12 22:21 by Stone Jiang
不错.
如果处理时间范围很大的,像历史问题,公元前207年这样的问题呢?

# re: Win32的时间类型  回复  更多评论  

2006-04-14 09:55 by cherokee
刚刚经历VC6的程序到VC8移植时CTime引起问题的教训。
VC6中CTime::GetTime()返回32位值,而VC8返回64位值,这样,sprintf()时仍然用%d来对应64位值就引起了内存异常。

# re: Win32的时间类型  回复  更多评论  

2006-11-22 15:42 by lovecpp
see :
http://www.vckbase.com/document/viewdoc/?id=1301
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值