如何使用Python格式化Hubspot API的日期值
我读过一些文档,其中说要传递Hubspot日期字段的值,您应该将日期格式化为UTC午夜。然而,我在Python中没有这样做的运气。我想我只是错过了能得到正确结果的魔法Python咒语。以下是我所拥有的:如何使用Python格式化Hubspot API的日期值,python,hubspot,Python,Hubspot,我读过一些文档,其中说要传递Hubspot日期字段的值,您应该将日期格式化为UTC午夜。然而,我在Python中没有这样做的运气。我想我只是错过了能得到正确结果的魔法Python咒语。以下是我所拥有的: from pytz import timezone, utc from hubspot.crm.contacts import SimplePublicObject, created_dt = # datetime from sqlalchemy query u
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
)
这将返回以下错误:
{"status":"error",
"message":"Property values were not valid: [{\"isValid\":false,\"message\":\"1570233600 is at 4:10:33.600 UTC, not midnight!\"...
}
你有没有尝试在约会时间通话中增加小时和分钟
datetime(
year=created_dt.year,
month=created_dt.month,
day=created_dt.day,
hour=0,
minute=0
)
啊,答案就在那里。Python
timestamp
以秒为单位返回时间,HubSpot期望微秒。我只需要乘以1000:
ts = int(utcdt.timestamp()*1000)
现在一切看起来都很好。使用Hubspot支持的“sanetime”模块:
然后,要获得日期:
yourdate = datetime.datetime.date()
hubspot_date = sanetime.time(yourdate )
或者,如果不需要依赖项:
#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
不,同样的结果。我还设置了秒=0,微秒=0。非常确定这是默认值。此解决方案适用于Hubspot datetime,但如果Hubspot字段是“日期选择器”属性类型(仅限日期),则会导致错误。