0

I have tried a wealth of options and got it down with like some hacked together parsing but I am curious how to do this with strptime?

item = "01/Jul/1995:00:00:01-0400"

checkdate = datetime.strptime(item,"%Y-%m-%dT:%H:%M%S%z")
checkdate = datetime.strptime(item,"%Y/%m/%dT:%H:%M:%S%z")
checkdate = datetime.strptime(item,"%Y-%b-%d:%H:%M%S%z")
checkdate = datetime.strptime(item,"%Y-%b-%dT:%H:%M:%S%z")
checkdate = datetime.strptime(item,"%Y/%m/%d:%H:%M:%S%z")

what i get for each attempt is :

ValueError: time data '01/Jul/1995:00:00:01-0400' does not match format '%Y/%m/%d:%H:%M:%S%z'

what is the correct strptime formatting for this?

EDIT: so you were correct and i did a small test

def split_date (stringdate):    
     datepart = [] 
     monthDict = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05',
 'Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
    split1 = [part for part in stringdate.split('/')]
    day = split1[0]
    month = split1[1]
    month = monthDict.get(month)
    split2 = [part for part in split1[2].split(":")]
    year = split2[0]
    hour = split2[1]
    minute = split2[2]
    split3 = [part for part in split2[3].split('-')]
    second = split3[0]
    timezone = split3[1]
    return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second), int(timezone)

datetime_received_split = []
datetime_received_strp = []
s = time.time()
for date in data.time_received:
    try: 
        datetime_received_split.append(split_date(date))
    except:
        split_fail.append(date)
e = time.time()
print ('split took {} s '.format(e-s))
s = time.time() 
for date in data.time_received:
    try: 
             datetime_received_strp.append(datetime.strptime(item,"%d/%b/%Y:%H:%M:%S-     %f"))
    except: 
        strp_fail.append(date)
e = time.time() 
print ('strp took {} s'.format(e-s))

and i found that the manual split was actually faster by a large margin?

Joe Sadaka
  • 175
  • 1
  • 9
  • Did you try one that actually matched the data? All of your attempts start with `%Y`, which is year, and your string starts with the day of the month, `%d`. Try building your format string [with the documentation](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior) – sco1 Apr 05 '17 at 03:03

2 Answers2

1

%z is supported in Python 3.2+.

So for Python2.x, have a look at How to parse dates with -0400 timezone string in python?

If you're using Python3.x you can try this:

from datetime import datetime

item = "01/Jul/1995:00:00:01-0400"
checkdate = datetime.strptime(item,"%d/%b/%Y:%H:%M:%S%z")

print(checkdate)

Result:

1995-07-01 00:00:01-04:00

See more details from strftime() and strptime() Behavior

Community
  • 1
  • 1
McGrady
  • 10,869
  • 13
  • 47
  • 69
1

I fixed your date conversion. What's great is %f is supported in both Python 2.7 and 3.x.

from datetime import datetime

item = "01/Jul/1995:00:00:01-0400"
checkdate = datetime.strptime(item,"%d/%b/%Y:%H:%M:%S-%f")

print(checkdate)
Neil
  • 14,063
  • 3
  • 30
  • 51