Thng tin: Lm vi?c v?i c?u trc FILETIME

D?ch tiu ? D?ch tiu ?
ID c?a bi: 188768 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

M?t th?i gian t?p tin ?i di?n cho cc ngy c? th? v th?i gian m t?i m?t t?p tin nh?t ?nh t?o, cu?i truy c?p, ho?c cu?i b?ng vn b?n ?. M?t th?i gian t?p tin ?c lu tr? trong m?t FILETIME c?u trc. C c?u ny ?c s? d?ng v?i nhi?u cu?c g?i Win32 API.

THNG TIN THM

C?u trc FILETIME ?i di?n cho s? l?ng cc kho?ng 100-nanosecond t? 1 thng m?t 1601. C?u trc bao g?m hai 32-bit gi tr? m k?t h?p ? t?o thnh m?t gi tr? duy nh?t 64-bit.
   typedef struct _FILETIME {
     DWORD dwLowDateTime;
     DWORD dwHighDateTime;
   } FILETIME;
				
Lu ? r?ng c?u trc FILETIME ?c d?a trn cc kho?ng 100-nanosecond. N l h?u ch ? xc ?nh cc bi?u t?ng sau khi lm vi?c v?i t?p l?n. Vi du:
   #define _SECOND ((int64) 10000000)
   #define _MINUTE (60 * _SECOND)
   #define _HOUR   (60 * _MINUTE)
   #define _DAY    (24 * _HOUR)
				

Th?c hi?n Arithmetics v?i t?p l?n

Ng?i ta th?ng c?n thi?t ? th?c hi?n m?t s? h?c n gi?n trn t?p l?n. Cho V d?, b?n c th? c?n ph?i bi?t khi m?t t?p tin l 30 ngy tu?i. ? th?c hi?n m?t s? h?c v? m?t th?i gian t?p tin, b?n c?n ph?i chuy?n ?i FILETIME ? quadword m?t (m?t s? nguyn 64-bit), th?c hi?n s? h?c, v sau chuy?n ?i k?t qu? Quay l?i m?t FILETIME.

Gi? s? ft l m?t c?u trc FILETIME c th?i gian sng t?o c?a m?t t?p tin, m?u m? sau y cho bi?t thm 30 ngy ? th?i gian:
   ULONGLONG qwResult;

   // Copy the time into a quadword.
   qwResult = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;

   // Add 30 days.
   qwResult += 30 * _DAY;

   // Copy the result back into the FILETIME structure.
   ft.dwLowDateTime  = (DWORD) (qwResult & 0xFFFFFFFF );
   ft.dwHighDateTime = (DWORD) (qwResult >> 32 );
				

Thi?t ?t t?p l?n

B?n c th? thi?t l?p cc t?p l?n cho m?t t?p tin b?ng cch s? d?ng ch?c nng SetFileTime().
   BOOL SetFileTime(
     HANDLE hFile,                     // Handle to the file.
     CONST FILETIME *lpCreationTime,   // Time the file was created.
     CONST FILETIME *lpLastAccessTime, // Time the file was last accessed.
     CONST FILETIME *lpLastWriteTime   // Time the file was last
                                       // written to.
   );
				
Ch?c nng ny cho php b?n s?a ?i cc sng t?o, tc quy?n truy c?p v cu?i vi?t l?n m khng thay ?i n?i dung c?a t?p tin. S? d?ng ch?c nng ny, b?n ph?i c m?t x? l? ? m? t?p tin. X? l? t?p tin ny c th? ?c l?y t? m?t g?i CreateFile() ho?c OpenFile(). T?p ph?i m? v?i Truy c?p GENERIC_WRITE. Sau khi cc l?n t?p tin ? ?c thi?t l?p, b?n nn pht hnh x? l? t?p tin thng qua m?t cu?c g?i ?n CloseHandle().

Gi? s? szFilename l m?t tn t?p h?p l? v ft l m?t c?u trc FILETIME, cc sau m?u m? b? ngy t?o cho cc t?p tin ? th?i gian ch?a trong ft:
   BOOL bResult;
   HANDLE hFile = CreateFile( szFilename,
      GENERIC_WRITE, // The file must be opened with write access.
      FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );

   if (hFile != INVALID_HANDLE_VALUE) {
      bResult = SetFileTime( hFile, &ft, NULL, NULL );
      CloseHandle(hFile);
   }
				

Hi?n th? cc t?p l?n

