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.

 採用された回答

Dave B
Dave B 2021 年 10 月 15 日

1 投票

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 件のコメント

Dave B
Dave B 2021 年 10 月 15 日
編集済み: Dave B 2021 年 10 月 15 日
@Mark Davidson - if this answer is not working for you, can you send us an example timestamp? As @Steven Lord described, you should use datetime not datenum. If you really have to use a datenum (e.g. for some legacy code) just call datenum on the datetime value:
ts_unix = 1634287957.292;
dt_matlab = datetime(1970,1,1) + seconds(ts_unix)
dt_matlab = datetime
15-Oct-2021 08:52:37
dn_matlab = datenum(dt_matlab)
dn_matlab = 7.3844e+05
Mark Davidson
Mark Davidson 2021 年 10 月 15 日
The timestamp I have is:
1546300800 which in Python returns...
01/01/2019, 00:00:00
Dave B
Dave B 2021 年 10 月 15 日
編集済み: Dave B 2021 年 10 月 15 日
So I think the datetime(1970,1,1) + seconds(unitxtime) approach gives the answer you're looking for:
a=datetime(1970,1,1) + seconds(1546300800)
a = datetime
01-Jan-2019
% note you can format datetimes however you like:
a.Format = 'MM/dd/uuuu, HH:mm:ss'
a = datetime
01/01/2019, 00:00:00
Mark Davidson
Mark Davidson 2021 年 10 月 15 日
編集済み: Mark Davidson 2021 年 10 月 15 日
Here is the Python code if that's at all helpful?
from datetime import datetime
timestamp = 1546300800
date_time = datetime.fromtimestamp(timestamp)
d = date_time.strftime("%m/%d/%Y, %H:%M:%S")
print("Output 1:", d)
Output 1: 01/01/2019, 00:00:00
Dave B
Dave B 2021 年 10 月 15 日
I think I'm still confused about what's missing:
timestamp = 1546300800;
date_time = datetime(1970,1,1) + seconds(timestamp);
date_time.Format = 'MM/dd/uuuu, HH:mm:ss';
fprintf("Output 1: %s\n", date_time)
Output 1: 01/01/2019, 00:00:00
Mark Davidson
Mark Davidson 2021 年 10 月 15 日
Perfect! Thank you.
Mark Davidson
Mark Davidson 2021 年 10 月 15 日
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

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 10 月 15 日

0 投票

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 件のコメント

Mark Davidson
Mark Davidson 2021 年 10 月 15 日
編集済み: Mark Davidson 2021 年 10 月 15 日
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.

サインインしてコメントする。

カテゴリ

製品

リリース

R2020b

質問済み:

2021 年 10 月 15 日

コメント済み:

2021 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by