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 datetimeでうるう年の慣習を「オフ」にする方法はありますか?

よろしくお願いします。

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 に答える