使用范例 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