使用 FileTimeToSystemTime 有误
今天写了个ftp文件遍历的程序,发现 两个文件的时间不对:
我服务器上有200708161.jpg 、200708162.jpg 、200708151.jpg三个文件创建时间分别为2007-8-16 8:30:30、2007-8-16 8:31:30、2007-8-15 8:30:30,我在ftp遍历的时候发现使用 FileTimeToSystemTime 转换200708151.jpg的时间是对的,二200708161.jpg的时间为2006-8-16 9:30:30 200708162.jpg的时间为 2006-8-16 9:31:30 ,请问这是什么原因啊
[解决办法]
我用过的关于文件时间的几个Functions,cheers!
Public Function FileLastWriteTime(ByVal hFile As Long) As Date
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hFile, ByVal 0&, ByVal 0&, dtFileTime)
' Convert FILETIME to a VB date value
FileLastWriteTime = FileTimeToDate(dtFileTime)
End Function
Public Function FileLastAccessTime(ByVal hFile As Long) As Date
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hFile, ByVal 0&, dtFileTime, ByVal 0&)
' Convert FILETIME to a VB date value
FileLastAccessTime = FileTimeToDate(dtFileTime)
End Function
Public Function FileCreationTime(ByVal hFile As Long) As Date
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hFile, dtFileTime, ByVal 0&, ByVal 0&)
' Convert FILETIME to a VB date value
FileCreationTime = FileTimeToDate(dtFileTime)
End Function
Public Function FilePreciseLastWriteTime(ByVal hFile As Long) As Variant
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hFile, ByVal 0&, ByVal 0&, dtFileTime)
' Convert FILETIME to a decimal value
With dtFileTime
FilePreciseLastWriteTime = MakeDecimal(.dwHighDateTime, .dwLowDateTime)
End With
End Function
Public Function FilePreciseLastAccessTime(ByVal hFile As Long) As Variant
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hFile, ByVal 0&, dtFileTime, ByVal 0&)
' Convert FILETIME to a decimal value
With dtFileTime
FilePreciseLastAccessTime = MakeDecimal(.dwHighDateTime, .dwLowDateTime)
End With
End Function
Public Function FilePreciseCreationTime(ByVal hFile As Long) As Variant
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fetch time stamp from open file
bSuccess = GetFileTime(hFile, dtFileTime, ByVal 0&, ByVal 0&)
' Convert FILETIME to a decimal value
With dtFileTime
FilePreciseCreationTime = MakeDecimal(.dwHighDateTime, .dwLowDateTime)
End With
End Function
Public Sub SetFileLastWriteTime(ByVal hFile As Long, ByVal NewValue As Variant)
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fill a FILETIME structure
SetTimeValue NewValue, dtFileTime
' Save new time stamp
bSuccess = SetFileTime(hFile, ByVal 0&, ByVal 0&, dtFileTime)
End Sub
Public Sub SetFileLastAccessTime(ByVal hFile As Long, ByVal NewValue As Variant)
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fill a FILETIME structure
SetTimeValue NewValue, dtFileTime
' Save new time stamp
bSuccess = SetFileTime(hFile, ByVal 0&, dtFileTime, ByVal 0&)
End Sub
Public Sub SetFileCreationTime(ByVal hFile As Long, ByVal NewValue As Variant)
Dim bSuccess As Boolean
Dim dtFileTime As FILETIME
' Fill a FILETIME structure
SetTimeValue NewValue, dtFileTime
' Save new time stamp
bSuccess = SetFileTime(hFile, dtFileTime, ByVal 0&, ByVal 0&)
End Sub
Public Function FileTimeFromDate(ByVal FromDate As Date) As FILETIME
'===========================================================================
' FileTimeFromDate - Converts a VB Date data type to a FILETIME structure.
'
' NOTE: The FILETIME structure is a structure of 100-nanosecond intervals
' since January 1, 1601. The VB Date data type is a floating point value
' where the value to the left of the decimal is the number of days since
' December 30, 1899, and the value to the right of the decimal represents
' the time.
'
' FromDate The VB DAte to convert.
'
' RETURNS A date/time value in the native Win32 FILETIME structure.
'
'===========================================================================
Dim Success As Boolean
Dim SysTime As SYSTEMTIME
' Create SYSTEMTIME from the input date.
Success = VariantTimeToSystemTime(FromDate, SysTime)
If Success Then
' Convert the SYSTEMTIME to the FILETIME
Success = SystemTimeToFileTime(SysTime, FileTimeFromDate)
End If
If Not Success Then
Err.Raise Err.Number, Err.Source, Err.Description
End If
End Function
Public Function FileTimeToDate( _
FILETIME As FILETIME, _
Optional ByVal ConvertToLocal As Boolean = True) As Date
'===========================================================================
' FileTimeToDate - Converts FILETIME structure to a VB Date data type.
'
' NOTE: The FILETIME structure is a structure of 100-nanosecond intervals
' since January 1, 1601. The VB Date data type is a floating point value
' where the value to the left of the decimal is the number of days since
' December 30, 1899, and the value to the right of the decimal represents
' the time.
'
' FileTime The FILETIME structure to convert.
' ConvertToLocal Optional. True to convert from UTC to local time.
'
' RETURNS A date/time value in the intrinsic VB Date data type.
'
'===========================================================================
Dim Success As Boolean
Dim SysTime As SYSTEMTIME
Dim ftdouble As Double
' Convert to FILETIME
Success = FileTimeToSystemTime(FILETIME, SysTime)
If Success Then
' Convert to a Variant date
Success = SystemTimeToVariantTime(SysTime, ftdouble)
End If
If Not Success Then
Err.Raise Err.Number, Err.Source, Err.Description
End If
' Return the result
FileTimeToDate = ftdouble
End Function
Public Sub SetTimeValue(ByVal NewValue As Variant, ByRef OutFileTime As FILETIME)
'===========================================================================
' SetTimeValue - Helper function to set the various OutFILETIME fields.
'
' NewValue A date represented by a Win32FindData object, a Date
' or String datatype, or a more precise Decimal datatype.
'
' OutFILETIME Pointer to a OutFILETIME structure
'===========================================================================
Dim dec As tagDECIMAL
Select Case TypeName(NewValue)
Case "Win32FileTime "
' Copy all of the OutFileTime bits over
CopyMemory ByVal VarPtr(OutFileTime), NewValue.lpFileTime, _
Len(OutFileTime)
Case "Decimal "
' Fill the DECIMAL structure
CopyMemory ByVal VarPtr(dec.wReserved), ByVal VarPtr(NewValue), LenB(dec)
' Fill the FILETIME structure
OutFileTime.dwHighDateTime = dec.Hi32
OutFileTime.dwLowDateTime = dec.Lo32
Case Else ' "Date ", "String "
' Throw error if not a date supplied
If Not IsDate(NewValue) Then
Err.Raise 5, "SetTimeValue "
End If
OutFileTime = FileTimeFromDate(NewValue)
End Select
End Sub