SQLiteでselect julianday('now')とすると、整数部と小数部からなる値が表示される。これはユリウス日と言われる値になるそうだが、この小数部の精度がどうなっているのか気になったので調査した。

結論からすると、x86アーキテクチャのLinuxで動作している場合、ミリ秒単位が限界となりそうだ。

SQLiteのソースだと、unixCurrentTimeInt64という関数で現在時刻を取得するようだ。この関数のコメントには、


/
Find the current time (in Universal Coordinated Time).  Write into *piNow
the current time and date as a Julian Day number times 86_400_000.  In
other words, write into *piNow the number of milliseconds since the Julian
epoch of noon in Greenwich on November 24, 4714 B.C according to the
proleptic Gregorian calendar.

On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
cannot be found.
/

とある。ユリウス日起源からのミリ秒をpiNowに書き込む、とある。実際、(プリプロセッサのelseパートがコンパイルされるとして)gettimeofday(&sNow, 0)が実行され、


piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;

が実行されて、マイクロ秒の1/1000単位までが保存される、つまりミリ秒が精度となる。

Comments