檔案時間

檔案時間是檔案在創建,修改,訪問的時間,
本地檔案時間,檔案從別的機器拷貝到本地機,本機對檔案時間的影響稱為檔案時間,
系統時間,是本地機的系統內部時間,精確到毫秒。
本地時間與UTC(世界協調時間)的轉換
此函式獲取系統時間
void CLog::GetSysTime(SYSTEMTIME* lpSysTime)//獲得系統時間
{
FILETIME CurFileTime;
::GetSystemTimeAsFileTime(&CurFileTime);//GetSystemTimeAsFileTime 首先獲得系統時間 UTC格式
::FileTimeToLocalFileTime(&CurFileTime,&CurFileTime);//FileTimeToLocalFileTime 將UTC檔案時間轉換成本地時間
::FileTimeToSystemTime(&CurFileTime,lpSysTime);// FileTimeToSystemTime 將64位檔案時間轉換成系統時間格式(UTC)
}
在Window系統中,時間日期本人知道的有以下數種
1.SystemTime:
system time is expressed in Coordinated Universal Time (UTC)
2.LocalTime:
指的是local PC的時間日期,即vb Date() Time()所傳回的日期時間
3.FileTime :
檔案儲存的時間格式前面兩種日期的格式都是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 IntegerEnd Type這TYPE SYSTEMTIME的格式夠清楚了,而FileTime則是Type FileTime dwLowDateTime As Long dwHighDateTime As LongEnd TypeThe FILETIME structure is a 64-bit value representing the number of100-nanosecond intervals since January 1, 1601
這三個時間是可以做轉換的,LocalTime對SystemTime最簡單SystemTime = LocalTime + 時間差 (以台灣來說 時間差 = -8小時)
我們可以用GetTimeZoneInformation()來取得時間差。而SystemTime和FileTime間的轉換則可用FileTimeToSystemTime() SystemTimeToFileTime()來轉換。剩下的就是更動時間的部份,那使用SetFileTime來完成
SetFileTime(
HANDLE hFile, // identifies the file
CONST FILETIME * lpftCreation, // time the file was created
CONST FILETIME * lpftLastAccess, // time the file was last accessed
CONST FILETIME * lpftLastWrite); // time the file was last written
如果不想更動日期,相對應的參數傳Null進去 。
另可參考讀取檔案建立時間及存取時間'以下程式在Form,需 textBox * 1 Command Button * 1Option ExplicitPrivate hFile As LongPrivate Sub Command1_Click()Dim lpct As FileTime, lplac As FileTime, lplwr As FileTimeDim ofs As OFSTRUCTDim tZone As TIME_ZONE_INFORMATIONDim ft As SYSTEMTIMEDim dtdate As DateDim bias As LonghFile = OpenFile("c:\prn2", ofs, OF_READWRITE)Call GetTimeZoneInformation(tZone)bias = tZone.bias ' 時間差, 以「分」為單位'計算出Coordinated Universal Time (UTC).dtdate = CDate(Text1.Text) + TimeSerial(0, bias, 0)ft.wYear = Year(dtdate)ft.wMonth = Month(dtdate)ft.wDay = Day(dtdate)ft.wHour = Hour(dtdate)ft.wMinute = Minute(dtdate)ft.wSecond = Second(dtdate)ft.wDayOfWeek = WeekDay(dtdate)ft.wMilliseconds = 0Call SystemTimeToFileTime(ft, lplwr)'更動hFile的時間,
第2個參數改Create DateTime'
第3個參數改Last Access DateTime'
第四個參數改Last Modify DateTimeCall SetFileTime(hFile, ByVal 0, ByVal 0, lplwr)Call CloseHandle(hFile)End SubPrivate Sub Form_Load()Text1.Text = "1998/06/03 5:50:10 AM"End SubOption ExplicitPublic Const OFS_MAXPATHNAME = 128Public Const OF_READ = &H0Public Const OF_READWRITE = &H2Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As ByteEnd TypeType SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As IntegerEnd TypeType FileTime dwLowDateTime As Long dwHighDateTime As LongEnd TypeType TIME_ZONE_INFORMATION bias As Long StandardName(32) As Integer StandardDate As SYSTEMTIME StandardBias As Long DaylightName(32) As Integer DaylightDate As SYSTEMTIME DaylightBias As LongEnd TypeDeclare Function GetTimeZoneInformation Lib "kernel32" _ (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As LongDeclare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, _ lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As LongDeclare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongDeclare Function SetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As Any, lpLastAccessTime As Any, lpLastWriteTime As Any) As LongDeclare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FileTime) As LongDeclare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME

相關詞條

熱門詞條

聯絡我們