2010-11-24 58 views

回答

1

好吧,這裏是我所做的解決這個:

  • 進行查詢到列表1元,得到了網站的語言環境
  • 設定時間進行查詢該元素,得到UTC的時間
  • 計算2個結果之間的差異。

代碼:

/// <summary> 
    /// Gets the difference between local time and UTC to calculate offset. 
    /// Makes a query to the list that returns 1 element with times using the locale as set in the site's settings. 
    /// Then makes another query for the same element with UTC times to calculate the difference. 
    /// </summary> 
    /// <param name="list">list to query</param> 
    /// <param name="client">WS object</param> 
    /// <returns>Time offset as TimeSpan object</returns> 
    private TimeSpan getSiteTZOffset(List list, WSLists.Lists client) 
    { 
     //Set up query parameters 
     XmlDocument xmlDoc = new XmlDocument(); 
     XmlNode emptyQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", ""); 
     XmlNode queryWithID = xmlDoc.CreateNode(XmlNodeType.Element, "Query", ""); 

     XmlNode ndOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", ""); 
     ndOptions.InnerXml = "<DateInUtc>True</DateInUtc>"; 

     XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable); 
     xnm.AddNamespace("z", "#RowsetSchema"); 

     // Gets the attribute that serves as modified date 
     MapAttribute modifiedDateAttr = attributes.Single(x => x.Value.DateTimeModifiedField).Value; 
     // Gets the Id attribute 
     MapAttribute idAttr = attributes.Single(x => x.Value.KeyReference).Value; 

     //Get 1 result with site's local time 
     XmlNode resLocalTime = client.GetListItems(list.ListID, list.ViewID, emptyQuery, null, "1", null, null); 
     XmlNodeList itemsLocalTime = resLocalTime.SelectNodes("//z:row", xnm); 

     // 2nd query filters on ID of the item returned by 1st query 
     queryWithID.InnerXml = string.Format("<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>", 
      itemsLocalTime[0].Attributes[idAttr.Name].Value); 

     //get the result with UTC time 
     XmlNode resUtc = client.GetListItems(list.ListID, list.ViewID, queryWithID, null, "1", ndOptions, null); 
     XmlNodeList itemsUtc = resUtc.SelectNodes("//z:row", xnm); 

     //Converts string values to DateTime objects 
     DateTime localTime = DateTime.Parse(itemsLocalTime[0].Attributes[modifiedDateAttr.Name].Value); 
     DateTime utcTime = getUtcTime(itemsUtc[0].Attributes[modifiedDateAttr.Name].Value); 
     // Gets offset 
     TimeSpan offset = localTime - utcTime; 

     return offset; 
    } 
+0

這是不可靠的與夏令時更改時區。使用6月份日期的二月份日期時,您將得到與UTC不同的偏移量。大多數TZ都有DST。幸運的是,到SP站點TZ的UTC偏移量可以通過[``GetList`](http://msdn.microsoft.com/en-us/library/lists.lists.getlist.aspx)列表`](http://msdn.microsoft.com/en-us/library/lists.aspx)Web服務。在這裏看到我的答案。 – 2012-11-24 12:39:45

1

一個SharePoint網站的時區可由Lists Web服務的GetList方法獲得。這似乎不可思議,要求有關的清單得到一個網站的區域設置,但僅此而已:-)

這是關於一個列表的方法響應的摘錄:

<List> 
    ... 
    <RegionalSettings> 
    <Language>1033</Language> 
    <Locale>1033</Locale> 
    <AdvanceHijri>0</AdvanceHijri> 
    <CalendarType>1</CalendarType> 
    <Time24>False</Time24> 
    <TimeZone>-60</TimeZone> 
    <SortOrder>2070</SortOrder> 
    <Presence>True</Presence> 
    </RegionalSettings> 
    ... 
</List> 

的時間區域以分鐘作爲的偏移量返回到UTC時間。如果您將其添加到網站本地時區的時間,您將獲得UTC時區中的時間。 (如果你想得到正確的值,你將不得不考慮夏令時的變化,並根據當時的季節來應用它們)。

// Instantiate the web service. Don't forget to dispose it later. 
// Append the web service suffix (/_vti_bin/Lists.asmx) to the URL 
// of the web site which time zone of you are interested in. 
// If you don't use IWA or the current user has no access to the 
// web site set the service.Credentials instead. 
var service = new Lists(); 
service.Url = "http://myhost/sites/mysite/_vti_bin/Lists.asmx"; 
service.UseDefaultCredentials = true; 
// Get all lists from the web site you want to get the time zone of. 
// Get the XML element at /Lists/List[1]. It can be any accessible list. 
// We will use just the unique list identifier from the atribute ID. 
var lists = service.GetListCollection(); 
var list = lists.ChildNodes.OfType<XmlElement>().FirstOrDefault(); 
if (list == null) 
    throw new ApplicationException("The web has no lists."); 
// Get information about the list which a little about the web too. 
// Get the XML element at /List/RegionalSettings/TimeZone. 
list = (XmlElement) service.GetList(list.GetAttribute("ID")); 
var regionalSettings = list.ChildNodes.OfType<XmlElement>().First(
    item => item.LocalName == "RegionalSettings"); 
var timeZone = regionalSettings.ChildNodes.OfType<XmlElement>().First(
    item => item.LocalName == "TimeZone"); 
// The offset can be added to the time in the web site local time zone 
// to get the UTC time. For example, UTC+1 has the offset -60 minutes. 
var utcOffset = new TimeSpan(0, int.Parse(timeZone.InnerText), 0); 

--- Ferda

+0

雖然這是一箇舊帖子,讓我告訴你,你是一個真正的GENIUS!真。你應該得到獎勵或什麼。這是對這個問題的唯一正確答案。你真了不起。 – MdMazzotti 2014-02-12 15:53:14

相關問題