[問題] 請教傳入日期+ms產生epoch時間

看板C_and_CPP作者 (飛天)時間8年前 (2017/12/28 12:33), 編輯推噓2(2015)
留言17則, 4人參與, 9年前最新討論串1/1
*[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

12/28 13:10, 8年前 , 1F
(1)你知道你ms的定義嗎(2)你知道epoch time的精確度嗎
12/28 13:10, 1F

12/28 13:12, 8年前 , 2F
(3)你這個「類似範例」到底是頭髮還是指甲類似
12/28 13:12, 2F

12/28 13:54, 8年前 , 3F
(4)你YYY只有三位,他的基準是1900, 1975還是2000年
12/28 13:54, 3F

12/28 13:55, 8年前 , 4F
(5)你YYYMMDD是字串還是int
12/28 13:55, 4F

12/28 14:08, 8年前 , 5F
YYYY 才對
12/28 14:08, 5F

12/28 14:08, 8年前 , 6F
YYYYMMDD 我已經有了 int ex: 20171211
12/28 14:08, 6F

12/28 14:17, 8年前 , 7F
(6)你知道你想要的「epoch 時間」是什麼東西嗎
12/28 14:17, 7F

12/28 17:06, 8年前 , 8F
好了放你一馬
12/28 17:06, 8F

12/28 17:07, 8年前 , 9F
他標準的作法是include <time.h>
12/28 17:07, 9F

12/28 17:08, 8年前 , 10F
做一個struct tm比如tm0 把你時間各個項目塞到裏面
12/28 17:08, 10F

12/28 17:08, 8年前 , 11F
然後呼叫mktime(tm0)得到那個結果
12/28 17:08, 11F

12/28 17:09, 8年前 , 12F
怎麼拆怎麼塞就由你發揮了
12/28 17:09, 12F

12/28 18:26, 8年前 , 13F
C++11以後 比較推chrono library, std::chrono
12/28 18:26, 13F

12/28 18:26, 8年前 , 14F
這是毫秒等級跨平台的時間函式庫 別再用平台限定的了
12/28 18:26, 14F

12/28 18:37, 8年前 , 15F
不然你覺得const char* a = "0";會是啥....
12/28 18:37, 15F

12/28 18:37, 8年前 , 16F
推錯文....
12/28 18:37, 16F

01/04 12:11, 9年前 , 17F
謝謝
01/04 12:11, 17F
文章代碼(AID): #1QH7GiXV (C_and_CPP)