filetime,systemtime相互转化,获取文件创建时间,访问时间,修改时间,获取指定时间之前之后的SYSTEMTIME
// deleteOldFiles.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
#pragma warning(disable:4996)
int main()
{
//typedef struct _SYSTEMTIME {
// WORDwYear;
// WORDwMonth;
// WORDwDayOfWeek;
// WORDwDay;
// WORDwHour;
// WORDwMinute;
// WORDwSecond;
// WORDwMilliseconds;
//} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
//得到当前UTC时间
SYSTEMTIME st;
GetSystemTime(&st);
char buf[128] = { 0 };
sprintf_s(buf,"%04d-%02d-%02d %02d:%02d:%02d:%03d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
cout << "UTC时间为" << buf << endl;
//得到本地时间
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", stLocal.wYear, stLocal.wMonth, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds);
cout << "本地时间为" << buf << endl;
//FileTime:Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC时间).
//从1601到现在经历的100纳秒数,8个字节变量
//typedef struct _FILETIME {
// DWORDdwLowDateTime;
// DWORDdwHighDateTime;
//} FILETIME, *PFILETIME, *LPFILETIME;
//SYSTEMTIME转成FILETIME
//BOOL
// WINAPI
// SystemTimeToFileTime(
// _In_ CONST SYSTEMTIME * lpSystemTime,
// _Out_ LPFILETIME lpFileTime
// );
//SYSTEMTIME转FILETIME
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
//FILETIME转SYSTEMTIME
SYSTEMTIME st2;
FileTimeToSystemTime(&ft, &st2);
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", st2.wYear, st2.wMonth, st2.wDay, st2.wHour, st2.wMinute, st2.wSecond, st2.wMilliseconds);
cout << "UTC时间为" << buf << endl;
//当地时间转UTC时间
FILETIME ftLocal;
SystemTimeToFileTime(&stLocal, &ftLocal);
LocalFileTimeToFileTime(&ftLocal, &ft);
FileTimeToSystemTime(&ft, &st2);
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", st2.wYear, st2.wMonth, st2.wDay, st2.wHour, st2.wMinute, st2.wSecond, st2.wMilliseconds);
cout << "UTC时间为" << buf << endl;
FileTimeToLocalFileTime(&ft, &ftLocal);
FileTimeToSystemTime(&ftLocal, &stLocal);
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", stLocal.wYear, stLocal.wMonth, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds);
cout << "本地时间为" << buf << endl;
//获取十天之前的日期
ULARGE_INTEGER u1,offset,u2;
offset.QuadPart = UInt32x32To64(10 * 24 * 3600, 10000000);
u1.LowPart = ftLocal.dwLowDateTime;
u1.HighPart = ftLocal.dwHighDateTime;
u2.QuadPart = u1.QuadPart - offset.QuadPart;
ftLocal.dwLowDateTime = u2.LowPart;
ftLocal.dwHighDateTime = u2.HighPart;
SYSTEMTIME stOld;
FileTimeToSystemTime(&ftLocal, &stOld);
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", stOld.wYear, stOld.wMonth, stOld.wDay, stOld.wHour, stOld.wMinute, stOld.wSecond, stOld.wMilliseconds);
cout << "十天前时间为" << buf << endl;
//获取文件的创建时间,修改时间,访问时间
HANDLE handleFile = CreateFileW(L"F:\\output\\测试文档.docx", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (handleFile != INVALID_HANDLE_VALUE)
{
FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime;
GetFileTime(handleFile, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime);
SYSTEMTIME creatTime, accessTime, modifyTime;
FILETIME ftCreationTimeLocal, ftLastAccessTimeLocal, stLastWriteTimeLocal;
FileTimeToLocalFileTime(&ftCreationTime, &ftCreationTimeLocal);
FileTimeToLocalFileTime(&ftLastAccessTime, &ftLastAccessTimeLocal);
FileTimeToLocalFileTime(&ftLastWriteTime, &stLastWriteTimeLocal);
FileTimeToSystemTime(&ftCreationTimeLocal, &creatTime);
FileTimeToSystemTime(&ftLastAccessTimeLocal, &accessTime);
FileTimeToSystemTime(&stLastWriteTimeLocal, &modifyTime);
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", creatTime.wYear, creatTime.wMonth, creatTime.wDay, creatTime.wHour, creatTime.wMinute, creatTime.wSecond, creatTime.wMilliseconds);
cout << "创建时间为" << buf << endl;
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", accessTime.wYear, accessTime.wMonth, accessTime.wDay, accessTime.wHour, accessTime.wMinute, accessTime.wSecond, accessTime.wMilliseconds);
cout << "访问时间为" << buf << endl;
memset(buf, 0, 128);
sprintf_s(buf, "%04d-%02d-%02d %02d:%02d:%02d:%03d", modifyTime.wYear, modifyTime.wMonth, modifyTime.wDay, modifyTime.wHour, modifyTime.wMinute, modifyTime.wSecond, modifyTime.wMilliseconds);
cout << "修改时间为" << buf << endl;
CloseHandle(handleFile);
}
return 0;
}