1
0

More than 1 year has passed since last update.

Pythonのdatetimeはawareとnaiveだよ(nativeじゃないよ)

Posted at

Python datetime

pythonの標準ライブラリdatetimneのdatetimeオブジェクトは時刻を扱う。
datetimeにはaware(タイムゾーン情報有)とnaive(タイムゾーン情報無)がある。

from datetime import datetime
from dateutil import tz

print(datetime.today()) # 2021-10-26 23:46:50.725275
print(datetime.today().tzinfo) # None つまりnaive

print(datetime(2021, 12, 31, 19, 55, 40, 0, tz.gettz('Asia/Tokyo'))) # 2021-12-31 19:55:40+09:00
print(datetime(2021, 12, 31, 19, 55, 40, 0, tz.gettz('Asia/Tokyo')).tzinfo) # tzfile('Japan') つまりaware

WEBで色んな記事を見ているとnaiveのことをnativeと誤記しているものが散見される。
aware(気づいている)の対義語だからnaive(ものを知らない)。

公式ドキュメント にも書いてある。

Aware オブジェクトと Naive オブジェクト¶
日時のオブジェクトは、それらがタイムゾーンの情報を含んでいるかどうかによって "aware" あるいは "naive" に分類されます。

間違えないようにしましょう。

P.S.

古い公式ドキュメント だと公式がnaiveと書くべきところをnativeと間違えている。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0