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

2024-05-20 17:33:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我读过一些文档,其中说要传递Hubspot日期字段的值,您应该将日期格式化为午夜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!\"...
 }

Tags: fromimportmessagedatetimedtpropsyearcrm
3条回答

使用Hubspot支持的“sanetime”模块:https://github.com/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

你有没有尝试在约会时间通话中增加小时和分钟

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

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

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

现在一切看起来都很好

相关问题 更多 >