0

我试图比较本地文件和 ftp 服务器上的文件之间的文件写入时间。本地机器上的文件时间有效,这是有道理的,但是当我查看 ftp 服务器上的文件时,它显示了两个不同的时间,通过 Windows 资源管理器和右键单击-> 属性。我发现了一个有效的 hack,它在我的代码中被注释掉了。有什么帮助吗?我希望文件时间正确地相互关联。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" ) );
4

1 回答 1

1

SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &ftpFileWriteTime)

那是行不通的。您必须通过服务器所在的时区,而不是您自己的时区。假设服务器甚至发送 UTC 时间戳,这在我上次放弃它时并不常见。找出它所在的时区应该是具有挑战性的。FTP还没有成熟。

于 2010-09-01T00:10:18.560 回答