0

I have a piece of code that currently runs without any issues on my local python (anaconda python 3.5.1) but is giving me an odd error when I run it in production (where I am under the impression the environment is very similar and the python version is the same).

Below is the relevant code snippet:

ds_date_format = "%Y%m%d"
rta_date_format = '%a %b %d %H:%M:%S %Z %Y'

d1 = datetime.datetime.strptime(date_ds, ds_date_format)
d2 = datetime.datetime.strptime(date_rta, rta_date_format).replace(hour=0, minute=0, second=0)

And the traceback for the error I am getting.

{
  "error": {
    "traceback": [
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.5/site-packages/tornado/web.py\", line 1445, in _execute\n    result = yield result\n",
      "  File \"/opt/conda/lib/python3.5/site-packages/tornado/gen.py\", line 1008, in run\n    value = future.result()\n",
      "  File \"/opt/conda/lib/python3.5/site-packages/tornado/concurrent.py\", line 232, in result\n    raise_exc_info(self._exc_info)\n",
      "  File \"<string>\", line 3, in raise_exc_info\n",
      "  File \"/opt/conda/lib/python3.5/site-packages/tornado/gen.py\", line 1017, in run\n    yielded = self.gen.send(value)\n",
      "  File \"<string>\", line 6, in _wrap_awaitable\n",
      "  File \"rtamatching.py\", line 171, in post\n    self.write(await self.rta_matcher.check_matches(data))\n",
      "  File \"rtamatching.py\", line 114, in check_matches\n    date_match_scores.append(date_similarity(date, user_feeds[key]['TransactionDate']))\n",
      "  File \"/opt/api/functions.py\", line 9, in date_similarity\n    d2 = datetime.datetime.strptime(date_rta, rta_date_format).replace(hour=0, minute=0, second=0)\n",
      "  File \"/opt/conda/lib/python3.5/_strptime.py\", line 500, in _strptime_datetime\n    tt, fraction = _strptime(data_string, format)\n",
      "  File \"/opt/conda/lib/python3.5/_strptime.py\", line 337, in _strptime\n    (data_string, format))\n",
      "ValueError: time data 'Wed Mar 09 21:59:50 PST 2016' does not match format '%a %b %d %H:%M:%S %Z %Y'\n"
    ],
    "code": 500,
    "message": "Internal Server Error"
  }
}

That value error seems odd to be because as far as I can tell, the format specified is correct.

Everaldo Aguiar
  • 4,016
  • 7
  • 26
  • 31
  • 2
    maybe you should try dateutil.parser.parse, it can prevent such struggle http://dateutil.readthedocs.io/en/stable/parser.html – DevLounge May 11 '16 at 19:41
  • 2
    if you use `date_rta = "Wed Mar 09 21:59:50 UTC 2016"`(change PST with UTC) it works, so it must be related to the timezone value. see [issue](http://bugs.python.org/issue22377) – M.T May 11 '16 at 19:48
  • These were all really helpful! @Apero I re-wrote the code using that and it works now! would that be a lot less efficient though? – Everaldo Aguiar May 11 '16 at 20:03
  • 1
    No idea regarding efficiency, but it is a really famous and widely used third-party module – DevLounge May 11 '16 at 20:04

1 Answers1

0

As pointed out by M.T, there appears to be an issue with strptime() and timezones. Re-writing my code as follows achieved the result I was expecting:

from dateutil.parser import parse    
...
d1 = parse(date_ds)
d2 = parse(date_rta)
...
Everaldo Aguiar
  • 4,016
  • 7
  • 26
  • 31