Need help formatting the time zone for the Google API

I got datetime from a big record (using google.cloud.bigquery library) and you need to send it to the sdk admin admin reporting API in rfc 3339 according to the 'startTime' parameter of this api method . The API expects datetime to look like this:

2010-10-28T10: 26: 35.000Z

This is usually possible by creating a python date and time without tzinfo and calling isoformat as follows:

>>> now = datetime.utcnow()
>>> now = now.isoformat("T") + "Z"
>>> now
'2017-06-01T13:05:32.586760Z'

The problem I am facing is that the timestamp coming from BigQuery includes a tzinfo object, and this causes isoformat to return text that the Google API cannot process.

>>> timestamp_from_bigquery
'datetime.datetime(2017, 5, 31, 16, 13, 26, 252000, tzinfo=<UTC>)'
>>> timestamp_from_bigquery.isoformat("T") + "Z"
'2017-05-31T16:13:26.252000+00:00Z'

, +00: 00 API Google startTime. +00: 00 , API, , python . - datetime?

, :

>>> timestamp_from_bigquery.replace(tzinfo=None)
'2017-05-31T16:13:26.252000+00:00Z'
+4
1

datetime.datetime.strftime():

datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
timestamp_from_bigquery.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

, , .

+3

Source: https://habr.com/ru/post/1678347/


All Articles