只是 Build 并添加到以前的评论 . 关键是当天计数由 datetime 和类 datetime 中的构造函数 fromordinal 以及相关的子类执行 . 例如,从Python Library Reference for 2.7,一个读取 fromordinal
返回对应于公历格里高利序数的日期,其中第1年1月1日有序数1.除非1 <= ordinal <= date.max.toordinal(),否则引发ValueError .
但是,公元0年仍然是一个(跳跃)年份,因此仍有366天需要考虑 . (闰年,就像2016年,恰好是四年前的四十四年 . )
这些是我用于类似目的的两个功能:
import datetime
def datetime_pytom(d,t):
'''
Input
d Date as an instance of type datetime.date
t Time as an instance of type datetime.time
Output
The fractional day count since 0-Jan-0000 (proleptic ISO calendar)
This is the 'datenum' datatype in matlab
Notes on day counting
matlab: day one is 1 Jan 0000
python: day one is 1 Jan 0001
hence an increase of 366 days, for year 0 AD was a leap year
'''
dd = d.toordinal() + 366
tt = datetime.timedelta(hours=t.hour,minutes=t.minute,
seconds=t.second)
tt = datetime.timedelta.total_seconds(tt) / 86400
return dd + tt
def datetime_mtopy(datenum):
'''
Input
The fractional day count according to datenum datatype in matlab
Output
The date and time as a instance of type datetime in python
Notes on day counting
matlab: day one is 1 Jan 0000
python: day one is 1 Jan 0001
hence a reduction of 366 days, for year 0 AD was a leap year
'''
ii = datetime.datetime.fromordinal(int(datenum) - 366)
ff = datetime.timedelta(days=datenum%1)
return ii + ff
希望这有助于并且乐于纠正 .