0

Although I passed "%a %b %d %H:%M:%S %Z %Y" as the format string in time.strptime(), it is operating on '%a %b %d %H:%M:%S %Y' and therefore causing error. Any idea on what might be causing it?

The same thing runs perfectly in python console but not in the actual code.

    Exception in thread Thread-7:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "proxy.py", line 137, in listenThread
    response = self.fetchRequest(raw_request, request)
  File "proxy.py", line 114, in fetchRequest
    if request['type'] == "GET" and self.is_cachable(request, response_headers):
  File "proxy.py", line 100, in is_cachable
    requestTime = time.mktime(time.strptime(self.request_log[request['url']][len(self.request_log[request['url']])-3]), "%a %b  %d %H:%M:%S %Z %Y")
  File "/usr/lib/python2.7/_strptime.py", line 478, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
    (data_string, format))
ValueError: time data 'Mon Apr  10 22:52:38 IST 2017' does not match format '%a %b %d %H:%M:%S %Y'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
vanillascotch
  • 194
  • 3
  • 13
  • Please show the actual code that caused the problem. – DYZ Apr 10 '17 at 17:40
  • You got an extra space between "Apr" and "10". – Philip Tzou Apr 10 '17 at 17:40
  • You must be running different code than you think you are. Python won't randomly use a different hard-coded string. – Bryan Oakley Apr 10 '17 at 17:40
  • The error shows it is using the format: '%a %b %d %H:%M:%S %Y' although you are passing the correct format. Please share your codes and sample data as well. Thanks. – jose_bacoy Apr 10 '17 at 17:41
  • It could be that `%Z` is not implemented See this: http://stackoverflow.com/questions/13556260/c-c-strptime-does-not-parse-z-timezone-name and this: http://bugs.python.org/issue22377 and this: http://stackoverflow.com/questions/3305413/python-strptime-and-timezones – Random Davis Apr 10 '17 at 17:41
  • Possible duplicate of [Python strptime() and timezones?](http://stackoverflow.com/questions/3305413/python-strptime-and-timezones) – Random Davis Apr 10 '17 at 17:42
  • Your *server* and your *source code* are out of sync. Your source code lines are inaccurate, because the exception clearly shows what the *actual* format was that was passed in. Make sure you restart your server properly. – Martijn Pieters Apr 10 '17 at 17:44

2 Answers2

1

The value for %Z comes from the tzinfo object attached to the datetime object, but there is no datetime object (and thus no tzinfo either) if you're using strptime() instead of strftime(). Indeed, the only tzinfo objects which come with the standard Python library are the fixed-offset timezone objects, and then only in Python 3. The standard library does not have a canonical list of these, besides a standard UTC value; instead, you're expected to create them as needed, or use pytz instead. As a result, the Python standard library has no mechanism for translating arbitrary three-letter codes into timezones. It should be able to handle 'UTC', some synonyms, and perhaps your local time as returned by your system locale, but that's about it.

(Hint: For most people, pytz is the Right Thing to use.)

Three-letter codes are not globally unique anyway, so there is no "right" way to do what you're asking for. You need to have something like an Olson database identifier (e.g. America/New_York is roughly synonymous with Eastern Standard/Daylight Time).

Kevin
  • 28,963
  • 9
  • 62
  • 81
  • Even for `%Z` the traceback should show the format passed in. That's not the case here. The only conclusion I can come to is that the *server is running stale bytecode*. The source code is newer. – Martijn Pieters Apr 10 '17 at 17:45
  • @MartijnPieters: It would be nice if OP's source matched OP's bytecode, but that unfortunately will not solve OP's actual problem, which is that they are trying to do something impossible. – Kevin Apr 10 '17 at 17:47
  • Sure, but *that's what the error is about*. The actual format used does not contain `%Z`. – Martijn Pieters Apr 10 '17 at 17:48
  • @MartijnPieters: Python is *supposed* to figure out .pyc files automatically. I'm marginally skeptical of your explanation. I imagine the `%Z` got silently removed at some point in the standard library. – Kevin Apr 10 '17 at 17:49
  • Also, it depends on the Python version, Python 3.2 and up **do** support `%Z`. – Martijn Pieters Apr 10 '17 at 17:49
  • I can reproduce such an error at will. Just start a process with the one version, then change the source code, then trigger the traceback. This is not necessarily about stale bytecode (although that too can be reproduced, all you need is a synching process that forgets about timestamps). – Martijn Pieters Apr 10 '17 at 17:50
  • @MartijnPieters: Fine, I concede that point. But Python 3.2+ *don't* support `%Z`, I just tried it: `ValueError: time data '2017-01-01 EST 00:00' does not match format '%Y-%m-%d %Z %H:%M'` – Kevin Apr 10 '17 at 17:52
  • Hrm, I may be misreading the footnote on that. – Martijn Pieters Apr 10 '17 at 17:55
  • It works for UTC: `datetime.strptime('2017-01-01 00:00 UTC', '%Y-%m-%d %H:%M %Z')`. – Martijn Pieters Apr 10 '17 at 17:58
  • Basically, it is platform dependent. On OS X, so far only UTC is working. – Martijn Pieters Apr 10 '17 at 17:59
  • @Kevin, it doesn't explain why this `>>> time.mktime(time.strptime('Mon Apr 10 23:25:38 IST 2017', "%a %b %d %H:%M:%S %Z %Y")) 1491846938.0 >>>` works perfectly – vanillascotch Apr 10 '17 at 18:00
  • 1
    Ah, this is http://bugs.python.org/issue22377; UTC, GMT, and BST (the latter two listed in `time.tzname`) all work for me, but no tz info is actually returned. That's because glibc is punting on this big time. – Martijn Pieters Apr 10 '17 at 18:00
  • @mx_pc: That does not work for me under either Python 2.7 or Python 3.4. – Kevin Apr 10 '17 at 18:01
  • @mx_pc: I wouldn't call that perfectly. It won't include the timezone info if you are on a glibc platform (only the strings in `time.tzname` are supported, plus UTC). – Martijn Pieters Apr 10 '17 at 18:01
1

Actualy I just made a very silly mistake

requestTime = time.mktime(time.strptime(self.request_log[request['url']][len(self.request_log[request['url']])-3]), "%a %b  %d %H:%M:%S %Z %Y")

the order of parenthesis is wrong in above, it should be like this

requestTime = time.mktime(time.strptime(self.request_log[request['url']][len(self.request_log[request['url']])-3], "%a %b  %d %H:%M:%S %Z %Y"))

I was actually passing only time string to time.strptime() leaving the format
Sorry for inconvinience caused

vanillascotch
  • 194
  • 3
  • 13