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 转字符串如下

#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());
}

转载于:https://my.oschina.net/lvyi/blog/731919

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值