Is it possible to output a timestamp formatted with a UTC designator 'Z' instead of '+00:00'?
E.g. eventbrite api requires the form: YYYY-MM-DDThh:mm:ssZ, but it only accepts UTC timezone formatted specifically as 2015-02-15T11:35:00Z.
I can't seem to find a way to force arraow (or dateutil) to output 'Z'.
P.S.: I know I can append 'Z' myself, but that's not what I am looking for.
I also miss an option to output the timezone name (such as CEST, BRT, PT, etc) when using .format
+1
I just got bitten by the same issue. For the purposes of moving forward I just conditionally replace the verbose timezone info with Z. I also needed to be explicit with the format so manually defined it rather than using the built in .isoformat() function (although it appeared to give the same results in this case)
iso8601_fmt = 'YYYY-MM-DDTHH:mm:ss.SSSZ'
timestamp_str = arrow.get(timestamp).format(iso8601_fmt)
timestamp_strz = re.sub(r'[+-]00:?00$', 'Z', timestamp_str)
I also need to go the other way and parse the timestamp_str only if it matches this iso format string.
timestamp = arrow.get(timestamp_strz) works but will also convert a random integer into a date.
timestamp = arrow.get(timestamp_strz, iso8601_fmt) does not work thanks to my Z on the end of the string, even though the format string has Z on the end. Somewhat counter-intuitive.
I had to pre-process my string simialrly:
timestamp = arrow.get(re.sub(r'Z$', '+0000', timestamp_strz), iso8601_fmt)
Is the code below helpful?
>>> import arrow
>>> arrow.now("utc")
<Arrow [2018-09-19T07:43:14.371000+00:00]>
>>> arrow.now("utc").strftime("%Y-%m-%dT%H-%M-%SZ")
'2018-09-19T07-43-56Z'
Following this answer: https://stackoverflow.com/a/42777551/5772365.
Once we implement #35 then something like this would work.
>>> dt=arrow.utcnow()
>>> dt
<Arrow [2019-07-28T16:23:29.819198+00:00]>
>>> dt.format("YYYY-MM-DDTHH:mm:ssZ")
'2019-07-28T16:23:29+0000'
>>> dt.format("YYYY-MM-DDTHH:mm:ss[Z]")
'2019-07-28T16:23:29Z'
Now that #688 has been merged the example I posted above will work!
I know this thread is closed, but for those who stumble across it and are copy/pasting the solution above, please note that if you are trying to render time into ISO 8601 then you'll want the 24 hour formatting option ("HH") rather than the 12-hour option ("hh"). (And if you are not trying to be ISO 8601 compliant then you'll probably want am/pm ("A", or "a") in there somewhere).
Incorrect (hh):
In [11]: arrow.utcnow().format("YYYY-MM-DDThh:mm:ss.SSS[Z]")
Out[11]: '2020-01-26T08:42:03.669Z'
Correct (HH):
In [12]: arrow.utcnow().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]")
Out[12]: '2020-01-26T20:42:05.835Z'
Thanks for the snippet @systemcatch ; it got me (eventually) where I needed to be. Cheers!
@epmoyer glad it helped you, I've corrected my snippet.
Most helpful comment
Once we implement #35 then something like this would work.