0

I'm attempting to pass a datetime string with a timezone offset into a Django url as a query param as follows:

http://0.0.0.0:8080/coordinates/12.231/34.322/?obstime=2021-06-10T18:39:41+1000

I'm attempting to parse this in the view as follows:

datetime.datetime.strptime(
    self.request.query_params.get('obstime'),
    "%Y-%m-%dT%H:%M:%S %z"
).isoformat()

So I'm utilising the format "%Y-%m-%dT%H:%M:%S %z".

I thought this would work, however I am seeing the following error:

time data '2021-06-10T18:39:41 1000' does not match format '%Y-%m-%dT%H:%M:%S %z'

I've tried to search for potential fixes for this error, but so far everything has come up dry.

Does anyone know the correct format I need to pass in to either the url endpoint or to the datetime.datetime.strptime() function...

Micheal J. Roberts
  • 3,735
  • 4
  • 37
  • 76
  • [The documentation for the `%z` format](https://docs.python.org/3.9/library/datetime.html#strftime-strptime-behavior) says: "UTC offset in the form `±HHMM[SS[.ffffff]]`". `10:00` doesn't seem to match this format – ForceBru May 10 '21 at 15:32
  • @ForceBru Yes, but in the URL I am passing it in the correct format: `?obstime=2021-06-10T18:39:41+1000` – Micheal J. Roberts May 10 '21 at 15:34
  • Yeah, I've just noticed this. Not sure why this happened... – ForceBru May 10 '21 at 15:34
  • 2
    `+` in a URL can be interpreted as a space. Try using `%2B` instead in the URL. See https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20#:~:text=A%20URL%20is%20a%20Uniform,page%20(in%20most%20cases).&text=For%20HTTP%20URLs%2C%20a%20space,part%20can%20be%20left%20unencoded. –  May 10 '21 at 15:37
  • Personally I would have done it differently, I would have used a timestamp, less headache that way and less different symbols in the url. You'd be able to interpret it simply by having `datetime.datetime.fromtimestamp(int(self.request.query_params.get('obstime'))).isoformat()` (though would be better to check if it is numeric before trying to cast it to int). – BoobyTrap May 10 '21 at 15:42

2 Answers2

1

According to the URL encoding rules, + becomes (space). If you want to use the + sign in the URL, you must substitute %2B.

Not ok:

?obstime=2021-06-10T18:39:41+1000 
(this becomes ?obstime=2021-06-10T18:39:41 1000)

Ok:

?obstime=2021-06-10T18:39:41%2B1000 
(this becomes ?obstime=2021-06-10T18:39:41+1000)

Also, you omitted the space sign. Perhaps if you request the URL like this, it will be handled normally.

?obstime=2021-06-10T18:39:41+%2B1000
(this becomes ?obstime=2021-06-10T18:39:41 +1000)

Related: How to encode the plus (+) symbol in a URL

youngminz
  • 1,364
  • 2
  • 14
  • 23
0

I think you need the '+'.

a_result = datetime.datetime.strptime('2021-06-10T18:39:41 +1000', '%Y-%m-%dT%H:%M:%S %z')
print('a result', a_result)

a result 2021-06-10 18:39:41+10:00

This will parse with out a space, so you need some token between the seconds and the timezone. I was able to make these work for India's 5:30 time zone

a_result = datetime.datetime.strptime('2021-06-10T18:39:41+05:30', '%Y-%m-%dT%H:%M:%S%z')
print(a_result)

2021-06-10 18:39:41+05:30

I also found this Python timezone '%z' directive for datetime.strptime() not available