Хорошо, вот что я сделал, чтобы решить эту проблему:
- сделать запрос к списку для 1 элемента, получая время, установленное для локали сайта
- сделать запрос для этого элемента, получая время в UTC
- Рассчитайте разницу между двумя результатами.
Код:
/// <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;
}