在FTP服务器上写时间很快

时间:2010-08-31 23:56:52

标签: c++ datetime filetime

我试图比较本地文件和ftp服务器上的文件之间的文件写入时间。本地机器上的文件时间是有效的,但是当我查看ftp服务器上的文件时,它会通过windows explorer和rightclick->属性显示两个不同的时间。我找到了一个有效的黑客,并在我的代码中发表了评论。有帮助吗?我希望文件时间能够正确地相互关联。 MFC,C ++,Windows 7 32位,VS 2008

代码:

            HINTERNET xmlHandle = NULL;
        WIN32_FIND_DATA ftpFileData;

        // find the file on the ftp server
        xmlHandle = FtpFindFirstFile( m_ftpHandle, _T("TPCFeed.xml"), &ftpFileData, INTERNET_FLAG_RELOAD, 0 );
        if( NULL != xmlHandle )
        {
            //-----------------------------------------------------------------------------------
            // get the write time of the ftp file
            SYSTEMTIME ftpFileWriteTime,
                       stUTC1;
            FILETIME ftp;
            FileTimeToSystemTime( &ftpFileData.ftLastWriteTime, &stUTC1 );
            SystemTimeToTzSpecificLocalTime( NULL, &stUTC1, &ftpFileWriteTime );

            // ----- HACK -------------------------------------------
            ftpFileWriteTime.wHour += 4; // this hack works
            SystemTimeToFileTime( &ftpFileWriteTime, &ftp );

            //-----------------------------------------------------------------------------------
            // get the write time of the local file
            HANDLE localFileHandle = NULL;
            localFileHandle = CreateFile( _T(_XML_FILENAME_PATH), FILE_READ_ATTRIBUTES,
                                     FILE_SHARE_READ, NULL, OPEN_EXISTING,
                                     NULL, NULL );
            if( INVALID_HANDLE_VALUE != localFileHandle )
            {
                // get local file time
                FILETIME localFileWriteTime,
                         local;
                GetFileTime( localFileHandle, NULL, NULL, &localFileWriteTime );

                SYSTEMTIME localFileWriteTime1,
                           stUTC;
                FileTimeToSystemTime( &localFileWriteTime, &stUTC );
                SystemTimeToTzSpecificLocalTime( NULL, &stUTC, &localFileWriteTime1 );
                SystemTimeToFileTime( &localFileWriteTime1, &local );
            //-----------------------------------------------------------------------------------
                int timeResult = CompareFileTime( &ftp, &local );
                if( -1 == timeResult )
                    AfxMessageBox( _T( "file on disk is later than ftp file, no need to download anything" ) );
                else if( 0 == timeResult )
                    AfxMessageBox( _T( "times are equal!" ) );
                else if( 1 == timeResult )
                    AfxMessageBox( _T( "file on ftp server is later than file on disk" ) );

1 个答案:

答案 0 :(得分:1)

  

SystemTimeToTzSpecificLocalTime(NULL,& stUTC1,& ftpFileWriteTime)

这不起作用。您必须通过服务器所在的时区,而不是您自己的时区。假设服务器甚至发送UTC时间戳,这在我最后一次放弃时并不常见。找出它所居住的时区应该具有挑战性。 FTP尚未成熟。