datetime 和 timestamp 相互转换

AO 从数据库读出来的格式通常为 datetime,返回给 CGI 时表示为 uint32 的形式。能够用如下代码进行转换。数据库

字符串转 timestamp

#include <time.h>
#include <stdio.h>

time_t strtotime(char* const date, char* const format="%Y%m%d%H%M%S")
{
	struct tm tm;
	strptime(date,format, &tm);
	time_t ft=mktime(&tm);
	return ft;
}

int main()
{
	printf("timestamp %d \n", strtotime("20160812180500"));
}

timestamp 转字符串

timestamp 转字符串以下ui

#include <stdio.h>
#include <time.h>
#include <string>
using namespace std;

string timetostr(time_t t) 
{
	struct tm* p;
	p = gmtime(&t);
	char s[80];
    strftime(s, 80, "%Y-%m-%d %H:%M:%S", p);
	return string(s);
}

int main(int argc, const char * argv[])
{    
    time_t t;
    t=1408413451;
    
	printf("%s\n", timetostr(t).c_str());
}
相关文章
相关标签/搜索