Old 05-26-2023, 06:18 PM   #1
Human being with feelings
Join Date: Feb 2013
Posts: 268
Default How to get precise time? e.g. time_precise()

I'm trying to synchronize an event inside Reaper with an event outside it.

The external event occurs at a known system time, given in Epoch time with microsecond precision.

Lua provides os.time() which is in Epoch time, but only has 1 second precision, which is not good enough for my usage.

Lua also provides os.clock() which has millisecond precision, but it measures process runtime, not system time, so useless for this.

The Reaper API provides reaper.time_precise() which has microsecond precision and claims to be a "system timestamp", but it's not in Epoch time.

Here is example output of time_precise() compared to os.time():

time_precise: 709179.6048847; os.time(): 1685148400
time_precise: 709179.6335426; os.time(): 1685148400
time_precise: 709179.6653186; os.time(): 1685148400
time_precise: 709179.6973375; os.time(): 1685148400
time_precise: 709179.7284991; os.time(): 1685148400
time_precise: 709179.7615574; os.time(): 1685148400
time_precise: 709179.7903648; os.time(): 1685148400
time_precise: 709179.8204834; os.time(): 1685148400
time_precise: 709179.8554246; os.time(): 1685148400
time_precise: 709179.8863632; os.time(): 1685148400
time_precise: 709179.9166774; os.time(): 1685148400
time_precise: 709179.9473127; os.time(): 1685148400
time_precise: 709179.9811168; os.time(): 1685148401
time_precise: 709180.0095143; os.time(): 1685148401
time_precise: 709180.0403439; os.time(): 1685148401
time_precise: 709180.0703263; os.time(): 1685148401
time_precise: 709180.1036351; os.time(): 1685148401
time_precise: 709180.1335290; os.time(): 1685148401
time_precise: 709180.1683076; os.time(): 1685148401
time_precise: 709180.2003164; os.time(): 1685148401
time_precise: 709180.2352957; os.time(): 1685148401
time_precise: 709180.2643796; os.time(): 1685148401
time_precise: 709180.2960534; os.time(): 1685148401

os.time() is giving me the correct system time as Epoch time. WTF is that time_precise() number? Is there some way to correlate it with system time? Does that correlation work cross platform?
EricTbone is offline   Reply With Quote
Old 05-26-2023, 06:25 PM   #2
Human being with feelings
cfillion's Avatar
Join Date: May 2015
Location: Québec, Canada
Posts: 4,365

On macOS time_precise gives a proper Unix timestamp. On Windows it seems to be seconds since boot?

Can combine the integer part of os.time with the fractional part of reaper.time_precise:

local ms_timestamp = os.time() + (reaper.time_precise() % 1)
...assuming Windows boot time doesn't start counting in the middle of a wall second.
cfillion is offline   Reply With Quote
Old Yesterday, 08:43 PM   #3
Human being with feelings
Join Date: Feb 2013
Posts: 268

Originally Posted by cfillion View Post
...assuming Windows boot time doesn't start counting in the middle of a wall second.
I tested, and yes, the boot time can start at any part of a second.

I wrote a Win32 app to compare the high precision (<1us) system time (GetSystemTimePrecise) with the high precision "time since boot" time (QueryPerformanceCounter). They differ 220ms and change.

So I guess I'll write a console app that spits out the difference, run that with reaper.ExecProcess, then use that to adjust `reaper.time_precise()`.

But, man, what a kludge. Par for the course with this API, I guess. *shrug*
EricTbone is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -7. The time now is 05:05 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.