0

I'm trying to fix an error that keeps popping up for some scripts I built. What I'm basically doing is retrieving some data from a Postgres database which includes a timestamptz field.

When I process that data, I sometimes get the following error:

ValueError: time data '2020-04-23T13:03:49.911533+00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'

The code where it happens:

last_seen_at_date = datetime.strptime(dict['last_seen_at'],'%Y-%m-%dT%H:%M:%S.%f%z').date()

What I don't understand is how this can happen. When I copy paste this string and use it as a variable then convert it to a date, it works perfectly.

Edit: Realized that this is working well on my Mac but not on my severs running on Ubuntu. Not sure how to fix this.

Any idea on what's happening?

Thank you,

johnson23
  • 286
  • 2
  • 16
  • you could check if `dateutil`'s [parser](https://dateutil.readthedocs.io/en/stable/examples.html#parse-examples) works out for you – FObersteiner May 07 '20 at 14:16

2 Answers2

0

'2020-04-23T13:03:49.911533+00:00'

That does looks like ISO 8601 format, so it should work with datetime.datetime.fromisoformat. I did test it using Python 3.7.3 and it seems to work correctly:

import datetime
dt_str ='2020-04-23T13:03:49.911533+00:00'
dt = datetime.datetime.fromisoformat(dt_str)
print(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)

Output:

2020 4 23 13 3 49

Can you test it in your code?

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Thanks @daweo ! Realized that this is working well on my Mac but not on my severs running on Ubuntu. Not sure how to fix this. – johnson23 May 07 '20 at 13:03
0

Fixed the issue using an existing answer. It's a problem with the different version of Python running on my servers.

Datetime strptime issue with a timezone offset with colons

johnson23
  • 286
  • 2
  • 16