Convert Python time to Matlab datum

7 visualizaciones (últimos 30 días)
Mark Davidson
Mark Davidson el 15 de Oct. de 2021
Comentada: Mark Davidson el 15 de Oct. de 2021
How do I convert Python timestamp to a Matlab datenum? I am sure this is a common issue, but I could not find a Matlab function to do this easily.

Respuesta aceptada

Dave B
Dave B el 15 de Oct. de 2021
Python has several ways it represents time. A common one is so called unix timestamps, which are the number of seconds since 1970:
secssince1970 = 1634287957.292;
datetime(1970,1,1) + seconds(secssince1970)
ans = datetime
15-Oct-2021 08:52:37
  7 comentarios
Mark Davidson
Mark Davidson el 15 de Oct. de 2021
Perfect! Thank you.
Mark Davidson
Mark Davidson el 15 de Oct. de 2021
This might be a useful Matlab fuction...
function [dnum,date_time]=python2matlabTime(timestamp)
date_time = datetime(1970,1,1) + seconds(timestamp);
dnum=datenum(date_time);
return

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 15 de Oct. de 2021
Don't convert to a datenum. Depending on how the Python timestamp represents the date and time, I suspect some of the values for the ConvertFrom parameter in the datetime function will be of use, something like:
x = 20211015;
dt = datetime(x, 'ConvertFrom', 'yyyymmdd')
dt = datetime
15-Oct-2021
  1 comentario
Mark Davidson
Mark Davidson el 15 de Oct. de 2021
Editada: Mark Davidson el 15 de Oct. de 2021
The time-stamp is in a number format a bit like the matlab datenumber but not the same. I think that it is the same as the unix timestamp, but I'm not sure.

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by