Trong th?i gian t?p tin d?a trn gi? ph?i h?p qu?c t? (UTC). UTC d?a trn th?i gian l?ng l?o ?c ?nh ngh?a nh ngy hi?n t?i v th?i gian trong ngy trong Greenwich, Anh. B?n s? r?t c th? mu?n hi?n th? th?i gian t?p tin v?i local (c ngh?a l, ngy thng v th?i gian trong ngy cho mi gi? c?a b?n). ? lm i?u ny, b?n c th? s? d?ng FileTimeToLocalFileTime() nh sau:
   BOOL FileTimeToLocalFileTime(
     CONST FILETIME *lpFileTime,  // Pointer to UTC file time to convert.
     LPFILETIME lpLocalFileTime   // Pointer to converted file time.
   );
Lu ? r?ng hm ny s? d?ng cc thi?t ?t hi?n th?i cho mi gi? v th?i gian ti?t ki?m nh sng ban ngy. V? v?y, n?u n l nh sng ban ngy ti?t ki?m th?i gian, i?u ny ch?c nng s? m?t th?i gian ti?t ki?m nh sng ban ngy vo ti kho?n, ngay c? khi th?i gian b?n ang chuy?n ?i l trong th?i gian tiu chu?n.

? hi?n th? th?i gian t?p tin m?t cch c ? ngh?a, tr?c tin b?n c?n ? chuy?n ?i n ? m?t th?i gian h? th?ng b?ng cch s? d?ng FileTimeToSystemTime() nh sau:
   BOOL FileTimeToSystemTime(
     CONST FILETIME *lpFileTime, // Pointer to file time to convert.
     LPSYSTEMTIME lpSystemTime   // Pointer to structure to receive
   );                            // system time.
				
C?u trc SYSTEMTIME ?i di?n cho m?t ngy v th?i gian b?ng cch s? d?ng c nhn cc thnh vin cho thng, ngy, nm, ngy trong tu?n, gi?, pht, giy, v millisecond.

N c?ng l thch h?p hn ? hi?n th? ngy thng v th?i gian trong m?t ?nh d?ng ph h?p v?i mi?n ?a phng hi?n t?i ?c ch?n cho h? th?ng. B?n c th? lm i?u ny b?ng cch s? d?ng GetDateFormat() v GetTimeFormat() nh sau:
   int GetDateFormat(
     LCID Locale,              // Locale for which date is to be formatted.
     DWORD dwFlags,            // Flags specifying function options.
     CONST SYSTEMTIME *lpDate, // Date to be formatted.
     LPCTSTR lpFormat,         // Date format string.
     LPTSTR lpDateStr,         // Buffer for storing formatted string.
     int cchDate               // Size of buffer.
   );

   int GetTimeFormat(
     LCID Locale,              // Locale for which time is to be formatted.
     DWORD dwFlags,            // Flags specifying function options.
     CONST SYSTEMTIME *lpTime, // Time to be formatted.
     LPCTSTR lpFormat,         // Time format string.
     LPTSTR lpTimeStr,         // Buffer for storing formatted string.
     int cchTime               // Size of buffer.
   );
				
B?ng cch i qua LOCALE_USER_DEFAULT nh tham s? ?u tin ? cc ch?c nng ny, b?n ni v?i h? ? ?nh d?ng thng qua ngy/gi? theo ? m?c ?nh ?nh d?ng cho mi?n ?a phng hi?n t?i. Trong tr?ng h?p ny, b?n c th? v?t qua NULL cc lpFormat tham s?.

Gi? ft l m?t c?u trc FILETIME c ch?a m?t gi tr? UTC, sau y m?u m? in ngy lu tr? trong ft:
   SYSTEMTIME st;
   char szLocalDate[255], szLocalTime[255];

   FileTimeToLocalFileTime( &ft, &ft );
   FileTimeToSystemTime( &ft, &st );
   GetDateFormat( LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
     szLocalDate, 255 );
   GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, szLocalTime, 255 );
   printf( "%s %s\n", szLocalDate, szLocalTime );
				

Thu?c tnh

ID c?a bi: 188768 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Win32 Application Programming Interface, khi ?c dng v?i:
    • Microsoft Windows 98 Standard Edition
    • Microsoft Windows Millennium Edition
T? kha:
kbapi kbdatetime kbfileio kbinfo kbkernbase kbmt KB188768 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:188768

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com