1. 程式人生 > >13/11位unix時間戳轉換成標準時間

13/11位unix時間戳轉換成標準時間

使用範例
	char *str="1320718222932";
	long long time1;
	sscanf(str,"%I64d",&time);	
	CString strTime=MillSecond2LocalTime(time,8);
/////////////////////////////////////////////////////////////////////////////


int CTimeChangeDlg::IsLeap(unsigned short year)
{
	return ((year%4==0)&&(year%100!=0)||(year%400==0));
}

CString CTimeChangeDlg::MillSecond2LocalTime(long long time,long timezone)
{
	const int monthLengths[2][13]={
		{0,31,59,90,120,151,181,212,243,273,304,334,365	},
		{0,31,60,91,121,152,182,213,244,274,305,335,366	}
	};
	const int yearLengths[2]={365,366};
	int year(0),month(0),minMonth(0),maxMonth(0),days(0),clock(0),isLeap(0),day(0),hour(0),minute(0),second(0);
	time/=1000;
	time+=timezone*60*60;
	days=time/86400;//天數
	clock=time%86400;//小時數

	if(clock<0)
	{
		clock+=86400;
		days-=1;
	}

	if(days>=0)
	{
		year=days/366;
		days-=year*365+(year+1)/4-(year+69)/100+(year+369)/400;

		for (year=year+1970;;year++)
		{
			isLeap=IsLeap(year);
			if(days<yearLengths[isLeap])
			{
				break;
			}
			days-=yearLengths[isLeap];
		}
	}
	else
	{
		year=days/366;
		days-=year*365+(year-2)/4-(year-30)/100+(year-30)/400;
		for(year=year+1970-1;;year--)
		{
			isLeap=false;
			days+=yearLengths[isLeap];
			if (days>=0)
			{
				break;
			}
		}
	}

	minMonth=0;
	maxMonth=12;
	for (month=5;month<12&&month>0;month=(minMonth+maxMonth)/2)
	{
		if (days<monthLengths[isLeap][month])
		{
			maxMonth=month;
		}
		else if (days>=monthLengths[isLeap][month+1])
		{
			minMonth=month;
		}
		else
		{
			break;
		}
	}
	days-=monthLengths[isLeap][month];
	month++;
	day=days+1;

	hour=clock/3600;
	clock=clock%3600;
	minute=clock/60;
	second=clock%60;

	CString t;
	t.Format(TEXT("%d-%02d-%02d %02d:%02d:%02d"),year,month,day,hour,minute,second);
	return t;
}


11位Unix 時間戳轉換系統標準形式時間

/************************ 轉換按鈕 *****************

//輸入UNIX字串 csInput = “1244915859”;

//輸出系統時間:csOutput = “2009-06-14 01:57:39”;

***************************************************/

void CUnixTimeDlg::OnBnClickedButton1()

{

    // TODO: 在此新增控制元件通知處理程式程式碼

    CString csInput,csOutput;

    m_input.GetWindowTextA(csInput); //獲取編輯框輸入Unix字串

 

    int nUnixTime = atoi(csInput);

    struct tm *newtime;

    __time64_t long_time = nUnixTime;

    newtime = _localtime64( &long_time ); // 值存在newtime 中

    csOutput.Format("%04d-%02d-%02d %02d:%02d:%02d"

       ,newtime->tm_year + 1900    //年需要加1900 

       ,newtime->tm_mon + 1     //月需要加1 (它是從0 到11)

         ,newtime->tm_mday

         ,newtime->tm_hour

       ,newtime->tm_min

       ,newtime->tm_sec

       );

    m_output.SetWindowTextA(csOutput); //編輯框輸出系統時間字串

}

系統標準形式時間轉換Unix 時間戳


//unix時間戳為1900年以來的秒數

//獲取系統當前時間的Unix時間戳

Cstring nowTime;

SYSTEMTIME sysTime;

GetLocalTime(&sysTime);

CTime m_tTime(sysTime);

time_t unixTime = m_tTime.GetTime();

nowTime.Format("%d",unixTime);//輸出UNIX時間戳字串

對於iOS它是從2001-01-01 00:00:00算起

需要做一個轉換417594327.880849(實際時間)+978278400(從1970-01-01 00:00:00至2001-01-01 00:00:00) + 8 * 3600 (UTC+8) = 1395901527.880849