Linuxcはタイムスタンプを取得します



Linux C Get Timestamp



現在の時刻の秒数とマイクロ秒数を取得します。このメソッドは、gettimeofday()関数を使用する必要があります。この関数がインポートする必要のあるヘッダーファイルはです。

関数の説明



int gettimeofday (struct timeval * tv, struct timezone * tz)

戻り値:関数は成功すると0を返し、失敗すると-1を返します

パラメータ



struct timeval{ Long tv_sec //second Long tv_usec ​​//microsecond } struct timezone { Int tz_minuteswest //How many minutes is the time difference from Greenwich Int tz_dsttime //The state of daylight saving time }

#include #include #include int main() { struct timeval tv gettimeofday(&tv, NULL) printf('second: %ld ', tv.tv_sec) // second printf('millisecond: %ld ', tv.tv_sec * 1000 + tv.tv_usec / 1000) // milliseconds printf('microsecond: %ld ', tv.tv_sec * 1000000 + tv.tv_usec) // microsecond sleep(3) // Let the program sleep for 3 seconds printf('---------------------sleep 3 second------------------- ') gettimeofday(&tv, NULL) printf('second: %ld ', tv.tv_sec) // second printf('millisecond: %ld ', tv.tv_sec * 1000 + tv.tv_usec / 1000) // milliseconds printf('microsecond: %ld ', tv.tv_sec * 1000000 + tv.tv_usec) // microsecond return 0 }

演算結果:

second: 1554963664 millisecond: 1554963664748 microsecond: 1554963664748007 ---------------------sleep 3 second------------------- second: 1554963667 millisecond: 1554963667748 microsecond: 1554963667748621