如何使用Python格式化Hubspot API的日期值

问题描述 投票:0回答:2

我已阅读文档说,要传递Hubspot日期字段的值,您应该将日期格式设置为midnight UTC。但是,我在Python中没有运气。我以为我只是错过了可以得到正确结果的神奇的Python咒语。这是我所拥有的:

    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!\"...
 }
python hubspot
2个回答
0
投票

您是否尝试在日期时间通话中增加小时和分钟数

datetime(
    year=created_dt.year,
    month=created_dt.month,
    day=created_dt.day,
    hour=0,
    minute=0
)

0
投票

啊,答案就在那里。 Python timestamp以秒为单位返回时间,HubSpot期望以微秒为单位。我只需要乘以1000:

ts = int(utcdt.timestamp()*1000)

现在看起来不错。

© www.soinside.com 2019 - 2024. All rights reserved.