C 獲取當前日期時間并轉換UNIX時間戳

xrhl7810 8年前發布 | 36K 次閱讀 C/C++

獲取當前日期時間并轉換UNIX時間戳,廢話就不多講了,直接上代碼,代碼也比較簡單,只是起到一個記錄的作用,所以代碼中就不帶注釋了.

#include <stdio.h>      /* puts */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */
#include <string>
#include <iostream>

int main ()
{
            std::string s;
            char stime[256] = { 0 };
            time_t now_time;
            time(&now_time);
            strftime(stime, sizeof(stime), "%F %H:%M:%S", localtime(&now_time));
            s = stime;
            std::cout << s << std::endl;
            struct tm tm;
            strptime(s.c_str(),"%F %H:%M:%S", &tm) ;
            time_t ft=mktime(&tm);
            int i_time = ft;
            std::cout << i_time << std::endl;
            
  return 0;
}
 本文由用戶 xrhl7810 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!