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 转字符串如下
#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());
}