C語言實現字符轉unix時間戳,需要先轉成tm類型,再得到它的Unix時間戳。附上實現代碼:
#include <stdio.h>#include <time.h>int strtotime(char datetime[]){struct tm tm_time;int unixtime;strptime(datetime, "%Y-%m-%d %H:%M:%S", &tm_time);unixtime = mktime(&tm_time);return unixtime;}另附上幾個時間相關的函數,做個筆記://當前時間char* get_curr_time(){char* strtime = (char *)malloc(sizeof(char)*20);memset(strtime, 0, sizeof(char)*20);time_t now;time(&now);strftime(strtime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&now));printf("Info: current time %s/n", strtime);return strtime;}//當前時間的unix時間戳int get_curr_unixtime(void){time_t now;int unixtime = time(&now);return unixtime;}//字符轉unix時間戳int strtotime(char datetime[]){struct tm tm_time;int unixtime;strptime(datetime, “%Y-%m-%d %H:%M:%S”, &tm_time);unixtime = mktime(&tm_time);return unixtime;}
以上這篇C語言實現字符轉unix時間戳的簡單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選