gpt4 book ai didi

python - 如何解决由于 Calendar API 升级导致创建的事件时间不匹配?

转载 作者:行者123 更新时间:2023-11-29 13:55:49 24 4
gpt4 key购买 nike

作为引用,我的时区是东部 - 纽约。

我正在将事件从 PostgreSQL 数据库插入到 Google 日历中。自 6 月初以来,我一直在使用 UTC-4,当时我终于将我的应用程序从 v2 迁移到 v3,并在 v2 中使用了几年。直到 8 月 18 日,它才给我正确的时间。 8 月 18 日,时间差了一小时,所以我将设置更改为 UTC-5。这工作了大约 2 个小时,然后我不得不将其重置回 UTC-4。

今天,8 月 21 日,又关闭了一个小时,我已将 UTC 设置回 -5。事件将按照应有的方式插入,但事件会中断一个小时,并且有时需要更改 UTC。我的服务器上的系统时间是正确的。

对正在发生的事情有什么想法吗?

我的一些代码片段:

#get an event from a PostgreSQL database to insert into a Google Calendar
curs.execute("SELECT c_event_title,c_name,c_event_date,c_event_starttime,c_event_endtime,c_department,seat_arrange,c_attendee_count from sched_421 where sched_id_421=%i;" %recnum)

mit=curs.fetchall() # mit IS NOW ALL THE RESULTS OF THE QUERY
for myrec in mit: # FOR THE ONE RECORD (EVENT) IN THE QUERY RESULTS

myend_time = time.strftime("%I:%M %p", time.strptime(str(myrec[4]),"%H:%M:%S"))
if myend_time[0]=='0': # Remove leading zero for 01:00 - 09:00
myend_time = myend_time[1:]

title = ' - %s %s - Group:%s' %(myend_time,myrec[0],myrec[5])
mycontent = myrec[0]+' - '+ myrec[5]
content = mycontent
where = where_dict[room_calendar]

# THIS IS WHERE THE UTC IS, SOMETIMES 4 WORKS SOMETIMES 5 WORKS
start_time = '%sT%s-05:00' %(myrec[2],myrec[3]) # Google format
end_time = '%sT%s-05:00' %(myrec[2],myrec[4]) # Google format

myend_time = '%s' %myrec[4] # User format (am/pm)
seat_arrange = '\nSeating - %s' %str(myrec[6])
attendee_count = '\nNumber of participants: %s' %str(myrec[7])
descript = str(myrec[0]) + ' ' + seat_arrange + attendee_count+ "\n Created By: me@somewhere.com"

# upload the event to the calendar
created_event = service.events().insert(calendarId=calendar_dict[room_calendar], body=event).execute()

最佳答案

您正在查看的日期是否位于夏令时开关的不同侧面?

Eastern Time Zone 3月到11月是UTC-4:00,11月到3月是UTC-5:00。

像这样对 TZ 偏移量进行硬编码是个坏主意,尤其是在使用夏令时的 TZ 中。最好将所有时间存储为 UTC,并仅在端点(数据输入和数据显示)应用 TZ 信息。

至少,您会希望有一些东西可以根据日期计算出正确的 TZ 偏移量,例如辅助函数或一些逻辑 block 。

我不确定您对数据库中的数据有多少控制权,所以这将决定您选择哪条路径。

理想情况下,您可以将数据库中的 3 个字段(日期、开始时间、结束时间)更改为 2 个(开始日期时间 UTC、结束日期时间 UTC)

关于python - 如何解决由于 Calendar API 升级导致创建的事件时间不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32143386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com