转换MATLAB'时出错;s datenum到Python';日期时间
我必须将MATLAB的datenum转换为Python的转换MATLAB'时出错;s datenum到Python';日期时间,python,matlab,datetime,Python,Matlab,Datetime,我必须将MATLAB的datenum转换为Python的datetime。 以下代码如下: import datetime matlab_datenum = 63650571169.50261 python_datetime = datetime.date.fromordinal(int(matlab_datenum)) + \ datetime.timedelta(days=matlab_datenum%1) - datetime.timedelta(days=366) print(m
datetime
。
以下代码如下:
import datetime
matlab_datenum = 63650571169.50261
python_datetime = datetime.date.fromordinal(int(matlab_datenum)) + \
datetime.timedelta(days=matlab_datenum%1) - datetime.timedelta(days=366)
print(matlab_datenum)
上述代码生成此错误:
OverflowError: Python int too large to convert to C long
如何解决此问题?您的datenum以秒为单位,与matlab历元相距。要转换为
datetime
,需要将秒数偏移为python理解的datetime
。此函数使用Unix历元作为该引用
代码:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
matlab_to_epoch_days = 719529 # days from 1-1-0000 to 1-1-1970
matlab_to_epoch_seconds = matlab_to_epoch_days * 24 * 60 * 60
def matlab_to_datetime(matlab_date_num_seconds):
# get number of seconds from epoch
from_epoch = matlab_date_num_seconds - matlab_to_epoch_seconds
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
print(matlab_to_datetime(63650571169.50261))
2017-01-01 10:12:49.502609
测试代码:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
matlab_to_epoch_days = 719529 # days from 1-1-0000 to 1-1-1970
matlab_to_epoch_seconds = matlab_to_epoch_days * 24 * 60 * 60
def matlab_to_datetime(matlab_date_num_seconds):
# get number of seconds from epoch
from_epoch = matlab_date_num_seconds - matlab_to_epoch_seconds
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
print(matlab_to_datetime(63650571169.50261))
2017-01-01 10:12:49.502609
结果:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
matlab_to_epoch_days = 719529 # days from 1-1-0000 to 1-1-1970
matlab_to_epoch_seconds = matlab_to_epoch_days * 24 * 60 * 60
def matlab_to_datetime(matlab_date_num_seconds):
# get number of seconds from epoch
from_epoch = matlab_date_num_seconds - matlab_to_epoch_seconds
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
print(matlab_to_datetime(63650571169.50261))
2017-01-01 10:12:49.502609
您的datenum以秒为单位。要转换为
datetime
,需要将秒数偏移为python理解的datetime
。此函数使用Unix历元作为该引用
代码:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
matlab_to_epoch_days = 719529 # days from 1-1-0000 to 1-1-1970
matlab_to_epoch_seconds = matlab_to_epoch_days * 24 * 60 * 60
def matlab_to_datetime(matlab_date_num_seconds):
# get number of seconds from epoch
from_epoch = matlab_date_num_seconds - matlab_to_epoch_seconds
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
print(matlab_to_datetime(63650571169.50261))
2017-01-01 10:12:49.502609
测试代码:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
matlab_to_epoch_days = 719529 # days from 1-1-0000 to 1-1-1970
matlab_to_epoch_seconds = matlab_to_epoch_days * 24 * 60 * 60
def matlab_to_datetime(matlab_date_num_seconds):
# get number of seconds from epoch
from_epoch = matlab_date_num_seconds - matlab_to_epoch_seconds
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
print(matlab_to_datetime(63650571169.50261))
2017-01-01 10:12:49.502609
结果:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
matlab_to_epoch_days = 719529 # days from 1-1-0000 to 1-1-1970
matlab_to_epoch_seconds = matlab_to_epoch_days * 24 * 60 * 60
def matlab_to_datetime(matlab_date_num_seconds):
# get number of seconds from epoch
from_epoch = matlab_date_num_seconds - matlab_to_epoch_seconds
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
print(matlab_to_datetime(63650571169.50261))
2017-01-01 10:12:49.502609
63650571169.50261
这个数字似乎很大,是一个MATLAB datenum首先,MATLAB datenum是从10000年1月0日开始的天数,这个数字相当于174385126年。这是外星人联系的时间吗?溢出问题可能与此63650571169有关。50261是一个datenum,表示日期加时间,例如2010-11-04 00:03:50.209589。那么,我怎样才能获得一个日期加一个时间呢?63650571169.50261
这个数字对于MATLAB datenum来说似乎很大首先,MATLAB datenum是从10000年1月0日开始的天数,这个数字相当于174385126年。这是外星人联系的时间吗?溢出问题可能与此63650571169有关。50261是一个datenum,表示日期加时间,例如2010-11-04 00:03:50.209589。那么,我怎样才能获得一个日期加一个时间呢?