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 创建同 ...
随机推荐
- Java 之 Web前端(五)
1.过滤器 a.定义:是一个中间组件,用于拦截源数据和目的数据之间的消息,并过滤二者之间传递的数据 b.步骤: ①建class继承Filter实现抽象方法 public class EncodingF ...
- BZOJ BLO 1123 (割点)【双连通】
<题目链接> 以下内容转自李煜东的<算法竞赛进阶指南> 题目大意:现在给定一张连通的无向图,不包含重边.现在输出$n$个整数,表示将第$i$个节点的所有与其它节点相关联的边去掉 ...
- mysql 数据库(二)数据库的基本操作
mysql 数据库(二)数据库的基本操作 用户管理,添加权限,创建,显示,使用数据库 1 显示数据库:show databases; 默认数据库: mysql - 用户权限相关数据 test - 用于 ...
- Rabbit的机器人-二分答案
Rabbit的机器人 思路 : 可以 推知 挡板的位置与最后 一步的方向有关 .如果是 R 根据题目要求那么最终结果一定是在>0的位置, 因为按照题意要求的最终不能回到重复走过的位置.所以如果有 ...
- SpringMVC(二五) JSTL View
项目中使用JSTL,SpringMVC会把视图由InternalView转换为JstlView. 若使用Jstl的fmt标签,需要在SpringMVC的配置文件中配置国际化资源文件. 实现过程: 1. ...
- CodeForces - 1016D 补零思想
题目连接: https://vjudge.net/problem/1753263/origin 其实这道题跟行列式里的分块发有点类似,但也是类似罢了. 主要的思想是每一行,每一列的第一行(或者最后一行 ...
- Centos 安装lnmp完整版
1.使用putty或类似的SSH工具登录服务器: 登录后运行 screen -S lnmp 2.下载并安装LNMP一键安装包: 我是CentOS系统,所以运行: wget -c http://soft ...
- [BZOJ3683]Falsita
[BZOJ3683]Falsita 题目大意: 一个\(n(n\le3\times10^5)\)个结点的树,每个结点有一个权值\(w_i\),\(m(m\le3\times10^5)\)次操作,操作包 ...
- Django 自定义模板语法
from django import template from django.utils.safestring import mark_safe register = template.Librar ...
- nginx -s reload时出现open() "/run/nginx.pid" failed (2: No such file or directory)错误
解决办法: 找到你的nginx.conf的文件夹目录,比如我的为/etc/nginx/nginx.conf,然后运行这个 nginx -c /etc/nginx/nginx.conf命令, 再运行 ...