dcsimg
Results 1 to 25 of 25

Thread: [RESOLVED] Date last modified is returned wrong

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Resolved [RESOLVED] Date last modified is returned wrong

    I am trying to get the "Date last modified" of a file or folder.
    I use the following code:
    Code:
    Public Function DateLastModified(ByVal sPath As String) As Date
       Dim fso As Object 'New FileSystemObject
       Dim fil As Object 'File
       Dim fol As Object 'Folder
       
       Set fso = CreateObject("Scripting.FileSystemObject")
       If fso.FolderExists(sPath) Then
          Set fol = fso.GetFolder(sPath)
          DateLastModified = fol.DateLastModified
       ElseIf fso.FileExists(sPath) Then
          Set fil = fso.GetFile(sPath)
          DateLastModified = fil.DateLastModified
       End If
    End Function
    And I call it like this:
    Code:
    Private Sub cmdGetDates_Click()
       Dim Path1                As String
       Dim Path2                As String
       Dim Path3                As String
       Dim s                    As String
       
       Path1 = "C:\temp2"
       Path2 = "C:\temp2\P1"
       Path3 = "C:\temp2\P1\List.txt"
       
       s = "Date last modified:" & vbCrLf
       s = s & Path1 & vbTab & vbTab & DateLastModified(Path1) & vbCrLf
       s = s & Path2 & vbTab & DateLastModified(Path2) & vbCrLf
       s = s & Path3 & vbTab & DateLastModified(Path3) & vbCrLf
       
       txtResP.Text = s
    End Sub
    The result is WRONG.
    Here are the actual date and times as shown by File explorer:
    http://i.imgur.com/is7STjW.jpg
    http://i.imgur.com/3gQaCik.jpg
    And this is the result of executing the above code:
    http://i.imgur.com/kTvUY6C.jpg

    Clearly, the result of the above code is wrong.
    How can I return the correct value of the "Date last modified" of a file or folder?
    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,139

    Re: Date last modified is returned wrong

    Did you try vb's native FileDateTime Function?
    http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Did you try vb's native FileDateTime Function?
    http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx
    I just tried it, and it also gives me the exact same wrong result.
    How can I get the correct datetime of a file's last modification (as shown by the file explorer)?
    Please advise.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,279

    Re: Date last modified is returned wrong

    You sure, you're not a victim of "UAC-path-virtualizing" (new filecontent written to "somewhere else")?

    On my machine I get the same date-results reported by all methods (FSO, VB-FileDate, Explorer) -
    when I change a File, the Explorer sometimes needs an explicit Refresh per F5, to reflect the new Date.

    Olaf

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    The story can be far more complicated than that.

    For example both VB6's FileDateTime() function and the FSO use an incorrect approach for time zone conversion, one that fails to handle Daylight Savings Time properly.

    While Shell32 (Explorer) can return times it can only do so reliably via the ExtendedProperty() method. But you only get back UTC times, and even then it seems to vary by a second or two from the times I get via API calls.

    Shell32's GetDetailsOf() method results are pretty worthless: (a.) you only get a String back, (b.) the precision is limited, and (c.) on various OSs the iColumn argument may have entirely different meanings and not return the values my program expects.

    I am in the -5 time zone, and since it is late December and I am in the northern hemisphere there is no DST offset on top of that.


    Here are my results for a very old folder from July 2007. The FileDateTime() function, the FSO, and my "bad conversion" values are all off 1 hour from the correct time in 2007 when that folder was created and last fiddled with.

    Only my "good" ("FileDateTimes Local" in the demo) time is anywhere near correct. However Shell32 (Explorer) appears to return "bad" values in its UI (Explorer windows, Property dialogs) - go figure. At least the reported times will match up with VB6's FileDateTime() function and the FSO.

    Name:  sshot1.jpg
Views: 4569
Size:  38.7 KB

    Note that 12:48 AM is 48 minutes after midnight.


    Here are my results for a recent folder. The times don't diverge as much here, at least as far as DST handling goes because this was run on the same day the folder was created:

    Name:  sshot2.jpg
