$ ./time.lua
CLOCK_REALTIME = 1294965605.435817435
CLOCK_SECOND = 1294965605.000000000
$ date -r 1294965605
Fri Jan 14 00:40:05 UTC 2011
require 'alien'
local timespec = alien.defstruct {
{ 'sec', 'long' },
{ 'nsec', 'long' },
}
local CLOCK_REALTIME = 0
local CLOCK_SECOND = 13
local def = alien.default
def.clock_gettime:types('int', 'int', 'pointer')
function clock_gettime(clock)
local tp = timespec:new()
def.clock_gettime(clock, tp())
return tp.sec, tp.nsec
end
-- here starts the program
sec, nsec = clock_gettime(CLOCK_REALTIME)
print('CLOCK_REALTIME = ' .. sec .. '.' .. string.format('%09d', nsec))
sec, nsec = clock_gettime(CLOCK_SECOND)
print('CLOCK_SECOND = ' .. sec .. '.' .. string.format('%09d', nsec))
Note that GNU date is available as gdate once you install the sysutils/coreutils package.I used a more pragmatic approach. Got the GNU core utils, compiled it, and copied the compiled date to /bin/date.gnu
The support for %N in FreeBSD's date(1) has landed (https://cgit.freebsd.org/src/commit/?id=eeb04a736cb9). It'll be available in FreeBSD 14.1.I'm missing the %N formatting (nanotime) in FreeBSD's date. And after searching the forums and net for a while, I still can't figure out how to get nanotime in FreeBSD.