[英]How can I get the date format for a date used to filter results in the google fit API in python?
I'm trying to do GET
query using as reference the example offered by the documentation of the API of google for sessions documented here which is as follows:我正在尝试使用谷歌 API 的文档提供的示例作为参考进行GET
查询,以获取此处记录的会话,如下所示:
curl --header "Authorization: Bearer ya29.1.yourtokenvalue" -X GET \
--header "Content-Type: application/json;encoding=utf-8" \
"https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=2014-04-01T00:00:00.000Z&endTime=2014-04-30T23:59:59.999Z"
As you can see the date as the following format:如您所见,日期格式如下:
2014-04-01T00:00:00.000Z
I'm currently the building the date in python as follows: from datetime import datetime, timedelta我目前正在 python 中构建日期,如下所示: from datetime import datetime, timedelta
PREVIOUS_DAYS=1
end = datetime.now().replace(minute=0, second=0)
start = end_date - timedelta(days = PREVIOUS_DAYS)
end_date = datetime.fromtimestamp(end).astimezone().strftime('%Y-%m-%dT%H:%M:%S.')
start_date = datetime.fromtimestamp(start).astimezone().strftime('%Y-%m-%dT%H:%M:%S.')
print str(start_date)
I have not idea what does de T mean and I can't find anything about what I think are mili seconds at the end of the timestamp in the strftime documentation .我不知道 de T 是什么意思,我在strftime 文档中找不到任何关于我认为时间戳末尾的毫秒数的信息。 I can also not find any information of it in the google API documentation so maybe this is just a standard I'm expected to know?我也无法在谷歌 API 文档中找到它的任何信息,所以也许这只是我应该知道的标准?
Looks like this is a standard format called ISO 8601. Python supports encoding any datetime object in this format using the isoformat function. The only difference you'll see is the "Z" postfix which means that the timezone is UTC.看起来这是一种称为 ISO 8601 的标准格式。Python 支持使用同种格式 function 以这种格式编码任何日期时间 object。您将看到的唯一区别是“Z”后缀,这意味着时区是 UTC。 Python will use a different notation "+00:00" which is also acceptable by the standard. Python 将使用标准也可接受的不同符号“+00:00”。
>>> from datetime import datetime, timezone
>>> datetime.now(timezone.utc).isoformat()
2014-04-01T00:00:00.000+00:00
If you really want to keep the "Z" postfix, it's ok to manually strip off the "+00:00", and add a "Z", but I believe that won't be necessary.如果你真的想保留“Z”后缀,可以手动去掉“+00:00”,然后添加一个“Z”,但我相信没有必要这样做。
>>> from datetime import datetime, timezone
>>> datetime.now(timezone.utc).isoformat()[:-6] + "Z"
2014-04-01T00:00:00.000Z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.