3

I'm working with some telemetry that uses timestamps measured in hours since January 1st at midnight of the current year.

So I get value 1 at time 8668.12034

I'd like to convert it to a more useful date format and of course I've been doing so with hardcoded math dividing into days, remainder hours, minutes, etc accounting for leap years... and it works but I'm sure there's a simple way using the datetime library or something right?

I'm thinking timedelta is the way to go since it's giving me a delta since the beginning of the year but does that account for leap years?

Curious how others would approach this issue, thanks for any advice.

Asterlux
  • 179
  • 9

1 Answers1

4
# import packages we need
import datetime

From elapsed hours to datetime.datetime object

You can for example do:

hours_elapsed = 1000
your_date = datetime.datetime(2020,1,1,0,0)+datetime.timedelta(hours=hours_elapsed)

(Of course change hours_elapsed to whatever hours elapsed in your case.)

your_date will be: datetime.datetime(2020, 2, 11, 16, 0)

Yes, timedelta does know about leap years.


Further processing

If want to process this further, can do, using getattr():

timeunits = ['year', 'month', 'day', 'hour', 'minute', 'second']
[getattr(your_date,timeunit) for timeunit in timeunits]

Resulting in:

[2020, 2, 11, 16, 0, 0]
zabop
  • 6,750
  • 3
  • 39
  • 84