3

I need to convert a decimal timestamp in a JSON file generated using LabVIEW into a string datetime so that I can POST it to an API I'm using. For instance, one such decimal timestamp is 3640111724.4817362; how can I do this?

EDIT: This article from NI describes how they format their timestamps. They're starting from a nonstandard epoch (01/01/1904 00:00:00.00 UTC), so in other words, Python's interpretation is 66 years ahead.

Tarek Allam
  • 139
  • 1
  • 8

2 Answers2

4

just use datetime.fromtimestamp from datetime and format It with strftime as you want:

EDIT: subtracting 66 years to match with datetime timestamp pattern

from dateutil.relativedelta import relativedelta
from datetime import datetime

timestamp = 3640111724.4817362

date = datetime.fromtimestamp(timestamp)
date = date - relativedelta(years=66)
print(date.strftime("%m/%d/%Y, %H:%M:%S"))

Output:

05/07/2019, 22:08:44
bcosta12
  • 2,364
  • 16
  • 28
  • 2
    I don't think that's correct though, the timestamp I gave as an example was generated in the past, so 2085 is kind of a red flag. – Tarek Allam Jul 31 '19 at 19:31
  • 1
    So We need to know how to convert this timestamp. Please, post the timestamp with our expected output – bcosta12 Jul 31 '19 at 19:33
  • 1
    Check the edit, the issue is a nonstandard epoch. If you subtract 66 from the year (the difference between 1970 and 1904) the timestamp turns out correctly. – Tarek Allam Jul 31 '19 at 19:36
3

The number of seconds between 1904-01-01 00:00:00 UTC and 1970-01-01 00:00:00 UTC is 2082844800, so you just need to adjust your LabView timestamp before creating your Python datetime object.

from datetime import datetime

timestamp = 3640111724.4817362
dt = datetime.fromtimestamp(timestamp - 2082844800)
print(dt)
# 2019-05-07 22:08:44.481736
benvc
  • 14,448
  • 4
  • 33
  • 54