Views: 4492
Size:  39.1 KB


    Note that the demo makes use of GetTimeZoneInformationForYear() which was not available until Windows Vista. One could substitute the less accurate GetTimeZoneInformation() call instead for Windows XP and earlier, but XP is dead now anyway.

    It gets confusing fast trying to unravel all of the issues.
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    As it says at:

    FileTimeToLocalFileTime function

    Remarks

    To account for daylight saving time when converting a file time to a local time, use the following sequence of functions in place of using FileTimeToLocalFileTime:

    1. FileTimeToSystemTime
    2. SystemTimeToTzSpecificLocalTime
    3. SystemTimeToFileTime
    Those recommendations make the difference between my "bad" and "good" local FileDateTimes values.
    Last edited by dilettante; Dec 31st, 2014 at 12:21 PM. Reason: typo

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Only my "good" ("FileDateTimes Local" in the demo) time is anywhere near correct.
    Thank you for your advice and the attached code that you provided.
    I took a look at that, and I have a couple of questions:

    1. Have I understood correctly that the best approach is this line:
    Code:
    flexTimes.TextMatrix(7, 2) = Format$(.LastModified, TIMESTAMP_FORMAT)
    which comes from this line of code in the class module:
    Code:
    mLastModified = FileTimeToDate(wfadFile.ftLastWriteTime)
    Please note that I am only interested in Last modified date, and not interested in the create date or last access date

    2. In my own code, I am using it like this:
    Code:
    Private Function GetPathDate(ByRef Path1 As String) As String
        'Fetch using FileDateTimes object:
        With New FileDateTimes
            .GetTimestamps Path1
            GetPathDate = Format$(.LastModified, TIMESTAMP_FORMAT)
        End With
    End Function
    My concern is about that new object that you are creating:
    Code:
    With New FileDateTimes
    Does this new object remain in memory?
    For example if I call my above sub GetPathDate millions of times in a loop, will that result in millions of instances of that object being created and not dropped, leading to going out of memory?
    Should I explicitly set that to Nothing? If yes, how?

    3. The above value, before being converted to string like this:
    Code:
    Format$(.LastModified, TIMESTAMP_FORMAT)
    is of type variant, because your function FileTimeToDate (defined in the class module) returns a variant.
    Now, let's say if I choose to NOT convert this value to a string, and instead keep it as is, how do I compare two such values?
    For example if I obtain the above variant-type datetime for two different files, how do I calculate the difference between these two datetimes? And how do I test to see if these two files' datetimes are equal or not?
    Thanks for your help.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    1. You could call the GetFileTime function instead of the GetFileAttributesEx function. You still get all 3 "times" but I suppose it may be slightly lighter-weight. GetFileAttributesEx is more compatible with FindFirstFile and FindNextFile, which rest of the code was originally being used with. Just ignore the 2 times you don't care about.

    2. There is no reason you have to use With New xxx, you can define a variable of the FileDateTimes type and set it to a New instance and re-use that as many times as you need before getting rid of it when done. I have no idea why you think you need to wrap it inside a new method as you show there.

    3. If you are not displaying the value there is no need to format it. The Variant results are Variant Date or Empty values, and for normal filesystems (FAT, FAT32, NTFS) you always get LastModified back as a Date.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    BTW, if you are getting the difference between timestamps for 2 files you do not want to use localized values. Use the UTC values instead to avoid problems when Daylight Savings Time is crossed. If you are trying to get file timestamp differences you don't want to use Date typed data anyway, and are far better off working with raw FILETIME typed data.

    Maybe see the recent example at this link:

    http://www.xtremevbtalk.com/showthre...28#post1407628

    It is suspiciously similar to where your questions are leading. The code there displays Creation timestamp differences, but can be very easily modified to use LastWriteTime instead.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    Here is another example, stripped way down.

    Name:  sshot.png
