0

I have the statements:

v  = datetime.strptime('2020-07-08T09:00:01+00:00', '%Y-%m-%dT%H:%M:%S%z')
print(v)

The part that doesn't work is the timezone. I know this because it works find if I don't use the timezone.

v  = datetime.strptime('2020-07-08T09:00:01', '%Y-%m-%dT%H:%M:%S')

This works as I expect in Jupyter; however, I can't get it to work in PyCharm. PyCharm configured to use a VE. Not using the same version of python.

It returns the error:

ValueError: time data '2020-07-08T09:00:01+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'

In PyCharm console:

print (sys.version_info)
sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)

In Jupiter

print (sys.version_info)
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)

Is there some other way to do this? A different code? Am I missing something?

elbillaf
  • 1,952
  • 10
  • 37
  • 73
  • 1
    In Python 3.7, `.strptime()` gained the ability to ignore that colon in the middle of the UTC offset. Previous versions would require the field to be written as `+0000`. – jasonharper Aug 07 '20 at 01:15
  • That's it! So I guess the fix is I manually remove that colon. thanks – elbillaf Aug 07 '20 at 01:53
  • 1
    With Py3.7+, you should be able to use `fromisoformat` instead of `strptime` ([a bit more efficient](https://stackoverflow.com/questions/13468126/a-faster-strptime)). – FObersteiner Aug 07 '20 at 04:58

0 Answers0