0

I tried to submit a time var with value of 2016-03-12T01:47:57+00:00 in a timestamp field, it gives me error saying to check the syntax for errors, however when I use a function to normalize the date

   t = datetime.datetime.strptime(data['time'], '%Y-%m-%dT%H:%M:%SZ').strftime('%Y-%m-%d:%H:%M:%S')

I get an error like this.

time data '2016-03-12T01:47:57+00:00' does not match format '%Y-%m-%dT%H:%M:%SZ'

soni-b3196413
  • 57
  • 1
  • 8

2 Answers2

2

What's causing your problem has already been clarified by others, but please allow me to suggest my favorite solution for cases such as yours:

from dateutil import parser
parser.parse(data['time'])

More about the dateutil module here.

lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48
  • I wish it was as easy as that .. I get `ImportError: No module named 'dateutil'` and when I try to install it via pip `pip install dateutil` I get the error `Could not find a version that satisfies the requirement dateutil (from versions: ) No matching distribution found for dateutil` so .. it is going to take me hours to install it, and may not even work then .. :\ – soni-b3196413 Mar 14 '16 at 14:34
  • Please try `pip install python-dateutil` – lucasnadalutti Mar 14 '16 at 14:38
1

There are a few problems here:

  1. The %Z (note the captial Z!) is for time zone, for example GMT. I think you want the lower case option: %z, which is for UTC offset.

    You can read here in the docs what all the options do :) https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

  2. You need the % symbol before each option. You cannot write %Sz, you must write %S%z. Otherwise Python is trying to match something like 2016-03-12T01:47:57z, rather than 2016-03-12T01:47:57+00:00

  3. Unfortunately, you can't use the %z option with strptime, see this answer: ISO to datetime object: 'z' is a bad directive

My solution:

It sounds like you don't even want to use the UTC offset. That's fine! If you can't change the way your date string is generated, perhaps this is the best option (though it's maybe a little dirty):

   t = datetime.datetime.strptime(data['time'][:-6], '%Y-%m-%dT%H:%M:%S')

This will remove the UTC offset from the string.

If you can change the way your datetime string is being generated, that would be a better solution, but I realise you might not be able to do so.

I hope this helps!

Community
  • 1
  • 1
lochsh
  • 366
  • 1
  • 12
  • I assumed that data['time'] is the example string that you gave: '2016-03-12T01:47:57+00:00' – lochsh Mar 14 '16 at 14:03
  • I am getting ` time data '2016-03-12T01:47:57' does not match format '%Y-%m-%d:%H:%M:%S'` .. I still can't figure out the issue here – soni-b3196413 Mar 14 '16 at 14:31
  • You're missing a capital T in your comment that was present in your question! Did you try copying and pasting the code snippet I provided? It should work – lochsh Mar 14 '16 at 14:32
  • time data `'%Y-%m-%dT%H:%M:%S'` does not match format `'2016-08-02T21:00:00'` – Viktor Aug 01 '16 at 07:36