使用 strptime 将字符串转换为具有时区偏移的 python 日期类型对象
Convert string to python date type object with timezone offset using strptime
使用 strptime
将此字符串 Tue Jan 10 2017 13:00:13 GMT 0800 (Taipei Standard Time)
转换为 python 日期类型对象的正确格式是什么?
我尝试了这个 question 的答案,但它对我不起作用:
date1 = datetime.strptime(strDate1, '%b %d %Y %I:%M%p')
ValueError: time data 'Tue Jan 10 2017 13:00:13 GMT 0800 (Taipei
Standard Time)' does not match format '%b %d %Y %I:%M%p'
您可以格式化没有时区的日期,稍后添加,
import pytz
date1=datetime.strptime('Tue Jan 10 2017 13:00:13', '%a %b %d %Y %H:%M:%S')
tz=pytz.timezone('Asia/Taipei')
tz.localize(date1)
名义上您可能希望能够使用 %z(小写 z)来转换 TZ 偏移量,但是对此的支持很粗略。所以你可以自己动手做:
import datetime as dt
import re
PARSE_TIMESTAMP = re.compile('(.*) ([+-]?\d+) \(.*\)$')
def my_datetime_parse(timestamp):
''' return a naive datetime relative to UTC '''
# find the standard time stamp, and the TZ offset and remove extra end
matches = PARSE_TIMESTAMP.match(timestamp).groups()
# convert the timestamp element
timestamp = dt.datetime.strptime(matches[0], '%a %b %d %Y %H:%M:%S %Z')
# calculate the timezone offset
tz_offset = matches[1]
sign = '+'
if tz_offset[0] in '-+':
sign = tz_offset[0]
tz_offset = tz_offset[1:]
tz_offset += '0' * (4 - len(tz_offset))
minutes = int(tz_offset[0:2]) * 60 + int(tz_offset[2:])
if sign == '-':
minutes = -minutes
# add the timezone offset to our time
timestamp += dt.timedelta(minutes=minutes)
return timestamp
date_string = 'Tue Jan 10 2017 13:00:13 GMT +0800 (Taipei Standard Time)'
print(my_datetime_parse(date_string))
这段代码产生:
2017-01-10 21:00:13
代码删除了 (Taipei Standard Time)
,因为它与 +0800
是多余的。
使用 strptime
将此字符串 Tue Jan 10 2017 13:00:13 GMT 0800 (Taipei Standard Time)
转换为 python 日期类型对象的正确格式是什么?
我尝试了这个 question 的答案,但它对我不起作用:
date1 = datetime.strptime(strDate1, '%b %d %Y %I:%M%p')
ValueError: time data 'Tue Jan 10 2017 13:00:13 GMT 0800 (Taipei Standard Time)' does not match format '%b %d %Y %I:%M%p'
您可以格式化没有时区的日期,稍后添加,
import pytz
date1=datetime.strptime('Tue Jan 10 2017 13:00:13', '%a %b %d %Y %H:%M:%S')
tz=pytz.timezone('Asia/Taipei')
tz.localize(date1)
名义上您可能希望能够使用 %z(小写 z)来转换 TZ 偏移量,但是对此的支持很粗略。所以你可以自己动手做:
import datetime as dt
import re
PARSE_TIMESTAMP = re.compile('(.*) ([+-]?\d+) \(.*\)$')
def my_datetime_parse(timestamp):
''' return a naive datetime relative to UTC '''
# find the standard time stamp, and the TZ offset and remove extra end
matches = PARSE_TIMESTAMP.match(timestamp).groups()
# convert the timestamp element
timestamp = dt.datetime.strptime(matches[0], '%a %b %d %Y %H:%M:%S %Z')
# calculate the timezone offset
tz_offset = matches[1]
sign = '+'
if tz_offset[0] in '-+':
sign = tz_offset[0]
tz_offset = tz_offset[1:]
tz_offset += '0' * (4 - len(tz_offset))
minutes = int(tz_offset[0:2]) * 60 + int(tz_offset[2:])
if sign == '-':
minutes = -minutes
# add the timezone offset to our time
timestamp += dt.timedelta(minutes=minutes)
return timestamp
date_string = 'Tue Jan 10 2017 13:00:13 GMT +0800 (Taipei Standard Time)'
print(my_datetime_parse(date_string))
这段代码产生:
2017-01-10 21:00:13
代码删除了 (Taipei Standard Time)
,因为它与 +0800
是多余的。