1

我正在读取时间变量用 udunits 编码的数据。这意味着时间变量是自 1-1-1 00:00:00 以来的小时数,但是,它不遵循标准的闰年约定。

我正在尝试将自 1-1-1 00:00:00 以来的小时数转换为 python datetime 对象,但最终得到不正确的答案,因为 datetime 确实遵循闰年惯例。例如:

t_data = 17584272    # This is time stamp for January 1st, 2007 in the data file
day = datetime.datetime(1,1,1) + datetime.timedelta(hours=t_data)
print(day)

结果:

>>> 2007-01-03 00:00:00

有没有办法在 python 日期时间中“关闭”闰年约定?

在此先感谢您的帮助!

4

1 回答 1

1

I think you're incorrect in your assessment that leap years aren't being used properly, all of the samples you've listed are consistent. There's just some offset that you need to account for. It's not necessary to know what the offset is, you just start with a known date and hour figure.

def udtimestamp(hours):
    return datetime.datetime(1990,1,1) + datetime.timedelta(hours=hours-17435256)

>>> for x in (17584272,1.7435256E7,1.747908E7,1.7522904E7,1.763688E7):
    print udtimestamp(x)

2007-01-01 00:00:00
1990-01-01 00:00:00
1995-01-01 00:00:00
2000-01-01 00:00:00
2013-01-01 00:00:00
于 2014-06-19T23:10:32.327 回答