Views: 4546
Size:  34.4 KB

    Those will not exactly match Explorer's reported LastModified values because Explorer actually gets it wrong when it handles DST, and can report times off by an hour.

    That bug may be fixed in a later version of Windows, but it was still present in Vista SP2. I don't really expect VB6's FileDateTime() function or the FSO to ever be fixed though.
    Attached Files Attached Files
    Last edited by dilettante; Jan 8th, 2015 at 08:16 AM. Reason: stripped down some more

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Thank you for all the help and the attached zip files.
    I went through them, and I am kind of confused.
    In your latest attachment "FDTs Example", you obtain the file dates directly from the FindFirstFile/FindNextFile functions, and then you do not do any conversion to system time and local time. The datetimes that are returned by FindFirstFile/FindNextFile functions are of FILETIME data type (cast as Currency), and you are directly using them (without conversion to system and local times) by simply subtracting two such times and then dividing by 1000. That should give the time difference in seconds.
    However, from your previous posts I gather that time must be converted to system time and then to local time as follows:
    Code:
    Private Function FileTimeToDate(ByRef FILETIME As FILETIME) As Variant
        Dim stUniversal As SYSTEMTIME
        Dim stLocal As SYSTEMTIME
        Dim Seconds As Integer
        
        With FILETIME
            If (.dwHighDateTime <> 0) And (.dwLowDateTime <> 0) Then
                FileTimeToSystemTime FILETIME, stUniversal
                UniversalToLocal stUniversal, stLocal
                With stLocal
                    Seconds = SymmetricArithmeticRoundToInteger(CDbl(.wSecond) _
                                                              + CDbl(.wMilliseconds) / 1000)
                    FileTimeToDate = DateSerial(.wYear, .wMonth, .wDay) _
                                   + TimeSerial(.wHour, .wMinute, Seconds)
                End With
            'Else
                'FileTimeToDate = Empty
            End If
        End With
    End Function
    The output of the above code is of VB Date type.
    If I were to modify the above code and use the stLocal value directly, that would be of type:
    Code:
    Private Type SYSTEMTIME
      wYear As Integer
      wMonth As Integer
      wDayOfWeek As Integer
      wDay As Integer
      wHour As Integer
      wMinute As Integer
      wSecond As Integer
      wMilliseconds As Integer
    End Type
    Now if I have two file datetimes of this SYSTEMTIME data type, I cannot then simple subtract them (the way you subtracted the two Currencies)
    So, how do I subtract two values of this SYSTEMTIME data type?
    Please note that my purpose is to compare an original file (on my computer) to a backup of that same file (on a flash memory or an external hard drive), and see if the file has changed since the last backup or not.
    So, how do I subtract and compare these two time stamps?
    How do I get the difference (in seconds)?
    Thanks.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    No, you do not want to convert to local time if your goal is to get the difference between times for two files. That would potentially introduce one or even two hours of inaccuracy.

    Just use the FILETIME values as shown and you're done.

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    By the way...

    Quote Originally Posted by IliaPreston View Post
    Please note that my purpose is to compare an original file (on my computer) to a backup of that same file (on a flash memory or an external hard drive), and see if the file has changed since the last backup or not.
    This is exactly what the Archive file attribute is for.

    When a file gets created or changed, the A bit (Archive) gets turned on. When you back the file up you should turn it off. It will stay off until the next change.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    In your FDTs example, you are obtaining the LastWriteTime as Currency, and that indicates miliseconds, so by dividing it by 1000, we can arrive at the number of seconds.
    Now, given the fact that I should not convert to local time, and instead should use FILETIME as is, I think, I can cast FILETIME as currency (so that I can then interpret it as miliseconds), but that doesn't work.
    I have modified your "GetPathTimeStamp" function to skip the conversion to local time, and also to return a value of type currency.
    Also, I have renamed it to "GetPathTimeStampCrncy":
    Code:
    Public Function GetPathTimeStampCrncy(ByVal PathToCheck As String) As Currency
        Dim wfadFile As WIN32_FILE_ATTRIBUTE_DATA
        Dim lngResult As Long
    
        lngResult = GetFileAttributesEx(StrPtr(PathToCheck), _
                                         GetFileExInfoStandard, _
                                         VarPtr(wfadFile))
        If lngResult = 0 Then
            Err.Raise &H8004A802, _
                      TypeName(Me), _
                      "Internal error. System error " _
                    & CStr(Err.LastDllError) _
                    & " calling GetFileAtributesEx"
        Else
            GetPathTimeStampCrncy = CCur(wfadFile.ftLastWriteTime)
            
        End If
    End Function
    The last line of the above code that is:
    Code:
            GetPathTimeStampCrncy = CCur(wfadFile.ftLastWriteTime)
    gives a type mismatch error.
    Please note that the type is as follows:
    Code:
    Private Type FILETIME
      dwLowDateTime As Long
      dwHighDateTime As Long
    End Type
    
    Private Type WIN32_FILE_ATTRIBUTE_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
    End Type
    So, "ftLastAccessTime" (which is of type FILETIME) should be able to be cast into Currency, because FILETIME consists of 8 bytes (two Longs (4 bytes each)) a,d currency is also 8 bytes.
    But, the type cast doesn't work, and gives me a type mismatch error.
    What is it that I am missing?
    Thanks.

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    There is no such thing as "casting" in VB6. Conversion functions convert which is a very different thing altogether.

    Instead:

    Code:
    Private Type WIN32_FILE_ATTRIBUTE_DATA
        dwFileAttributes As Long
        ftCreationTime As Currency
        ftLastAccessTime As Currency
        ftLastWriteTime As Currency
        nFileSizeHigh As Long
        nFileSizeLow As Long
    End Type
    Now there is no need to convert. The necessary "casting" is done behind the back of the VB6 compiler simply by mapping the bit pattern of a FILETIME to a Currency, which is all languages supporting "casting" do anyway. Casting means "let's pretend."

    The only caveat here is that Currency is signed and FILETIME is unsigned. Not an issue for us here because even the positive range of a Currency gives us 29000 years of milliseconds and the base value for FILETIME values is January 1, 1601.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Thank you for the help and advice.
    I understand that if I declare WIN32_FILE_ATTRIBUTE_DATA this way (as you correctly propose):
    Code:
    Private Type WIN32_FILE_ATTRIBUTE_DATA
        dwFileAttributes As Long
        ftCreationTime As Currency
        ftLastAccessTime As Currency
        ftLastWriteTime As Currency
        nFileSizeHigh As Long
        nFileSizeLow As Long
    End Type
    then the value of ftLastWriteTime (which will be returned as Currency) is in milliseconds, and therefore I can take it from there and do my calculations.
    However, just for curiosity's sake, I am wondering if I declare WIN32_FILE_ATTRIBUTE_DATA the other way (with dates within it being of type FILETIME) where FILETIME itself is declared this way:
    Code:
    Private Type FILETIME
      dwLowDateTime As Long
      dwHighDateTime As Long
    End Type
    Then, how do I calculate the difference (in milliseconds) between two such values, in other words, how do I write this function:
    Code:
    Private Function FILETIME_Diff(ByRef FT1 As FILETIME, ByRef FT2 As FILETIME) As Currency
       ...... What would the code be in here ???
    End Function
    Let me clarify one more time that, I totally agree that simply declaring those datetime items as currency, is the superior choice, and I am going to take that approach.
    But, I still feel a strong urge to understand how to write the function FILETIME_Diff (mentioned above), even though, I am not going to use it in this specific situation in my backup program.
    It is just for curiosity's sake.
    Can you please advice as to how to write that FILETIME_Diff function?
    Thanks.

  17. #17
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    It really isn't worth the bother.

    It requires a lot of gymnastics and is so seldom needed that you would search for a very long time to even find one example of doing this. It is so rarely required that nearly everyone just punts and says to use Currency:

    How To Do 64-bit Arithmetic in VBA applies to VB6.

    Faking 64-bit Integers

  18. #18
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    If you really insist on this and don't consider use of Currency arithmetic some kind of "cheat" as well, you can always do something like:

    Code:
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    
    Private Function Diff(ByRef FTMin As FILETIME, ByRef FTSub As FILETIME) As Currency
        Diff = (CCur(FTMin.dwLowDateTime) * 0.0001@ + CCur(FTMin.dwHighDateTime) * 429496.7296@) _
             - (CCur(FTSub.dwLowDateTime) * 0.0001@ + CCur(FTSub.dwHighDateTime) * 429496.7296@)
    End Function

    Name:  sshot.png
