简体   繁体   English

如何获取用于在 python 中过滤 google fit API 结果的日期的日期格式?

[英]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.

相关问题 如何格式化日期时间数据以适应谷歌日历 api 的日期字段? - How do I format datetime data to fit google calendar api's date field? 如何将numpy数组的两列中的日期和时间转换为matplotlib可以使用的datetime格式? - How do I get date and time from two columns of a numpy array into a datetime format that can be used by matplotlib? 过滤来自 json 响应的结果,以仅显示没有结束日期的重复事件。 Python 和谷歌日历 API - Filter results from json response to only show recurring events with no end date. Python and Google Calendar API 如何以所需的格式获取Python日期? - How do I get my Python date in the format that I want? 无法让python正确格式化日期 - can't get python to format date correctly 如何在python中获取当前日期时间的字符串格式? - How do I get a string format of the current date time, in python? 如何在Python中过滤带有其他日期的日期数据框? - How can I filter a dataframe of dates with other date in Python? 如何将此日期格式解析为日期时间? 蟒蛇/熊猫 - How can I parse this date format into datetime? Python/Pandas 如何使用Python在ISO中格式化日期? - How can I format date in ISO using Python? 如何在 Python 3 中更改 csv 中的日期格式 - How can I change date format in csv in Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM