一、系统时间和本地时间
系统时间:即世界时间,所有地区的系统时间都是一致的
本地时间:由于时间分区,本地时间代表当地时区的时间
/*
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
*/
SYSTEMTIME sysTime = {0};
SYSTEMTIME localTime = {0};
//获取系统时间。和GetLocalTime不同,该函数返回的为UTC时间
GetSystemTime(&sysTime);
//获取当地时间。
GetLocalTime(&localTime );
//中国时区的信息
TI
ME_ZONE_INFORMATION DEFAULT_TIME_ZONE_INFORMATION = {-480};
//将UTC时间转换为中国失去的本地时间
SystemTimeToTzSpecificLocalTime(&DEFAULT_TIME_ZONE_INFORMATION,&sysTime,&localTime);
TIME_ZONE_INFORMATION的时差是以分钟为单位的,UTC时间比北京时间晚8个小时,所以需要减去8个小时:8*60=480分。其他时区,可以依次类推进行更改。
二、CRT时间
time_t time( time_t *timer);
time_t类型为32位或64位整型,具体类型由编译系统决定。
此函数用来获得从1970年1月1日子夜(这个时刻在不同的CRT实现中可能会不一样)到当前时刻以来所流逝的时间,以秒为单位。这个时间差叫做日历时间(Calendar Time ),即CRT时间。
需要借助time来获取CRT日历时间,利用localtime获取可读的日期信息
#if(0)
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#endif
//tm_wday,0:代表星期日,6:代表星期六
struct tm * pNowTime;
time_t time;
time(&time);//获取CRT时间,精度为秒
struct tm * pNowTime=localtime(ime_t *time);//转换为可读的CRT时间
三、文件时间
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME;
文件时间(FileTime)的存储方式则与本地时间、系统时间有些不同,它使用64位的数据长度存储。
引用MSDN上面的一句原话“A file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).”
这个64位的值记录了自1601年1月1日0点以来的以100纳秒(ns)为单位的格林威治时间间隔!将这个数据转换为秒的话要除以10^7(1秒 = 10^9纳秒,这里是100纳秒单位)
请注意 FILETIME 结构基于 100 纳秒为时间间隔。最好使用文件的时间时,定义下列符号。例如:
#define _SECOND ((int64) 10000000)
#define _MINUTE (60 * _SECOND)
#define _HOUR (60 * _MINUTE)
#define _DAY (24 * _HOUR)
执行 Arithmetics 文件时间
另外,往往有必要对文件的时间执行简单的算术运算。例如,您可能需要知道文件后 30 天。文件时间执行算术运算,您需要将 FILETIME 转换为 quadword (64 位整数),执行算术运算,然后将结果转换回 FILETIME。
假定包含文件的创建时间的 FILETIME 结构英尺,则下面的代码示例添加到时间 30 天:
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 );
设置文件时间
您可以通过使用 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.
);
此函数,可以修改创建、 上次访问,以及上次写入时间,而不更改文件的内容。若要使用此功能,必须打开的文件有句柄。此文件句柄可以从调用 CreateFile() 或 OpenFile()。以 GENERIC_WRITE 访问权限,必须打开该文件。在设置文件时间之后,应释放通过调用 CloseHandle() 的文件句柄。
假定 szFilename 是一个有效的文件名和英尺 FILETIME 结构,下面的代码示例将该文件的创建日期设置为英尺中包含的时间:
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);
}
显示文件的时间
文件时间基于协调通用时间 (UTC)。在英国格林威治标准,基于 UTC 时间松散定义为当前日期和时间。您很可能希望显示文件时间相对于本地时间 (即,日期和您所在时区的时间)。若要执行此操作,可以使用下列 FileTimeToLocalFileTime():
BOOL FileTimeToLocalFileTime(
CONST FILETIME *lpFileTime, // Pointer to UTC file time to convert.
LPFILETIME lpLocalFileTime // Pointer to converted file time.
);
请注意此函数使用的当前设置所在的时区与夏时制。因此,如果它是夏时制,此函数将考虑夏时制,即使您要转换的时间是标准时间。
以有意义的方式显示文件时,首先需要将其转换为系统时间,使用 FileTimeToSystemTime(),如下所示:
BOOL FileTimeToSystemTime(
CONST FILETIME *lpFileTime, // Pointer to file time to convert.
LPSYSTEMTIME lpSystemTime // Pointer to structure to receive
); // system time.
引用:FILE文件结构