如何解决由于日历 API 升级而导致的已创建活动的时间不匹配?



作为参考,我的时区是东部-纽约。

我插入事件从PostgreSQL数据库到谷歌日历。我从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()

您看到的日期是在夏令时开关的不同侧面吗?

东部时区从3月到11月是UTC-4:00,从11月到3月是UTC-5:00。

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

至少,你会希望有一些东西计算正确的TZ偏移量,基于日期,像一个辅助函数或一些逻辑块。

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

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

我不得不改变这段代码:

#  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

to(检查事件是否在夏令时,这在v2中是不必要的)

if bool (pytz.timezone('America/New_York').dst(datetime.datetime(myrec[2].year,myrec[2].month,myrec[2].day), is_dst=None)):
  utc_offset = '4'
else:
  utc_offset = '5'
start_time   = '%sT%s-0%s:00' %(myrec[2],myrec[3],utc_offset)
end_time     = '%sT%s-0%s:00' %(myrec[2],myrec[4],utc_offset)

最新更新