c++ time_t和struct tm及时间戳使用
使用方法
如下:
char cnow[32] = { 0 }; time_t now = time(null); struct tm *pnow = localtime(&now); sprintf(cnow, "%04d:%02d:%02d %02d:%02d:%02d", pnow->tm_year + 1900, pnow->tm_mon + 1, pnow->tm_mday, pnow->tm_hour, pnow->tm_min, pnow->tm_sec);
转换为时间戳的方法
如下:
(unsigned int)now
这样便转化成了标准时间戳的形式。
如果需要把此项写入数据库,sql语句为
sprintf(chsql,"insert into table (timestamp) values (from_unixtime(%d));",(unsigned int)now);
注意这个timestamp 这列要是 timestamp类型或者是datetime类型。
写入到数据库之后的格式为
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论