使用C#添加Google日历活动时的时差问题 [英] Time difference issue when adding a Google Calendar Event using C#

查看:113
本文介绍了使用C#添加Google日历活动时的时差问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"Google.GData.Calendar" API使用c#将事件添加到Google日历.但是,在我的数据库和Google日历中创建的事件之间存在时间差异.

I am using "Google.GData.Calendar" API to add Events to the Google Calendar using c#. But there is time difference between the events created in my DB and Google Calendar.

以下是使用Google.GData API将事件添加到Google日历的代码:

Following is the Code to add an Event to Google Calendar using the Google.GData API:

public static void AddEventToGoogle()
{
    //string timezone = "US Mountain Standard Time";

    Uri postUri = new Uri("https://www.google.com/calendar/feeds/default/private/full");
    try
    {
        GOAuthRequestFactory authFactory = new GOAuthRequestFactory("cl", "MyApp");
        authFactory.ConsumerKey =  ConfigurationManager.AppSettings["GConsumerKey"].ToString();
        authFactory.ConsumerSecret =  ConfigurationManager.AppSettings["GConsumerKeySecret"].ToString();
        authFactory.Token = "xxxxxxxxxx";
        authFactory.TokenSecret = "xxxxxx";
        Google.GData.Calendar.CalendarService service = new Google.GData.Calendar.CalendarService(authFactory.ApplicationName);
        service.RequestFactory = authFactory;

        EventEntry entry = new EventEntry();
        entry.Title.Text = "Test Google Event Create";
        entry.Content.Content = "Test Google Event Create";
        entry.Notifications = false;

        When eventTime = new When();
        eventTime.AllDay = false;
        eventTime.StartTime = new DateTime(2013, 5, 9, 8, 0, 0);
        eventTime.EndTime = new DateTime(2013, 5, 9, 11, 0, 0);
        entry.Times.Add(eventTime);

        // Send the request and receive the response:
        EventEntry insertedEntry = service.Insert(postUri, entry);
        if (insertedEntry != null && !string.IsNullOrEmpty(insertedEntry.EventId))
        {
            //Get the insertedEntry.EventId
        }
    }
    catch (GDataRequestException gre)
    {
        HttpWebResponse response = (HttpWebResponse)gre.Response;
    }
}

但是使用上面的代码,我要创建的事件和Google日历中的条目存在时差.我在Google日历中使用的时区是(GMT-07:00)山区时间-亚利桑那州".当我将Google日历的时区更改为CST,PST等时,就会发生这种情况.

But with the above code there is a time difference with the event I intended to create and the entry in the Google Calendar. The TimeZone I have in my Google Calendar is "(GMT-07:00) Mountain Time - Arizona". This is happening when i change the TimeZones of my Google Calendar to CST, PST,...

在这种情况下,或者在向Google日历添加事件时,我需要在什么地方指定TimeZone,请提出解决方法.

Please suggest a workaround for this situation or where do I need to specify the TimeZone while adding an event to Google Calendar.

使用添加事件的完整方法更新了我的问题

推荐答案

GData库使用v2的Google API.我认为您需要将代码迁移到v3,以便为事件的时区获得适当的支持.

The GData library uses v2 of the Google APIs. I think you will need to migrate your code to v3 in order to get proper support for time zones on events.

例如,请参见此页面,其中显示了创建重复项目的示例事件. (切换到".NET"标签".

For example, see this page which shows an example of creating a recurring event. (Switch to the ".NET" tab".

以下一些资源可以为您提供帮助:

Here are some resources that may help you:

  • Migration Guide
  • What's new in v3 (it doesn't mention time zones though)
  • Client libraries (see the ".NET" tab)

我确实浏览了您的代码和v2 GData库的参考指南.我只在CalendarEntryEventFeedEventQuery类上看到TimeZone属性.由于您没有使用任何这些功能,因此我认为在v2中是不可能的.

I did look through your code and the reference guide to the v2 GData library. I only see TimeZone properties on the CalendarEntry, EventFeed, and EventQuery classes. Since you're not using any of those, I don't think it is possible in v2.

更新

它在v2 API中是可能.您可以在ICAL格式的<gd:recurrence>标记中传递时区.请查看本文档中的示例后的注释.

It is possible in the v2 API. You can pass a time zone in the ICAL formatted <gd:recurrence> tag. Take a look at the note following the example in this documentation.

但是,它似乎没有显示为.Net GData客户端库中EventEntry类的属性.因此,我的建议是-使用v3 api和客户端库.

However, it doesn't appear that it is exposed as a property of the EventEntry class in the .Net GData client library. So my recommendation stands - use the v3 api and client library.

这篇关于使用C#添加Google日历活动时的时差问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