Views: 4482
Size:  16.8 KB
    Attached Files Attached Files

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Thanks for the help.
    I used this code:
    Code:
    Private Type WIN32_FILE_ATTRIBUTE_DATA
        dwFileAttributes As Long
        ftCreationTime       As Currency
        ftLastAccessTime     As Currency
        ftLastWriteTime      As Currency
        nFileSizeHigh As Long
        nFileSizeLow As Long
    End Type
    
    Public Function GetPathTimeStampCrncy(ByVal PathToCheck As String) As Currency      ' This is the number of milliseconds
        Dim wfadFile As WIN32_FILE_ATTRIBUTE_DATA
        Dim lngResult As Long
    
        lngResult = GetFileAttributesEx(StrPtr(PathToCheck), _
                                         GetFileExInfoStandard, _
                                         VarPtr(wfadFile))
        If lngResult = 0 Then
            Err.Raise &H8004A802, _
                      TypeName(Me), _
                      "Internal error. System error " _
                    & CStr(Err.LastDllError) _
                    & " calling GetFileAtributesEx"
        Else
           GetPathTimeStampCrncy = wfadFile.ftLastWriteTime
        End If
    End Function
    to obtain the file timestamps (for the purpose of comparing them). This function returns the value as a Currency type and the value is in milliseconds.

    Then I wrote a little piece of code to compare the file timestamps of all the files in one folder with the file timestamps of their corresponding copies in a backup folder.
    The result is quite strange.
    There are in total 2940 files in that folder.
    Of those, 2235 files that I had backed up manually (by dragging and dropping in Windows Explorer) before my backup application was ready, have the exact same file timestamps. In other words, the file timestamps of all those files match their corresponding backup copies. There is a perfect match to the millisecond.
    But, the rest of the files, that is 705 files that I have backed up recently using my backup application, show a tiny mismatch between each file and its corresponding backup file. The mismatch is quite random and quite wild and unpredictable, but always less than 2 seconds. The maximum mismatch is 1998.1000 milliseconds (almost 2 seconds).
    My backup application uses this code to copy files:
    Code:
    Private Declare Function CopyFileW Lib "kernel32" (ByVal lpExistingFilename As Long, ByVal lpNewFilename As Long, ByVal bFailIfExists As Long) As Long
    
    Public Sub FileCopy(ByVal Source As String, ByVal Destination As String, Optional ByVal bFailIfExists As Boolean = False)
       CopyFileW StrPtr(Source), StrPtr(Destination), bFailIfExists
    End Sub
    Looks like your advice about the file time is perfectly correct, and I appreciate it.
    I have calculated a whole lot of file time stamps (the number of milliseconds since Jan. 01, 1601 12:00 AM) and for all the files the result of my calculation matches the result that the above function GetPathTimeStampCrncy produces (with a fixed deviation of 18000 seconds (= 5 hours)). (I don't know what the reason for that 5 hour deviation is but, it shouldn't be a problem because it is a fixed amount for all files).
    So, there is no problem with time stamp calculation, however, the time stamps between the files and their corresponding backups (copies) mismatch by less than 2 seconds.
    Why does this file copy procedure cause this kind of time stamp mismatch for every single file that it copies?
    Thanks.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Can someone please help me with this?
    I copy files, using this code:
    Code:
    Private Declare Function CopyFileW Lib "kernel32" (ByVal lpExistingFilename As Long, ByVal lpNewFilename As Long, ByVal bFailIfExists As Long) As Long
    
    Public Sub FileCopy(ByVal Source As String, ByVal Destination As String, Optional ByVal bFailIfExists As Boolean = False)
       CopyFileW StrPtr(Source), StrPtr(Destination), bFailIfExists
    End Sub
    and the source file and the destination file's time stamps are always slightly different (between 0 and 2 seconds).
    Why are the time stamps different?
    Shouldn't the copied file have the exact same time stamp as the original file?
    Please help.
    Thanks.

  21. #21
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,279

    Re: Date last modified is returned wrong

    Quote Originally Posted by IliaPreston View Post
    Private Declare Function CopyFileW Lib "kernel32" (ByVal lpExistingFilename As Long, ByVal lpNewFilename As Long, ByVal bFailIfExists As Long) As Long

    ...the source file and the destination file's time stamps are always slightly different (between 0 and 2 seconds).
    Why are the time stamps different?
    Shouldn't the copied file have the exact same time stamp as the original file?
    Just to make sure... do you copy against a "Remote-machine" (a different PC, Server or NAS),
    which offers a Network-Share?

    Olaf

  22. #22
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,230

    Re: Date last modified is returned wrong

    Useful notes here:

    Evolution of the File Property Timestamp

    Back to the evolution. In Windows 2000, the formatting of timestamps was modified slightly to say Yesterday, Today or Tomorrow for dates that were within one day of the current date. For example, instead of saying Created: Sunday, February 14, 2009, 7:00:00 AM, the property sheet would say Created: Today, February 14, 2009, 7:00:00 AM when viewed on Feb. 14. Nothing essential changed; this was just a little visual tweak to make things prettier.

    In Windows Vista, the time portion of the timestamp was made a bit friendlier: If the time and date refer to the current day, then the time portion of the timestamp is given in relative notation: Created: Today, February 14, 2009, 15 minutes ago.

    And most recently, in Windows 7, the file property sheet General page shows timestamps based on the time zone that was in effect at your current location when the file was created rather than based on the current time, bringing the property sheet more in alignment with the way managed code presents timestamps. Finally, the file you created at noon on July 4 will show as having been created at noon even during the winter months. This change takes advantage of the new so-called Dynamic Time Zones, which permit the Daylight Saving Time rules for a time zone to vary from year to year. This allows Windows to know that a file created on Oct. 30, 2006, in Redmond was created during Pacific Standard Time, whereas a file created on Oct. 30, 2007, was created during Pacific Daylight Time, thanks to the change in Daylight Saving Time rules in the United States that took effect in 2007.

    Note, however, that the historical information that comes with Windows does not go back to years before 1987, when the rules were different still, so timestamps prior to 1987 may still end up converted incorrectly.
    So Windows 7 now handles the 1 hour discrepency in most of the common ways of retrieving LastModified.


    Also see:

    Why do timestamps change when I copy files to a floppy? and File Times.

    Which also apply to flash drives and some smaller hard drive partitions formatted as FAT or FAT32.

    Note, for example, that FAT records last-write time only to two-second accuracy. So if you copy a file from NTFS to FAT, the last-write time can change by as much as two seconds.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2010
    Posts
    416

    Re: Date last modified is returned wrong

    Just to make sure... do you copy against a "Remote-machine" (a different PC, Server or NAS),
    which offers a Network-Share?
    No. I am copying from my desktop computer's C drive to a USB flash memory attached to the computer.

    Note, for example, that FAT records last-write time only to two-second accuracy. So if you copy a file from NTFS to FAT, the last-write time can change by as much as two seconds.
    That is true.
    I copy from my C drive that is NTFS to a flash memory that is FAT32.
    Now, everything makes perfect sense.
    All of the files that I copied follow that maximum inaccuracy.
    I have copied thousands of files and the maximum timestamp inconsistency that I get is less than 2000 milliseconds.
    Therefore, I am going to set this item to resolved.
    Thank you everyone for all your great help and advice.

  24. #24
    Addicted Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    250

    Re: Date last modified is returned wrong

    Quote Originally Posted by dilettante View Post
    There is no such thing as "casting" in VB6. Conversion functions convert which is a very different thing altogether.

    Instead:

    Code:
    Private Type WIN32_FILE_ATTRIBUTE_DATA
        dwFileAttributes As Long
        ftCreationTime As Currency
        ftLastAccessTime As Currency
        ftLastWriteTime As Currency
        nFileSizeHigh As Long
        nFileSizeLow As Long
    End Type
    Now there is no need to convert. The necessary "casting" is done behind the back of the VB6 compiler simply by mapping the bit pattern of a FILETIME to a Currency, which is all languages supporting "casting" do anyway. Casting means "let's pretend."

    The only caveat here is that Currency is signed and FILETIME is unsigned. Not an issue for us here because even the positive range of a Currency gives us 29000 years of milliseconds and the base value for FILETIME values is January 1, 1601.
    A related issue... I actually NEED to convert from a FILETIME to a Currency. My situation is different than the original poster but a web search brought me to this thread. How is a FileTime converted to a Currency? I tried a CopyMemory of one variable to the other but that didn't seem to work. I thought I could do this since they are both the same size in memory.

    Thanks in advance

  25. #25
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,560

    Re: [RESOLVED] Date last modified is returned wrong

    CopyMemory works just fine. Proof-of-concept:

    Code:
    
    Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal bytes As Long)
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    
    Private Sub Form_Load()
        Dim f1 As FILETIME
        Dim f2 As FILETIME
        Dim c As Currency
    
    
        f1.dwLowDateTime = 5
        f1.dwHighDateTime = 10
    
        CopyMemory c, f1, 8
        MsgBox c
    
    
        CopyMemory f2, c, 8
    
        MsgBox f2.dwLowDateTime
        MsgBox f2.dwHighDateTime
    
    
    
    End Sub
    
    

    I just did this proof-of-concept in the default form's Form_Load event of a new project.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width