Anyone successful with LocalTimeFromUtcTimeRequest
-
2012년 4월 16일 월요일 오후 7:54
I'm attempting to use the LocalTimeFromUtcTimeRequest,but have found it to be largely unhelpful. The result LocalTime property seems to depend on my server's timezone. I found this specifically to be an issue with Crm Online, where the message would return an incorrect local time (12pm UTC -> 4am EDT), but I can duplicate it on premise by just changing around my server time zone and getting different results.
Is anyone able to successfully use this method to convert times based on user settings?
모든 응답
-
2012년 4월 17일 화요일 오전 1:11
It depends largely on how you are attempting to use it; but yes, it does work correctly.
DateTimes are stored in the CRM as UTC. So every time you perform a retrieval of a DateTime field, it will be represented in UTC format. You can subsequently use the LocalTimeFromUtcTimeRequest to convert that UTC DateTime to a Local DateTime according to a specific User's TimeZoneCode.
The following is a paraphrased version of an example from the SDK:
var currentUserSettings = xrm.RetrieveMultiple( new QueryExpression(UserSettings.EntityLogicalName) { ColumnSet = new ColumnSet("localeid", "timezonecode"), Criteria = new FilterExpression { Conditions = { new ConditionExpression("systemuserid", ConditionOperator.EqualUserId) } } }).Entities[0].ToEntity<UserSettings>(); int _timeZoneCode = currentUserSettings.TimeZoneCode.GetValueOrDefault(); DateTime theDateTime = ( from a in xrm.AccountSet select a.CreatedOn.GetValueOrDefault() ).Take(1).FirstOrDefault(); var request = new LocalTimeFromUtcTimeRequest { TimeZoneCode = _timeZoneCode, UtcTime = theDateTime }; var response = (LocalTimeFromUtcTimeResponse)xrm.Execute(request); Console.WriteLine(String.Concat("Calling LocalTimeFromUtcTimeRequest. UTC time: ", theDateTime.ToString("MM/dd/yyyy HH:mm:ss"), ". Local time: ", response.LocalTime.ToString("MM/dd/yyyy HH:mm:ss")));Wherein the basic steps are:
- Retrieve current User (or whomever you wish to represent DateTime) TimeZoneCode
- Retrieve DateTimes that require conversion
- Use LocalTimeFromUtcTimeRequest to convert
It doesn't matter what TimeZone the server is set to, provided the Time is set correctly to correspond with the selected TimeZone.
--pogo (pat) @ pogo69.wordpress.com
-
2012년 4월 17일 화요일 오후 12:22
I completely understand the theory, but in practice, I am not seeing those results!
Here's my code (35 is EST, where I am located)
LocalTimeFromUtcTimeRequest request = new LocalTimeFromUtcTimeRequest(); request.TimeZoneCode = 35; request.UtcTime = new DateTime(2012, 4, 16, 12, 0, 0, DateTimeKind.Utc); LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)service.Execute(request); Console.WriteLine(response.LocalTime.ToString("MM/dd/yyyy HH:mm:ss"));When my computer is set to EST, I correctly get:
04/16/2012 08:00:00
But, when I set my computer to Alaska (GMT-9) I get:
04/16/2012 04:00:00Same code!
-
2012년 4월 17일 화요일 오후 1:36We tend to have unpredictable results when the time zone code submitted does not match the machine's time zone setting. What are the results in your scenario if you change the time zone code to 3 (Alaska) when you have your computer set to Alaska time zone? If the results you retrieve are localized, you're computer may be trying to apply an additional offset. Thus the erroneous appearing response.
-
2012년 4월 17일 화요일 오후 1:55
With my computer set to Alaska, and the code set to Alaska, I get
04/16/2012 00:00:00
Which is also wrong... and confuses me more.
This all started because CRM Online is always returning a time which is useless to me. What is the use of this code if my computer is applying an additional offset. When does this work?
-
2012년 4월 18일 수요일 오전 12:29
Wow. I did some more testing based on your experiences and... I'm surprised to be seeing the same. What appears to be causing the issue (from what I can determine without more testing) is that the results are incorrect when the TimeZones on the CRM Server and the machine from which the request is sent differ.
That is; I tried your code above and got the correct result - my machine's Time Zone and the CRM Server to which I sent the request are the same.
When I change my machine's Time Zone and run the same code - wrong result!!
Unless there is some way of programatically eliciting the CRM Server's Time Zone, I don't know what can be done about the discrepancy.
But to summarise from my minimally additional experimentation - it appears to work when CRM Server and machine from which Web Service Request is sent are set to use the same Time Zone.
--pogo (pat) @ pogo69.wordpress.com
-
2012년 4월 18일 수요일 오후 12:13
Thank you for reproducing (I was starting to feel a little insane)! The situation where the CRM server and application making the request are in the same timezone works great for us until we use CRM Online. *shrug*
-
2012년 4월 24일 화요일 오전 7:44
I've create workaround for this problem which might help,But I'm not sure it doesn't have any holes.
Please let me know if you have any comments
http://dynamicslollipops.blogspot.com/2012/03/mscrm-2011-using-whole-number-time-zone.html
Yair Rozenberg
- 답변으로 제안됨 yairrose 2012년 4월 25일 수요일 오전 7:49
- 답변으로 표시됨 KTurbyfill 2012년 4월 25일 수요일 오후 12:43
-
2012년 4월 24일 화요일 오후 1:47Thanks! That is exactly the code I was really hoping to avoid having to write ;) It seems like the web service should return a DateTimeOffset object instead of a DateTime object, and then I'd be golden.