[英]How to format a date value for Hubspot API using Python
I have read the docs saying that to pass the value for a Hubspot date field you should format your Date as midnight UTC.我已阅读文档说要传递 Hubspot 日期字段的值,您应该将日期格式设置为午夜UTC。 However, I've had no luck doing so in Python.
但是,我在 Python 中没有这样做。 I assume I am just missing the magic Python incantation that will get the right result.
我想我只是错过了能得到正确结果的魔法 Python 咒语。 Here is what I have:
这是我所拥有的:
from pytz import timezone, utc
from hubspot.crm.contacts import SimplePublicObject,
created_dt = # datetime from sqlalchemy query
utcdt = utc.localize(
datetime(
year=created_dt.year,
month=created_dt.month,
day=created_dt.day
)
)
ts = int(utcdt.timestamp())
props = SimplePublicObjectInput({"last_booking": str(ts)})
return client.crm.companies.basic_api.update(
hs_id, simple_public_object_input=props
)
this returns this error:这将返回此错误:
{"status":"error",
"message":"Property values were not valid: [{\"isValid\":false,\"message\":\"1570233600 is at 4:10:33.600 UTC, not midnight!\"...
}
Ah, the answer was right there.啊,答案就在那里。 Python
timestamp
returns the time in seconds, and HubSpot expects microseconds. Python
timestamp
以秒为单位返回时间,HubSpot 需要微秒。 I just had to multiply by 1000:我只需要乘以 1000:
ts = int(utcdt.timestamp()*1000)
now all looks good.现在一切看起来都不错。
did you try adding hours and minutes to your datetime call您是否尝试在日期时间通话中添加小时和分钟
datetime(
year=created_dt.year,
month=created_dt.month,
day=created_dt.day,
hour=0,
minute=0
)
Use the Hubspot supported "sanetime" module: https://github.com/HubSpot/sanetime使用 Hubspot 支持的“sanetime”模块: https://github.com/HubSpot/sanetime
Then to get a date:然后得到一个日期:
yourdate = datetime.datetime.date()
hubspot_date = sanetime.time(yourdate )
Or if you do not want a dependency:或者,如果您不想要依赖项:
#convert datetime to UTC
your_utc_datetime = your_datetime.astimezone(pytz.UTC)
#replace time with midnight
your_utc_date_midnight = your_utc_datetime.replace(hour=0,minute=0,second=0, microsecond=0)
# convert to epoch (Python 3.3+)
your_hubspot_date = your_utc_date_midnight.timestamp()*1000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.