湛江手机网站制作,怎样做视频直播网站,公司简介模板免费下载完整版免费,公司做哪个网站比较好unix timestamp 与 可视化时间/常规时间进行转换。
最近工作中需要根据可视化时间得到unix timestamp#xff0c;完成工作之后记录下来了#xff0c;防止下次遇到此问题时#xff0c;又需要重新梳理#xff0c;直接上代码了 #xff1a;
#include iostream
#inc…unix timestamp 与 可视化时间/常规时间进行转换。
最近工作中需要根据可视化时间得到unix timestamp完成工作之后记录下来了防止下次遇到此问题时又需要重新梳理直接上代码了
#include iostream
#include string
#include ctime
#include string.hvoid unix_timestamp_2_str(long timestamp, char strTime[], int bufLen) {struct tm tm *localtime((time_t *)timestamp);strftime(strTime, bufLen - 1, %Y-%m-%d %H:%M:%S, tm);strTime[bufLen - 1] \0;
}time_t strtime_2_unix_timestamp(const char *timestamp) {struct tm tm;memset(tm, 0, sizeof(tm));sscanf(timestamp, %d-%d-%d %d:%d:%d, tm.tm_year, tm.tm_mon, tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec);tm.tm_year - 1900;tm.tm_mon--;return mktime(tm);
}int main () {char strTime[100] {0};long now_timestamp 1619716862;unix_timestamp_2_str(now_timestamp, strTime, sizeof(strTime));std::cout timestamp now_timestamp --- strTime strTime std::endl;std::string strtime(2021-04-29 17:21:02);time_t timestamp strtime_2_unix_timestamp(strtime.c_str());std::cout timestamp timestamp ---- strtime ctime(timestamp) std::endl;return 0;
} 结果如下
timestamp1619716862 --- strTime2021-04-29 17:21:02strtimeThu Apr 29 17:21:02 2021--- timestamp 1619716862
注意点
(1)、timestamp的类型是long与time_t一致。
(2)、unix timestamp是一个long类型的值从1970年1月1日00:00::00到本时刻经历的second数。