在Linux系統中,毫秒級的時間戳可以通過/proc/uptime
文件來獲取
要獲取納秒級的時間戳,你可以使用clock_gettime()
函數。以下是一個C語言的示例:
#include <stdio.h>
#include <time.h>
int main() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
long long nanoseconds = ts.tv_sec * 1000000000 + ts.tv_nsec;
printf("納秒級時間戳: %lld\n", nanoseconds);
return 0;
}
編譯并運行這個程序,你將得到當前的納秒級時間戳。請注意,這個方法依賴于系統支持高精度計時器。在大多數Linux發行版中,這是默認啟用的。