Python3 で日付処理覚書
概要
AWS のリソースデータを Python の SDK である boto3 を使って取得した場合、
日付データは datetime.datetime(2020, 8, 23, 10, 14, 9, tzinfo=tzlocal())
のように取得される。
その値を dateutil.tz を利用してタイムゾーンを意識した値として利用する方法の覚書。
※dateutil は AWS Lambda では標準で使用できる
dateutil - powerful extensions to datetime — dateutil 2.8.1 documentation
datetime の timezone ごとの値
ソース
import datetime import dateutil.tz # 現在時刻 print('----- local') datetime_local = datetime.datetime.now(dateutil.tz.tzlocal()) print(repr(datetime_local)) print(datetime_local) print('----- utc') datetime_utc = datetime.datetime.now(dateutil.tz.tzutc()) print(repr(datetime_utc)) print(datetime_utc) print('----- jst') datetime_jst = datetime.datetime.now(dateutil.tz.gettz('Asia/Tokyo')) print(repr(datetime_jst)) print(datetime_jst)
結果(タイムゾーンが UTC の環境)
----- local datetime.datetime(2020, 8, 23, 12, 25, 33, 93313, tzinfo=tzlocal()) 2020-08-23 12:25:33.093313+00:00 ----- utc datetime.datetime(2020, 8, 23, 12, 25, 33, 93706, tzinfo=tzutc()) 2020-08-23 12:25:33.093706+00:00 ----- jst datetime.datetime(2020, 8, 23, 21, 25, 33, 94216, tzinfo=tzfile('/usr/share/zoneinfo/Asia/Tokyo')) 2020-08-23 21:25:33.094216+09:00
結果(タイムゾーンが JST の環境)
----- local datetime.datetime(2020, 8, 23, 21, 26, 59, 65988, tzinfo=tzlocal()) 2020-08-23 21:26:59.065988+09:00 ----- utc datetime.datetime(2020, 8, 23, 12, 26, 59, 66953, tzinfo=tzutc()) 2020-08-23 12:26:59.066953+00:00 ----- jst datetime.datetime(2020, 8, 23, 21, 26, 59, 163724, tzinfo=tzfile('Japan')) 2020-08-23 21:26:59.163724+09:00
Local から UTC / JST への変換
ソース
import datetime import dateutil.tz # 変換 datetime_base = datetime.datetime.now(dateutil.tz.tzlocal()) print('----- Local') datetime_local = datetime_base print(repr(datetime_local)) print(datetime_local) print('----- UTC') datetime_utc = datetime_base.astimezone(dateutil.tz.tzutc()) print(repr(datetime_utc)) print(datetime_utc) print('----- JST') datetime_jst = datetime_base.astimezone(dateutil.tz.gettz('Asia/Tokyo')) print(repr(datetime_jst)) print(datetime_jst)
結果(タイムゾーンが UTC の環境)
----- Local datetime.datetime(2020, 8, 23, 12, 53, 46, 149610, tzinfo=tzlocal()) 2020-08-23 12:53:46.149610+00:00 ----- UTC datetime.datetime(2020, 8, 23, 12, 53, 46, 149610, tzinfo=tzutc()) 2020-08-23 12:53:46.149610+00:00 ----- JST datetime.datetime(2020, 8, 23, 21, 53, 46, 149610, tzinfo=tzfile('/usr/share/zoneinfo/Asia/Tokyo')) 2020-08-23 21:53:46.149610+09:00
結果(タイムゾーンが JST の環境)
----- Local datetime.datetime(2020, 8, 23, 21, 53, 47, 787886, tzinfo=tzlocal()) 2020-08-23 21:53:47.787886+09:00 ----- UTC datetime.datetime(2020, 8, 23, 12, 53, 47, 787886, tzinfo=tzutc()) 2020-08-23 12:53:47.787886+00:00 ----- JST datetime.datetime(2020, 8, 23, 21, 53, 47, 787886, tzinfo=tzfile('Japan')) 2020-08-23 21:53:47.787886+09:00
オマケ:byte文字列から datetime への変換
※dns.resolver などで証明書の有効期限を取得したときなど
ソース
import datetime import dateutil.tz byte_datetime = b'20210708151000Z' # 文字列に print(byte_datetime.decode('ascii')) # 「Z」を「+00:00」としてから datetime に変換 not_after = datetime.datetime.strptime(byte_datetime.decode('ascii').replace('Z', '+00:00'), '%Y%m%d%H%M%S%z') print(repr(not_after)) print('----- Local') datetime_local = not_after.astimezone(dateutil.tz.tzlocal()) print(datetime_local) print(datetime_local.strftime('%Y/%m/%d')) print('----- UTC') datetime_utc = not_after.astimezone(dateutil.tz.tzutc()) print(datetime_utc) print(datetime_utc.strftime('%Y/%m/%d')) print('----- JST') datetime_local = not_after.astimezone(dateutil.tz.gettz('Asia/Tokyo')) print(datetime_local) print(datetime_local.strftime('%Y/%m/%d'))
結果(タイムゾーンが UTC の環境)
20210708151000Z datetime.datetime(2021, 7, 8, 15, 10, tzinfo=datetime.timezone.utc) ----- Local 2021-07-08 15:10:00+00:00 2021/07/08 ----- UTC 2021-07-08 15:10:00+00:00 2021/07/08 ----- JST 2021-07-09 00:10:00+09:00 2021/07/09
結果(タイムゾーンが JST の環境)
20210708151000Z datetime.datetime(2021, 7, 8, 15, 10, tzinfo=datetime.timezone.utc) ----- Local 2021-07-09 00:10:00+09:00 2021/07/09 ----- UTC 2021-07-08 15:10:00+00:00 2021/07/08 ----- JST 2021-07-09 00:10:00+09:00 2021/07/09