fake-xrm-easy icon indicating copy to clipboard operation
fake-xrm-easy copied to clipboard

Unable to replicate behavior of Date Only fields

Open vinaymenda opened this issue 4 years ago • 6 comments

This is related to Date and Time fields with behavior set as Date Only.

When we query such fields from Dynamics 365, we get DateTime field with Kind = Unspecified. However, when we query from FakeXrmEasy, the DateTime field has Kind = Utc.

Is there a way we can specify the behavior of the Date and Time field so that FakeXrmEasy returns the DateTime exactly the way Dynamics 365 does (i.e., the Kind property set to Unspecified).

Note: We are using late bound types.

vinaymenda avatar Oct 15 '19 06:10 vinaymenda

I'm experiencing the same issue.

mikewama avatar Jul 21 '20 22:07 mikewama

Same issue with version: FakeItEasy 3.2.0 FakeXrmEasy.9 1.44.0

Although I try to avoid unit test specific code I did add this extension method to work around this issue. public static DateTime ConvertDateTimeOnlyFieldToUniversalDateTime(this DateTime str) { if (str.Kind == DateTimeKind.Unspecified) { var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); return TimeZoneInfo.ConvertTimeToUtc(str, timeZone); } return str.ToUniversalTime(); } Idea being if unknown kind was pulled from Dynamics our business can assume it is Central Standard Time and convert it to Utc and if any other kind just do normal ToUniversalTime.

Jakeace17 avatar Jan 21 '22 14:01 Jakeace17

Hello @Jakeace17, may I suggest the following blog post I wrote regarding DateTime https://xrm.al/blog/datetime-zone That being said, I think that handling automatic date conversions in the same way that dataverse does is very difficult because other than use UTC nothing else is well documented.

BetimBeja avatar Jan 21 '22 14:01 BetimBeja

Hello @Jakeace17, may I suggest the following blog post I wrote regarding DateTime https://xrm.al/blog/datetime-zone That being said, I think that handling automatic date conversions in the same way that dataverse does is very difficult because other than use UTC nothing else is well documented.

Thank you @BetimBeja for the quick reply. I agree documentation is a struggle and the fact that a Date field that is Date Only having a Kind of Unspecified is something I only now noticed despite working with Dynamics for over 5 years now. Granted we very rarely us "Date Only" and just recently started mapping that field to a field we use to drive SLA on cases where time is very important.
So my goal was to take that Unspecified kind fields value and make it UTC in a plugin (were honestly I don't trust local so I assume Central Standard Time as that is the main time zone our business operates in and likely the context under which the Unspecified field was set with). I will keep in mind the extension you referenced in this link but wonder how they determine what local is while running in plugins? I know what region our Dynamics is running in (Central) but if I remember correctly it can move to other regions for disaster recovery or when ever Microsoft deems it should so want to be ready for that.

Jakeace17 avatar Jan 21 '22 16:01 Jakeace17

Hello @Jakeace17, may I suggest the following blog post I wrote regarding DateTime https://xrm.al/blog/datetime-zone That being said, I think that handling automatic date conversions in the same way that dataverse does is very difficult because other than use UTC nothing else is well documented.

Thank you @BetimBeja for the quick reply. I agree documentation is a struggle and the fact that a Date field that is Date Only having a Kind of Unspecified is something I only now noticed despite working with Dynamics for over 5 years now. Granted we very rarely us "Date Only" and just recently started mapping that field to a field we use to drive SLA on cases where time is very important.
So my goal was to take that Unspecified kind fields value and make it UTC in a plugin (were honestly I don't trust local so I assume Central Standard Time as that is the main time zone our business operates in and likely the context under which the Unspecified field was set with). I will keep in mind the extension you referenced in this link but wonder how they determine what local is while running in plugins? I know what region our Dynamics is running in (Central) but if I remember correctly it can move to other regions for disaster recovery or when ever Microsoft deems it should so want to be ready for that.

The problem is that in a plugin all dates are UTC (even kind unspecified) and you should calculate or convert to the user local time by maintaining the kind as Unspecified. If you make Kind Local it will be using the TimeZone of the server where the code is running. in the repository I mention in my blog post you can find the actual implementation I use. There are some automatic conversions that the system applies in the pipeline if you change a value in the context to the "wrong" Kind

BetimBeja avatar Jan 21 '22 18:01 BetimBeja

@vinaymenda @Jakeace17 @BetimBeja There was an implementation by another contributor who introduced a DateTimeBehavior property to manipulate these conversions.

According to what you said, DateOnly fields should return Kind = Unespecified, however that implementation is actually returning Utc.

If this had to be addressed, I would move that implementation to apply such behavior based on metadata instead. DateOnly fields are part of the metadata of entities so it fits more naturally there to me.

jordimontana82 avatar Jan 21 '22 22:01 jordimontana82