1

Python 3.3 を使用しています。IMAP サーバーから電子メールを取得し、それを標準の電子メール ライブラリから電子メールのインスタンスに変換しています。

私はこれをします:

message.get("date")

たとえば、次のようになります。

Wed, 23 Jan 2011 12:03:11 -0700

time.strftime()これをうまくフォーマットできるように、これを入れられるものに変換したいと思います。UTC ではなく、現地時間で結果が必要です。

非常に多くの関数、非推奨のアプローチ、およびサイド ケースがあり、最新のルートが不明ですか?

4

3 に答える 3

4

このようなもの?

>>> import time
>>> s = "Wed, 23 Jan 2011 12:03:11 -0700"
>>> newtime = time.strptime(s, '%a, %d %b %Y %H:%M:%S -0700')
>>> print(time.strftime('Two years ago was %Y', newtime))
Two years ago was 2011 # Or whatever output you wish to receive.
于 2013-06-08T11:23:02.703 に答える
0

これを行う:

import email, email.utils, datetime, time    

def dtFormat(s):
  dt = email.utils.parsedate_tz(s)
  dt = email.utils.mktime_tz(dt)
  dt = datetime.datetime.fromtimestamp(dt)
  dt = dt.timetuple()
  return dt

次にこれ:

s = message.get("date")    # e.g. "Wed, 23 Jan 2011 12:03:11 -0700"
print(time.strftime("%Y-%m-%d-%H-%M-%S", dtFormat(s)))

これを与える:

2011-01-23-21-03-11
于 2013-06-09T10:05:25.300 に答える