getTime,localtime,mktime

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/FEIFAZHE/article/details/78410350

//  获取系统时间(精确到秒)

time_t t = getTime();

char szTimeData[64];

memset(szTimeData,0,64);

//  转换到本地时间

strftime( szTimeData, sizeof(szTimeData), "%Y/%m/%d %X",localtime(&t));

// 转换成秒

tm timeinfo;   
sscanf(szTimeData, "%d/%d/%d %d:%d:%d",    
&timeinfo.tm_year, &timeinfo.tm_mon, &timeinfo.tm_mday,    
&timeinfo.tm_hour, &timeinfo.tm_min, &timeinfo.tm_sec); 
timeinfo.tm_year -= 1900;
timeinfo.tm_mon -= 1;
long ntimet = (long)mktime(&timeinfo);
展开阅读全文

mktime()返回-1

02-21

#include rn#include rn#include rn#include rn#include "sys\stat.h"rn#include "memory.h"rn#include rnrntypedef struct datern rn int dwYear;rn int dwMonth; rn int dwDay; rn int dwHour; rn int dwMin; rn int dwSec;rn tDate;rnrnvoid main()rnrn char aszFileName[50];rn struct stat FileInfo;rn struct tm TempTime;rn struct tm* pTempTime;rn tDate date;rn time_t dwFileTime;rn time_t dwTempTime;rn char aszBufTime[65];rnrn memset(&TempTime,0,sizeof(tm));rnrn FILE *fpFileList,*fpFileTime,*fpReport;rnrn if((fpFileList=fopen("c:\\FileList.txt","r"))==NULL)rn rn printf("打开文件名称列表失败!");rn rnrn if((access("c:\\FileTime.txt",0))==-1)rn rn if((fpFileTime=fopen("c:\\FileTime.txt","w"))==NULL)rn rn printf("打开文件信息列表失败!");rn rnrn while(!feof(fpFileList))rn rn fscanf(fpFileList,"%s",aszFileName);rn printf("%s\n",aszFileName);rnrn stat(aszFileName,&FileInfo);rn printf("%d\n",FileInfo.st_mtime);rn fprintf(fpFileTime,"%d ",FileInfo.st_mtime);rnrn rn fclose(fpFileTime);rn rn elsern rn rewind(fpFileList);rn if((fpFileTime=fopen("c:\\FileTime.txt","r"))==NULL)rn rn printf("打开文件信息列表失败!");rn rn rn if((fpReport=fopen("c:\\Report.txt","w"))==NULL)rn rn printf("打开输出报告文件失败!");rn rn rn printf("输入时间基准: ");rn scanf("%d-%d-%d %d:%d:%d",&date.dwYear,&date.dwMonth,&date.dwDay,&date.dwHour,&date.dwMin,&date.dwSec);rn rn TempTime.tm_year=date.dwYear-1990;rn TempTime.tm_mon=date.dwMonth-1;rn TempTime.tm_mday=date.dwDay;rn TempTime.tm_hour=date.dwHour;rn TempTime.tm_min=date.dwMin;rn TempTime.tm_sec=date.dwSec;rn TempTime.tm_isdst=0;rnrn printf("%d-%d-%d %d:%d:%d\n",TempTime.tm_year,TempTime.tm_mon,TempTime.tm_mday,TempTime.tm_hour,TempTime.tm_min,TempTime.tm_sec);rnrn dwTempTime=mktime(&TempTime);rn printf("%d\n",dwTempTime);rnrn while((!feof(fpFileList))&&(!feof(fpFileTime)))rn rn fscanf(fpFileList,"%s",aszFileName);rn stat(aszFileName,&FileInfo);rn fscanf(fpFileTime,"%d",&dwFileTime);rnrn if(dwFileTime!=FileInfo.st_mtime)rn rn pTempTime=localtime(&FileInfo.st_mtime);rn sprintf(aszBufTime,"%04d-%02d-%02d %02d:%02d:%02d",pTempTime->tm_year+1900,pTempTime->tm_mon+1,pTempTime->tm_mday,pTempTime->tm_hour,pTempTime->tm_min,pTempTime->tm_sec);rn fprintf(fpReport,"%s %s\n",aszFileName,aszBufTime);rn rnrn rn fclose(fpFileTime);rn fclose(fpReport);rn rn fclose(fpFileList);rn system("pause");rnrnrnrn 论坛

没有更多推荐了,返回首页