How to elegantly handle a local datetime and UTC datetime

I have been working with a client that has a Hybrid environment. They have Azure tables, but their API is on prem and their internal Sql Server. To keep the data sync there is a WebJob that is running every 5 minutes and syncing on prem data with Azure tables.

Problem they faced is the Azure webjob running on Azure and uses the UTC and the local uses the local time. They can’t really change the on prem to use the UTC instead. So I have to create something when it is time to send data to Azure it needs to be converted to UTC and when it comes from Azure, it should be local for the stored procs.

Best solution I found is to create a custom Serializer that will do the work for me. check the code below.

using Newtonsoft.Json;

namespace My.Utilities
{
public static class Json
{
private static readonly JsonSerializerSettings SerializeSettings = new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Utc };
private static readonly JsonSerializerSettings DeserializeSettings = new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Local };

public static string Serialize<T>(T @object) where T: class
{
return JsonConvert.SerializeObject(@object, SerializeSettings);
}

public static T Deserialize<T>(string json) where T : class
{
return JsonConvert.DeserializeObject<T>(json, DeserializeSettings);
}
}
}

Hope you like it. Love to hear any suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *