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

1、获取1970以来的时间戳

#include "stdafx.h"
#include <iostream>  
#include <time.h>  
#include <sys/timeb.h>  
using namespace std;  

int main()  
{  
	long long time_last;  
	time_last = time(NULL);
	cout<<time_last<<endl;  //1970时间戳秒数方法1  

	struct timeb t1;  
	ftime(&t1);
	cout<<t1.time<<endl;    //1970时间戳秒数方法2  
	cout<<t1.time*1000 + t1.millitm<<endl; //总毫秒数  

	system("pause");  
	return 0;   
}  

2、FILETIME转SYSTEMTIME

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

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

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

// 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 <time.h>
#include <stdio.h>

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 <string>
#include <iostream>
#include <time.h>
#include <sys/timeb.h>
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);
}








  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值