Anne Sophie
Anne Sophie

Reputation: 177

Python: Convertion of seconds from an initial date to datetime

I have measurements taken from 1st January 1993. They were recorded in second elapsed from that date. I would like to have them in date time.

I know in MatLab the function would be

time = datenum(1993,01,01,00,00, time)

However, I struggle to find an equivalent function in Python.

I have tried:

datetime.fromordinal(time) doesn't work because 'module object has no attribute fromordinal'?

datetime.datetime(time) doesn't work (I have a matrix because there are many scans done)

https://docs.python.org/3/library/datetime.html

Upvotes: 0

Views: 610

Answers (3)

VPfB
VPfB

Reputation: 17247

It is like UNIX time, but with a different start.

You can compute the offset once:

>>> import datetime as dt
>>> dt.datetime(1993,1,1).timestamp()
725842800.0

and use it in your program:

OFFSET = 725842800.0
mydate = dt.datetime.fromtimestamp(OFFSET + seconds_from_1993)

Upvotes: 0

msi_gerva
msi_gerva

Reputation: 2078

Let us say you have list of timevalues in seconds starting from 1993-01-01 00:00.

Easiest would be:

datevec=[datetime.datetime(1993,1,1,0)+datetime.timedelta(seconds=val) for val in timevector]

Upvotes: 0

amanbirs
amanbirs

Reputation: 1108

You will first have to create a datetime object for Jan 1st 1993 and then add the number of seconds to that date. The code below should help you get started.

from datetime import datetime, timedelta

original_date = datetime.strptime('01-01-1993', '%d-%m-%Y')
original_date + timedelta(seconds= 10000)

output: datetime.datetime(1993, 1, 1, 2, 46, 40)

Upvotes: 1

Related Questions