C++时间函数用法总结(部分)

1、获取1970以来的时间戳

#include "stdafx.h"
#include   
#include   
#include   
using namespace std;  

int main()  
{  
	long long time_last;  
	time_last = time(NULL);
	cout<

2、FILETIME转SYSTEMTIME

FILETIME是1601年1月1日至今的数字(单位是100纳秒即0.1微秒)。

SYSTEMTIME是系统时间,该结构体成员分别是现今时间体系的年、月、星期几、日、小时、分钟、秒、毫秒。

#include "stdafx.h"
#include 
#include 
#include 

// GetLastWriteTime - Retrieves the last-write time and converts
//                    the time to a string
//
// Return value - TRUE if successful, FALSE otherwise
// hFile      - Valid file handle
// lpszString - Pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize)
{
	FILETIME ftCreate, ftAccess, ftWrite;
	SYSTEMTIME stUTC, stLocal;
	DWORD dwRet;

	// Retrieve the file times for the file.
	if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
		return FALSE;

	// Convert the last-write time to local time.
	FileTimeToSystemTime(&ftWrite, &stUTC);
	SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

	// Build a string showing the date and time.
	dwRet = StringCchPrintf(lpszString, dwSize, 
		TEXT("%02d/%02d/%d  %02d:%02d"),
		stLocal.wMonth, stLocal.wDay, stLocal.wYear,
		stLocal.wHour, stLocal.wMinute);

	if( S_OK == dwRet )
		return TRUE;
	else return FALSE;
}

int _tmain(int argc, TCHAR *argv[])
{
	HANDLE hFile;
	TCHAR szBuf[MAX_PATH];

	//if( argc != 2 )
	//{
	//	printf("This sample takes a file name as a parameter\n");
	//	return 0;
	//}
	char fileName[128] = {"C:\\config.ini"};//注意需要C盘存在该文件
	hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, 0, NULL);

	if(hFile == INVALID_HANDLE_VALUE)
	{
		printf("CreateFile failed with %d\n", GetLastError());
		return 0;
	}
	if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))
		_tprintf(TEXT("Last write time is: %s\n"), szBuf);
}

3、时间戳转字符串时间

方法1

string Timestamp2StrTime(time_t lTimestamp)
{
	struct tm *pTime;
	lTimestamp += 8*3600;//8小时
	pTime = gmtime(&lTimestamp);//GMT+8*3600=CST,获取CST时间
	char sTime[80] = {0};
	//strftime(sTime, 80, "%Y-%m-%d %H:%M:%S", pTime);//方法1
	//sprintf(sTime,"%04d-%02d-%02d %02d:%02d:%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday,
	//	pTime->tm_hour,pTime->tm_min,pTime->tm_sec );
	sprintf(sTime,"%04d-%02d-%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday);
	return sTime;
}

方法2

string Timestamp2StrTime2(time_t lTimestamp)
{
	struct tm *pTime;
	pTime = localtime(&lTimestamp);//
	char sTime[80] = {0};
	//strftime(sTime, 80, "%Y-%m-%d %H:%M:%S", pTime);//方法1
	sprintf(sTime,"%04d-%02d-%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday);
	return sTime;
}

4、时间戳转tm和tm转时间戳

#include "stdafx.h"
#include 
#include 

int main( void )
{
	time_t lTimestamp;
	time(&lTimestamp);
	struct tm *pTime;  
	pTime = localtime(&lTimestamp);//时间戳转tm
	long lTimestamp2 = mktime(pTime);//tm转时间戳
	int days = 60;
	pTime->tm_mday = pTime->tm_mday + days;
	char sTime[80] = {0};  
	//strftime(sTime, 80, "%Y-%m-%d %H:%M:%S", pTime);//方法1  
	sprintf(sTime,"%04d-%02d-%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday); 

	getchar();
	return 0;
}

5、文件时间、系统时间转时间戳

long FileTime2Timestamp(FILETIME ftTime)
{
	SYSTEMTIME stTime = {0}; //最近访问时间
	if( !::FileTimeToLocalFileTime(&ftTime, &ftTime))//转换成本地FileTime,防止相差8个小时
		return 0;
	if( !::FileTimeToSystemTime( &ftTime, &stTime))
		return 0;
	struct tm structTime;
	structTime.tm_year = stTime.wYear-1900;
	structTime.tm_mon = stTime.wMonth-1;
	structTime.tm_mday = stTime.wDay;
	structTime.tm_hour = 0;
	structTime.tm_min = 0;
	structTime.tm_sec = 0;
	//structTime.tm_hour = stTime.wHour;
	//structTime.tm_min = stTime.wMinute;
	//structTime.tm_sec = stTime.wSecond;
	long lTimestamp = mktime(&structTime);
	return lTimestamp;
}

6、CST时间(邮件时间)转字符串时间


#include 
#include 
#include 
#include 
using namespace std;

//string mailTime = "Tue, 15 Nov 2016 19:33:18 +0800";
string CSTTime2StrTime(string mailTime)
{
	char xingqi[4];//星期几
	struct tm st;
	time_t tt;
	char mon[4];
	char tmpbuf[128];
	//string stdStrTime;
	char mn[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	sscanf(mailTime.c_str(), "%3s, %2d %3s %4d %2d:%2d:%2d +0800", xingqi,
		&st.tm_mday, mon, &st.tm_year, &st.tm_hour, &st.tm_min, &st.tm_sec);
	for (int i=0;i<12;i++) if (0==stricmp(mn[i],mon)) {st.tm_mon=i; break;}
	st.tm_year-=1900;
	tt=mktime(&st);//获取时间戳
	if (-1!=tt) {
		strftime(tmpbuf,128,"%Y-%m-%d %H:%M:%S\n",localtime(&tt));//时间戳转字符串时间
		//stdStrTime = tmpbuf
		printf(tmpbuf);//2011-12-08 15:25:03
	} else {
		printf("[%s] is Invalid time string!\n",mailTime.c_str());
	}
	return tmpbuf;
}

int main( ) 
{
	using namespace std;
	string mailTime = "Tue, 15 Nov 2016 19:33:18 +0800";
	string strStdTime = CSTTime2StrTime(mailTime);
}








你可能感兴趣的:(C++基础)