#include "stdafx.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <direct.h> #include <Windows.h> #include <time.h> #include <sys/types.h> #include <sys/utime.h> int DateWeek(int year, int month, int day) { if (month==1||month==2) { year -=1; month +=12; } return (day+1+2*month+3*(month+1)/5+year+(year/4)-year/100+year/400)%7; } int sum_day(int year,int month,int day) { int days = day; int day_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i; for(i=1;i<month;i++)days += day_month[i]; int leap=0; if((year%4==0&&year%100==0)||(year%400==0))leap=1; if(leap && month>=3)days=days+1; return days; } int _tmain(int argc, _TCHAR* argv[]) { SYSTEMTIME st; FILETIME ft; st.wYear = 2007; st.wMonth = 8; st.wDay = 25; st.wHour = 15; st.wDayOfWeek = 5; st.wMinute =1; st.wSecond = 1; st.wMilliseconds = 1; #if 0 _utimbuf mvTime; time_t rawtime; struct tm *timeinfo; timeinfo = localtime(&rawtime); timeinfo->tm_hour = st.wHour; timeinfo->tm_isdst = 0; timeinfo->tm_mday = st.wDay; timeinfo->tm_min = st.wMinute; timeinfo->tm_mon = st.wMonth; timeinfo->tm_sec = st.wSecond; timeinfo->tm_wday = DateWeek(st.wYear,st.wMonth,st.wDay); timeinfo->tm_yday = sum_day(st.wYear,st.wMonth,st.wDay); timeinfo->tm_year = st.wYear - 1900; mvTime.actime = mktime(timeinfo); mvTime.modtime = mktime(timeinfo); _wutime(TEXT("F:/type.txt"),&mvTime); #else HANDLE hFile = CreateFile(TEXT("F:/type.txt"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if(hFile!=INVALID_HANDLE_VALUE) { SystemTimeToFileTime(&st,&ft); int res=SetFileTime(hFile,&ft,&ft,&ft); CloseHandle(hFile); } #endif }
,SystemTimeToFileTime接口计算出错。不知道具体原因。然后Google找到另外一种解决办法就是用utime来修改(修改/访问)时间使用_wutime修改的代码
本文出自:https://blog.csdn.net/evea804/article/details/32139941