Understanding Time Zone and Golden Rule to Manage them

Posted by: admin
Category: Timezone

Standard Time Zone

Overview:

With technology the world has become a global market and its customer trends to be available in different location across the globe. These customers work on application and would like to know the exact time when their respective events are scheduled to occur. Like my flight flying from New York – USA to Delhi – India will have a time difference that is mapped with Universal time zone. I would prefer looking at the local time-zone of my current location, that will allow me to plan accordingly. It would not be a good idea for me to add 9.5 hours to manage the timings. This is the reason why the developer needs to understand various concept of Time Zone.

In this blog I will try to cover all the major concept pertaining to Time Zone and how this needs to be implemented in a software application that can debut as a globally acceptable software.

A time zone is a geographical region in which the same standard time is used. That is expressed as an offset from Coordinated Universal Time (UTC).

The local time is calculated by applying the regional time zone offset to the UTC time.

UTC Time:

Coordinated Universal Time (expressed to UTC) is the primary time standard that the world regulates its clocks in terms of date and time.

Time zones around the world are expressed using positive or negative offsets from UTC.

UTC offset is the difference in time between UTC time and a location’s observed time.

UTC+05:30 is an identifier for a time offset from UTC of +05:30.

Let’s take an example.

The current date is 2019-04-15 in our local time. If we use that as the UTC date part, all records that range from 2019-04-15 00:00 to 2019-04-15 23:59:59 in UTC.

.Net Framework provides a class called as TimeZoneInfo class, this provides two properties, that give your code access to predefined time zone objects.

  • UTC
  • Local

Applications are typically requiring that the dates are stored in time zone, means storing DateTime values as UTC, so that you have a consistent baseline date value.

Let’s store date in UTC:

DateTime dateNow = DateTime.Now;

var utc = TimeZoneInfo.ConvertTimeToUtc(dateNow);

Note: If you are using MS-SQL as backend you can also use functions such as GETUTCDate()

Local Time:

 To get the local time you can use : convertedDate.ToLocalTime();

Also use, TimeZoneInfo.Local to know Time Zone of your system.

  • Convert UTC Time to Current Time Zone: ConvertTimeFromUtc(UTC, TimeZoneInfo.Local);

Get the default timezone for a country (via CultureInfo)

.NET Framework provides a specialized class to manage culture, this is known as CultureInfo and its responsible to represents information about specific culture. This class is under the namespace of System.Globalization

For Example :

  • Globalization.CultureInfo.CurrentCulture.ToString();

That returns “en-US”, You can also customized current thread Like..

 

You can also use the CultureInfo.CurrentCulture property along with the HttpRequest.UserLanguages property.

For Example:

 

If you live in Germany and your computer uses the German language, the result will be “de-DE”.

As there are several cultures and languages across the globe and it’s not always necessary to carter all of them, rather targeting few your defined audience is always preferred. There is various way to store these culture information in your .net application. Few are mentioned below.

a. You can also define a default culture key in your .config file.

<configuration>

<appSettings>

<add key=“DefaultCulture” value=“en-CA”/>

</appSettings>

</configuration>

And use that key value in your application as following way.

CultureInfo culture = new CultureInfo(ConfigurationManager.AppSettings[“DefaultCulture”].ToString());

System.Globalization.DateTimeFormatInfo Class

That defines the format of the date as below.

For Example:

This class is very useful for the purpose of personalization as it allows the user to display date and time as per their requirement. For more formats and details on this class please refer this link

Why Culture / Timezone comes into picture?

Extending the example shared in the 1st paragraph let’s say we are in process of designing an application. That has a problem statement that it should be capable to handle user from various geographic location and the application is to be hosted in cloud server environment such as Azure or AWS based in USA.

At this time, we are not aware of current end user’s local time and time zone. They select different culture like en-us, ta-In, en-CA even they may access application from Denmark/Sweden.

Now the problem is we used DateTime.Now to store the record created/updated date, etc. These dates will be based on the server TimeZone where as the user is accessing from Denmark/Sweden. Here to our rescue come UTC TimeZone, we use standard time UTC that is, DateTime.UtcNow while saving it to the database.

Appointment record created on 26 May 2019, 3:15 P.M Swedish time

 

The Golden Rules to manage Time Zone in this era of Global Software Application is to

  1. Saving Date / Time in Database or Any persistent layer

Use UTC  (This can be done from application layer or from the data layer)

  1. Displaying Date / Time

Use Local Date /Time, i.e. Fetch data from the persistent layer and convert its to the end user’s Local date time.(This Should be only done from the presentation layer, as it will give you exact local settings of the end user)

Author:  Darshna Trivedi

Let’s build your dream together.