1

What is the correct format to convert this string Tue Jan 10 2017 13:00:13 GMT 0800 (Taipei Standard Time) to a python date type object using strptime?

I tried the answer from this question and it is not working for me:

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'

Community
  • 1
  • 1
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117

2 Answers2

2

You can format the date without timezone and add it later,

 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)
franklinsijo
  • 17,784
  • 4
  • 45
  • 63
1

Nominally you would want to be able to use the %z (lowercase z) to convert the TZ offset, however support for this is sketchy. So you can DIY it:

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

This code produces:

2017-01-10 21:00:13

The code removes the (Taipei Standard Time) since it is redundant with the +0800.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • this will works with dynamic datetime string format? – Shift 'n Tab Jan 10 '17 at 06:00
  • This code assumes that there is a parenthesized field with `(extranaous stuff)` at the end that needs to be removed. It also assumes that there is a timezone offset that needs to be parsed. This could be done with the %z format if it were better supported. – Stephen Rauch Jan 10 '17 at 06:04