Skip to main content

DateTime and space

By Betim Beja ·

One of the most complex tasks in programing is working with dates. In a given moment the sun shines only part of the world, so it is daytime somewhere, and at that same time is nighttime somewhere else. To simplify our lives we have the concept of local time, so you can do your nine to five job. But since we need to cooperate on a global scale, we need the Universal Coordinated Time (UTC), so that we can compare events generated at different time and space.

In the .NET world we have the DateTime struct to help us handle time in our programs. One important property of this object is the Kind property. It can be one of the three values:

  • Universal - The represented time is according to UTC
  • Unspecified - The represented time might be related to any time zone (the implementer of the code should know the related time zone)
  • Local - The represented time is according to the TimeZone of the computer the code is running on

When developing applications that run calculations on Dates on machines that might have a different timezone than that of the user, you should be aware that the Local TimeZone is related to the TimeZone of the machine running the code and not to the TimeZone of the user's machine.

As you can see in the following code, when converting to the UserLocal TimeZone the DateTime Kind must be specified as Unspecified, otherwise it will be related to the TimeZone of the machine running the code which might be different from the TimeZone of the user's machine

ConvertToLocal.cs
using System;

namespace AlbanianXrm.DateTimes.Extensions.Extensions
{
    public static class TimeZoneInfoExtensions
    {
        public static DateTime ConvertToLocal(this TimeZoneInfo timeZoneInfo, DateTime utcDateTime)
        {
            var adjustment = timeZoneInfo.GetAdjustmentRule(utcDateTime);
            if (adjustment == null)
            {
                return DateTime.SpecifyKind(utcDateTime.Add(timeZoneInfo.BaseUtcOffset), DateTimeKind.Unspecified);
            }
            var timeDifference = timeZoneInfo.BaseUtcOffset;
            if (timeZoneInfo.SupportsDaylightSavingTime && timeZoneInfo.IsDaylightSavingTime(utcDateTime))
            {
                timeDifference = timeDifference.Add(adjustment.DaylightDelta);
            }
            return DateTime.SpecifyKind(utcDateTime.Add(timeDifference), DateTimeKind.Unspecified);
        }

        public static TimeZoneInfo.AdjustmentRule GetAdjustmentRule(this TimeZoneInfo localTimezone, DateTime date)
        {
            var adjustments = localTimezone.GetAdjustmentRules();
            // Iterate adjustment rules for time zone
            foreach (TimeZoneInfo.AdjustmentRule adjustment in adjustments)
            {
                // Determine if this adjustment rule covers year desired
                if (adjustment.DateStart <= date && adjustment.DateEnd >= date)
                    return adjustment;
            }
            return null;
        }
    }
}

If you ever need to work with time in the Microsoft Dataverse world, I have developed some extensions for Plugins or Custom Workflow Actions that simplify a lot the conversion of time from UTC to the User Local TimeZone. You can find the source code here. Also you can just install the NuGet packages on your project and use the PluginBase or WorkflowBase and the extensions.

As mentioned on the presentation "Test your code with FakeXrmEasy" these extensions have been tested, and have helped me build highly performant plugins.