LoginSignup
0
0

More than 1 year has passed since last update.

#python 初心者が Twitter API で得られる created_at の String を日本時間で年月日出力しようとした記録コード

Last updated at Posted at 2019-02-11

#python 初心者が Twitter API で得られる created_at の String を日本時間で年月日出力しようとした記録コード

Script

import pytz
import time
import datetime

tweet = {}
tweet['created_at'] = 'Wed May 23 23:01:13 +0000 2007'

t = time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y')

utc = pytz.timezone('UTC')

d = datetime.datetime(*t[:6], tzinfo=utc)
print(d)

tm = d.astimezone(pytz.timezone('Asia/Tokyo'))
print(tm)

zn = tm.strftime('%Y-%m-%d %H:%M:%S %Z%z')
print(zn)

ymd = tm.strftime('%Y-%m-%d')
print(ymd)

Result

2007-05-23 23:01:13+00:00
2007-05-24 08:01:13+09:00
2007-05-24 08:01:13 JST+0900
2007-05-24

Etc

  • 動いてる気はする
  • 日本人にはタイムゾーン周りがつらいのです、言語問わず
  • datetime.py っていうスクリプト名を使ってたら、動かなかったので驚き

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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