LoginSignup
0
0

More than 3 years have passed since last update.

naive -> aware

Last updated at Posted at 2020-02-20
  • 夏時間とか考慮しなくて良いならdatetimeだけでいけるのは良い
  • str -> naive -> aware(UTC) にしてからあつかう
  • datetime.replacetzinfo とか datetimetzinfo には pytz.timezone("Asia/Tokyo") を食わせてはいけない

まず import ,UTC/JSTの定義,日付文字列の用意


    from datetime import datetime, timedelta, timezone

    UTC = timezone.utc
    JST = timezone(timedelta(hours=+9), "JST")
    val = "2020/02/15T13:59:27Z"

string -> naive


    # 末尾のtimezone情報削るかただの文字列として扱う.strptimeに%zとか%Zとか渡さない
    naive = datetime.strptime(val, "%Y/%m/%dT%H:%M:%SZ")

naive -> aware


    aware_utc = naive.replace(tzinfo=UTC)

aware -> naive


    naive = aware_utc.replace(tzinfo=None)

違うtimezoneでの時間にする


    aware_jst = aware_utc.astimezone(JST)

違うtimezoneに書き換える


    aware_jst = aware_utc.replace(tzinfo=JST)

これらをダラダラと書きなぐったのがこれ

0
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
0
0