hl2ss icon indicating copy to clipboard operation
hl2ss copied to clipboard

How do I convert data.timestamp to UTC time

Open desigood opened this issue 2 years ago • 4 comments

In the client stream series file I see a data.timestamp print. But I try to read and analyze the author's code and instructions. Due to my poor programming background, I couldn't figure out how to convert data.timestamp to UTC time. Can the author explain how to convert a timestamp to UTC via python? Thank you very much!

desigood avatar Nov 08 '23 14:11 desigood

First, obtain the UTC offset from the server:

client = hl2ss_lnm.ipc_rc(host, hl2ss.IPCPort.REMOTE_CONFIGURATION)
client.open()
utc_offset = client.get_utc_offset(32)
client.close()

Then add the UTC offset to the timestamps to convert them to UTC:

utc_timestamp = data.timestamp + utc_offset

jdibenes avatar Nov 09 '23 00:11 jdibenes

Dear developers, I'm having issues with the timestamps, even after following this solution. I am assuming that all timestamps are in nanoseconds.

data.timestamp: 146716183355 (1970-01-01 01:02:26.716183)
utc_offset: 133509951844495077 (1974-03-26 07:05:51.844495)
data.timestamp + utc_offset: 133510098560678432 (1974-03-26 07:08:18.560678)
current_time: 1.7065362561664888e+18 (2024-01-29 14:50:56.166489)
current_time - utc_offset: 1.5730263043219937e+18 (2019-11-06 08:45:04.321994)
current_time - (data.timestamp + utc_offset): 1.5730261576058104e+18 (2019-11-06 08:42:37.60581)

It seems like the timestamp is not related to the UNIX epoch (1970-1-1) but to some other reference point. Any suggestions? What am I doing wrong?

Thanks in advance!

javiercmh avatar Jan 29 '24 13:01 javiercmh

Hello, All timestamps are in hundreds of nanoseconds (1 timestamp unit = 100 ns). The utc_offset given by the server converts timestamps to Windows Filetime.

jdibenes avatar Jan 29 '24 17:01 jdibenes

Thank you very much! In case someone else finds it useful, this is how I converted utc_timestamps to usual datetime:

from datetime import datetime, timezone

filetime = data.timestamp + self.utc_offset
human_time = datetime.utcfromtimestamp((filetime - 116444736000000000) / 10**7)
human_time = human_time.replace(tzinfo=timezone.utc)  # make it timezone-aware (optional)

The code above subtracts January 1, 1970 in timestamp and divides it by 10**7 to account for the use of hundreds of nanoseconds (1).

javiercmh avatar Jan 30 '24 10:01 javiercmh