Windows系统时间(FILETIME和SYSTEMTIME)
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8654298
欢迎关注微博:http://weibo.com/MoreWindows
前面的《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;
- }
程序运行结果如下:

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8654298
欢迎关注微博:http://weibo.com/MoreWindows
http://blog.csdn.net/morewindows/article/details/8654298
Windows系统时间(FILETIME和SYSTEMTIME)的更多相关文章
- python有超时的windows系统时间设置代码
手边的笔记本用久了,cmos电池可能又没电了.每次开机时间都不对,导致访问一些有https的网页会出现警告信息. 于是找了找通过python脚本设置系统时间的方法,发现了两种,其一是调用socket直 ...
- [2014.5.22][UBUNTU]Ubuntu与Windows系统时间不同步的问题
安装Ubuntu+Windows双系统时会遇到Windows和Ubuntu系统时间不同步的问题,这是由于Windows系统默认读取主板bios等硬件系统时间作为OS的当地时间;而MAc,Linux类的 ...
- Windows系统时间会偶尔自动回拨吗?
为什么80%的码农都做不了架构师?->>> Spring boot 项目 通过日志记录插入sql操作用时 long start2 = System.currentTimeMi ...
- windows系统时间(SYSTEMTIME)
GetSystemTime函数获得当前的UTC时间,GetLocalTime获得当前的本地时间 UTC是协调世界时(Universal Time Coordinated)英文缩写,是由国际无线电咨询委 ...
- 批量自动修改windows系统时间
windows下测试时,也许你的系统有一个功能,需要将服务器时间改到未来的某一天,但由于每一天可能都有定时的任务要走,所以直接改到未来某一天,可能系统或数据会不正常,需要一天一天改直到那一天. 如果人 ...
- bat 同步windows系统时间
需要使用管理员权限运行 net start w32timew32tm /config /updatew32tm /resync /rediscovernet stop w32timepause
- Windows/Linux双系统时间错乱问题
问题描述 安装双系统后,切换系统的时候(Windows)系统时间会错乱 解决方式 百度经验Win/Lin 双系统时间错误的调整 注意1:最后两步更改硬件UTC时间 注意2:适用Windows系统为Wi ...
- VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
随机推荐
- MHA 一主两从搭建-脚本VIP-自动切换
环境介绍:主机名 IP MHA角色 MySQL角色node1 192.168.56.26 Node MySQL Master node2 192.168.56.27 Node MySQL Master ...
- C++中string类的操作函数。
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- 【55.70%】【codeforces 557A】Ilya and Diplomas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 小雷FansUnion:我有了第一个付费客户(第一个徒弟)
很高兴地告诉大家一个振奋人心的消息,我刚刚拥有了第一个付费客户. 第一个付费客户是山东青岛的一个上班族,有2年.Net经验,今年转Java开发.对我比较信任,在我的建议下,选择了"拜师学艺& ...
- Android JNI编程(一)——JNI概念以及C语言Dev-C++开发环境搭建、编写HelloWorld
版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 一:JNI是什么呢? JNI:JNI是JavaNative Interfac ...
- CSS自己主动换行、强制不换行、强制断行、超出显示省略号
P标签是默认是自己主动换行的,因此设置好宽度之后,可以较好的实现效果,可是近期的项目中发现,使用ajax载入数据之后.p标签内的内容没有换行,导致布局错乱,于是尝试着使用换行样式,尽管攻克了问题.可是 ...
- AndroidStudio如何配置NDK/JNI开发环境
参考文章: http://www.th7.cn/Program/Android/201509/550864.shtml http://www.open-open.com/lib/view/open14 ...
- C++网络编程方面的开源项目
Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力. ...
- [Angular] Export directive functionalities by using 'exportAs'
Directive ables to change component behaives and lookings. Directive can also export some APIs which ...
- QPalette实例教程(QWidget自带的颜色设置工具,对Window的各个部分都可设置颜色)
QPalette是一款非常好用的颜色设置工具: 头文件:#include <QPalette> (^-^我没有用这个头文件也可以使用QPalette) 常用函数: void setBrus ...