WINDOWS API ——GETFILETIME——获取文件时间
GetSystemTime(LPSYSTEMTIME lpSystemTime)
得到系统时间,这个时间是标准的UTC时间,也就是没有包含任何时区的时间的
GetLocalTime(LPSYSTEMTIME lpSystemTime)
得到当前时区的时间,它获取的是系统设置的地区的当地时间
FILETIME结构包含了文件或目录的日期和时间信息:(自1601年1月1日以来,单位为100纳秒)
typedef struct _FILETIME {
DWORD dwLowDateTime; //低32位
DWORD dwHighDateTime; //高32位
} FILETIME, *PFILETIME;
SYSTEMTIME结构包含了用户可识别的系统日期信息:
typedef struct _SYSTEMTIME {
WORD wYear;//年
WORD wMonth;//月
WORD wDayOfWeek;//一周的第几天
WORD wDay;//日
WORD wHour;//小时
WORD wMinute;//分
WORD wSecond;//秒
WORD wMilliseconds;//毫秒
} SYSTEMTIME, *PSYSTEMTIME;
=======================================================
函数FileTimeToSystemTime用来将文件时间格式转换为标准系统时间格式:
BOOL WINAPI FileTimeToSystemTime( __in const FILETIME *lpFileTime, //文件时间 __out LPSYSTEMTIME lpSystemTime //系统时间 );
函数FileTimeToLocalTime用来将文件时间格式转换为本地文件时间:
BOOL WINAPI FileTimeToLocalFileTime(
__in const FILETIME* lpFileTime,//文件时间
__out LPFILETIME lpLocalFileTime//本地文件时间
);
函数SystemTimeToFileTime则是将标准系统时间转换成文件时间格式:
BOOL WINAPI SystemTimeToFileTime(
__in const SYSTEMTIME *lpSystemTime,//系统时间
__out LPFILETIME lpFileTime//文件时间
);
函数SystemTimeToTzSpecificLocalTime是将标准系统时间转换为本地系统时间
BOOL WINAPI SystemTimeToTzSpecificLocalTime(
__in LPTIME_ZONE_INFORMATION lpTimeZone,//时区结构
__in LPSYSTEMTIME lpUniversalTime,//系统时间
__out LPSYSTEMTIME lpLocalTime//本地时间
);
=======================================================
GetSystemTime函数用来获得系统时间:
void WINAPI GetSystemTime(
__out LPSYSTEMTIME lpSystemTime
);
GetFileTime函数用来获得一个文件或目录的创建的时间、最后访问的时间以及最后修改的时间:
BOOL WINAPI GetFileTime(
__in HANDLE hFile, //文件或目录句柄
__out_opt LPFILETIME lpCreationTime, //返回的创建的日期和时间信息
__out_opt LPFILETIME lpLastAccessTime, //返回的最后访问的日期和时间信息
__out_opt LPFILETIME lpLastWriteTime //返回的最后修改的日期和时间信息 );
实例:
CString strPath("D:\\test.txt");
HANDLE hFile = CreateFile(strPath,
GENERIC_WRITE| GENERIC_READ, //必须有GENERIC_READ属性才能得到时间
FILE_SHARE_READ,
NULL,
TRUNCATE_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
SYSTEMTIME sysTime;
GetSystemTime(&sysTime);//这里得到的时间是标准系统时间,也就是0时区的时间。
GetLocalTime(&sysTime);//这里得到的是本地时间,也就是标准时间+时区时间
FILETIME fCreateTime, fAccessTime, fWriteTime;
GetFileTime(&hFile, &fCreateTime, &fAccessTime, &fWriteTime);//获取文件时间
CString strTime;
//将文件时间转换为本地系统时间的两种方式:
//(1)
FileTimeToLocalFileTime(&fCreateTime,&localTime);//将文件时间转换为本地文件时间
FileTimeToSystemTime(&localTime, &sysTime);//将文件时间转换为本地系统时间
//(2)
FileTimeToSystemTime(&fCreateTime, &sysTime);//将文件时间转换为标准系统时间
SystemTimeToTzSpecificLocalTime(&sysTime, &sysTime)//将标准系统时间转换为本地系统时间
strTime.Format(_T("%4d年%2d月%2d日,%2d:%2d:%2d"),
sysTime.wYear,
sysTime.wMonth,
sysTime.wDay,
sysTime.wHour,
sysTime.wMinute,
sysTime.wSecond
);
}
修文件创建时间,例子:
#include <Windows.h>
#include <stdio.h> bool ConvertFileTimeToLocalTime(const FILETIME *lpFileTime, SYSTEMTIME *lpSystemTime)
{
if (!lpFileTime || !lpSystemTime) {
return false;
}
FILETIME ftLocal;
FileTimeToLocalFileTime(lpFileTime, &ftLocal);
FileTimeToSystemTime(&ftLocal, lpSystemTime);
return true;
} bool ConvertLocalTimeToFileTime(const SYSTEMTIME *lpSystemTime, FILETIME *lpFileTime)
{
if (!lpSystemTime || !lpFileTime) {
return false;
} FILETIME ftLocal;
SystemTimeToFileTime(lpSystemTime, &ftLocal);
LocalFileTimeToFileTime(&ftLocal, lpFileTime);
return true;
} int main()
{
HANDLE hFile;
FILETIME ftCreate, ftAccess, ftWrite;
SYSTEMTIME stCreate, stAccess, stWrite;
int year, month, day; hFile = CreateFile(L"C:\\1.txt", GENERIC_READ | GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
printf("CreateFile error: %d", GetLastError());
ExitProcess();
}
GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite);
ConvertFileTimeToLocalTime(&ftCreate, &stCreate);
ConvertFileTimeToLocalTime(&ftAccess, &stAccess);
ConvertFileTimeToLocalTime(&ftWrite, &stWrite); printf("yyyy-MM-dd:");
scanf("%d-%d-%d", &year, &month, &day);
stAccess.wYear = stWrite.wYear = year;
stAccess.wMonth = stWrite.wMonth = month;
stAccess.wDay = stWrite.wDay = day; ConvertLocalTimeToFileTime(&stAccess, &ftAccess);
ConvertLocalTimeToFileTime(&stWrite, &ftWrite); SetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite);
CloseHandle(hFile);
return ;
}
WINDOWS API ——GETFILETIME——获取文件时间的更多相关文章
- Windows API教程文件系统
本篇文章主要介绍了"Windows API教程文件系统",主要涉及到Windows API教程文件系统方面的内容,对于Windows API教程文件系统感兴趣的同学可以参考一下. ...
- Windows API 函数列表 附帮助手册
所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...
- 在VBA中使用Windows API
VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...
- WinSpy涉及的windows api
WinSpy涉及的windows api WinSpy是仿造微软Spy++的开源项目,但只涉及Spy++的窗口句柄.窗口的属性.styles.类名子窗口.进程线程信息等查找功能.功能虽然不算强大,但涉 ...
- Windows API Finishing
input { font-size: 14px; height: 26px } td { border-style: none; border-color: inherit; border-width ...
- Windows API函数大全(完整)
Windows API函数大全,从事软件开发的朋友可以参考下 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一 ...
- Windows API Hook
原文地址:http://blog.sina.com.cn/s/blog_628821950100xmuc.html 原文对我的帮助极大,正是由于看了原文.我才学会了HOOK.鉴于原文的排版不是非常好, ...
- [windows菜鸟]Windows API函数大全(完整)
Windows API函数大全,从事软件开发的朋友可以参考下 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一 ...
- WINDOWS API 大全(一)
1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同 ...
随机推荐
- POJ 3090 Visible Lattice Points 【欧拉函数】
<题目链接> 题目大意: 给出范围为(0, 0)到(n, n)的整点,你站在(0,0)处,问能够看见几个点. 解题分析:很明显,因为 N (1 ≤ N ≤ 1000) ,所以无论 N 为多 ...
- ECMAScript6 入门 函数的扩展
为函数参数设定默认值 function log(x, y = 'World') { console.log(x, y); } log('Hello') // Hello World log('Hell ...
- 团队协作统一vue代码风格,vscode做vue项目时的一些配置
1. 安装Vetur 扩展 主要是用于让vscode能识别vue文件,对vue代码进行高丽处理,并且它内置了一些代码格式化的设置 2. 安装ESLint 如果你的项目已经开启了eslint规范, 再有 ...
- 利用svg描边+css3实现边框逐渐消失小动画
首先简单的描述一下svg中两个属性: stroke-dasharray:表示每个虚线的长短. stroke-dashoffset:表示首个虚线的偏移量. 当两者都特别大的时候就会消失掉 直接上代码: ...
- 机器学习模型从windows下 spring上传到预发布会导致模型不可加载
1.通过上传到redis,程序通过redis拉取模型,解决问题. 2.问题原因初步思考为windows下模型文件上传到 linux导致,待继续跟进查找.
- 一道有意思的找规律题目 --- CodeForces - 964A
题目连接: https://vjudge.net/problem/1502082/origin 这一题第一眼看过去貌似是模拟,但是根据其范围是1e9可以知道,如果暴力解基本上是不可能的(不排除大佬级优 ...
- windows提交代码到git仓库
进入git bash git config --global user.name '仓库名' git config --global user.email '2531099@163.com' git ...
- 原生CSS设置网站主题色—CSS变量赋值
定义CSS变量 在css文件顶部定义css变量,注意必须以--开头,使用:root包括这几个变量 :root { --main-bg-color: #ff7675; --color1: #fbfee9 ...
- leetCode中老出现的针对一个int中每个数字的处理
一个int比如322,我想找happy number就得3平方加2平方再加2平方,怎样找到一个一个的数字,就是322%10,得到2,然后/10,然后再%,就可以依次求得每位上的数字 happy num ...
- win7生成ssh key配置到gitlab
测试服务上使用ip访问gitlab,比如http://192.168.0.2/,创建用户并登陆后创建一个项目,比如git@gitlab.demo.com:demo/helloworld.git 如果想 ...