Django DateTime格式谷歌日历

时间:2016-02-01 10:28:14

标签: json django datetime

我正在尝试在Google日历中创建一个活动https://developers.google.com/google-apps/calendar/create-events#add_an_event

google api会返回此错误:

  

“无效的值:格式无效:”“2016-02-01T08:00:00-00:00”“”

我尝试正确格式化日期,但仍无法正常工作

这是我的代码:

 date_s = reservation.date_start.strftime('%Y-%m-%dT%H:%M:%S-00:00')  
 date_e = reservation.date_end.strftime('%Y-%m-%dT%H:%M:%S-00:00')

   date_start = json.dumps(date_s , cls=DjangoJSONEncoder)   
   date_end = json.dumps(date_e, cls=DjangoJSONEncoder)

   event = {
     'summary': summary,
     'location': location,
     'description': 'A chance to hear more about Google\'s developer products.',
     'start': {
         'dateTime': date_start,
         'timeZone': 'Europe/Paris',
     },
     'end': {
         'dateTime': date_end,
         'timeZone': 'Europe/Paris',
     }, }

   service = build('calendar', 'v3', http=http)   
   event = service.events().insert(calendarId='primary', body=event).execute()

2 个答案:

答案 0 :(得分:2)

您不需要对日期进行json编码,因为您已经将它们作为strftime()生成的字符串,因此json.dumps()只会添加一个不需要的语法错误的引号。

答案 1 :(得分:0)

dateTime字符串的最后一部分是时区偏移量。问题可能是您指定的偏移量与timeZone属性中给出的时区不匹配。文档指定您不需要同时提供两者,因此请尝试仅使用'%Y-%m-%dT%H:%M:%S作为时间格式字符串(应该转换为变量而不能复制)。