Windows获取时间函数(使用GetLocalTime,GetSystemTime,SystemTimeToTzSpecificLocalTime,GetFileTime API函数

获取本地时间

typedef struct _SYSTEMTIME {  
  WORD wYear;  
  WORD wMonth;  
  WORD wDayOfWeek;  
  WORD wDay;  
  WORD wHour;  
  WORD wMinute;  
  WORD wSecond;  
  WORD wMilliseconds;
} SYSTEMTIME,  
  *PSYSTEMTIME;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1.GetLocalTime获取的是本地时区时间

SYSTEMTIME localSysTime;
GetLocalTime(&localSysTime);
  • 1
  • 2

2.GetSystemTime获取的是UTC时间

SYSTEMTIME utcSysTime;
GetSystemTime(&utcSysTime);
  • 1
  • 2

Note: 
UTC(Universal Time Coordinated),协调世界时,又称世界标准时间或世界协调时间. 
UTC与格林尼治平均时一样,都与英国伦敦的本地时相同. 
整个地球分为二十四个时区,每个时区都有自己的本地时间. 
北京时区是东八区,领先UTC八个小时.(UTC+8) 
伦敦时间为UTC+0. 
也就是说,若全球标准时间是2012-07-04 00:00:00,则北京时间为2012-07-04 08:00:00.

3.UTC时间和具体时区时间的转换

<1> UTC Time –> Local Time

BOOL WINAPI SystemTimeToTzSpecificLocalTime(
  __in          LPTIME_ZONE_INFORMATION lpTimeZone,
  __in          LPSYSTEMTIME lpUniversalTime,
  __out         LPSYSTEMTIME lpLocalTime
);
  • 1
  • 2
  • 3
  • 4
  • 5

lpTimeZone 
A pointer to a TIME_ZONE_INFORMATION structure that specifies the time zone of interest. 
If lpTimeZone is NULL, the function uses the currently active time zone. 
所以将lpTimeZone设为NULL就会将UTC时间转换为本地时间

<2> Local Time –> UTC Time

BOOL WINAPI TzSpecificLocalTimeToSystemTime(
  __in          LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
  __in          LPSYSTEMTIME lpLocalTime,
  __out         LPSYSTEMTIME lpUniversalTime
);
  • 1
  • 2
  • 3
  • 4
  • 5

类同.

File Time

我们在Windows系统中获取文件的创建时间,存取时间,修改时间可以使用下面的API.

BOOL WINAPI GetFileTime(
  __in          HANDLE hFile,
  __out         LPFILETIME lpCreationTime,
  __out         LPFILETIME lpLastAccessTime,
  __out         LPFILETIME lpLastWriteTime
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

获取的时间为UTC FILETIME.

typedef struct _FILETIME {  
  DWORD dwLowDateTime;  
  DWORD dwHighDateTime;
} FILETIME, *PFILETIME;
  • 1
  • 2
  • 3
  • 4

Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601. 
表示从时间1601-06-01起的100纳秒间隔数.

对于时间的显示,使用SYSTEMTIME为好. 
而对于时间的计算&比较,使用FILETIME为好.

Note:使用GetFileTime获取的FILETIME为UTC FILETIME.

FILETIME比较函数:

LONG WINAPI CompareFileTime(
  __in          const FILETIME* lpFileTime1,
  __in          const FILETIME* lpFileTime2
);
  • 1
  • 2
  • 3
  • 4

-1— First file time is earlier than second file time. 
0 —- First file time is equal to second file time. 
1 —- First file time is later than second file time.

FILETIME <–> SYSTEMTIME

FileTimeToSystemTime 
SystemTimeToFileTime 
FileTimeToLocalFileTime 
LocalFileTimeToFileTime

对于利用GetFileTime获取的UTC FILETIME怎样转换为Local SYSTEMTIME 
GetFileTime–>UTC FILETIME–>(FileTimeToSystemTime)–>UTC SYSTEMTIME–>(SystemTimeToTzSpecificLocalTime)–>Local SYSTEMTIME 
GetFileTime–>UTC FILETIME–>(FileTimeToLocalFileTime)–>Local FILETIME–>(FileTimeToSystemTime)–>Local SYSTEMTIME

时间间隔的运算

将FILETIME–>LARGE_INTEGER,再通过LARGE_INTEGER进行运算

typedef union _LARGE_INTEGER {  
  struct {    
    DWORD LowPart;    
    LONG HighPart;  
  };  
  struct {    
    DWORD LowPart;    
    LONG HighPart;  
  } u;  
  LONGLONG QuadPart;
} LARGE_INTEGER,  *PLARGE_INTEGER;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

eg:

FILETIME time1;
FILETIME time2;
LARGE_INTEGER liTime1;
LARGE_INTEGER liTime2;
liTime1.LowPart = time1.dwLowDateTime;
liTime1.HighPart = time1.dwHighDateTime;
liTime2.LowPart = time2.dwLowDateTime;
liTime2.HighPart = time2.dwHighDateTime;
LARGE_INTEGER liElapsedTime;
liElapsedTime.QuadPart = liTime2.QuadPart - liTime1.QuadPart;
liElapsedTime.QuadPart /= 10000000;  //相差的秒数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

时间的转换公式: 
1微秒 = 1000 纳秒 
1毫秒 = 1000 微秒 
1秒 = 1000毫秒

 

http://blog.csdn.net/hisinwang/article/details/45116133

posted @ 2018-01-18 00:41  findumars  Views(7058)  Comments(0Edit  收藏  举报