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());
}
相關文章
相關標籤/搜索