日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux下unix timestamp 与 可视化时间/常规时间进行转换

發(fā)布時(shí)間:2023/12/20 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下unix timestamp 与 可视化时间/常规时间进行转换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

unix timestamp 與 可視化時(shí)間/常規(guī)時(shí)間進(jìn)行轉(zhuǎn)換。

最近工作中需要根據(jù)可視化時(shí)間得到unix timestamp,完成工作之后記錄下來了,防止下次遇到此問題時(shí),又需要重新梳理,直接上代碼了

#include <iostream> #include <string> #include <ctime> #include <string.h>void 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; }

?

結(jié)果如下:
?

timestamp=1619716862 ---> strTime=2021-04-29 17:21:02strtime=Thu Apr 29 17:21:02 2021---> timestamp = 1619716862

注意點(diǎn):

(1)、timestamp的類型是long,與time_t一致。

(2)、unix timestamp是一個(gè)long類型的值,從1970年1月1日00:00::00到本時(shí)刻經(jīng)歷的second數(shù)。

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的linux下unix timestamp 与 可视化时间/常规时间进行转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。