看板 C_and_CPP 關於我們 聯絡資訊
*[36m開發平台(Platform): (Ex: Win10, Linux, ...) Linux 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Gcc 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) NA 問題(Question): 如何回傳epoch時間 傳入(YYYMMDD, ms) ms的格式由 0 ~235959999 回傳epoch時間 找到類似的範列如下: 這是抓系統時間: * timestamp example */ #include <stdio.h> /* printf */ #include <time.h> /* time_t, time (for timestamp in second) */ #include <sys/timeb.h> /* ftime, timeb (for timestamp in millisecond) */ #include <sys/time.h> /* gettimeofday, timeval (for timestamp in microsecond) */ int main () { /* Example of timestamp in millisecond. */ struct timeb timer_msec; long long int timestamp_msec; /* timestamp in millisecond. */ if (!ftime(&timer_msec)) { timestamp_msec = ((long long int) timer_msec.time) * 1000ll + (long long int) timer_msec.millitm; } else { timestamp_msec = -1; } printf("%lld milliseconds since epoch\n", timestamp_msec); return 0; } 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.19.162 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1514435628.A.85F.html
MOONRAKER: (1)你知道你ms的定義嗎(2)你知道epoch time的精確度嗎 12/28 13:10
MOONRAKER: (3)你這個「類似範例」到底是頭髮還是指甲類似 12/28 13:12
MOONRAKER: (4)你YYY只有三位,他的基準是1900, 1975還是2000年 12/28 13:54
MOONRAKER: (5)你YYYMMDD是字串還是int 12/28 13:55
TwoDemon: YYYY 才對 12/28 14:08
TwoDemon: YYYYMMDD 我已經有了 int ex: 20171211 12/28 14:08
LPH66: (6)你知道你想要的「epoch 時間」是什麼東西嗎 12/28 14:17
MOONRAKER: 好了放你一馬 12/28 17:06
MOONRAKER: 他標準的作法是include <time.h> 12/28 17:07
MOONRAKER: 做一個struct tm比如tm0 把你時間各個項目塞到裏面 12/28 17:08
MOONRAKER: 然後呼叫mktime(tm0)得到那個結果 12/28 17:08
MOONRAKER: 怎麼拆怎麼塞就由你發揮了 12/28 17:09
Killercat: C++11以後 比較推chrono library, std::chrono 12/28 18:26
Killercat: 這是毫秒等級跨平台的時間函式庫 別再用平台限定的了 12/28 18:26
Killercat: 不然你覺得const char* a = "0";會是啥.... 12/28 18:37
Killercat: 推錯文.... 12/28 18:37
TwoDemon: 謝謝 01/04 12:11