2

I have read a few stackoverflow posts but still can't figure this out...

I want to crawl craigslist post posted within last 48 hours. Posted time is in the following format for craigslist:

2013-03-15, 7:43PM MDT

I have tried

string = "2013-03-15, 7:43PM MDT"

time.strptime(string, "%Y-%m-%d, %I:%M%p %Z")

But apparent the format doesnt match the string. What should be the format for this time string?

Mattios550
  • 372
  • 1
  • 5
  • 18
WYH
  • 53
  • 1
  • 6

1 Answers1

2

The problem is the MDT. Python's %Z doesn't support that (at least it seems so to me). There are probably better solutions, but this one should work:

import time
import datetime

#use the UTC which Python understands
a="2013-03-15, 7:43PM MDT".replace("MDT","UTC")
fs="%Y-%m-%d, %I:%M%p %Z"
c=time.strptime(a, fs)

#converting from UTC to MDT (time difference)
dt = datetime.datetime.fromtimestamp(time.mktime(c)) - datetime.timedelta(hours=6)
print dt
kren470
  • 335
  • 1
  • 3
  • 13