Windows系统时间(FILETIME和SYSTEMTIME)
来源:互联网 发布:旋风冲锋龙卷风知乎 编辑:程序博客网 时间:2024/05/01 06:30
前面的《Windows 各种计时函数总结》介绍了Windows系统常用的5种计时函数——标准C/C++下的time()及clock(),在Windows系统下的API接口timeGetTime()、GetTickCount()及QueryPerformanceCounter()。下面来介绍下Windows系统中表示时间的两个结构体——FILETIME和SYSTEMTIME及相关函数。
先来看看这两个结构体的定义:
1. FILETIME
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
typedef struct _FILETIME {
DWORDdwLowDateTime;
DWORDdwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;
它在MSDN上的说明——Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
2. SYSTEMTIME
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
typedef struct _SYSTEMTIME {
WORDwYear;
WORDwMonth;
WORDwDayOfWeek;
WORDwDay;
WORDwHour;
WORDwMinute;
WORDwSecond;
WORDwMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
这个就不用解释了,和大家平常表示时间的方法一样,都是日期(年-月-日)加时间(小时:分钟:秒)
与这两个结构体相关的函数主有6个——GetSystemTime、GetLocalTime、SystemTimeToFileTime、FileTimeToSystemTime、LocalFileTimeToFileTime、FileTimeToLocalFileTime。下面来看看这些函数的用法:
一.得到当前UTC时间
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
void GetSystemTime(LPSYSTEMTIMElpSystemTime);
二.得到当地时间
void GetLocalTime(LPSYSTEMTIMElpSystemTime);
三.SYSTEMTIME转成FILETIME
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
BOOLSystemTimeToFileTime(
const SYSTEMTIME* lpSystemTime,
LPFILETIMElpFileTime
);
四.FILETIME转成SYSTEMTIME
BOOLFileTimeToSystemTime(
const FILETIME* lpFileTime,
LPSYSTEMTIMElpSystemTime
);
五.当地时间转成UTC时间
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
BOOLLocalFileTimeToFileTime(
const FILETIME* lpLocalFileTime,
LPFILETIMElpFileTime
);
六.UTC时间转成当地时间
BOOLFileTimeToLocalFileTime(
const FILETIME* lpFileTime,
LPFILETIMElpLocalFileTime
);
下面再给出一个示例,这个示例主要使用两个功能:
1.对获取当前当地系统时间
2.打开一个文件,并将文件的创建时间,修改时间,访问时间从FILETIME类型转成SYSTEMTIME类型。
完整代码如下:
- // Windows系统时间(FILETIME和SYSTEMTIME)
- //By MoreWindows-(http://blog.csdn.net/morewindows/article/details/8654298)
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- class CWindowsDateAndTime
- {
- public:
- static void GetCurrentLocalSystemTime(char *pstrDate, char *pstrTime);
- static void FileTimeToLocalSystemTime(FILETIME &ft, char *pstrDate, char *pstrTime);
- };
- //得到当前当地时间
- void CWindowsDateAndTime::GetCurrentLocalSystemTime(char *pstrDate, char *pstrTime)
- {
- SYSTEMTIME st;
- GetLocalTime(&st);
- if (pstrDate != NULL)
- sprintf(pstrDate, "%d-%d-%d", st.wYear, st.wMonth, st.wDay);
- if (pstrTime != NULL)
- sprintf(pstrTime, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond);
- }
- //文件时间转成当地时间
- void CWindowsDateAndTime::FileTimeToLocalSystemTime(FILETIME &ft, char *pstrDate, char *pstrTime)
- {
- FILETIME localft;
- FileTimeToLocalFileTime(&ft, &localft);
- SYSTEMTIME st;
- FileTimeToSystemTime(&localft, &st);
- if (pstrDate != NULL)
- sprintf(pstrDate, "%d-%d-%d", st.wYear, st.wMonth, st.wDay);
- if (pstrTime != NULL)
- sprintf(pstrTime, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond);
- }
- int main(int argc, char *argv[])
- {
- printf(" Windows系统时间(FILETIME和SYSTEMTIME) \n");
- printf(" -- By MoreWindows( http://blog.csdn.net/morewindows/article/details/8654298 ) --\n\n");
- const int MAX_LEN = 30;
- char strDate[MAX_LEN], strTime[MAX_LEN];
- CWindowsDateAndTime::GetCurrentLocalSystemTime(strDate, strTime);
- printf("当前系统时间: %s %s\n", strDate, strTime);
- const char* pstrFileName = "D:\\MoreWindows.txt";
- printf("文件%s:\n", pstrFileName);
- HANDLE handleFile = CreateFile(pstrFileName, GENERIC_READ,
- FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
- FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime;
- GetFileTime(handleFile, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime);
- CWindowsDateAndTime::FileTimeToLocalSystemTime(ftCreationTime, strDate, strTime);
- printf("创建时间: %s %s\n", strDate, strTime);
- CWindowsDateAndTime::FileTimeToLocalSystemTime(ftLastAccessTime, strDate, strTime);
- printf("访问时间: %s %s\n", strDate, strTime);
- CWindowsDateAndTime::FileTimeToLocalSystemTime(ftLastWriteTime, strDate, strTime);
- printf("修改时间: %s %s\n", strDate, strTime);
- getch();
- return 0;
- }
程序运行结果如下:
- Windows系统时间(FILETIME和SYSTEMTIME)
- Windows系统时间(FILETIME和SYSTEMTIME)
- Windows系统时间(FILETIME和SYSTEMTIME)
- Windows系统时间(FILETIME和SYSTEMTIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME) .
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)
- Windows中的时间(SYSTEMTIME和FILETIME)&CRT中的时间(time_t和tm)
- Windows中如何计算时间间隔(2)使用SYSTEMTIME和FILETIME
- Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换
- Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换
- Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换
- 使用DLL文件生成LIB文件实现动态链接
- OBJECT TO HEX FILE CONVERTER OH51 V2.6 COPYRIGHT KEIL ELEKTRONIK GmbH 1991 - 2001问题的解决
- css练手-android机器人
- CString截取字符串全攻略
- Mahout架构初探及KMeans算法分布式实现的研究
- Windows系统时间(FILETIME和SYSTEMTIME)
- 一份比较详细的DOS命令说明
- 3.easyui+ztree案例:使用easyui布局
- 适用于Java程序员的通信顺序进程
- iframe去边框,无边框,使用大全
- Java线程详解
- linux 内核参数调整优化网络
- _lread(),_lclose()_lseek()三个函数
- 用导电颜料画乐器来“纸上弹琴”,Arduino新玩法