Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output a timestamp with UTC designator 'Z' #256

Closed
drozzy opened this issue Aug 12, 2015 · 8 comments
Closed

Output a timestamp with UTC designator 'Z' #256

drozzy opened this issue Aug 12, 2015 · 8 comments

Comments

@drozzy
Copy link

drozzy commented Aug 12, 2015

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.

@rafaelsierra
Copy link
Contributor

I also miss an option to output the timezone name (such as CEST, BRT, PT, etc) when using .format

@ghost
Copy link

ghost commented Mar 9, 2017

+1

@andrewleech
Copy link

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)

@gaoxinge
Copy link

gaoxinge commented Sep 19, 2018

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.

@systemcatch
Copy link
Collaborator

systemcatch commented Jul 28, 2019

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'

@systemcatch
Copy link
Collaborator

Now that #688 has been merged the example I posted above will work!

@epmoyer
Copy link

epmoyer commented Jan 26, 2020

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!

@systemcatch
Copy link
Collaborator

@epmoyer glad it helped you, I've corrected my snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants